-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgenerate_random_dag.py
84 lines (70 loc) · 1.97 KB
/
generate_random_dag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import itertools
import random
import math
import networkx as nx
import numpy as np
from networkx.generators.classic import complete_graph
def gnm_random_graph(n, m, seed=None, directed=True):
"""Return the random graph G_{n,m}.
Produces a graph picked randomly out of the set of all graphs
with n nodes and m edges.
Parameters
----------
n : int
The number of nodes.
m : int
The number of edges.
seed : int, optional
Seed for random number generator (default=None).
directed : bool, optional (default=False)
If True return a directed graph
"""
if directed:
G=nx.DiGraph()
g = nx.DiGraph()
else:
G=nx.Graph()
g = nx.Graph()
G.add_nodes_from(range(n))
G.name="gnm_random_graph(%s,%s)"%(n,m)
if seed is not None:
random.seed(seed)
if n==1:
return G
max_edges=n*(n-1)
if not directed:
max_edges/=2.0
if m>=max_edges:
return nx.complete_graph(n,create_using=G)
nlist=list(G.nodes())
edge_count=0
while edge_count < m:
# generate random edge,u,v
u = random.choice(nlist)
v = random.choice(nlist)
if u>=v or G.has_edge(u,v):
continue
else:
G.add_edge(u,v)
edge_count = edge_count+1
permutation = np.random.permutation(n)
#print permutation
new_edges = []
for e in G.edges():
u,v = e
new_edges.append((permutation[u],permutation[v]))
g.add_edges_from(new_edges)
print("is_directed_acyclic_graph: %s" % nx.is_directed_acyclic_graph(g))
return g
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-n","--num_nodes",default= 300, help = "number of nodes")
parser.add_argument("-m","--num_edges",default = 1000,help = "number of edges")
parser.add_argument("-g","--graph_file",default = " ", help = "saved to graph file")
args = parser.parse_args()
n = int(args.num_nodes)
m = int(args.num_edges)
graph_file = args.graph_file
g = gnm_random_graph(n, m, seed=None, directed=True)
nx.write_edgelist(g,graph_file,data = False)