MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / random_DAG

Function random_DAG

numpy_ml/utils/graphs.py:336–363  ·  view source on GitHub ↗

Create a 'random' unweighted directed acyclic graph by pruning all the backward connections from a random graph. Parameters ---------- n_vertices : int The number of vertices in the graph. edge_prob : float in [0, 1] The probability of forming an edge betwee

(n_vertices, edge_prob=0.5)

Source from the content-addressed store, hash-verified

334
335
336def random_DAG(n_vertices, edge_prob=0.5):
337 """
338 Create a 'random' unweighted directed acyclic graph by pruning all the
339 backward connections from a random graph.
340
341 Parameters
342 ----------
343 n_vertices : int
344 The number of vertices in the graph.
345 edge_prob : float in [0, 1]
346 The probability of forming an edge between two vertices in the
347 underlying random graph, before edge pruning. Default is 0.5.
348
349 Returns
350 -------
351 G : :class:`Graph` instance
352 The resulting DAG.
353 """
354 G = random_unweighted_graph(n_vertices, edge_prob, directed=True)
355
356 # prune edges to remove backwards connections between vertices
357 G = DiGraph(G.vertices, [e for e in G.edges if e.fr < e.to])
358
359 # if we pruned away all the edges, generate a new graph
360 while not len(G.edges):
361 G = random_unweighted_graph(n_vertices, edge_prob, directed=True)
362 G = DiGraph(G.vertices, [e for e in G.edges if e.fr < e.to])
363 return G

Callers 3

test_random_DAGFunction · 0.90

Calls 2

random_unweighted_graphFunction · 0.85
DiGraphClass · 0.85

Tested by 2

test_random_DAGFunction · 0.72