Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s...
Transcript of Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s...
![Page 1: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/1.jpg)
Minimum Spanning Trees and Prim’s Algorithm
Version of October 23, 2014
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 1 / 39
![Page 2: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/2.jpg)
Outline
Spanning trees and minimum spanning trees (MST).
Tools for solving the MST problem.
Prim’s algorithm for the MST problem.
The ideaThe algorithmAnalysis
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 2 / 39
![Page 3: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/3.jpg)
Spanning Trees
Definition
A subgraph T of a undirected graph G = (V ,E ) is a spanning treeof G if it is a tree and contains every vertex of G
Example
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 3 / 39
![Page 4: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/4.jpg)
Spanning Trees
Theorem
Every connected graph has a spanning tree.
Question
Why is this true?
Question
Given a connected graph G , how can you find a spanning tree ofG?
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 4 / 39
![Page 5: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/5.jpg)
Weighted Graphs
Definition
A weighted graph is a graph, in which each edge has a weight(some real number) Could denote length, time, strength, etc.
Example
Definition
Weight of a graph: The sum of the weights of all edges
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 5 / 39
![Page 6: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/6.jpg)
Minimum Spanning Trees
Definition
A Minimum spanning tree (MST) of an undirected connectedweighted graph is a spanning tree of minimum weight (among allspanning trees).
Example
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 6 / 39
![Page 7: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/7.jpg)
Remark
The minimum spanning tree may not be unique
Example
1
2
6724
1
2
6724
MST1 MST2weighted graph
1
2 2
100
6724
Note: if the weights of all the edges are distinct, MST is provablyunique (proof will follow from later results).
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 7 / 39
![Page 8: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/8.jpg)
Minimum Spanning Tree Problem
Definition (MST Problem)
Given a connected weighted undirected graph G , design analgorithm that outputs a minimum spanning tree (MST) of G .
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 8 / 39
![Page 9: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/9.jpg)
Outline
Spanning trees and minimum spanning trees (MST).
Tools for solving the MST problem.
Prim’s algorithm for the MST problem.
The ideaThe algorithmAnalysis
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 9 / 39
![Page 10: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/10.jpg)
Tools for solving the MST Problem
A tree is an acyclic graph
1 start with an empty graph
2 try to add edges one at a time, subject to not creating a cycle
3 if after adding each edge we are sure that the resulting graphis a subset of some minimum spanning tree, then, after n − 1steps we are done.
Hard part is ensuring (3)!
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 10 / 39
![Page 11: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/11.jpg)
Generic Algorithm for MST problem
Definition
Let A be a set of edges such that A ⊆ T , where T is some MST.Edge (u, v) is safe edge for A, if A ∪ {(u, v)} is also a subset ofsome MST
If at each step, we can find a safe edge (u, v), we can grow aMST
Generic-MST(G, w)
beginA= EMPTY;while A does not form a spanning tree do
find an edge (u, v) that is safe for A;add (u, v) to A;
endreturn A
end
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 11 / 39
![Page 12: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/12.jpg)
Some Definitions
Definition
Let G = (V ,E ) be a connected and undirected graph.A cut (S ,V − S) of G is a partition of V .
Example
Definition
An edge (u, v) ∈ E crosses the cut (S ,V − S) if one of itsendpoints is in S , and the other is in V − S .A cut respects a set A of edges if no edge in A crosses the cut.An edge is a light edge crossing a cut if its weight is the minimumof any edge crossing the cut.
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 12 / 39
![Page 13: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/13.jpg)
How to Find a Safe Edge?
Lemma
Let G = (V ,E ) be a connected, undirected graph with areal-valued weight function w defined on E
A be a subset of E that is included in some minimumspanning tree for G .
Let
(S ,V − S) be any cut of G that respects A
(u, v) be a light edge crossing the cut (S ,V − S)
Then, edge (u, v) is safe for A.
This implies we can find a safe edge by
1 first finding a cut that respects A,
2 then finding a light edge crossing thatcut.
That light edge is a safe edge.Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 13 / 39
![Page 14: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/14.jpg)
Proof
Let A ⊆ T , where T is a MST.
Case 1: (u, v) ∈ T
A ∪ {(u, v)} ⊆ T .Hence (u, v) is safe for A.
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 14 / 39
![Page 15: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/15.jpg)
Proof (cont’d)
Case 2: (u, v) /∈ T
Idea: construct another MST T′
s.t. A ∪ {(u, v)} ⊆ T ′.Consider the unique path P in T from u to v .Since u and v are on opposite sides of the cut (S ,V − S),
There is at least one edge in P that crosses the cut.Let (x , y) be such an edge.
Since the cut respects A, (x , y) 6∈A.
Since (u, v) is a light edge crossing the cut, we havew(u, v)≤w(x , y).
u
v
xy
S V − S
u
v
xy
S V − S
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 15 / 39
![Page 16: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/16.jpg)
Adding (u, v) to T , creates a cycle with P.Removing any edge from this cycle gives a tree again.In particular, adding (u, v) and removing (x , y) creates a newtree T
′.
The weight of T′
is
w(T ′) = w(T )− w(x , y) + w(u, v)
≤ w(T )
Since T is a MST, W (T ) ≤W (T ′) so W (T ′) = W (T ) andT is also an MST.
But A ∪ {(u, v)} ⊆ T′, so (u, v), is safe for A.
The Lemma is proved.
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 16 / 39
![Page 17: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/17.jpg)
Outline
Spanning trees and minimum spanning trees (MST).
Tools for solving the MST problem.
Prim’s algorithm for the MST problem.
The ideaThe algorithmAnalysis
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 17 / 39
![Page 18: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/18.jpg)
Prim’s Algorithm
The generic algorithm gives us an idea how to ’grow’ a MST.
If you read the theorem and proof carefully, you will noticethat the choice of a cut (and hence a corresponding lightedge) in each iteration is arbitrary.
We can select any cut (that respects current edge set A) andfind a light edge crossing that cut to proceed.
Different ways of chosing cuts correspond to differentalgorithms.
The two major ones are Prim’s algorithm and Kruskal’salgorithm,
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 18 / 39
![Page 19: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/19.jpg)
Prim’s Algorithm
Prim’s algorithm
grows a tree, adding a new light edge in each iteration,creating a new tree.
Growing a tree
Start by picking any vertex r to be the root of the tree.
While the tree does not contain all vertices in the graph:find shortest edge leaving tree and add it to the tree.
We will show that these steps can be implemented in totalO(E · logV ).
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 19 / 39
![Page 20: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/20.jpg)
More Details
Example
e
2420
r
ab
c
d
26
f
g ir
ab
c
d e
f
g i
8 8
12
1614
new
242026
1614
12
23 23
new edge
1212
h h
Step 0:
Choose any element r ; set S = {r} and A = ∅.(Take r as the root of our spanning tree.)
Step 1:
Find a lightest edge such that one endpoint is in S and theother is in V \ S .
Add this edge to A and its (other) endpoint to S .
Step 2:
If V \ S = ∅, then stop and output (minimum) spanning tree(S ,A);Otherwise, go to Step 1.
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 20 / 39
![Page 21: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/21.jpg)
Worked Example
a
b
c
d
e
f
g
4
8
9
8
2
1
9
7
10
5
6
2
b
c
d
e
f
g
4
8
10
8
2
1
7
95
6
2
S={a}
Step 0
aV \ S = {b,c,d,e,f,g}
9
Connected graph
lightest edge = {a,b}
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 21 / 39
![Page 22: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/22.jpg)
Prim’s Example – Continued
b
c
d
e
f
g
4
8
9
8
2
1
9
7
10
5
6
2
c
d
e
f
g
8
9
10
8
2
1
7
95
6
2
a
a
Step 1.1
Step 1.1 after
4
S={a}
S={a,b}b
before
V \ S = {b,c,d,e,f,g}
V \ S = {c,d,e,f,g}
lightest edge = {a,b}
lightest edge = {b,d}, {a,c}
A={}
A={{a,b}}
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 22 / 39
![Page 23: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/23.jpg)
Prim’s Example – Continued
c
d
e
f
g
4
8
9
8
2
1
9
7
10
5
6
2
c
e
f
g
8
10
8
2
1
7
95
6
2
a
a
4b
beforeb Step 1.2 S={a,b}
Step 1.2 after
S={a,b,d}
d
V \ S = {c,d,e,f,g}
V \ S = {c,e,f,g}9
lightest edge = {b,d}, {a,c}
lightest edge = {d,c}
A={{a,b}}
A={{a,b},{b,d}}
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 23 / 39
![Page 24: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/24.jpg)
Prim’s Example – Continued
c
e
f
g
4
8
9
8
2
1
9
7
10
5
6
2
e
f
g
8
9
10
8
2
1
7
95
6
2
a
a
4b
beforeb
d
Step 1.3
Step 1.3 after
S={a,b,d}d
S={a,b,c,d}
V \ S = {c,e,f,g}
V \ S = {e,f,g}
c
lightest edge = {d,c}
lightest edge = {c,f}
A={{a,b},{b,d}}
A={{a,b},{b,d},{c,d}}
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 24 / 39
![Page 25: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/25.jpg)
Prim’s Example – Continued
e
f
g
4
8
9
8
2
1
9
7
10
5
6
2
e
g
8
9
10
8
2
1
7
95
6
2
a
a
4b
beforeb
d
d
c
S={a,b,c,d}
V \ S = {e,f,g}
c
S={a,b,c,d,f}V \ S = {e,g}
Step 1.4
Step 1.4 after
f
lightest edge = {c,f}
lightest edge = {f,g}
A={{a,b},{b,d},{c,d}}
A={{a,b},{b,d},{c,d},{c,f}}
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 25 / 39
![Page 26: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/26.jpg)
Prim’s Example – Continued
e
g
4
8
9
8
2
1
9
7
10
5
6
2
e
8
9
10
8
2
1
7
95
6
2
a
a
4b
beforeb
d
d
c
c
f
S={a,b,c,d,f}V \ S = {e,g}
f
Step 1.5
Step 1.5 after
S={a,b,c,d,f,g}V \ S = {e}
{f,g}}
g
lightest edge = {f,g}
lightest edge = {f,e}
A={{a,b},{b,d},{c,d},{c,f}}
A={{a,b},{b,d},{c,d},{c,f},
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 26 / 39
![Page 27: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/27.jpg)
Prim’s Example – Continued
e4
8
9
8
2
1
9
7
10
5
6
2
8
9
10
8
2
1
7
95
6
2
a
a
4b
beforeb
d
d
c
c
f
f
g
S={a,b,c,d,f,g}V \ S = {e}
{f,g}}
g
Step 1.6
Step 1.6 after
S={a,b,c,d,e,f,g}V \ S = {}
{f,g},{f,e}}
MST completed
e
lightest edge = {f,e}
A={{a,b},{b,d},{c,d},{c,f},
A={{a,b},{b,d},{c,d},{c,f},
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 27 / 39
![Page 28: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/28.jpg)
Outline
Spanning trees and minimum spanning trees (MST).
Strategy for solving the MST problem.
Prim’s algorithm for the MST problem.
The ideaThe algorithmAnalysis
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 28 / 39
![Page 29: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/29.jpg)
Recall Idea of Prim’s Algorithm
Step 0: Choose any element r and set S = {r} and A = ∅.(Take r as the root of our spanning tree.)
Step 1: Find a lightest edge such that one endpoint is in Sand the other is in V \ S .Add this edge to A and its (other) endpoint to S .
Step 2: If V \ S = ∅, then stop and output the minimumspanning tree (S ,A); Otherwise go to Step 1.
Questions
1 Why does this produce a minimum spanning tree?
2 How does the algorithm find the lightest edge and update Aefficiently?
3 How does the algorithm update S efficiently?
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 29 / 39
![Page 30: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/30.jpg)
Prim’s Algorithm
Question
How does the algorithm update S efficiently?
Answer: Color the vertices.
Initially all are white.
Change the color to black when the vertex is moved to S .
Use color[v ] to store color.
Question
How does the algorithm find a lightest edge and update Aefficiently?
Answer:
1 Use a priority queue to find the lightest edge.
2 Use pred[v ] to update A.
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 30 / 39
![Page 31: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/31.jpg)
Reviewing Priority Queues
Priority Queue is a data structure
can be implemented as a heap
Supports the following operations:
Insert(u, key): Insert u with the key value key in Q.
u = Extract-Min(): Extract the item with minimum key value.
Decrease-Key(u, new -key): Decrease u’s key value to new -key .
Remark: We already saw how to implement Insert and Extract-Min(and Delete) in O(log |Q|) time.Same ideas can also be used to implement Decrease-Key inO(log |Q|) time.Alternatively, can implement Decrease-Key using Delete followedby Insert.
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 31 / 39
![Page 32: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/32.jpg)
Using a Priority Queue to Find the Lightest Edge
Each item of the queue is a pair (u, key [u]), where• u is a vertex in V \ S ,• key [u] is the weight of the lightest edge from u to any vertex in S .(The endpoint of this edge in S is stored in pred [u], which is used tobuild the MST tree.)
r
ab
c
d e
f
g ir
ab
c
d e
f
g i
242026
1614
8
242026
1614
8
1212
23 23
new edgekey[f] = 8, pred[f] = e
1212
key[i] = infinity, pred[i] = nil key[i] = 23, pred[i] = f
After adding the new edgeand vertex f, update the key[v] and pred[v] for each vertex v adjacent to f
key[g] = 16, pred[g] = c
key[h] = 24, pred[h] = b
f has the minimum key
h h
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 32 / 39
![Page 33: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/33.jpg)
Description of Prim’s Algorithm
beginforeach u ∈ V do
color [u] = WHITE; key [u] = +∞; // initialize
endkey [r ] = 0; pred [r ] = NIL; // start at root
Q = new PriQueue(V ); // put vertices in Qwhile Q is nonempty do
u= Q.Extract-Min(); // lightest edge
foreach v ∈ adj [u] doif (color [v ] = WHITE)&&(w [u, v ] < key [v ]) then
key [v ] = w [u, v ]; // new lightest edge
Q.Decrease-Key(v , key [v ]);pred [v ] = u;
end
endcolor [u] = BLACK;
end
end
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 33 / 39
![Page 34: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/34.jpg)
Description of Prim’s Algorithm...
When the algorithm terminates, Q = ∅ and the MST is
T = {{v , pred [v ]} : v ∈ V \ {r}}.
The pred pointers define the MST as an inverted tree rootedat r .
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 34 / 39
![Page 35: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/35.jpg)
Example for Running Prim’s Algorithm
a
b
c
d
e
f
1
2
3
4
5
1
103
4
u
key[u]
pred[u]
a b c d e f
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 35 / 39
![Page 36: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/36.jpg)
Outline
Spanning trees and minimum spanning trees (MST).
Strategy for solving the MST problem.
Prim’s algorithm for the MST problem.
The ideaThe algorithmAnalysis
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 36 / 39
![Page 37: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/37.jpg)
Analysis of Prim’s Algorithm...
beginforeach u ∈ V do
key [u] = +∞; color [u] = WHITE; // O(V )endkey [r ] = 0; pred [r ] = NIL;Q = new PriQueue(V ); // O(V )while Q is nonempty do
u= Q.Extract-Min(); // Do this for each vertex
foreach v ∈ adj [u] do// Do the following for each edge twice
if (color [v ] = WHITE)&&(w [u, v ] < key [v ]) thenkey [v ] = w [u, v ]; pred [v ] = u;Q.Decrease-Key(v , key [v ]); // This is bottleneck
end
endcolor [u] = BLACK;
end
end
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 37 / 39
![Page 38: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/38.jpg)
Analysis of Prim’s Algorithm
The data structure PriQueue (heap) supports the following twooperations:
(O(|V |) for creating new Priority Queue
O(logV ) for Extract-Min on a PriQueue of size at most V .Total cost: O(V logV )
O(logV ) time for Decrease-Key on a PriQueue of size atmost V .Total cost: O(E logV ).
Total cost is then O((V + E ) logV ) = O(E logV )
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 38 / 39
![Page 39: Minimum Spanning Trees and Prim's Algorithm · algorithms. The two major ones are Prim’s algorithm and Kruskal’s algorithm, Version of October 23, 2014 Minimum Spanning Trees](https://reader030.fdocuments.net/reader030/viewer/2022040412/5edc7768ad6a402d66672264/html5/thumbnails/39.jpg)
Going Further
A more advanced Priority Queue implementation called FibonnaciHeaps allow
O(1) for inserting each item
O(log |V |) for Extract-Min
O(1) (amortized) for each Decrease-Key
Since algorithm performs |V | Inserts, |V | Extract-Mins and at mostE Decrease-Keys this leads to a O(|E |+ |V | log |V |) algorithm,improving upon the O(E logV ) more naive implementation.
Version of October 23, 2014 Minimum Spanning Trees and Prim’s Algorithm 39 / 39