MCPcopy Index your code
hub / github.com/ddbourgin/numpy-ml / random_unweighted_graph

Function random_unweighted_graph

numpy_ml/utils/graphs.py:302–333  ·  view source on GitHub ↗

Generate an unweighted Erdős-Rényi random graph [*]_. References ---------- .. [*] Erdős, P. and Rényi, A. (1959). On Random Graphs, *Publ. Math. 6*, 290. Parameters ---------- n_vertices : int The number of vertices in the graph. edge_prob : float in [0, 1

(n_vertices, edge_prob=0.5, directed=False)

Source from the content-addressed store, hash-verified

300
301
302def random_unweighted_graph(n_vertices, edge_prob=0.5, directed=False):
303 """
304 Generate an unweighted Erdős-Rényi random graph [*]_.
305
306 References
307 ----------
308 .. [*] Erdős, P. and Rényi, A. (1959). On Random Graphs, *Publ. Math. 6*, 290.
309
310 Parameters
311 ----------
312 n_vertices : int
313 The number of vertices in the graph.
314 edge_prob : float in [0, 1]
315 The probability of forming an edge between two vertices. Default is
316 0.5.
317 directed : bool
318 Whether the edges in the graph should be directed. Default is False.
319
320 Returns
321 -------
322 G : :class:`Graph` instance
323 The resulting random graph.
324 """
325 vertices = list(range(n_vertices))
326 candidates = permutations(vertices, 2) if directed else combinations(vertices, 2)
327
328 edges = []
329 for (fr, to) in candidates:
330 if np.random.rand() <= edge_prob:
331 edges.append(Edge(fr, to))
332
333 return DiGraph(vertices, edges) if directed else UndirectedGraph(vertices, edges)
334
335
336def random_DAG(n_vertices, edge_prob=0.5):

Callers 3

test_all_pathsFunction · 0.90
test_is_acyclicFunction · 0.90
random_DAGFunction · 0.85

Calls 3

EdgeClass · 0.85
DiGraphClass · 0.85
UndirectedGraphClass · 0.85

Tested by 2

test_all_pathsFunction · 0.72
test_is_acyclicFunction · 0.72