Graphs MST
Graphs MST
Central office
Wiring: Naïve Approach
Central office
Expensive!
Wiring: Better Approach
Central office
• Prim’s algorithm: Start with any one node in the spanning tree,
and repeatedly add the cheapest edge, and the node it leads to,
for which the node is not already in the spanning tree.
• Here, we consider the spanning tree to consist of both nodes and edges
7
Links Will Form a Spanning Tree
Cost (T) = 47 + 23 + 75 + 74 + 55 + 74 + 79
= 427
Minimum Weight Spanning Trees
7576562804644601479086318651590413464814067\
83308840339247043281018024279971356804708193\
5219466686248779296875
Minimum Spanning Tree (MST)
A minimum spanning tree is a subgraph of an undirected weighted
graph G, such that
9 b 9 b
a 2 6 a 2 6
d d
4 5 4 5
5 4 5 4
5 e 5 e
c c
Prim’s Algorithm
Prim’s algorithm is a “greedy” algorithm
Greedy algorithms find solutions based on a sequence of choices which are “locally” optimal at each step.
Initialization
a. Pick a vertex r to be the root
b. Set D(r) = 0, parent(r) = null
c. For all vertices v V, v r, set D(v) =
d. Insert all vertices into priority queue P,
using distances as the keys
9 Vertex Parent
b
e -
a 2 6 e a b c d
d
4 5 0
5 4
5 e
c
Prim’s Algorithm
While P is not empty: //P is priority queue
5 e
c
Vertex Parent
e -
While P is not empty: //P is priority queue
a c b b e
1. Select the next vertex u to add to the tree
2 4 5 c d
u = P.deleteMin()
d e
2. Update the weight of each vertex w adjacent to a d
u which is not in the tree (i.e., w P)
If weight(u,w) < D(w),
a. parent(w) = u d is deleted , and we update
b. D(w) = weight(u,w) the distances and parent for its adjacent vertices
c. Update the priority queue to reflect
new distance for w (updated nodes gets new parent !)
Prim’s algorithm
9
b Vertex Parent
a 2 6 e -
d a c b b e
4 5 c d
5 4 2 4 5
d e
5 e a d
c
5 e
c Vertex Parent
e -
b e
The final minimum spanning tree c d
d e
a d
Running time of Prim’s algorithm (without heaps)
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
32
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
34
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
35
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
36
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
37
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
38
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
39
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
40
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
41
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
42
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
43
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
44
Example 2704
867 BOS
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
45
Data Structures
for Kruskal’s Algorithm
|E| times:
Pick the lowest cost edge…
findMin/deleteMin
|E| times:
Data structures?
If u and v are not already connected…
…connect u and v. Runtime?
find representative
union