Minimum Spanning Trees Kruskals Algorithm Kruskals Algorithm Kruskal

  • Slides: 30
Download presentation
Minimum Spanning Trees Kruskal’s Algorithm

Minimum Spanning Trees Kruskal’s Algorithm

Kruskal’s Algorithm Kruskal() { T = ; for each v V Make. Set(v); sort

Kruskal’s Algorithm Kruskal() { T = ; for each v V Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 2 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 3 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 4 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 5 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 9 14 17 T =

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 9 14 17 T = ; 8 25 5 for each v V 21 13 1? Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 6 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 7 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2? 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2? 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 8 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 9 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 9 14 17 T =

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 9 14 17 T = ; 8 25 5? for each v V 21 13 1 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 10 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 11 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8? 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 12 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 13 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 9? 14 17 T =

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 9? 14 17 T = ; 8 25 5 for each v V 21 13 1 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 14 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 15 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13? Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 16 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 17 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14? 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14? 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 18 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 19 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17? T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17? T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 20 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19? { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19? { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 21 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21? 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 22 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25? 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 23 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 24 9 1 12/17/2021

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ;

Kruskal’s Algorithm Run the algorithm: Kruskal() 2 19 { 14 17 T = ; 8 25 5 for each v V 21 13 Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 25 9 1 12/17/2021

Correctness Of Kruskal’s Algorithm ● Sketch of a proof that this algorithm produces an

Correctness Of Kruskal’s Algorithm ● Sketch of a proof that this algorithm produces an MST for T: ■ Assume algorithm is wrong: result is not an MST ■ Then algorithm adds a wrong edge at some point ■ If it adds a wrong edge, there must be a lower weight edge (cut and paste argument) ■ But algorithm chooses lowest weight edge at each step. Contradiction ● Again, important to be comfortable with cut and paste arguments David Luebke 26 12/17/2021

Kruskal’s Algorithm What will affect the running time? Kruskal() { T = ; for

Kruskal’s Algorithm What will affect the running time? Kruskal() { T = ; for each v V Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 27 12/17/2021

Kruskal’s Algorithm What will affect the running time? Kruskal() 1 Sort { O(V) Make.

Kruskal’s Algorithm What will affect the running time? Kruskal() 1 Sort { O(V) Make. Set() calls T = ; O(E) Find. Set() calls for each v V O(V) Union() calls (Exactly how many Union()s? ) Make. Set(v); sort E by increasing edge weight w for each (u, v) E (in sorted order) if Find. Set(u) Find. Set(v) T = T U {{u, v}}; Union(Find. Set(u), Find. Set(v)); } David Luebke 28 12/17/2021

Kruskal’s Algorithm: Running Time ● To summarize: ■ Sort edges: O(E lg E) ■

Kruskal’s Algorithm: Running Time ● To summarize: ■ Sort edges: O(E lg E) ■ O(V) Make. Set()’s ■ O(E) Find. Set()’s ■ O(V) Union()’s David Luebke 29 12/17/2021

The Algorithms of Kruskal and Prim ● Kruskal’s Algorithm ■ A is a forest

The Algorithms of Kruskal and Prim ● Kruskal’s Algorithm ■ A is a forest ■ The safe edge added to A is always a least-weight edge in the graph that connects two distinct components