Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 0 -
1 ∞ -
2 ∞ -
3 ∞ -
4 ∞ -
5 ∞ -
6 ∞ -
5
2
1
15
3
2
11
3
1
1
Fringe: [(0: 0), (1: ∞), (2: ∞), (3: ∞), (4: ∞), (5: ∞), (6: ∞)]
4
0
∞
∞
∞
∞
∞
∞
1
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 0 -
1 ∞ -
2 ∞ -
3 ∞ -
4 ∞ -
5 ∞ -
6 ∞ -
5
2
1
15
3
2
11
3
1
1
4
0
∞
∞
∞
∞
∞
∞
1
Fringe: [(1: ∞), (2: ∞), (3: ∞), (4: ∞), (5: ∞), (6: ∞)]
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 2 0
2 1 0
3 ∞ -
4 ∞ -
5 ∞ -
6 ∞ -
5
2
1
15
3
2
11
3
1
1
Fringe: [(2: 1), (1: 2), (3: ∞), (4: ∞), (5: ∞), (6: ∞)]
4
∞
∞
∞
∞
∞
∞
1
2
1
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 0 -
1 2 0
2 1 0
3 ∞ -
4 ∞ -
5 ∞ -
6 ∞ -
5
2
1
15
3
2
11
3
1
1
Fringe: [(2: 1), (1: 2), (3: ∞), (4: ∞), (5: ∞), (6: ∞)]
4
∞
∞
∞
∞
1
2
1
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 2 0
2 0
3 ∞ -
4 ∞ -
5 ∞ -
6 ∞ -
5
2
1
15
3
2
11
3
1
1
Fringe: [(1: 2), (3: ∞), (4: ∞), (5: ∞), (6: ∞)]
4
∞
∞
∞
∞
1
2
Note: Vertex removal in this implementation of Prim’s is equivalent to edge addition in the conceptual version of Prim’s.
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 2 0
2 0
3 ∞ -
4 1 2
5 15 2
6 ∞ -
5
2
1
15
3
2
11
3
1
1
Fringe: [(4: 1), (1: 2), (5: 15), (3: ∞), (6: ∞)]
4
∞
∞
∞
∞
1
2
1
15
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 2 0
2 0
3 ∞ -
4 1 2
5 15 2
6 ∞ -
5
2
1
15
3
2
11
3
1
1
Fringe: [(4: 1), (1: 2), (5: 15), (3: ∞), (6: ∞)]
4
∞
∞
1
2
1
15
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 2 0
2 0
3 ∞ -
4 1 2
5 15 2
6 ∞ -
5
2
1
15
3
2
11
3
1
1
Fringe: [(4: 1), (1: 2), (5: 15), (3: ∞), (6: ∞)]
4
∞
∞
1
2
1
15
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 2 0
2 0
3 ∞ -
4 2
5 15 2
6 ∞ -
5
2
1
15
3
2
11
3
1
1
Fringe: [(1: 2), (5: 15), (3: ∞), (6: ∞)]
4
∞
∞
1
2
15
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 2 0
2 0
3 2 4
4 2
5 4 4
6 3 4
5
2
1
15
3
2
11
3
1
1
Fringe: [(1: 2), (3: 2), (6: 3), (5: 4)]
4
∞
∞
1
2
15
2
3
4
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 2 0
2 0
3 2 4
4 2
5 4 4
6 3 4
5
2
1
15
3
2
11
3
1
1
Fringe: [(1: 2), (3: 2), (6: 3), (5: 4)]
4
1
2
2
3
4
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 0
2 0
3 2 4
4 2
5 4 4
6 3 4
5
2
1
15
3
2
11
3
1
1
Fringe: [(3: 2), (6: 3), (5: 4)]
4
1
2
3
4
No need to consider edges with weight 5 and 3 since other side is already marked!
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 0
2 0
3 2 4
4 2
5 4 4
6 3 4
5
2
1
15
3
2
11
3
1
1
Fringe: [(3: 2), (6: 3), (5: 4)]
4
1
2
3
4
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 0
2 0
3 4
4 2
5 4 4
6 3 4
5
2
1
15
3
2
11
3
1
1
Fringe: [(6: 3), (5: 4)]
4
1
3
4
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 0
2 0
3 4
4 2
5 4 4
6 1 3
5
2
1
15
3
2
11
3
1
1
Fringe: [(6: 1), (5: 4)]
4
1
3
4
1
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 0
2 0
3 4
4 2
5 4 4
6 1 3
5
2
1
15
3
2
11
3
1
1
Fringe: [(6: 1), (5: 4)]
4
1
4
1
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 0
2 0
3 4
4 2
5 1 6
6 3
5
2
1
15
3
2
11
3
1
1
Fringe: [(5: 1)]
4
1
4
1
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 0
2 0
3 4
4 2
5 1 6
6 3
5
2
1
15
3
2
11
3
1
1
Fringe: [(5: 1)]
4
1
1
Prim’s Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from tree.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
# distTo edgeTo
0 -
1 0
2 0
3 4
4 2
5 6
6 3
5
2
1
15
3
2
11
3
1
1
Fringe: []
4
1