-
Notifications
You must be signed in to change notification settings - Fork 15
/
graph.py
61 lines (51 loc) · 1.97 KB
/
graph.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
import networkx as nx
import os
class RandomGraph(object):
def __init__(self, node_num, p, k=4, m=5, graph_mode="WS"):
self.node_num = node_num
self.p = p
self.k = k
self.m = m
self.graph_mode = graph_mode
def make_graph(self):
# reference
# https://networkx.github.io/documentation/networkx-1.9/reference/generators.html
# Code details,
# In the case of the nx.random_graphs module, we can give the random seeds as a parameter.
# But I have implemented it to handle it in the module.
if self.graph_mode is "ER":
graph = nx.random_graphs.erdos_renyi_graph(self.node_num, self.p)
elif self.graph_mode is "WS":
graph = nx.random_graphs.connected_watts_strogatz_graph(self.node_num, self.k, self.p)
elif self.graph_mode is "BA":
graph = nx.random_graphs.barabasi_albert_graph(self.node_num, self.m)
return graph
def get_graph_info(self, graph):
in_edges = {}
in_edges[0] = []
nodes = [0]
end = []
for node in graph.nodes():
neighbors = list(graph.neighbors(node))
neighbors.sort()
edges = []
check = []
for neighbor in neighbors:
if node > neighbor:
edges.append(neighbor + 1)
check.append(neighbor)
if not edges:
edges.append(0)
in_edges[node + 1] = edges
if check == neighbors:
end.append(node + 1)
nodes.append(node + 1)
in_edges[self.node_num + 1] = end
nodes.append(self.node_num + 1)
return nodes, in_edges
def save_random_graph(self, graph, path):
if not os.path.isdir("saved_graph"):
os.mkdir("saved_graph")
nx.write_yaml(graph, "./saved_graph/" + path)
def load_random_graph(self, path):
return nx.read_yaml("./saved_graph/" + path)