0% found this document useful (0 votes)
6 views

3_1 Graph minimum spanning tree

Uploaded by

Ravi Rane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

3_1 Graph minimum spanning tree

Uploaded by

Ravi Rane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 59

Minimum Spanning Trees

Minimum Spanning Tree


● Problem: given a connected, undirected,
weighted graph:

6 4
5 9

14 2
10
15

3 8
Minimum Spanning Tree
● Problem: given a connected, undirected,
weighted graph, find a spanning tree using
edges that minimize the total weight
6 4
5 9

14 2
10
15

3 8
Minimum Spanning Tree
● Which edges form the minimum spanning tree
(MST) of the below graph?

A
6 4
5 9
H B C

14 2
10
15
G E D
3 8
F
Minimum Spanning Tree
● Answer:

A
6 4
5 9
H B C

14 2
10
15
G E D
3 8
F
Applications of MST
■ Find the least expensive way to connect a set of
cities, terminals, computers, etc.
Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u  Q
key[u] = ;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
Q = V[G]; 5 9

for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
p[r] = NULL;
3 8
while (Q not empty)
u = ExtractMin(Q); Run on example graph
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
  
p[r] = NULL;
3  8
while (Q not empty)
u = ExtractMin(Q); Run on example graph
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
r 0  
p[r] = NULL;
3  8
while (Q not empty)
u = ExtractMin(Q); Pick a start vertex r
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
u 0  
p[r] = NULL;
3  8
while (Q not empty)
u = ExtractMin(Q); Red vertices have been removed from Q
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
u 0  
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q); Red arrows indicate parent pointers
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
14  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
u 0  
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
14  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0  
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
14  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
10  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
10  
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
10 2 
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
Q = V[G]; 5 9
10 2 
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
u
Q = V[G]; 5 9
10 2 
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6  4
u
Q = V[G]; 5 9
10 2 9
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4 4
u
Q = V[G]; 5 9
10 2 9
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4 4
u
Q = V[G]; 5 9
5 2 9
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u
MST-Prim(G, w, r) 6 4 4
Q = V[G]; 5 9
5 2 9
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) u 6 4 4
Q = V[G]; 5 9
5 2 9
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u
MST-Prim(G, w, r) 6 4 4
Q = V[G]; 5 9
5 2 9
for each u  Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4 4
Q = V[G]; 5 9
5 2 9
for each u  Q
key[u] = ; 14 2 u
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Review: Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u  Q
key[u] = ;
key[r] = 0;
What is the hidden cost in this code?
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Review: Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u  Q
key[u] = ;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
DecreaseKey(v, w(u,v));
Review: Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u  Q
key[u] = ; How often is ExtractMin() called?
key[r] = 0;
How often is DecreaseKey() called?
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
DecreaseKey(v, w(u,v));
Review: Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u  Q What will be the running time?
key[u] = ; A: Depends on queue
key[r] = 0; binary heap: O(E lg V)
p[r] = NULL;
Fibonacci heap: O(V lg V + E)
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Using Fibonacci Heaps

• Depending on the heap implementation, running time


could be improved!
Kruskal’s Algorithm
Kruskal()
{
T = ;
for each v  V
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1?
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2? 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5?
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8? 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9?
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13? 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14? 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17?
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19?
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21? 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25?
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm
Kruskal() Run the algorithm:
2 19
{
9
T = ; 14 17
8 25
for each v  V 5
MakeSet(v); 21 13 1
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm

Kruskal() What will affect the running time?


{
T = ;
for each v  V
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm

Kruskal() What will affect the running time?


{ 1 Sort
O(V) MakeSet() calls
T = ;
O(E) FindSet() calls
for each v  V
O(V) Union() calls
MakeSet(v); (Exactly how many Union()s?)
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
Kruskal’s Algorithm: Running Time
● To summarize:
■ Sort edges: O(E lg E)
■ O(V) MakeSet()’s
■ O(E) FindSet()’s
■ O(V) Union()’s
● Upshot:
■ Best disjoint-set union algorithm makes above 3
operations take O(E(E,V)),  almost constant
■ Overall thus O(E lg E), almost linear w/o sorting

You might also like