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

Minimum Spanning Tree.pptx

The document provides an introduction to Minimum Spanning Trees (MST) and details the Kruskal algorithm for finding an MST using a greedy approach. It explains the Disjoint Set Union (DSU) data structure, which supports union and find operations necessary for the algorithm. Additionally, the document includes examples and homework problems related to MST.

Uploaded by

minh.tn.hust
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Minimum Spanning Tree.pptx

The document provides an introduction to Minimum Spanning Trees (MST) and details the Kruskal algorithm for finding an MST using a greedy approach. It explains the Disjoint Set Union (DSU) data structure, which supports union and find operations necessary for the algorithm. Additionally, the document includes examples and homework problems related to MST.

Uploaded by

minh.tn.hust
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

EngineerPro - K01

Minimum Spanning Tree

Quang Hoang, 2024.


1
1. Minimum spanning tree introduction.
2. Kruskal algorithm.
Contents 3. DSU data structure
4. Examples.
5. Homework.

2
1. Minimum spanning tree
introduction.

3
What is a spanning tree?

- Given an undirected graph G = (V, E).


- A spanning tree is a set of edges:
○ no cycle
○ connect all nodes of graph
- Yes, it is indeed a tree
- 1 graph can have multiple spanning trees

4
Minimum Spanning Tree

- A minimum spanning tree is a spanning tree with the minimum sum of edge weights.

5
2. Kruskal algorithm

6
Kruskal algorithm

- We use greedy algorithm to solve the problem


- We sort the list of edges by weights ascendingly.
- Initially, we init a forest with n trees.
- Each time, we pick the smallest edge, if 2 vertices belong to different trees, we connect 2 trees
and join them into a new tree.
- We repeat until we only have 1 tree left.

7
Sorted edges

(1, 2, 1)
(2, 3, 2)
(1, 3, 3)
(1, 4, 4)
(3, 4, 5)
Sorted edges

(1, 2, 1)
(2, 3, 2)
(1, 3, 3)
(1, 4, 4)
(3, 4, 5)
Sorted edges

(1, 2, 1)
(2, 3, 2)
(1, 3, 3)
(1, 4, 4)
(3, 4, 5)
Sorted edges

(1, 2, 1)
(2, 3, 2)
(1, 3, 3)
(1, 4, 4)
(3, 4, 5)
DSU - Disjoint Set Union (Union-Find)

- imagine there are n stones, n sets, initially each stone is in one set.
- A data structure supports following operations:
○ Union(u, v): given 2 stones u and v. If u and v are not in same set, merge them into 1
set.
○ Find(x): find the set that contains stone x.

12
DSU - Disjoint Set Union

- Represent each set as a rooted tree


- ID of the set is the root of the tree
- Use array parent[1..n]:
○ parent[i] = j if j is parent of i
○ parent[i] = i if i is root.

- Initially, each stone is in each


own set, so we init
parent[i] = i for i = 1, .., n

13
DSU - Find

- Find(x): given stone x, find its set ID.

while x != parent[x]: x ← parent[x]


return x

- Runtime: O(tree height)


Example: Find(7) = 4

14
DSU - Union

- Union(u, v) ⇔ join 2 trees of u and v


- Idea: hang one tree under the root of the other tree.
We should hang the shorter tree under root of the
higher tree to keep the new tree shallow.

15
for i = 0..n-1: rank[i] = 0
Union(u, v) // merge tree contain u and tree contain v
rootU = Find(u)
rootV = Find(v)
if rootU == rootV: return // same tree, do nothing
if rank[rootU] > rank[rootV]: // hang rootV under rootU
parent[rootV] = rootU // rootU becomes new tree’s root
else:
parent[rootU] = rootV // hang rootU under rootV
if rank[rootU] == rank[rootV]: // update the height of new tree
rank[rootV]+=1
Kruskal Implementation
sort(edges.begin(), edges.end()) // O(mlogm), m is number of edges
int mst = 0; // sum of weight in MST
int count = 0; // number of edge in the MST
vector<vector<int>> res;
for(auto e: edges){ // O(m)
int u = e[0];
int v = e[1];
int w = e[2];
if (find(u) != find(v)) { // can merge
mst += w;
count++;
res.push_back(e);
union(u, v);
}
}
if count != n-1 return -1 // when original graph is not connected => no MST
return mst
Space:
- O(n) for Union-find
- O(m) for edges

=>

total space: O(n+m)


extra space: O(n)

n is number of nodes, m is number of edges


4. Examples

19
Problem

- https://leetcode.com/problems/connecting-cities-with-minimum-cost/
○ https://leetcode.com/problems/connecting-cities-with-minimum-cost/submissions/131
1278102/
- https://leetcode.com/problems/min-cost-to-connect-all-points/
○ https://leetcode.com/problems/min-cost-to-connect-all-points/submissions/687940391/

- https://leetcode.com/problems/optimize-water-distribution-in-a-village/

20
5. Homework

21
Problem

- https://leetcode.com/problems/find-critical-and-pseudo-critical-edges-in-minimum-spanning-tre
e/
- Hint:

22
Problem

- https://leetcode.com/tag/minimum-spanning-tree/
-

23

You might also like