Mst(engl)
-
Upload
yliu8698 -
Category
Technology
-
view
230 -
download
0
Transcript of Mst(engl)
![Page 1: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/1.jpg)
Minimum-Spanning Trees
Minimum- Spanning TreesMinimum- Spanning Trees1. Concrete example: computer connection
2. Definition of a Minimum- Spanning Tree
3. The Crucial Fact about Minimum- Spanning Trees
4. Algorithms to find Minimum- Spanning Trees - Kruskal‘s Algorithm - Prim‘s Algorithm - Barůvka‘s Algorithm
![Page 2: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/2.jpg)
Minimum-Spanning Trees
Imagine: You wish to connect all the computers in an office building using the least amount of cable
a weighted graph problem !!
- Each vertex in a graph G represents a computer- Each edge represents the amount of cable needed to connect all computers
Concrete exampleConcrete example
![Page 3: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/3.jpg)
Minimum-Spanning Tree
We are interested in:
Finding a tree T that contains all the verticesof a graph G spanning treespanning treeand has the least total weight over allsuch trees minimum-spanning treeminimum-spanning tree (MST)(MST)
Tuv
uvwTw),(
)),(()(
![Page 4: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/4.jpg)
Crucial Fact
min-weight“bridge“ edge
The Crucial Fact about MSTThe Crucial Fact about MST
eV 1
V 2
![Page 5: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/5.jpg)
Crucial Fact
The Crucial Fact about MST-
The basis of the following algorithms
Proposition: Let G = (V,E) be a weighted graph, and let and
be two disjoint nonempty sets such that .
Furthermore, let e be an edge with minimum weight from
among those with one vertex in and the other in .
There is a minimum- spanning tree T that has e as one of
its edges.
21 VVV 1V 2V
1V 2V
![Page 6: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/6.jpg)
Crucial Fact
The Crucial Fact about MST-
The basis of the following algorithms
Justification: There is no minimum- spanning tree that has e as one of
of ist edges. The addition of e must create a cycle.
There exists an edge f (one endpoint in the other in ).
Choose: . By removing f from , a
spanning tree is created, whose total weight is no more than
before. A new MSTcontaining e
Contradiction!!!
There is a MST containing e after all!!!
1V 2V)()( fwew }{eT
![Page 7: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/7.jpg)
MST-Algorithms
Input: A weighted connected graph G = (V,E) with n vertices and m edges
Output: A minimum- spanning tree T
Tuv
uvwTw),(
)),(()(
MST-AlgorithmsMST-Algorithms
![Page 8: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/8.jpg)
Kruskal's Algorithm
Kruskal‘s AlgorithmKruskal‘s Algorithm
1. Each vertex is in its own cluster
2. Take the edge e with the smallest weight - if e connects two vertices in different clusters, then e is added to the MST and the two clusters, which are connected by e, are merged into a single cluster - if e connects two vertices, which are already in the same cluster, ignore it
3. Continue until n-1 edges were selected
![Page 9: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/9.jpg)
Kruskal's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 10: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/10.jpg)
Kruskal's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 11: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/11.jpg)
Kruskal's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 12: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/12.jpg)
Kruskal's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 13: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/13.jpg)
Kruskal's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 14: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/14.jpg)
Kruskal's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
cycle!!
![Page 15: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/15.jpg)
Kruskal's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 16: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/16.jpg)
Kruskal's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 17: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/17.jpg)
Kruskal's Algorithm
C
FE
A B
D
3
2
1 2
2
minimum- spanning tree
![Page 18: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/18.jpg)
Kruskal's Algorithm
The correctness of Kruskal‘s AlgorithmThe correctness of Kruskal‘s Algorithm
Crucial Fact about MSTs
Running time: O ( m log n )
By implementing queue Q as a heap, Q could be initialized in O ( m ) time and a vertex could be extracted in each iteration in O ( log n ) time
![Page 19: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/19.jpg)
Kruskal's Algorithm
Input: A weighted connected graph G with n vertices and m edges Output: A minimum-spanning tree T for G
for each vertex v in G do Define a cluster C(v) {v}.Initialize a priority queue Q to contain all edges in G, using weights as keys.T while Q do Extract (and remove) from Q an edge (v,u) with smallest weight. Let C(v) be the cluster containing v, and let C(u) be the cluster containing u. if C(v) C(u) then Add edge (v,u) to T. Merge C(v) and C(u) into one cluster, that is, union C(v) and C(u).return tree T
Code FragmentCode Fragment
![Page 20: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/20.jpg)
Prim's Algorithm
Prim‘s AlgorithmPrim‘s Algorithm
1. All vertices are marked as not visited
2. Any vertex v you like is chosen as starting vertex and is marked as visited (define a cluster C)
3. The smallest- weighted edge e = (v,u), which connects one vertex v inside the cluster C with another vertex u outside of C, is chosen and is added to the MST.
4. The process is repeated until a spanning tree is formed
![Page 21: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/21.jpg)
Prim's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 22: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/22.jpg)
Prim's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 23: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/23.jpg)
Prim's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
We could delete these edges because of Dijkstra‘s label D[u] for each vertex outside of the cluster
![Page 24: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/24.jpg)
Prim's Algorithm
C
FE
A B
D
3
4
2
1 2
3
2
![Page 25: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/25.jpg)
Prim's Algorithm
C
FE
A B
D
3
2
1 2
3
2
![Page 26: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/26.jpg)
Prim's Algorithm
C
FE
A B
D
3
2
1 2
2
3
![Page 27: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/27.jpg)
Prim's Algorithm
C
FE
A B
D
3
2
1 2
2
![Page 28: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/28.jpg)
Prim's Algorithm
C
FE
A B
D
3
2
1 2
2
![Page 29: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/29.jpg)
Prim's Algorithm
C
FE
A B
D
3
2
1 2
2
minimum- spanning tree
![Page 30: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/30.jpg)
Prim's Algorithm
The correctness of Prim‘s AlgorithmThe correctness of Prim‘s Algorithm
Crucial Fact about MSTs
Running time: O ( m log n )
By implementing queue Q as a heap, Q could be initialized in O ( m ) time and a vertex could be extracted in each iteration in O ( log n ) time
![Page 31: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/31.jpg)
Baruvka's Algorithm
BarBarůůvka‘s Algorithmvka‘s Algorithm
1. For all vertices search the edge with the smallest weightof this vertex and mark these edges
2. Search connected vertices (clusters) and replace them by a “new“ vertex (cluster)
3. Remove the cycles and, if two vertices are connected by more than one edge, delete all edges except the “cheapest“
iC
![Page 32: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/32.jpg)
Baruvka's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 33: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/33.jpg)
Baruvka's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 34: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/34.jpg)
Baruvka's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2 iC
![Page 35: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/35.jpg)
Baruvka's Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
![Page 36: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/36.jpg)
Baruvka's Algorithm
C
FE
A B
D
3
2
1 2
2
![Page 37: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/37.jpg)
Baruvka's Algorithm
C
FE
A B
D
3
2
1 2
2
minimum- spanning tree
![Page 38: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/38.jpg)
Baruvka's Algorithm
The correctness of BarThe correctness of Barůvkaůvka‘s Algorithm‘s Algorithm
Crucial Fact about MSTs
Running time: O ( m log n )
The number of edges is at least reduced by half in each step.
Number of steps: O ( log n )
![Page 39: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/39.jpg)
Comparison
ComparisonComparison
Kruskal‘s, Prim‘s, and Borůvka‘s
algorithm
![Page 40: Mst(engl)](https://reader036.fdocuments.us/reader036/viewer/2022062418/55619f67d8b42ae1538b4867/html5/thumbnails/40.jpg)
Comparison
ComparisonComparison
Although each of the above algorithms has the same worth-case running time, each one achieves this running time using different data structures and different approaches to build the MST.
there is no clear winner among these three algorithms