lec-18-mst
lec-18-mst
IITB India
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 1
Topic 18.1
Labeled graph
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 2
Labeled graph
c
4
6
3
a b
Definition 18.1
A graph G = (V , E ) is consists of 9 1
▶ set V of vertices and
d
▶ set E of pairs of
▶ unordered pairs from V and
▶ labels from Z+ (known as
The above is a labeled graph G = (V , E ), where
length).
V = {a, b, c, d} and
For e ∈ E , we will write L(e) to denote
the length. E = {({a, c}, 3), ({a, c}, 4), ({a, d}, 9), ({b, c}, 6),
({b, d}, 1)}.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 4
Example: MST
Example 18.2
Consider the following spanning tree (green edges). Is this an MST?
c
4
6
3
a b
9 1
d
We can achieve an MST by replacing 9 by 6.
Observation:
▶ consider an edge e that is not part of the spanning tree.
▶ add e to the spanning tree, which must form exactly one cycle.
▶ if e is not the maximum edge in the cycle, the spanning tree is not the minimum.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 5
Observation: minimum edge will always be part of MST.
Apply the previous observation, the edge will definitely replace another edge.
Example 18.3
In the following spanning tree, if we add edge 1, we can easily remove another edge and obtain a
spanning tree with lower length.
c
4
6
3
a b
9 1
d
Example 18.4
In the following graph, the minimum tree edges form a cycle.
c
4
6
3
a b
9 1
d
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 7
Topic 18.2
Kruskal’s algorithm
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 8
Idea: Keep collecting minimum edges, while avoiding cycle-causing edges.
Maybe. Let us try.
Example 18.5
In the following graph, let us construct MST.
c
4
✗
6
3
a b
9 1
d
This is an MST.
6 return (V , E ′ )
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 10
How do we check connectedness?
We will show that checking connectedness can be implemented in O(log |V |) using union-find data
structure.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 11
Example: connected sets
Example 18.6
Let us see connected sets in the progress of Kruskal’s algorithm.
r1 r2
e1 e2
Exercise 18.1
Prove that merge is O(1) and IsCoonect is O(log n).
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 13
Correctness of Kruskal’s algorithm
Theorem 18.1
For a graph G = (V , E ), MST (G ) returns an MST of G .
Proof.
Let us assume edge lengths are unique. (can be relaxed!)
Let us suppose MST (G ) returns edges e1 , ...., en , which are written in increasing order.
Let us suppose an MST consists of edges e1′ , ..., en′ , which are written in increasing order.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 14
Correctness of Kruskal’s algorithm(2)
Proof(Contd.)
case: L(e1 ) > L(ei′ ):
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 15
Correctness of Kruskal’s algorithm(3)
Proof(Contd.)
case: L(e1 ) < L(ei′ ):
Consider graph e1′ , ..., en′ , ei , which has exactly one cycle. Let C be the cycle.
For all e ∈ C − {ei }, L(ei ) > e because e1′ , ..., en′ is MST. (Why?)
Exercise 18.2
a. Prove the above theorem after relaxing the condition of unique edge lengths.
b. Prove that MST is unique if edge lengths are unique.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 16
Example: MST is not unique if edge lengths are not unique.
Example 18.7
The following graph has multiple MSTs.
c c
1 1
1 1
a b a b
1 1 1 1
d d
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 17
Topic 18.3
Prim’s algorithm
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 18
Cut of a graph
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 19
The minimum edge of a cut will always be part of MST.
Theorem 18.2
For a labeled graph G = (V , E ), the minimum edge of a non-empty cut C will be part of MST.
Proof.
Let C be a cut of G for some set S ⊆ E and e ∈ C be the
minimum edge.
c
e
Let us assume MST G ′ (green) does not contain e.
e′
Since both S and E − S are not empty, G′ ∩ C ̸= ∅ and for a b
each e ′ ∈ G ′ ∩ C and L(e ′ ) > L(e).
Keep expanding MST over visited vertices by adding the minimum edge connecting to the rest.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 21
Example: cut progress
Example 18.8
Let us see MST construction via cuts.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 22
Operations during entering the visited set
visited unvisited
When a vertex v moves from the unvisited set to the visited set, we need to delete blue edges from
the cut and add green edges to the cut.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 23
Prim’s algorithm
Algorithm 18.2: MST( Graph G = (V , E ), vertex r )
1 for v ∈ V do v .visited := False ;
2 r .visited := True;
3 for e = ({v , r }, ) ∈ E do cut := cut ∪ {e} ;
4 while cut ̸= ∅ do
5 ({v , v ′ }, ) := cut.min();
6 Assume(¬v .visited ∧ v ′ .visited); // This condition is always true
7 for e = ({v , w }, ) ∈ E do
8 if w .visited then
9 cut.delete(e) // Cost: O(log |E |)
10 else
11 cut.insert(e) // Cost: O(log |E |)
12 v .visited := True
Running time: O(|E | log |E |) because every edge will be inserted and deleted.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 24
Data structure for cut
We may use a heap to store the cut since we need a minimum element.
Since searching in the heap is expensive, we need to keep the pointer from the edge to the node of
the heap.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 25
Prim’s algorithm: with an optimization
Algorithm 18.3: MST( Graph G = (V , E ), vertex r )
1 Heap unvisited;
2 for v ∈ V do
3 v .visited := False;
4 unvisited.insert(v , ∞) // Will heapify help?
5 unvisited.decreasePriority (r , 0);
6 while unvisited ̸= ∅ do
7 v := unvisited.deleteMin();
8 for e = ({v , w }, k) ∈ E do
9 if ¬w .visited then
10 unvisited.decreasePriority (w , k) // Cost: O(log |V |)
11 v .visited := True
Running time: O(|V | + |E | log |V |) because every edge is visited to change the priority of a vertex.
Commentary: decreasePriority reduces the priority of an element in the heap. deleteMin returns the minimum element in the heap and deletes the element in the heap.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 26
Topic 18.4
Tutorial problems
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 27
Exercise: proving with non-unique lengths
Example 18.9
Modify proof of theorems 18.1 and 18.2 to support non-unique edges.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 28
Exercise: non-unique lengths
Example 18.10
Kruskal’s algorithm can return different spanning trees for the same input graph G, depending on
how it breaks ties when the edges are sorted into order. Show that for each minimum spanning
tree T of G, there is a way to sort the edges of G in Kruskal’s algorithm so that the algorithm
returns T .
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 29
Exercise: minimum spanning directed rooted tree(arborescence)
Definition 18.6
A graph G = (V , E ) is a directed rooted tree if for each v , v ′ ∈ V there is exactly one path
between v and v ′ .
Example 18.11
Show that Kruskal’s and Prim’s algorithm will not find a minimum spanning directed rooted tree.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 30
End of Lecture 18
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 31