Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... ·...
Transcript of Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... ·...
![Page 1: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/1.jpg)
Kruskal’s Minimum Spanning Tree Algorithm &Union-Find Data Structures
Slides by Carl Kingsford
Jan. 21, 2013
Reading: AD 4.5–4.6
![Page 2: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/2.jpg)
Greedy minimum spanning tree rules
All of these greedy rules work:
1. Starting with any root node, add the frontier edge with thesmallest weight. (Prim’s Algorithm)
2. Add edges in increasing weight, skipping those whose additionwould create a cycle. (Kruskal’s Algorithm)
3. Start with all edges, remove them in decreasing order ofweight, skipping those whose removal would disconnect thegraph. (“Reverse-Delete” Algorithm)
![Page 3: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/3.jpg)
Prim’s Algorithm
Prim’s Algorithm: Starting with any root node, add the frontieredge with the smallest weight.
Theorem. Prim’s algorithm produces a minimum spanning tree.
ur
v
e
S = set of nodes already in
the tree when e is added
![Page 4: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/4.jpg)
Cycle Property
Theorem (Cycle Property). Let C be a cycle in G. Lete = (u, v) be the edge with maximum weight on C. Then e is notin any MST of G .
Suppose the theorem is false. Let T be a MST that contains e.
Deleting e from T partitions vertices into 2 sets:
S (that contains u) and V − S (that contains v).
Cycle C must have some other edge f that goes from S and V −S .
Replacing e by f produces a lower cost tree, contradicting that Tis an MST.
![Page 5: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/5.jpg)
Cycle Property, Picture
V
S V-S
u v
e
f
![Page 6: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/6.jpg)
MST Property Summary
1. Cut Property: The smallest edge crossing any cut must be inall MSTs.
2. Cycle Property: The largest edge on any cycle is never in anyMST.
![Page 7: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/7.jpg)
Reverse-Delete Algorithm
Reverse-Delete Algorithm: Remove edges in decreasing order ofweight, skipping those whose removal would disconnect the graph.
Theorem. Reverse-Delete algorithm produces a minimumspanning tree.
vu
e = (u,v)
Because removing e won't disconnect the graph, there must be another path between u and v
Because we're removing in order of decreasing weight, e must be the largest edge on that cycle.
![Page 8: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/8.jpg)
Kruskal’s Algorithm
Kruskal’s Algorithm: Add edges in increasing weight, skippingthose whose addition would create a cycle.
Theorem. Kruskal’s algorithm produces a minimum spanning tree.
Proof. Consider the point when edge e = (u, v) is added:
vu
S = nodes to which v has a pathjust before e is added
u is in V-S(otherwise there
would be a cycle)
e = (u,v)
![Page 9: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/9.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 10: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/10.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 11: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/11.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 12: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/12.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 13: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/13.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 14: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/14.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 15: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/15.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 16: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/16.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 17: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/17.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 18: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/18.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 19: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/19.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 20: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/20.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 21: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/21.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 22: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/22.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 23: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/23.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 24: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/24.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 25: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/25.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 26: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/26.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 27: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/27.jpg)
Example run of Kruskal’s
0
1
2
3
4
5
67
8
9
10
11
12
13
14
15
16
17
18
19
![Page 28: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/28.jpg)
Another example
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
![Page 29: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/29.jpg)
Data Structure for Kruskal’s Algorithm
Kruskal’s Algorithm: Add edges in increasing weight, skippingthose whose addition would create a cycle.
How would we check if adding an edge {u, v} would create a cycle?
I Would create a cycle if u and v are already in the samecomponent.
I We start with a component for each node.
I Components merge when we add an edge.
I Need a way to: check if u and v are in same component andto merge two components into one.
![Page 30: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/30.jpg)
Data Structure for Kruskal’s Algorithm
Kruskal’s Algorithm: Add edges in increasing weight, skippingthose whose addition would create a cycle.
How would we check if adding an edge {u, v} would create a cycle?
I Would create a cycle if u and v are already in the samecomponent.
I We start with a component for each node.
I Components merge when we add an edge.
I Need a way to: check if u and v are in same component andto merge two components into one.
![Page 31: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/31.jpg)
Data Structure for Kruskal’s Algorithm
Kruskal’s Algorithm: Add edges in increasing weight, skippingthose whose addition would create a cycle.
How would we check if adding an edge {u, v} would create a cycle?
I Would create a cycle if u and v are already in the samecomponent.
I We start with a component for each node.
I Components merge when we add an edge.
I Need a way to: check if u and v are in same component andto merge two components into one.
![Page 32: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/32.jpg)
Data Structure for Kruskal’s Algorithm
Kruskal’s Algorithm: Add edges in increasing weight, skippingthose whose addition would create a cycle.
How would we check if adding an edge {u, v} would create a cycle?
I Would create a cycle if u and v are already in the samecomponent.
I We start with a component for each node.
I Components merge when we add an edge.
I Need a way to: check if u and v are in same component andto merge two components into one.
![Page 33: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/33.jpg)
Data Structure for Kruskal’s Algorithm
Kruskal’s Algorithm: Add edges in increasing weight, skippingthose whose addition would create a cycle.
How would we check if adding an edge {u, v} would create a cycle?
I Would create a cycle if u and v are already in the samecomponent.
I We start with a component for each node.
I Components merge when we add an edge.
I Need a way to: check if u and v are in same component andto merge two components into one.
![Page 34: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/34.jpg)
Union-Find Abstract Data Type
The Union-Find abstract data type supports the followingoperations:
I UF.create(S) — create the data structure containing |S | sets,each containing one item from S .
I UF.find(i) — return the “name” of the set containing item i .
I UF.union(a,b) — merge the sets with names a and b into asingle set.
![Page 35: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/35.jpg)
A Union-Find Data Structure
1
2
6
7
17
4
1 3 8
2 10 5
6
7 12 16
17
4 11 14 15
13 9
UF Items:
1 2 1 4 2 6 7 1 1 2 4 7 1 4 4 7 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
UF Sets Array:
1
2
6
7
17
4
UF Sizes:
5
3
1
3
1
4
![Page 36: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/36.jpg)
Implementing the union & find operations
make union find(S) Create data structures on previous slide.Takes time proportional to the size of S.
find(i) Return UF.sets[i].Takes a constant amount of time.
union(x,y) Use the “size” array to decide which set is smaller.Assume x is smaller.Walk down elements i in set x, setting sets[i ] = y.Set size[y] = size[y] + size[x].
![Page 37: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/37.jpg)
Runtime of array-based Union-Find
Theorem. Any sequence of k union operations on a collection of nitems takes time at most proportional to k log k.
Proof. After k unions, at most 2k items have been involved in aunion. (Each union can touch at most 2 new items).
We upper bound the number of times set[v ] changes for any v :
I Every time set[v ] changes, the size of the set that v is in atleast doubles. why?
I So, set[v ] can have changed at most log2(2k) times.
At most 2k items have been modified at all, and each updated atmost log2(2k) times =⇒ 2k log2(2k) work.
![Page 38: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/38.jpg)
Running time of Kruskal’s algorithm
Sorting the edges: ≈ m log m for m edges.m ≤ n2, so log m < log n2 = 2 log n
Therefore sorting takes ≈ m log n time.
At most 2m “find” operations: ≈ 2m time.
At most n − 1 union operations: ≈ n log n time.
=⇒ Total running time of ≈ m log n + 2m + n log n.
The biggest term is m log n since m > n if the graph is connected.
![Page 39: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/39.jpg)
Another way to implement Union-Find
1
3 8
13
9
2
10 5
6 7
12
16
17 2
10 5
5
![Page 40: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/40.jpg)
Another way to implement Union-Find
1
3 8
13
9
2
10 5
6 7
12
16
17 2
10 5
5
union(1,2)
![Page 41: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/41.jpg)
Tree-based Union-Find
make union find(S) Create |S | trees each containing a single itemand size 1. Takes time proportional to the size of S .
find(i) Follow the pointer from i to the root of its tree.
union(x,y) If the size of set x is < that of y , make y point to x .Takes constant time.
![Page 42: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/42.jpg)
Runtime of tree-based Find
Theorem. find(i) takes time ≈ log n in a tree-based union-finddata structure containing n items.
Proof. The depth of an item equals the number of times the set itwas in was renamed.
The size of the set containing v at least doubles every time thename of the set containing v is changed.
The largest number of times the size can double is log2 n.
![Page 43: Kruskal’s Minimum Spanning Tree Algorithm & Union …ckingsf/class/02713-s13/lectures/lec03... · Kruskal’s Minimum Spanning Tree Algorithm & Union-Find Data Structures Slides](https://reader031.fdocuments.net/reader031/viewer/2022022419/5a78ddff7f8b9a5a148ce18f/html5/thumbnails/43.jpg)
Running time of Kruskal’s algorithm usingtree-based union-find
Same running time as using the array-based union-find:
I Sorting the edges: ≈ m log n for m edges.
I At most 2m “find” operations: ≈ log n time each.
I At most n − 1 union operations: ≈ n time.
=⇒ Total running time of ≈ m log n + 2m log n + n.
The biggest term is m log n since m > n if the graph is connected.