Lec 10 Mon
Lec 10 Mon
Week 10 - Monday
Ilir Dema
1
Logistics
• Assignment A3 due Sunday, July 24
2
Minimum Spanning Tree
3
The Graph of interest today
A connected undirected weighted graph
8
2
10 5
3
5 12
4
It has the smallest total weight
It’s a connected,
acyclic subgraph
5
A Minimum Spanning
Tree
8
2
10 5
3
5 12
7
Applications of MST
Connect all components with the least
amount of wiring.
8
Other applications
➔ Cluster analysis
➔ Approximation algorithms for the “travelling
salesman problem”
➔ ...
9
In order to understand
minimum spanning tree
we need to first understand
tree
10
Tree:
undirected connected acyclic graph
11
The MST of a connected graph G = (V, E)
|V|
has________________ vertices.
because “spanning”
because “tree”
12
Now we are ready to talk about
algorithms
13
Idea #1
Start with T = G.E, then keep deleting edges until
an MST remains.
Idea #2
Start with empty T, then keep adding edges
until an MST is built.
14
Hint
15
Note: Here T is an edge set
Idea #1
Start with T = G.E, then keep deleting edges until
an MST remains.
In worst-case, need to
Idea #2 add O(|V|) edges
Start with empty T, then keep adding edges
until an MST is built.
This is more efficient! 16
So, let’s explore more of Idea #2,
i.e.,
building an MST by adding edges
one by one
i.e.,
we “grow” a tree
17
The generic growing algorithm
GENERIC-MST(G=(V, E, w)):
|T| < |V|-1
T ← ∅
while T is not a spanning tree:
find a “safe” edge e
T ← T ∪ {e}
return T
If we make sure T
is always a subset GENERIC-MST(G=(V, E, w)):
20
The generic growing algorithm
GENERIC-MST(G=(V, E, w)):
|T| < |V|-1
T ← ∅
while T is not a spanning tree:
find a “safe” edge e
T ← T ∪ {e}
return T
➔ Kruskal’s algorithm
➔ Prim’s algorithm
22
Theorem (Corollary 23.1 in CLRS)
● Let G(V, E) be a connected, weighted, undirected graph.
● Let T be a subset of E that is included in some MST for G
● let C be a connected component (tree) in the forest GT(V, T)
● Let (u, v) be a min-weight edge crossing C and some other
component in GT.
● Then, edge (u, v) is safe for T.
8
a b 2
10 5
3 c
5 12
e d
24
Two things that need to be worried about when
actually implementing the algorithm
25
Overview: Prim’s and Kruskal’s
Keep track of
Find minimum
connected
weight edge
components
26
Prim’s Kruskal’s
https://trendsofcode.files.wordpress.com/2014/09/p
rim-algorithm-animation-2.gif
https://trendsofcode.files.wordpress.com/2014/09/kruskal_
algorithm.gif 27
Prim’s MST algorithm
28
Prim’s algorithm: Idea
➔ Start from an arbitrary vertex as root
➔ Focus on growing one tree, add one edge at a time.
The tree is one component, each of the other
(isolated) vertices is a component.
➔ Add which edge? Among all edges that are incident
to the current tree (edges crossing components),
pick one with the minimum weight.
➔ How to get that minimum? Store all candidate
vertices in a Min-Priority Queue whose key is the
weight of the crossing edge (incident to tree).
29
PRIM-MST(G=(V, E, w)):
key[v] keeps the “shortest distance”
1 T ← {} between v and the current tree
2 for all v in V:
3 key[v] ← ∞ pi[v] keeps who, in the tree, is v
4 pi[v] ← NIL connected to via lightest edge.
a 0 NIL
8
a b 2
b ∞ NIL
3 5
10 c
c ∞ NIL
5 12
e d
d ∞ NIL
8
Q key pi
a b 2
b ∞ →8 NIL
5 →a
3 10 c
c ∞ NIL
5 12
e d
d ∞ NIL
e ∞ →3 NIL
→a 32
ExtractMin (#2)
then update neighbours’ keys
e: 3, a
8
Q key pi
a b 2
b 8→5 a →e
3 5
10 c
c ∞ NIL
5 12
e d
d ∞ →5 NIL
→e
33
ExtractMin (#3)
then update neighbours’ keys
b: 5, e
8
Q key pi
a b 2
c ∞ →2 NIL
5 →b
3 10 c
d 5 e
5 12
e d
Could also have extracted d
since its key is also 5 (min)
34
ExtractMin (#4)
then update neighbours’ keys
c: 2, b
8
Q key pi
a b 2
d 5 e
3 5
10 c
5 12
e d
35
ExtractMin (#4)
then update neighbours’ keys
d: 5, e
8
Q key pi
a b 2
3 5 Q is empty now.
10 c
5 12
e d
MST grown!
36
Correctness of Prim’s
The added edge is always a “safe” edge, i.e., the
minimum weight edge crossing components (vertices in
the tree and the rest) because of ExtractMin.
8
a b 2
3 5
10 c
5 12
e d
37
Runtime analysis: Prim’s
➔ Assume we use binary min heap to
implement the priority queue.
➔ Each ExtractMin take O(log V)
➔ In total V ExtractMin’s
➔ In total, check at most O(E) neighbours, each
check neighbour could lead to a
DecreaseKey which takes O(log V)
39
Kruskal’s MST
algorithm
40
Kruskal’s algorithm: idea
➔ Sort all edges according to weight, then start
adding to MST from the lightest one.
➔ Constraint: added edge must NOT cause a cycle
◆ In other words, the two endpoints of the edge must
belong to two different trees (components).
➔ The whole process is like unioning small trees into
a big tree.
41
m = |E|
Pseudocode
KRUSKAL-MST(G(V, E, w)):
1 T ← {}
2 sort edges so that w(e1)≤w(e2)≤...≤w(em)
3 for i ← 1 to m:
4 # let (ui, vi) = ei
5 if ui and vi in different components:
6 T ← T ∪ {ei}
42
Example
6
a b 2
3 5
10 c
9 12
e d
43
Add (b, c), the lightest edge
6
a b 2
3 5
10 c
9 12
e d
44
Add (a, e), the 2nd lightest
6
a b 2
3 5
10 c
9 12
e d
45
Add (b, e), the 3rd lightest
6
a b 2
3 5
10 c
9 12
e d
46
Add (a, b), the 4th lightest ...
No! a, b are in the same component
Add (d, e) instead!
6
a b 2
3 5
10 c
9 12
e d
47
MST grown!
Add (d, e) ...
6
a b 2
3 5
10 c
9 12
e d
48
Correctness of Kruskal’s
The added edge is always a “safe” edge, because
it is the minimum weight edge among all edges
that cross components
6
a b 2
3 5
10 c
9 12
e d
49
m = |E|
Runtime ...
sorting takes O(E log E)
KRUSKAL-MST(G(V, E, w)):
1 T ← {}
2 sort edges so that w(e1)≤w(e2)≤...≤w(em)
3 for i ← 1 to m:
4 # let (ui, vi) = ei
5 if ui and vi in different components:
6 T ← T ∪ {ei}
52
Next
Exercises for BFS, DFS, MST
53