-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrim's_Algorithm.py
47 lines (37 loc) · 1.35 KB
/
Prim's_Algorithm.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
class Graph():
def __init__(self, vertices):
self.vertices = vertices
self.graph = []
def prim_mst(self, start):
visited = [False] * self.vertices
min_weight = [float('inf')] * self.vertices
min_weight[start] = 0
parent = [-1] * self.vertices
for _ in range(self.vertices):
min_vertex = -1
for v in range(self.vertices):
if not visited[v] and (min_vertex == -1 or min_weight[v] < min_weight[min_vertex]):
min_vertex = v
visited[min_vertex] = True
for u in range(self.vertices):
if self.graph[min_vertex][u] != 0 and not visited[u] and self.graph[min_vertex][u] < min_weight[u]:
min_weight[u] = self.graph[min_vertex][u]
parent[u] = min_vertex
mst_edges = []
for v in range(self.vertices):
if parent[v] != -1:
mst_edges.append((parent[v] + 1, v + 1, self.graph[parent[v]][v]))
return mst_edges
g = Graph(5)
g.graph = [
[0, 10, 0, 30, 100],
[10, 0, 50, 0, 0],
[0, 50, 0, 20, 10],
[30, 0, 20, 0, 60],
[100, 0, 10, 60, 0]
]
starting_vertex = 0
mst_edges = g.prim_mst(starting_vertex)
print("Minimum Spanning Tree edges:")
for edge in mst_edges:
print(f"({edge[0]} - {edge[1]}) with weight {edge[2]}")