Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab97c73

Browse files
Add Graph Visualization and Adjustments
1 parent ae2a82d commit ab97c73

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

‎Driver.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from EagerNaivePrims import prims_mst as eager_naive_prims
66
from BinaryHeapPrims import prims_mst as binary_heap_prims
77
from FibHeapPrims import prims_mst as fib_heap_prims
8+
from VisualizeMst import visualize
89

910
def read(grf):
1011

@@ -24,22 +25,27 @@ def read(grf):
2425
exit()
2526

2627
def main():
28+
grf = Graph(representation = "matrix")
29+
read(grf)
30+
adj_mat = grf.change_representation()
31+
2732
prims = [lazy_naive_prims, eager_naive_prims, binary_heap_prims, fib_heap_prims][2]
2833
representation = {lazy_naive_prims: "matrix",
2934
eager_naive_prims: "matrix",
3035
binary_heap_prims: "lists",
3136
fib_heap_prims: "lists"} [prims]
3237

33-
grf = Graph(representation = representation)
34-
read(grf)
35-
3638
start = time()
3739
precursor = prims(grf)
3840
end = time()
3941

4042
mst, cost = compute_mst_and_cost(precursor, grf)
41-
43+
4244
print(f"Cost = {cost}")
4345
print(f"Duration: {(end - start) * 10**(9) : .0f} ns")
4446

47+
edges_in_mst = [(ind, precursor[ind]) for ind in range(1, grf.nfverts)]
48+
49+
visualize(adj_mat, edges_in_mst)
50+
4551
main()

‎Graph.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ def inner_loop(u):
5151
inner_loop(u)
5252

5353
def change_representation(self):
54+
# Changes representation from matrix to lists or vice versa
55+
# and returns old representation
5456

5557
if self.representation == "matrix":
5658
adj_lists = defaultdict(dict)
5759
for i, row in enumerate(self.graph):
5860
for j, ele in enumerate(row):
5961
if ele != 0: adj_lists[i][j] = ele
62+
temp = self.graph
6063
self.graph = adj_lists
6164
self.representation = "lists"
65+
return temp
6266

6367
else:
6468
adj_mat = []
@@ -71,8 +75,10 @@ def change_representation(self):
7175
ele = 0
7276
row.append(ele)
7377
adj_mat.append(row)
78+
temp = self.graph
7479
self.graph = adj_mat
7580
self.representation = "matrix"
81+
return temp
7682

7783
def fill_with_zeros(self):
7884
# Fill the adjacency matrix with zeros to initialize mst.

‎VisualizeMst.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import networkx as nx
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
from Graph import Graph as myGraph
5+
from sys import stdin
6+
7+
def visualize(matrix, edges_in_mst):
8+
9+
grf = nx.from_numpy_matrix(np.array(matrix))
10+
11+
pos = nx.spring_layout(grf)
12+
edge_labels = nx.get_edge_attributes(grf, 'weight')
13+
14+
nx.draw_networkx_nodes(grf, pos, node_color = 'pink')
15+
nx.draw_networkx_labels(grf, pos)
16+
nx.draw_networkx_edges(grf, pos, edge_color = 'purple', width = 1.5) # show all edges, thin lines
17+
nx.draw_networkx_edges(grf, pos, edgelist = edges_in_mst, edge_color = 'red', width = 2.5) # highlight mst
18+
nx.draw_networkx_edge_labels(grf, pos, edge_labels = edge_labels)
19+
20+
plt.show()
21+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /