Download presentation
Presentation is loading. Please wait.
Published byÁrpád Bogdán Modified over 5 years ago
1
Kruskal’s Idea Adding one edge at a time Non-decreasing order No cycle
Build a minimum cost spanning tree T by adding edges to T one at a time Non-decreasing order Select the edges for inclusion in T in non-decreasing order of the cost No cycle An edge is added to T if it does not form a cycle N-1 edges will be selected Since G is connected and has n > 0 vertices, exactly n-1 edges will be selected J.-J. Chen, CE NTPU May 25, 2015
2
Examples for Kruskal’s Algorithm
10 12 14 16 18 22 24 25 28 1 2 3 4 5 6 28 16 12 18 24 22 25 10 14 1 2 3 4 5 6 1 2 3 4 5 6 10 1 2 3 4 5 6 10 12 1 2 3 4 5 6 10 12 14 1 2 3 4 5 6 10 12 14 16 6/9 J.-J. Chen, CE NTPU May 25, 2015
3
10 1 2 3 4 5 6 1 2 3 4 5 6 12 10 10 14 14 16 14 16 16 25 12 12 18 22 22 22 24 cost = 25 28 J.-J. Chen, CE NTPU May 25, 2015
4
Kruskal’s Algorithm Find the n-1 edges O(e log e) T= ;
while((T contains less than n-1 edges) && (E is not empty) ) { choose an edge(v,w) from E of lowest cost; delete(v,w) from E; if((v,w) does not create a cycle in T) add(v,w) to T; else discard(v,w); } If (T contains fewer than n-1 edges) { cout << “No spanning tree” << endl; O(e log e) It can be proved that: kruskal’s algorithm is optimal if there is a spanning tree, kruskal will find it If there is a min-cost spanning tree U, then there exists a cost-preserving transformation that maps U to the one Kruskal find J.-J. Chen, CE NTPU May 25, 2015
5
Kruskal’s Algorithm 1 5 6 2 4 3 choose T= ;
while((T contains less than n-1 edges) && (E is not empty) ) { choose an edge(v,w) from E of lowest cost; delete(v, w) from E; if((v, w) does not create a cycle in T) add(v, w) to T; else discard(v, w); } If (T contains fewer than n-1 edges) { cout << “No spanning tree” << endl; 28 10 1 14 16 5 6 2 24 25 18 12 4 choose 22 3 J.-J. Chen, CE NTPU May 25, 2015
6
(tree all the time vs. forest)
Prim’s Algorithm (1) (tree all the time vs. forest) // Assume that G has at least one vertex TV={0}; // start with vertex 0 and no edges For (T= ; T contains fewer than n-1 edges; add(u,v) to T) { let (u,v) be a least-cost edge such that uTV and vTV; If(there is no such edge) break; Add v to TV } If(T contains fewer than n-1 edges) { cout << “No spanning tree” << end; J.-J. Chen, CE NTPU May 25, 2015
7
Prim’s Algorithm (2) 1 2 3 4 5 6 28 16 12 18 24 22 25 10 14 1 2 3 4 5 6 10 1 2 3 4 5 6 10 25 22 1 2 3 4 5 6 10 25 J.-J. Chen, CE NTPU May 25, 2015
8
Prim’s Algorithm (3) 1 2 3 4 5 6 28 16 12 18 24 22 25 10 14 1 2 3 4 5 6 10 25 22 12 1 2 3 4 5 6 10 25 22 12 16 1 2 3 4 5 6 10 25 22 12 16 14 J.-J. Chen, CE NTPU May 25, 2015
9
Prim’s Algorithm (cont’d)
28 10 1 14 16 5 6 2 24 25 18 12 4 22 3 J.-J. Chen, CE NTPU May 25, 2015
10
Sollin’s Algorithm Sollin’s Algorithm 1 5 6 2 4 3
Selects several edges for inclusion in T at each stage. At the start of a stage, the selected edges forms a spanning forest. During a stage we select a minimum cost edge that has exactly one vertex in the tree edge for each tree in the forest. Repeat until only one tree at the end of a stage or no edges remain for selection. Stage 1: (0, 5), (1, 6), (2, 3), (3, 2), (4, 3), (5, 0), (6, 1){(0, 5)}, {(1, 6)}, {(2, 3), (4, 3)} Stage 2: {(0, 5), (5, 4)}, {(1, 6), (1, 2)}, {(2, 3), (4, 3), (1, 2)} Result: {(0, 5), (5, 4), (1, 6), (1, 2), (2, 3), (4, 3)} 28 10 1 14 16 5 6 2 24 25 18 12 4 22 3 J.-J. Chen, CE NTPU May 25, 2015
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.