-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prims Algorithm
55 lines (38 loc) · 1.31 KB
/
Prims Algorithm
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
// To find the minimum spanning tree for given graph.
// TC: O(V^2) here. (O(ElogV) with binary heap)
INF = float('inf')
def selectMinVertex(dist, addedToMST):
mini = INF
node = -1
for i in range(len(dist)):
if not addedToMST[i] and dist[i] < mini:
mini = dist[i]
node = i
return node
def primsMST(graph):
V = len(graph)
dist = [INF] * V
addedToMST = [False] * V
parent = [-1] * V
dist[0] = 0 # source vertex
for i in range(V - 1): # v - 1 edges
u = selectMinVertex(dist, addedToMST) # selecting minimum dist vertex greedy method
addedToMST[u] = True # included in mst
for j in range(V): # computing adjacent vertices
# 3 conditions:-
- Edge is between u and j
- vertex j is not in mst
- edge weight is smaller
if graph[u][j] != 0 and not addedToMST[j] and dist[j] > graph[u][j]:
dist[j] = graph[u][j]
parent[j] = u
for i in range(1, V):
print(parent[i], '->', i, ':', dist[i])
V = 6
graph = [[0, 4, 6, 0, 0, 0],
[4, 0, 6, 3, 4, 0],
[6, 6, 0, 1, 8, 0],
[0, 3, 1, 0, 2, 3],
[0, 4, 8, 2, 0, 7],
[0, 0, 0, 3, 7, 0]]
primsMST(graph)