Download presentation
Presentation is loading. Please wait.
1
An Illustration of Prim's Algorithm
Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida
2
Goal: find a minimum spanning tree (MST) for the given graph.
4 A D 1 5 7 2 E 3 4 B C 9 Note that there may be more than one valid MST for a graph. A deterministic algorithm will always produce the same MST for a given graph, but precisely which MST it produces will depend on your tie-breaking mechanisms. We will see an illustration of this here.
3
Start by initializing the value at each vertex to infinity
start vertex inf (This vertex is included in the MST right off the bat, as denoted here by its color.) 4 A D 1 5 7 2 E inf 3 4 B C 9 inf inf Start by initializing the value at each vertex to infinity (denoted here by “inf”), except for the start vertex, which has a value of zero.
4
4, A 4 A D 1 5 7 2 E 1, A 3 4 B C 9 7, A inf Update the values at vertices adjacent to A. (If the edge weight from A to some vertex is less than the value already listed at that vertex, we update that value accordingly.)
5
The value associated with vertex E (1) was less than the values at the other white vertices (5, 7, infinity), so we added this vertex to the MST. Here, the A next to the 1 indicates that the edge we use to connect E is the one going to vertex A. Vertices A and E are now in the MST, as is the edge connecting them. 4, A 4 A D 1 5 7 2 E 1, A 3 4 B C 9 7, A inf Of all the vertices not already in the MST (the white vertices), we pick the one with the smallest value associated with it and add it to the MST via the edge that got us there with the cost indicated.
6
We don't even consider updating the values at vertices that are already in the MST.
Here, the edge from E to D has a cost greater than the one already listed at this node, so we do not change this value. 4, A 4 A D 1 5 7 2 E 1, A 3 4 B C 9 3, E 4, E Update the values at vertices adjacent to the new vertex, E. (If the edge weight from E to some vertex is less than the value already listed at that vertex, we update that value accordingly.)
7
4, A 4 A D 1 5 7 2 E 1, A 3 4 B C 9 3, E 4, E Of all the vertices not already in the MST (the white vertices), we pick the one with the smallest value associated with it and add it to the MST via the edge that got us there with the cost indicated.
8
4, A 4 A D 1 5 Recall that we don't even consider updating the values at vertices that are already in the MST, like vertices A and B. 7 2 E We do consider updating the value at C, but since 9 is greater than 4, we decide not to make a change. 1, A 3 4 B C 9 3, E 4, E Update the values at vertices adjacent to the new vertex, B. (If the edge weight from B to some vertex is less than the value already listed at that vertex, we update that value accordingly.)
9
4, A I'm choosing to include C because it comes before D in the alphabet, but we could just as easily have chosen vertex D here. Any time we perform a tie breaker like this, it means there is more than one MST for the graph (all of which are equally valid)! 4 A D 1 5 7 2 E 1, A 3 4 B C 9 3, E 4, E Of all the vertices not already in the MST (the white vertices), we pick the one with the smallest value associated with it and add it to the MST via the edge that got us there with the cost indicated.
10
We update this one, since 2 is less than the previous value of 4.
2, E 4 A D 1 5 Recall that we don't even consider updating the values at vertices that are already in the MST, like vertices B and E. 7 2 E 1, A 3 4 B C 9 3, E 4, E Update the values at vertices adjacent to the new vertex, C. (If the edge weight from C to some vertex is less than the value already listed at that vertex, we update that value accordingly.)
11
This was the only vertex not in the MST already, so of course it's the one we pick.
2, E 4 A D 1 5 7 2 E 1, A 3 4 B C 9 3, E 4, E Of all the vertices not already in the MST (the white vertices), we pick the one with the smallest value associated with it and add it to the MST via the edge that got us there with the cost indicated.
12
Resulting MST A D 1 2 E 3 4 B C Now that all the vertices are included, the algorithm terminates and we have our MST.
13
Another Perfectly Valid MST
4 A D 1 2 E 3 B C Note that this is also a valid MST for the original graph, and we would have come up with it if we had decided to include vertex D instead of vertex C back on slide 9.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.