11.05a.Prim's_algorithm
11.05a.Prim's_algorithm
Prim’s algorithm
ece.uwaterloo.ca
[email protected]
Outline
Strategy
v1
Prim’s algorithm
4
Strategy
v2
e1
v1
Prim’s algorithm
5
Strategy
Strategy:
– Suppose we have a known minimum spanning tree on k < n vertices
– How could we extend this minimum spanning tree?
Prim’s algorithm
6
Strategy
Add that edge ek with least weight that connects this minimum
spanning tree to a new vertex vk + 1
– This does create a minimum spanning tree on k + 1 nodes—there is no
other edge we could add that would connect this vertex
– Does the new edge, however, belong to the minimum spanning tree on
all n vertices?
vk + 1
ek
Prim’s algorithm
7
Strategy
vk + 1
ek
Prim’s algorithm
8
Strategy
vk + 1
ek
e
Prim’s algorithm
9
Strategy
ek e 0
vk + 1
ek
e
Prim’s algorithm
10
Strategy
vk + 1
ek + 1
e
Prim’s algorithm
11
Strategy
vk + 1
ek
e
Prim’s algorithm
12
Strategy
Recall that we did not prescribe the value of k, and thus, k could be
any value, including k = 1
vk + 1
ek
e
Prim’s algorithm
13
Strategy
Recall that we did not prescribe the value of k, and thus, k could be
any value, including k = 1
– Given a single vertex e1, it forms a minimum spanning tree on one
vertex
v1
Prim’s algorithm
14
Strategy
v2
e1
v1
Prim’s algorithm
15
Prim’s Algorithm
For example:
– Add three member variables to the vertex class
– Track three tables
Prim’s algorithm
17
Prim’s Algorithm
Initialization:
– Select a root node and set its distance as 0
– Set the distance to all other vertices as ∞
– Set all vertices to being unvisited
– Set the parent pointer of all vertices to 0
Prim’s algorithm
18
Prim’s Algorithm
Prim’s Algorithm
Halting Conditions:
– There are no unvisited vertices which have a distance < ∞
If there are vertices with distance ∞, then the graph is not connected
and we only have a minimum spanning tree of the connected sub-
graph containing the root
Prim’s algorithm
20
Prim’s Algorithm
Let us find the minimum spanning tree for the following undirected
weighted graph
Prim’s algorithm
21
Prim’s Algorithm
Distance Parent
1 F 0 0
2 F ∞ 0
3 F ∞ 0
4 F ∞ 0
5 F ∞ 0
6 F ∞ 0
7 F ∞ 0
8 F ∞ 0
9 F ∞ 0
Prim’s algorithm
22
Prim’s Algorithm
Distance Parent
1 T 0 0
2 F 4 1
3 F ∞ 0
4 F 1 1
5 F 8 1
6 F ∞ 0
7 F ∞ 0
8 F ∞ 0
9 F ∞ 0
Prim’s algorithm
23
Prim’s Algorithm
Prim’s Algorithm
Distance Parent
1 T 0 0
2 F 2 4
3 F ∞ 0
4 T 1 1
5 F 8 1
6 F ∞ 0
7 F 9 4
8 F 8 4
9 F ∞ 0
Prim’s algorithm
25
Prim’s Algorithm
Prim’s Algorithm
Distance Parent
1 T 0 0
2 T 2 4
3 F 2 2
4 T 1 1
5 F 6 2
6 F 1 2
7 F 9 4
8 F 8 4
9 F ∞ 0
Prim’s algorithm
27
Prim’s Algorithm
Prim’s Algorithm
Distance Parent
1 T 0 0
2 T 2 4
3 F 2 2
4 T 1 1
5 F 3 6
6 T 1 2
7 F 9 4
8 F 7 6
9 F 8 6
Prim’s algorithm
29
Prim’s Algorithm
Prim’s Algorithm
Distance Parent
1 T 0 0
2 T 2 4
3 T 2 2
4 T 1 1
5 F 2 3
6 T 1 2
7 F 9 4
8 F 7 6
9 F 8 6
Prim’s algorithm
31
Prim’s Algorithm
At this point, we can extend the tree by adding the edge (3, 5)
Prim’s algorithm
32
Prim’s Algorithm
Distance Parent
1 T 0 0
2 T 2 4
3 T 2 2
4 T 1 1
5 T 2 3
6 T 1 2
7 F 4 5
8 F 1 5
9 F 5 5
Prim’s algorithm
33
Prim’s Algorithm
At this point, there are three possible edges which we could include
which will extend the tree
Prim’s Algorithm
Distance Parent
1 T 0 0
2 T 2 4
3 T 2 2
4 T 1 1
5 T 2 3
6 T 1 2
7 F 4 5
8 T 1 5
9 F 3 8
Prim’s algorithm
35
Prim’s Algorithm
Distance Parent
1 T 0 0
2 T 2 4
3 T 2 2
4 T 1 1
5 T 2 3
6 T 1 2
7 F 4 5
8 T 1 5
9 T 3 8
Prim’s algorithm
36
Prim’s Algorithm
And neither are there any vertices to update when visiting vertex 7
Distance Parent
1 T 0 0
2 T 2 4
3 T 2 2
4 T 1 1
5 T 2 3
6 T 1 2
7 T 4 5
8 T 1 5
9 T 3 8
Prim’s algorithm
37
Prim’s Algorithm
Prim’s Algorithm
Distance Parent
1 T 0 0
2 T 2 4
3 T 2 2
4 T 1 1
5 T 2 3
6 T 1 2
7 T 4 5
8 T 1 5
9 T 3 8
Prim’s algorithm
39
Prim’s Algorithm
To summarize:
– we begin with a vertex which represents the root
– starting with this trivial tree and iteration, we find the shortest edge
which we can add to this already existing tree to expand it
Can we do better?
– Recall, we only need the shortest edge next
– How about a priority queue?
• Assume we are using a binary heap
• We will have to update the heap structure—this requires additional work
Prim’s algorithm
41
Thus, the total run time is O(|V| ln(|V|) + |E| ln(|V|)) = O(|E| ln(|V|))
Prim’s algorithm
42
Summary
References
Wikipedia, http://en.wikipedia.org/wiki/Minimum_spanning_tree
Wikipedia, http://en.wikipedia.org/wiki/Prim’s_algorithm
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.