Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | isabella-adrian |
View: | 45 times |
Download: | 1 times |
David Luebke 2 04/20/23
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));
}
David Luebke 3 04/20/23
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:
David Luebke 4 04/20/23
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:
David Luebke 5 04/20/23
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:
David Luebke 6 04/20/23
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:
David Luebke 7 04/20/23
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:
David Luebke 8 04/20/23
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:
David Luebke 9 04/20/23
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:
David Luebke 10 04/20/23
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:
David Luebke 11 04/20/23
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:
David Luebke 12 04/20/23
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:
David Luebke 13 04/20/23
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:
David Luebke 14 04/20/23
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:
David Luebke 15 04/20/23
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:
David Luebke 16 04/20/23
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:
David Luebke 17 04/20/23
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:
David Luebke 18 04/20/23
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:
David Luebke 19 04/20/23
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:
David Luebke 20 04/20/23
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:
David Luebke 21 04/20/23
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:
David Luebke 22 04/20/23
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:
David Luebke 23 04/20/23
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:
David Luebke 24 04/20/23
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:
David Luebke 25 04/20/23
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:
David Luebke 26 04/20/23
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 27 04/20/23
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?
David Luebke 28 04/20/23
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? 1 Sort
O(V) MakeSet() callsO(E) FindSet() callsO(V) Union() calls
(Exactly how many Union()s?)
David Luebke 29 04/20/23
Kruskal’s Algorithm: Running Time
● To summarize: ■ Sort edges: O(E lg E) ■ O(V) MakeSet()’s■ O(E) FindSet()’s■ O(V) Union()’s