L15 Graph Part04
L15 Graph Part04
Graph-Based Algorithms
CSE373: Design and Analysis of Algorithms
Definition of MST
• Let 𝐺 = (𝑉, 𝐸) be a connected, undirected graph.
• For each edge (𝑢, 𝑣) in 𝐸, we have a weight 𝑤(𝑢, 𝑣) specifying the
cost (length of edge) to connect 𝑢 and 𝑣.
• We wish to find a (acyclic) subset 𝑇 of 𝐸 that connects all of the
vertices in 𝑉 and whose total weight is minimized.
• Since the total weight is minimized, the subset 𝑇 must be acyclic
(no circuit).
• Thus, 𝑇 is a tree. We call it a spanning tree.
• The problem of determining the tree 𝑇 is called the minimum-
spanning-tree problem.
Application of MST
In the design of electronic circuitry, it is often necessary to make a set
of pins electrically equivalent by wiring them together.
To interconnect n pins, we can use n-1 wires, each connecting two
pins.
We want to minimize the total length of the wires.
Minimum Spanning Trees can be used to model this problem.
Application of MST
Problem: Laying Telephone Wire
Central office
Application of MST
Wiring: Naïve Approach
Central office
Expensive!
Application of MST
Wiring: Better Approach
Central office
GENERIC_MST(G,w)
1 A=∅
2 while A does not form a spanning tree
3 find an edge (u,v) that is safe for A
4 A = A∪{(u,v)}
5 return A
How to Find a Safe Edge
We need some definitions and a theorem.
• A cut (𝑆, 𝑉 − 𝑆) of an undirected graph 𝐺 = (𝑉, 𝐸) is a
partition of 𝑉.
• An edge crosses the cut (𝑆, 𝑉 − 𝑆) if one of its
endpoints is in 𝑆 and the other is in 𝑉 − 𝑆.
• An edge is a light edge crossing a cut if its weight is the
minimum of any edge crossing the cut.
How to Find a Safe Edge
8 7
b c d 9
4
2
S↑ 11 14 e ↑S
a i 4
7 6
10
8
V-S↓ h g f ↓ V-S
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴=∅
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎 𝑏 𝑐 {𝑑} {𝑒} {𝑓} {𝑔} {ℎ} {𝑖}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴=∅
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎 𝑏 𝑐 {𝑑} {𝑒} {𝑓} {𝑔} {ℎ} {𝑖}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = {(𝑔, ℎ)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎 𝑏 𝑐 {𝑑} {𝑒} {𝑓} {𝑔, ℎ} {𝑖}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = {(𝑔, ℎ)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎 𝑏 𝑐 {𝑑} {𝑒} {𝑓} {𝑔, ℎ} {𝑖}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , (𝑐, 𝑖)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎 𝑏 𝑐, 𝑖 {𝑑} {𝑒} {𝑓} {𝑔, ℎ}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , (𝑐, 𝑖)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎 𝑏 𝑐, 𝑖 {𝑑} {𝑒} {𝑓} {𝑔, ℎ}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , (𝑓, 𝑔)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎 𝑏 𝑐, 𝑖 {𝑑} {𝑒} {𝑓, 𝑔, ℎ}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , (𝑓, 𝑔)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎 𝑏 𝑐, 𝑖 {𝑑} {𝑒} {𝑓, 𝑔, ℎ}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , (𝑎, 𝑏)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎, 𝑏 𝑐, 𝑖 {𝑑} {𝑒} {𝑓, 𝑔, ℎ}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , (𝑎, 𝑏)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎, 𝑏 𝑐, 𝑖 {𝑑} {𝑒} {𝑓, 𝑔, ℎ}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , (𝑐, 𝑓)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎, 𝑏 𝑐, 𝑓, 𝑔, ℎ, 𝑖 {𝑑} {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , (𝑐, 𝑓)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎, 𝑏 𝑐, 𝑓, 𝑔, ℎ, 𝑖 {𝑑} {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , (𝑐, 𝑓)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎, 𝑏 𝑐, 𝑓, 𝑔, ℎ, 𝑖 {𝑑} {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , 𝑐, 𝑓 , (𝑐, 𝑑)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎, 𝑏 𝑐, 𝑑, 𝑓, 𝑔, ℎ, 𝑖 {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , 𝑐, 𝑓 , (𝑐, 𝑑)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎, 𝑏 𝑐, 𝑑, 𝑓, 𝑔, ℎ, 𝑖 {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , 𝑐, 𝑓 , (𝑐, 𝑑)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: 𝑎, 𝑏 𝑐, 𝑑, 𝑓, 𝑔, ℎ, 𝑖 {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , 𝑐, 𝑓 , 𝑐, 𝑑 , (𝑎, ℎ)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: {𝑎, 𝑏, 𝑐, 𝑑, 𝑓, 𝑔, ℎ, 𝑖} {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , 𝑐, 𝑓 , 𝑐, 𝑑 , (𝑎, ℎ)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: {𝑎, 𝑏, 𝑐, 𝑑, 𝑓, 𝑔, ℎ, 𝑖} {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , 𝑐, 𝑓 , 𝑐, 𝑑 , (𝑎, ℎ)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: {𝑎, 𝑏, 𝑐, 𝑑, 𝑓, 𝑔, ℎ, 𝑖} {𝑒}
Kruskal's Algorithm: Example
•The edges are considered by the algorithm in sorted order by weight.
•The edge under consideration at each step is shown with a red
weight number.
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Sorted edges: (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
𝐴 = { 𝑔, ℎ , 𝑐, 𝑖 , 𝑓, 𝑔 , 𝑎, 𝑏 , 𝑐, 𝑓 , 𝑐, 𝑑 , 𝑎, ℎ , (𝑑, 𝑒)}
𝑉𝑒𝑟𝑡𝑒𝑥 𝑠𝑒𝑡𝑠: {𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑓, 𝑔, ℎ, 𝑖}
Prim's Algorithm
MST_PRIM(G,w,r)
1 for each 𝑢 in 𝐺. 𝑉
2 𝑢. 𝑘𝑒𝑦 = ∞
3 𝑢. 𝜋 =NIL
4 𝑟. 𝑘𝑒𝑦 = 0
5 𝑄 = 𝐺. 𝑉 //Q is a min-priority queue
6 while 𝑄 ≠ ∅
7 𝑢 =EXTRACT_MIN(𝑄)
8 for each 𝑣 ∈ 𝐺. 𝐴𝑑𝑗[𝑢]
9 if 𝑣 ∈ 𝑄 and 𝑤 𝑢, 𝑣 < 𝑣. 𝑘𝑒𝑦
10 𝑣. 𝜋 = 𝑢
11 𝑣. 𝑘𝑒𝑦 = 𝑤(𝑢, 𝑣)
Prim's Algorithm: Example
the root 8 7
b c d
vertex 4 9
2
a 11 14 e
i 4
7 6
8 10
h g f
1 2
Prim's Algorithm: Example