-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
64 lines (50 loc) · 1.95 KB
/
graph.py
File metadata and controls
64 lines (50 loc) · 1.95 KB
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
import time
import networkx as nx
import matplotlib.pyplot as plt
from themes.colors import EDGE_COLOR, NODE_COLOR
class Graph_Visualizer:
def __init__(self, is_directed=False, nodes=None, edges=None,is_random=False):
self.is_directed = is_directed
self.is_random = is_random
self.nodes = nodes
self.edges = edges
if self.is_random:
self.G = self.generate_random_connected_graph()
else:
if self.is_directed:
self.G = nx.DiGraph()
else:
self.G = nx.Graph()
if nodes:
self.G.add_nodes_from(nodes)
if edges:
self.G.add_edges_from(edges)
def generate_random_connected_graph(self):
while True:
G = nx.gnm_random_graph(n=self.nodes,m=self.edges,directed=self.is_directed)
if nx.is_connected(G):
return G
def visualize_graph(self, pos):
plt.figure()
nx.draw(self.G, pos, with_labels=True)
plt.show()
time.sleep(0.5)
# def visualize_search(self,pos):
# for i,node in enumerate(traverse_order,start=1):
# plt.clf()
# nx.draw(self.G,pos,with_labels=True,node_color=[NODE_COLOR if n==node else EDGE_COLOR for n in self.G.nodes])
# plt.pause(0.3)
# plt.show()
# time.sleep(0.5)
if __name__ == "__main__":
# Example usage:
# nodes = [1, 2, 3, 4]
# edges = [(1, 2), (2, 3), (3, 4), (4, 1)]
# pos = nx.spring_layout(nodes) # Example layout
# graph_visualizer = Graph_Visualizer(nodes=nodes, edges=edges)
nodes = [1,2,3,4]
edges = [(1, 2), (2, 3), (3, 4), (4, 1)]
pos = nx.spring_layout(nodes) # Example layout
graph_visualizer = Graph_Visualizer(nodes=nodes, edges=edges)
G = Graph_Visualizer(nodes=20,edges=20,is_random=True)
G.visualize_graph(pos)