-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractical-10.py
executable file
·94 lines (78 loc) · 2.81 KB
/
practical-10.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
{
"Author": "Fenil Gandhi",
"Version": "Python 3.6.2",
"Description": "Implement prim’s algorithm",
"Input Parameters": "Graph Object constructed using each edge",
"Input": {
(0, 1): 7,
(0, 3): 8,
(1, 2): 6,
(1, 3): 3,
(2, 3): 4,
(2, 4): 2,
(2, 5): 5,
(3, 4): 3,
(4, 5): 2,
},
"Output": "Using Prim's Algorithm, we get MST having 17 weight of edges [(0, 1), (1, 3), (3, 4), (4, 2), (4, 5)]",
}
class Graph():
def __init__(self):
self.vertices = []
self.edges = {}
self.max_len = -1
def addEdge(self, a, b, length):
if a not in self.vertices:
self.vertices.append(a)
self.edges[a] = {}
if b not in self.vertices:
self.vertices.append(b)
self.edges[b] = {}
self.edges[a][b] = length
self.edges[b][a] = length
if length > self.max_len:
self.max_len = length + 1
def getEdges(self, a):
if a in self.vertices:
return self.edges[a]
def Prims(self, starting_node):
mst_weight = 0
mst_edges = []
distance_to_nodes = dict([(v, (self.max_len, None)) for v in self.vertices])
# Add the starting node
distance_to_nodes[starting_node] = (-1, None)
for vertex, length in self.getEdges(starting_node).items():
if (distance_to_nodes[vertex][0] > length):
distance_to_nodes[vertex] = (length, starting_node)
while True:
min_edge = self.max_len
selected_vertex = None
for vertex, length in distance_to_nodes.items():
# Vertex already selected
if length[0] < 0:
continue
elif (length[0] < min_edge):
selected_vertex = vertex
min_edge = length[0]
if (min_edge == self.max_len):
break
else:
mst_edges.append((distance_to_nodes[selected_vertex][1], selected_vertex))
mst_weight += distance_to_nodes[selected_vertex][0]
distance_to_nodes[selected_vertex] = (-1, None)
for vertex, length in self.getEdges(selected_vertex).items():
if (distance_to_nodes[vertex][0] > length):
distance_to_nodes[vertex] = (length, selected_vertex)
print("Using Prim's Algorithm, we get MST having {0} weight of edges {1}".format(mst_weight, mst_edges))
if __name__ == '__main__':
graph = Graph()
graph.addEdge(0, 1, 7)
graph.addEdge(0, 3, 8)
graph.addEdge(1, 2, 6)
graph.addEdge(1, 3, 3)
graph.addEdge(2, 3, 4)
graph.addEdge(2, 4, 2)
graph.addEdge(2, 5, 5)
graph.addEdge(3, 4, 3)
graph.addEdge(4, 5, 2)
graph.Prims(0)