MCPcopy Create free account
hub / github.com/alibaba/GraphScope / gnm_random_graph

Function gnm_random_graph

python/graphscope/nx/generators/random_graphs.py:165–191  ·  view source on GitHub ↗
(n, m, seed=None, directed=False)

Source from the content-addressed store, hash-verified

163@patch_docstring(nxa.gnm_random_graph)
164@py_random_state(2)
165def gnm_random_graph(n, m, seed=None, directed=False):
166 if directed:
167 G = nx.DiGraph()
168 else:
169 G = nx.Graph()
170 G.add_nodes_from(range(n))
171
172 if n == 1:
173 return G
174 max_edges = n * (n - 1)
175 if not directed:
176 max_edges /= 2.0
177 if m >= max_edges:
178 return complete_graph(n, create_using=G)
179
180 nlist = list(G)
181 edge_count = 0
182 while edge_count < m:
183 # generate random edge,u,v
184 u = seed.choice(nlist)
185 v = seed.choice(nlist)
186 if u == v:
187 continue
188 else:
189 G.add_edge(u, v)
190 edge_count = edge_count + 1
191 return G
192
193
194@patch_docstring(nxa.newman_watts_strogatz_graph)

Callers 2

test_random_graphMethod · 0.90
random_shell_graphFunction · 0.85

Calls 3

add_nodes_fromMethod · 0.95
add_edgeMethod · 0.95
complete_graphFunction · 0.90

Tested by 1

test_random_graphMethod · 0.72