2
2
Prim's Algorithm.
3
3
4
4
Determines the minimum spanning tree(MST) of a graph using the Prim's Algorithm
5
-
6
- Create a list to store x the vertices.
7
- G = [vertex(n) for n in range(x)]
8
-
9
- For each vertex in G, add the neighbors:
10
- G[x].addNeighbor(G[y])
11
- G[y].addNeighbor(G[x])
12
-
13
- For each vertex in G, add the edges:
14
- G[x].addEdge(G[y], w)
15
- G[y].addEdge(G[x], w)
16
-
17
- To solve run:
18
- MST = prim(G, G[0])
19
5
"""
20
6
21
7
import math
22
8
23
9
24
- class vertex :
10
+ class Vertex :
25
11
"""Class Vertex."""
26
12
27
13
def __init__ (self , id ):
@@ -36,7 +22,7 @@ def __init__(self, id):
36
22
self .key = None
37
23
self .pi = None
38
24
self .neighbors = []
39
- self .edges = {} # [ vertex:distance]
25
+ self .edges = {} # { vertex:distance}
40
26
41
27
def __lt__ (self , other ):
42
28
"""Comparison rule to < operator."""
@@ -46,34 +32,72 @@ def __repr__(self):
46
32
"""Return the vertex id."""
47
33
return self .id
48
34
49
- def addNeighbor (self , vertex ):
35
+ def add_neighbor (self , vertex ):
50
36
"""Add a pointer to a vertex at neighbor's list."""
51
37
self .neighbors .append (vertex )
52
38
53
- def addEdge (self , vertex , weight ):
39
+ def add_edge (self , vertex , weight ):
54
40
"""Destination vertex and weight."""
55
41
self .edges [vertex .id ] = weight
56
42
57
43
44
+ def connect (graph , a , b , edge ):
45
+ # add the neighbors:
46
+ graph [a - 1 ].add_neighbor (graph [b - 1 ])
47
+ graph [b - 1 ].add_neighbor (graph [a - 1 ])
48
+ # add the edges:
49
+ graph [a - 1 ].add_edge (graph [b - 1 ], edge )
50
+ graph [b - 1 ].add_edge (graph [a - 1 ], edge )
51
+
52
+
58
53
def prim (graph , root ):
59
54
"""
60
55
Prim's Algorithm.
61
56
Return a list with the edges of a Minimum Spanning Tree
62
57
prim(graph, graph[0])
63
58
"""
64
- A = []
59
+ a = []
65
60
for u in graph :
66
61
u .key = math .inf
67
62
u .pi = None
68
63
root .key = 0
69
- Q = graph [:]
70
- while Q :
71
- u = min (Q )
72
- Q .remove (u )
64
+ q = graph [:]
65
+ while q :
66
+ u = min (q )
67
+ q .remove (u )
73
68
for v in u .neighbors :
74
- if (v in Q ) and (u .edges [v .id ] < v .key ):
69
+ if (v in q ) and (u .edges [v .id ] < v .key ):
75
70
v .pi = u
76
71
v .key = u .edges [v .id ]
77
72
for i in range (1 , len (graph )):
78
- A .append ([graph [i ].id , graph [i ].pi .id ])
79
- return A
73
+ a .append ((int (graph [i ].id ) + 1 , int (graph [i ].pi .id ) + 1 ))
74
+ return a
75
+
76
+
77
+ def test_vector () -> None :
78
+ """
79
+ # Creates a list to store x vertices.
80
+ >>> x = 5
81
+ >>> G = [Vertex(n) for n in range(x)]
82
+
83
+ >>> connect(G, 1, 2, 15)
84
+ >>> connect(G, 1, 3, 12)
85
+ >>> connect(G, 2, 4, 13)
86
+ >>> connect(G, 2, 5, 5)
87
+ >>> connect(G, 3, 2, 6)
88
+ >>> connect(G, 3, 4, 6)
89
+ >>> connect(G, 0, 0, 0) # Generate the minimum spanning tree:
90
+ >>> MST = prim(G, G[0])
91
+ >>> for i in MST:
92
+ ... print(i)
93
+ (2, 3)
94
+ (3, 1)
95
+ (4, 3)
96
+ (5, 2)
97
+ """
98
+
99
+
100
+ if __name__ == "__main__" :
101
+ import doctest
102
+
103
+ doctest .testmod ()
0 commit comments