+ All Categories
Home > Documents > Minimum Spanning Trees

Minimum Spanning Trees

Date post: 01-Jan-2016
Category:
Upload: isabella-adrian
View: 45 times
Download: 1 times
Share this document with a friend
Description:
Minimum Spanning Trees. Kruskal’s Algorithm. 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}}; - PowerPoint PPT Presentation
Popular Tags:
30
Minimum Spanning Trees Kruskal’s Algorithm
Transcript

Minimum Spanning Trees

Kruskal’s Algorithm

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

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


Recommended