-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_graph.py
208 lines (188 loc) · 6.97 KB
/
generate_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# other graphs candidates to check
import networkx as nx
import matplotlib.pyplot as plt
import random
#-> Cycle Graph C8
def cycle_graph_c8():
G = nx.cycle_graph(8)
plt.figure(figsize=(6, 6))
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=500)
plt.title("Cycle Graph C8")
plt.show()
return G
# Path Graph P16
def path_graph_p16():
G = nx.path_graph(16)
plt.figure(figsize=(12, 2))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=True, node_color='lightgreen', edge_color='gray', node_size=300)
plt.title("Path Graph P16")
plt.show()
return G
#-> Complete Bipartite Graph K8,8
def complete_bipartite_graph_k88():
G = nx.complete_bipartite_graph(8, 8)
plt.figure(figsize=(8, 6))
pos = nx.bipartite_layout(G, nodes=range(8))
nx.draw(G, pos, with_labels=True, node_color=['lightcoral'] * 8 + ['lightblue'] * 8,
edge_color='gray', node_size=300)
plt.title("Complete Bipartite Graph K8,8")
plt.show()
return G
#-> Complete Bipartite Graph K8,8
def complete_bipartite_graph_k_nn(n):
G = nx.complete_bipartite_graph(n, n)
plt.figure(figsize=(8, 6))
pos = nx.bipartite_layout(G, nodes=range(n))
nx.draw(G, pos, with_labels=True, node_color=['lightcoral'] * n + ['lightblue'] * n,
edge_color='gray', node_size=300)
plt.title("Complete Bipartite Graph K{},{}".format(n,n))
plt.show()
return G
# Star Graph S16
def star_graph_s16():
G = nx.star_graph(16)
plt.figure(figsize=(8, 8))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=True, node_color='gold', edge_color='gray', node_size=300)
plt.title("Star Graph S16")
plt.show()
return G
# Grid Graph 8x4
def grid_graph_8x4():
G = nx.grid_graph(dim=[8, 4])
plt.figure(figsize=(12, 6))
pos = {node: node for node in G.nodes()}
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=300)
plt.title("Grid Graph 8x4")
plt.show()
return G
# Grid Graph 8x4
def grid_graph_nxm(n,m):
G = nx.grid_graph(dim=[n, m])
plt.figure(figsize=(12, 6))
pos = {node: node for node in G.nodes()}
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=300)
plt.title("Grid Graph {}x{}".format(n,m))
plt.show()
return G
#-> 4-Regular Graph with 8 Vertices
def regular_graph_4_8():
G = nx.random_regular_graph(d=4, n=8, seed=42)
plt.figure(figsize=(6, 6))
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, node_color='lightgreen', edge_color='gray', node_size=500)
plt.title("4-Regular Graph with 8 Vertices")
plt.show()
return G
#-> Cubic (3-Regular) Graph with 16 Vertices
def cubic_graph_3_16():
G = nx.random_regular_graph(d=3, n=16, seed=42)
plt.figure(figsize=(8, 6))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=True, node_color='lightcoral', edge_color='gray', node_size=300)
plt.title("Cubic (3-Regular) Graph with 16 Vertices")
plt.show()
return G
# Disjoint Union of Four C4 Cycles
def disjoint_union_c4():
cycles = [nx.cycle_graph(4) for _ in range(4)]
G = nx.disjoint_union_all(cycles)
plt.figure(figsize=(12, 6))
pos = {}
shift_x = 0
for component in nx.connected_components(G):
subgraph = G.subgraph(component)
pos_sub = nx.circular_layout(subgraph, scale=1, center=(shift_x, 0))
pos.update(pos_sub)
shift_x += 3
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=300)
plt.title("Disjoint Union of Four C4 Cycles")
plt.show()
return G
# Complete Bipartite Graph K16,16
def complete_bipartite_graph_k1616():
G = nx.complete_bipartite_graph(16, 16)
plt.figure(figsize=(12, 6))
pos = nx.bipartite_layout(G, nodes=range(16))
nx.draw(G, pos, with_labels=False, node_color=['lightcoral'] * 16 + ['lightblue'] * 16,
edge_color='gray', node_size=100)
plt.title("Complete Bipartite Graph K16,16")
plt.show()
return G
# 5-Dimensional Hypercube Graph Q5
def hypercube_graph_q5():
G = nx.hypercube_graph(5)
plt.figure(figsize=(10, 8))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=False, node_color='lightgreen', edge_color='gray', node_size=200)
plt.title("5-Dimensional Hypercube Graph Q5")
plt.show()
return G
# Tree Graph with 8 Vertices
def tree_graph_8():
G = nx.balanced_tree(r=2, h=2)
G.add_edge(6, 7)
plt.figure(figsize=(8, 6))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=300)
plt.title("Tree Graph with 8 Vertices")
plt.show()
return G
# Wheel Graph W16
def wheel_graph_w16():
G = nx.wheel_graph(16)
plt.figure(figsize=(8, 8))
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, node_color='lightcoral', edge_color='gray', node_size=300)
plt.title("Wheel Graph W16")
plt.show()
return G
#-> Random Connected Graph with 16 Vertices
def random_connected_graph_16(p=0.15):
#n, p = 16, 0.25
n=16
while True:
G = nx.erdos_renyi_graph(n, p, seed=random.randint(1, 10000))
if nx.is_connected(G):
break
plt.figure(figsize=(10, 8))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=False, node_color='lightgreen', edge_color='gray', node_size=100)
plt.title("Random Connected Graph with 16 Vertices")
plt.show()
return G
# Expander Graph with 32 Vertices
def expander_graph_32():
G = nx.random_regular_graph(4, 32, seed=42)
plt.figure(figsize=(10, 8))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=False, node_color='lightblue', edge_color='gray', node_size=100)
plt.title("Expander Graph with 32 Vertices")
plt.show()
return G
#-> Expander Graph with n Vertices
def expander_graph_n(n):
G = nx.random_regular_graph(4, n, seed=42)
plt.figure(figsize=(10, 8))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=False, node_color='lightblue', edge_color='gray', node_size=100)
plt.title("Expander Graph with {} Vertices".format(n))
plt.show()
return G
# Planar Connected Graph with 16 Vertices
def planar_connected_graph_16():
G = nx.grid_graph(dim=[8, 2])
G = nx.convert_node_labels_to_integers(G)
additional_edges = [(0, 9), (1, 10), (2, 11), (3, 12), (4, 13), (5, 14), (6, 15),
(7, 15), (8, 7)]#, (6, 15), (14, 1), (1, 13), (10, 9), (0, 10), (12, 2), (8, 7)]
G.add_edges_from([e for e in additional_edges if e[0] < 16 and e[1] < 16])
assert nx.check_planarity(G)[0], "Graph is not planar."
pos = {node: (node // 2, node % 2) for node in G.nodes()}
plt.figure(figsize=(16, 8))
nx.draw(G, pos, with_labels=False, node_color='lightcoral', edge_color='gray', node_size=100)
plt.title("Planar Connected Graph with 16 Vertices")
plt.axis('equal')
plt.show()
return G