Download - Minimum Spanning Trees
![Page 1: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/1.jpg)
Minimum Spanning Minimum Spanning TreesTrees
CS 146CS 146Prof. Sin-Min LeeProf. Sin-Min Lee
Regina WangRegina Wang
![Page 2: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/2.jpg)
Spanning Tree properties:Spanning Tree properties:
On a connected graph G=(V, E), a spanning tree:
• is a connected subgraph• acyclic• is a tree (|E| = |V| - 1) • contains all vertices of G (spanning)
![Page 3: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/3.jpg)
Spanning treesSpanning trees• Suppose you have a connected undirected graph
– Connected: every node is reachable from every other node– Undirected: edges do not have an associated direction
• ...then a spanning tree of the graph is a connected subgraph in which there are no cycles
• Total of 16 different spanning trees for the graph below
A connected,undirected graph
Four of the spanning trees of the graph
![Page 4: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/4.jpg)
Finding a spanning treeFinding a spanning tree• BFS• DFS• Greedy algorithm
An undirected graph Result of a BFSstarting from top
Result of a DFSstarting from top
![Page 5: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/5.jpg)
Minimum weight spanning tree (MST)Minimum weight spanning tree (MST)
On a weighted graph, A MST:• connects all vertices through edges with
least weights.
• has w(T) ≤ w(T’) for every other spanning tree T’ in G
• Adding one edge not in MST will create a cycle
![Page 6: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/6.jpg)
Finding Minimum Spanning TreeFinding Minimum Spanning Tree
Two “greedy” algorithms• Kruskal’s and Prim’s• make best decision at every stage• locally optimal choice globally optimal
solution.• Greedy algorithms do not always yields
optimal solutions.
![Page 7: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/7.jpg)
Kruskal’s algorithmKruskal’s algorithm EdgeEdge first first1. Arrange all edges in a list (L) 1. Arrange all edges in a list (L) in non-decreasing orderin non-decreasing order2.2. Select edges from L, and include Select edges from L, and include
that in set T, avoid cycle.that in set T, avoid cycle.3.3. Repeat 3 until T becomes a tree Repeat 3 until T becomes a tree
that covers all verticesthat covers all vertices
![Page 8: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/8.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
![Page 9: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/9.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
![Page 10: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/10.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
![Page 11: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/11.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
![Page 12: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/12.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
![Page 13: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/13.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
![Page 14: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/14.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
![Page 15: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/15.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
Skip
Skip {7,8} to avoid cycle
![Page 16: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/16.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
Skip
Skip {5,6} to avoid cycle
Skip
![Page 17: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/17.jpg)
Kruskal’s AlgorithmKruskal’s Algorithm{{1,2}1,2} 1122
{{3,4}3,4} 1212
{{1,8}1,8} 1313
{{4,5}4,5} 1313
{{2,7}2,7} 1414
{{3,6}3,6} 1414
{{7,8}7,8} 1414
{{5,6}5,6} 1414
{5,8}{5,8} 1515
{6,7}{6,7} 1515
{1,4}{1,4} 1616
{2,3}{2,3} 1616
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
MST is formed
![Page 18: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/18.jpg)
Prim’s algorithmPrim’s algorithm
Start form any arbitrary Start form any arbitrary vertexvertex Find the edge that has minimum Find the edge that has minimum
weight form weight form allall known vertices known vertices Stop when the tree covers all Stop when the tree covers all
verticesvertices
![Page 19: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/19.jpg)
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
1212
1414
1
Start from any arbitrary vertex
Prim’s algorithmPrim’s algorithm
![Page 20: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/20.jpg)
8 5
1 4
2 3
7 6
14
15
15
14
13 1316
16
12
1414
1
2
Weight:12
The best choice is {1,2}
Prim’s algorithmPrim’s algorithm
12What’s next?
![Page 21: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/21.jpg)
8 5
1 4
2 3
7 6
14
15
15
14
1316
16
12
1414
1
2
Weight:12+13
After {1,8} we may choose {2,7} or {7,8}There are more than one MST
Prim’s algorithmPrim’s algorithm
12What’s next?
813
![Page 22: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/22.jpg)
8 5
1 4
2 3
7 6
14
15
15
14
1316
16
12
14
1
2
Let’s choose {2, 7} at this moment
Prim’s algorithmPrim’s algorithm
12 We are free to choose {5,8} or {6,7} but not {7,8} because we need to avoid cycle
813
14
7
![Page 23: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/23.jpg)
8 5
1 4
2 3
7 6
14
15
14
1316
16
12
14
1
2
Let’s choose {5,8} at this moment
Prim’s algorithmPrim’s algorithm
12
The best choice is now {4,5}
813
14
7
15 5
![Page 24: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/24.jpg)
8 5
1 4
2 3
7 6
14
15
14
16
16
12
14
1
2
Prim’s algorithmPrim’s algorithm
12The best choice is now {4,3}
813
14
7
15 513
4
![Page 25: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/25.jpg)
8 5
1 4
2 3
7 6
14
15
14
16
1614
1
2
Prim’s algorithmPrim’s algorithm
12The best choice is now {3,6} or {5,6}
813
14
7
15 513
412
3
![Page 26: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/26.jpg)
8 5
1 4
2 3
7 6
14
15
14
16
16
1
2
Prim’s algorithmPrim’s algorithm
12A MST is formed
Weight = 93
813
14
7
15 513
412
3
614
![Page 27: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/27.jpg)
Prim’s algorithm – more Prim’s algorithm – more complexcomplex
05
20
54
3
4414
9932
8100
1114
31
19
![Page 28: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/28.jpg)
Prim’s algorithm – more Prim’s algorithm – more complexcomplex
05
20
54
3
4414
9932
8100
1114
31
19
![Page 29: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/29.jpg)
Prim’s algorithmPrim’s algorithm
05
20
54
3
4414
9932
8100
1114
31
19
![Page 30: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/30.jpg)
Prim’s algorithmPrim’s algorithm
05
20
54
3
4414
9932
8100
1114
31
19
![Page 31: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/31.jpg)
Prim’s algorithmPrim’s algorithm
05
20
54
3
4414
9932
8100
1114
31
19
![Page 32: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/32.jpg)
Prim’s algorithmPrim’s algorithm
05
20
54
3
4414
9932
8100
1114
31
19
![Page 33: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/33.jpg)
Prim’s algorithmPrim’s algorithm
05
20
54
3
4414
9932
8100
1114
31
19
![Page 34: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/34.jpg)
Prim’s algorithmPrim’s algorithm
05
20
54
3
4414
9932
8100
1114
31
19
![Page 35: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/35.jpg)
Prim’s algorithmPrim’s algorithm
05
20
54
3
4414
9932
8100
1114
31
19
MST is formed
![Page 36: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/36.jpg)
Compare Prim and KruskalCompare Prim and Kruskal
• Both have the same output MST• Kruskal’s begins with forest and merge into a tree• Prim’s always stays as a tree• If you don’t know all the weight on edges use
Prim’s algorithm• If you only need partial solution on the graph
use Prim’s algorithm
![Page 37: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/37.jpg)
Compare Prim and KruskalCompare Prim and Kruskal
ComplexityKruskal: O(NlogN) comparison sort for edgesPrim: O(NlogN) search the least weight edge for every vertice
![Page 38: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/38.jpg)
Why do we need MST? Why do we need MST?
• a reasonable way for clustering points in space into natural groups
• can be used to give approximate solutions to hard problems
![Page 39: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/39.jpg)
Minimizing costsMinimizing costs• Suppose you want to provide solution to :
– electric power, Water, telephone lines, network setup
• To minimize cost, you could connect locations using MST
• However, MST is not necessary the shortest path and it does not apply to cycle
![Page 40: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/40.jpg)
MST don’t solve TSPMST don’t solve TSP• Travel salesman problem (TSP) can not be
solved by MST : salesman needs to go home (what’s the cost going home?) TSP is a cycle Use MST to approximate solve TSP by exhaustive approach try every permutation on cyclic graph
![Page 41: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/41.jpg)
More practiceMore practice
Kruskal's AlgorithmKruskal's Algorithm applet applet
Prim’s AlgorithmPrim’s Algorithm applet applet
![Page 42: Minimum Spanning Trees](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a73550346895dc7d9e9/html5/thumbnails/42.jpg)
The EndThe End
Thank you Thank you