RAIK 283 Data Structures & Algorithms
description
Transcript of RAIK 283 Data Structures & Algorithms
![Page 2: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/2.jpg)
2
RAIK 283Data Structures & Algorithms
Giving credit where credit is due:» Most of slides for this lecture are based on slides
created by Dr. David Luebke, University of Virginia.
» Some examples and slides are based on lecture notes created by Dr. Chuck Cusack, Hope College, Dr. Jim Cohoon, University of Virginia, and Dr. Ben Choi, Louisiana Technical University.
» I have modified them and added new slides
![Page 3: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/3.jpg)
3
Greedy Algorithms
Main Concept: Make the best or greedy choice at any given step.
Choices are made in sequence such that» Each individual choice is best according to some limited “short-
term” criterion, that is not too expensive to evaluate
» Once a choice is made, it cannot be undone! Even if it becomes evident later that it was a poor choice Sometimes life is like that
The goal is to » take a sequence of locally optimal actions, hoping to yield a
globally optimal solution.
![Page 4: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/4.jpg)
4
When to be Greedy
Greedy algorithms apply to problems with» Optimal substructures: optimal solutions contain optimal sub-
solutions.
» The greedy choice property: an optimal solution can be obtained by making the greedy choice at each step.
» Many optimal solutions, but we only need one such solution.
![Page 5: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/5.jpg)
5
Minimum Spanning Tree (MST)
A spanning tree for a connected, undirected graph, G=(V, E) is
1. a connected subgraph of G that forms an
2. undirected tree incident with each vertex.
In a weighted graph G=(V, E, W), » the weight of a subgraph is the sum of the weights of
the edges in the subgraph.
A minimum spanning tree (MST) for a weighted graph is
» a spanning tree with the minimum weight.
![Page 6: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/6.jpg)
6
Minimum Spanning Tree
Problem: given a connected, undirected, weighted graph:
1410
3
6 4
5
2
9
15
8
find a spanning tree using edges that minimize the total weight
![Page 7: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/7.jpg)
7
Minimum Spanning Tree
Which edges form the minimum spanning tree (MST) of the graph?
H B C
G E D
F
A
1410
3
6 4
5
2
9
15
8
![Page 8: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/8.jpg)
8
Minimum Spanning Tree
Answer:
H B C
G E D
F
A
1410
3
6 4
5
2
9
15
8
![Page 9: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/9.jpg)
9
Another Example
Given a weighted graph G=(V, E,W), find a MST of G
3
64
5
6
3
3
5
7
2
![Page 10: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/10.jpg)
10
Finding a MST
MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees
Principal greedy methods: algorithms by Prim and Kruskal Prim
» Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree
Intermediary solution is a subtree
Kruskal» Grow a tree by repeatedly adding the least cost edge that does not
introduce a cycle among the edges included so far Intermediary solution is a spanning forest
![Page 11: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/11.jpg)
11
Prime’s Algorithm (High-Level Pseudocode)
Prime(G)
//Input: A weighted connected graph G = <V, E>
//Output: ET --- the set of edges composing MST of G
VT = {v0}
ET =
for i = 1 to |V| - 1 do
find a minimum-weight edge e* = (u*, v*) among all the edges (u, v) such that u is in VT and v is in V-VT
VT = VT {v*}
ET = ET {e*}
return ET
![Page 12: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/12.jpg)
12
Prime’s Algorithm (High-Level Pseudocode)
Prime(G)
//Input: A weighted connected graph G = <V, E>
//Output: ET --- the set of edges composing MST of G
VT = {v0}
ET =
for i = 1 to |V| - 1 do
find a minimum-weight edge e* = (u*, v*) among all the edges (u, v) such that u is in VT and v is in V-VT
VT = VT {v*}
ET = ET {e*}
return ET
H B C
G E D
F
A
1410
3
6 45
2
9
15
8
![Page 13: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/13.jpg)
13
Prime’s Algorithm (High-Level Pseudocode)
Prime(G)
//Input: A weighted connected graph G = <V, E>
//Output: ET --- the set of edges composing MST of G
VT = {v0}
ET =
for i = 1 to |V| - 1 do
find a minimum-weight edge e* = (u*, v*) among all the edges (u, v) such that u is in VT and v is in V-VT
VT = VT {v*}
ET = ET {e*}
return ET
H B C
G E D
F
A
1410
3
6 45
2
9
15
8
![Page 14: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/14.jpg)
14
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree
Intermediary solution is a subtree
![Page 15: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/15.jpg)
15
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
1410
3
6 45
2
9
15
8
Run on example graph
![Page 16: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/16.jpg)
16
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
1410
3
6 45
2
9
15
8
Run on example graph
![Page 17: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/17.jpg)
17
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
0
1410
3
6 45
2
9
15
8
Pick a start vertex r
r
![Page 18: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/18.jpg)
18
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
0
1410
3
6 45
2
9
15
8
Red vertices have been removed from Q
u
![Page 19: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/19.jpg)
19
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
0
3
1410
3
6 45
2
9
15
8
Red arrows indicate parent pointers
u
![Page 20: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/20.jpg)
20
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
14
0
3
1410
3
6 45
2
9
15
8
u
![Page 21: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/21.jpg)
21
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
14
0
3
1410
3
6 45
2
9
15
8u
![Page 22: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/22.jpg)
22
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
14
0 8
3
1410
3
6 45
2
9
15
8u
![Page 23: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/23.jpg)
23
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10
0 8
3
1410
3
6 45
2
9
15
8u
![Page 24: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/24.jpg)
24
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10
0 8
3
1410
3
6 45
2
9
15
8u
![Page 25: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/25.jpg)
25
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2
0 8
3
1410
3
6 45
2
9
15
8u
![Page 26: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/26.jpg)
26
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2
0 8 15
3
1410
3
6 45
2
9
15
8u
![Page 27: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/27.jpg)
27
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2
0 8 15
3
1410
3
6 45
2
9
15
8
u
![Page 28: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/28.jpg)
28
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2 9
0 8 15
3
1410
3
6 45
2
9
15
8
u
![Page 29: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/29.jpg)
29
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 30: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/30.jpg)
30
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 31: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/31.jpg)
31
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 32: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/32.jpg)
32
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 33: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/33.jpg)
33
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 34: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/34.jpg)
34
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 35: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/35.jpg)
35
Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
What is the hidden cost in this code?
![Page 36: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/36.jpg)
36
Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));
delete the smallest element from the min-heap
decrease an element’s value in the min-heap
(outline an efficient algorithm for it)
![Page 37: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/37.jpg)
37
Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));
How often is ExtractMin() called?How often is DecreaseKey() called?
![Page 38: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/38.jpg)
38
Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
What will be the running time?
There are n=|V| ExtractMin calls and m=|E| DecreaseKey calls.
The priority Q implementation has a large impact on
performance.
E.g., O((n+m)lg n) = O(m lg n) using min-heap for Q(for connected graph, n – 1 m)
![Page 39: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/39.jpg)
39
Finding a MST
Principal greedy methods: algorithms by Prim and Kruskal
Prim» Grow a single tree by repeatedly adding the least cost edge that
connects a vertex in the existing tree to a vertex not in the existing tree
Intermediary solution is a subtree
Kruskal» Grow a tree by repeatedly adding the least cost edge that does not
introduce a cycle among the edges included so far Intermediary solution is a spanning forest
![Page 40: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/40.jpg)
40
MST Applications?
H B C
G E D
F
A
1410
3
6 4
5
2
9
15
8
![Page 41: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/41.jpg)
41
MST Applications
Network design» telephone, electrical power, hydraulic, TV cable, computer,
road
MST gives a minimum-cost network connecting all sites
MST: the most economical construction of a network
![Page 42: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/42.jpg)
42
Kruskal’s Algorithm (High-Level Pseudocode)
Kruskal(G)//Input: A weighted connected graph G = <V, E>
//Output: ET --- the set of edges composing MST of GSort E in nondecreasing order of the edge weight
ET = ; encounter = 0 //initialize the set of tree edges and its size
k = 0 //initialize the number of processed edgeswhile encounter < |V| - 1
k = k+1
if ET {ek} is acyclic
ET = ET {ek}; encounter = encounter + 1
return ET
![Page 43: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/43.jpg)
43
Kruskal’s Algorithm (High-Level Pseudocode)
Kruskal(G)//Input: A weighted connected graph G = <V, E>
//Output: ET --- the set of edges composing MST of GSort E in nondecreasing order of the edge weight
ET = ; encounter = 0 //initialize the set of tree edges and its size
k = 0 //initialize the number of processed edgeswhile encounter < |V| - 1
k = k+1
if ET {ek} is acyclic
ET = ET {ek}; encounter = encounter + 1
return ET
H B C
G E D
F
A
1410
3
6 45
2
9
15
8
![Page 44: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/44.jpg)
44
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far
Intermediary solution is a spanning forest
![Page 45: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/45.jpg)
45
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 46: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/46.jpg)
46
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 47: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/47.jpg)
47
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 48: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/48.jpg)
48
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1?
5
13
1725
148
21
Run the algorithm:
![Page 49: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/49.jpg)
49
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 50: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/50.jpg)
50
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2? 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 51: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/51.jpg)
51
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 52: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/52.jpg)
52
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5?
13
1725
148
21
Run the algorithm:
![Page 53: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/53.jpg)
53
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 54: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/54.jpg)
54
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148?
21
Run the algorithm:
![Page 55: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/55.jpg)
55
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 56: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/56.jpg)
56
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9?
1
5
13
1725
148
21
Run the algorithm:
![Page 57: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/57.jpg)
57
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 58: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/58.jpg)
58
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13?
1725
148
21
Run the algorithm:
![Page 59: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/59.jpg)
59
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 60: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/60.jpg)
60
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
14?8
21
Run the algorithm:
![Page 61: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/61.jpg)
61
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 62: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/62.jpg)
62
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
17?25
148
21
Run the algorithm:
![Page 63: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/63.jpg)
63
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19?
9
1
5
13
1725
148
21
Run the algorithm:
![Page 64: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/64.jpg)
64
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21?
Run the algorithm:
![Page 65: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/65.jpg)
65
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725?
148
21
Run the algorithm:
![Page 66: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/66.jpg)
66
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 67: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/67.jpg)
67
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));
}
2 19
9
1
5
13
1725
148
21
Run the algorithm:
![Page 68: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/68.jpg)
68
Kruskal’s Algorithm
Kruskal()
{
T = ; for each v V MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
}
What will affect the running time?Let n=|V| and m=|E|
O(n) MakeSet() calls1 Sort
O(m) FindSet() callsO(n) Union() calls
![Page 69: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/69.jpg)
69
Kruskal’s Algorithm: Running Time
To summarize: » Sort edges: O(m lg m) » O(n) MakeSet()’s» O(m) FindSet()’s» O(n) Union()’s
Upshot: » Best disjoint subsets union-find algorithm makes above 3
operations only slightly worse than linear» Refer to the textbook for a discussion on the algorithms» Overall thus O(m lg m)
![Page 70: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/70.jpg)
70
In-Class Exercise (9.1.7)
![Page 71: RAIK 283 Data Structures & Algorithms](https://reader030.fdocuments.us/reader030/viewer/2022012922/5681571b550346895dc4bb0b/html5/thumbnails/71.jpg)
71
In-Class Exercise
Applying Kruskal’s algorithm to 9.2.1 (b)