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

lec-18-mst

Uploaded by

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

lec-18-mst

Uploaded by

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

CS213/293 Data Structure and Algorithms 2023

Lecture 18: Graphs - minimum spanning tree

Instructor: Ashutosh Gupta

IITB India

Compile date: 2023-11-06

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)}.

L(({a, c}, 3)) = 3.


cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 3
Minimum spanning tree (MST)

Consider a labeled graph G = (V , E ). Example 18.1


Definition 18.2
A spanning tree of G is a labeled tree (V , E ′ ), c
4
where E ′ ⊆ E . 6
3
a b
Definition 18.3 P
A length of G is L(e). 9 1
e∈E
d
Definition 18.4
A minimum spanning tree of G is a spanning The above is an MST of the graph in the
tree G ′ such that the length of G is minimum. previous slide.

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

Can we keep applying this observation on greater and greater edges?


cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 6
Idea: Should we create MST using minimum |V | − 1 edges?

No. The method will not always work.

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.

Are we lucky? Or, do we have a real procedure?


cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 9
Kruskal’s algorithm

Algorithm 18.1: MST( Graph G = (V , E ), vertex v )


1 Elist := sorted list of edges in E according to their labels;
2 E ′ = ∅; In ({v , v ′ }, ), indicates that

for e = ({v , v }, ) ∈ Elist do
3 the value is don’t care.
4 if v and v ′ are not connected in (V , E ′ ) then
5 E ′ := E ′ ∪ {e}

6 return (V , E ′ )

Running time complexity O(|E |log (|E |) +|E | × IsConnected)


| {z }
sorting

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 10
How do we check connectedness?

We maintain sets of connected vertices.

The sets merge as the algorithm proceeds.

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.

Initial connected sets: {{a}, {b}, {c}, {d}}.

After adding edge 1: {{a}, {c}, {b, d}}.


c
4

After adding edge 3: {{a, c}, {b, d}}. 6
3
a b
When we consider edge 4: a and c are already connected.
9 1
After adding edge 6: {{a, b, c, d}}. d

Each time we consider an edge we need to check if both


ends are in the same set or not.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 12
Union find
Equivalent classes are maintained as forest. The data structure has two operations.
▶ IsConnect(e1,e2): traverse to their root and see if the roots are the same
▶ merge(r1,r2): make the root of the taller tree parent of the root of the other tree

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.

Let i be the smallest index such that ei ̸= ei′ . ...

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′ ):

Kruskal must have considered ei′ before ei .

e1 , ...., ei−1 , ei′ has a cycle because Kruskal skipped ei′ .

Therefore, e1′ , ..., en′ has a cycle. Contradiction. ...

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?)

Therefore, C ⊆ {e1 , ..., ei }.

Therefore, e1 , ..., ei has a cycle. Contradiction.

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

Consider a labeled graph G = (V , E ).


Definition 18.5
For a set S ⊆ V , a cut C of G is {({v , v ′ }, ) ∈ E |v ∈ S ∧ v ′ ∈
/ S}.

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).

G ′ ∪ {e} has a cycle containing e and some e ′ ∈ G ′ ∩ C . d

Therefore, G ′ ∪ {e} − {e ′ } is a spanning with a smaller


length. Contradiction.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 20
Prim’s idea

Start with a single vertex in the visited set.

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.

We start with vertex a. The cut has edges 4, 3, and 9.


c
Since the minimum edge on the cut is 3, we add the edge to MST 4
and visited = {a, c}. Now cut has edges 6 and 9. 6
3
a b
Since the minimum edge on the cut is 6, we add the edge to MST
and visited = {a, c, b}. Now cut has edges 1 and 9. 9 1
d
Since the minimum edge on the cut is 1, we add the edge to MST.

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.

We need to be careful while deleting an edge from the heap.

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

You might also like