90 likes | 100 Views
Graph Algorithms Continued. Kruskal’s Algorithm. a. 4. 6. 5. b. u. 14. 2. 10. c. v. 3. 8. 15. d. f. Kruskal's Algorithm: Main Idea.
E N D
Graph Algorithms Continued Kruskal’s Algorithm
a 4 6 5 b u 14 2 10 c v 3 8 15 d f Kruskal's Algorithm:Main Idea solution = { } while ( more edges in E) do// Selection select minimum weight edgeremove edge from E// Feasibility if (edge closes a cycle with solution so far)then reject edgeelse add edge to solution// Solution check if |solution| = |V | - 1 return solution return null // when does this happen? Cutler/Head
a 4 6 9 5 b e g 14 2 10 15 c f h 3 8 d Kruskal's Algorithm: 1. Sort the edges E in non-decreasing weight2. T 3. For each v V create a set.4. repeat 5. Select next {u,v} E, in order 6. ucomp find (u) 7. vcomp find (v) 8. ifucomp vcomp then8. add edge (u,v) to T9. union ( ucomp,vcomp )10.untilT contains |V | - 1 edges11. returntree T C= { {a}, {b}, {c}, {d}, {e}, {f}, {g}, {h} }C is a forest of trees. Cutler/Head
Kruskal - Disjoint setAfter Initialization A Sorted edges T 2 A B 2 B 6 B C 5 5 A C 6 7 C G B D 7 D 1. Sort the edges E in non-decreasing weight2. T 3. For each v V create a set. D A B C Disjoint data set for G Cutler/Head
Kruskal - add minimum weight edge if feasible A T Sorted edges 2 A B 2 (A, B) 6 B B C 5 5 7 A C 6 C G B D 7 D 5. for each {u,v} in ordered E6. ucomp find (u) 7. vcomp find (v) 8. ifucomp vcomp then9. add edge (v,u) to T10. union( ucomp,vcomp ) Find(A) Find(B) A B C D Disjoint data set for G A C D B After merge(A, B) Cutler/Head
Kruskal - add minimum weight edge if feasible A T Sorted edges 2 A B 2 (A, B) (B, C) 6 B B C 5 5 7 A C 6 C G B D 7 D 5. for each {u,v} in ordered E6. ucomp find (u) 7. vcomp find (v) 8. ifucomp vcomp then9. add edge (v,u) to T10. union ( ucomp,vcomp ) Find(B) Find(C) A C D B After merge(A, C) A B C D Cutler/Head
Kruskal - add minimum weight edge if feasible A T Sorted edges 2 A B 2 (A, B) (B, C) 6 B B C 5 5 7 A C 6 C G B D 7 D 5. for each {u,v} in ordered E6. ucomp find (u) 7. vcomp find (v) 8. ifucomp vcomp then9. add edge (v,u) to T10. union ( ucomp,vcomp ) Find(A) Find(C) A D B C A and C in same set Cutler/Head
Kruskal - add minimum weight edge if feasible A T Sorted edges 2 A B 2 (A, B) (B, C) (B, D) 6 B B C 5 5 7 A C 6 C G B D 7 D 5. for each {u,v} in ordered E6. ucomp find (u) 7. vcomp find (v) 8. ifucomp vcomp then9. add edge (v,u) to T10. union ( ucomp,vcomp ) Find(B) Find(D) A D B C A After merge B C D Cutler/Head
Kruskal's Algorithm: Time Analysis Count1 = ( E lg E ) Count2= (1) Count3= ( V ) Count4 = O( E ) Using Disjoint set-height andpath compression Count4(6+7+10)= O((E +V) (V)) Sorting dominates the runtime. We get T( E,V ) = ( E lg E), so for a sparse graph we get ( V lg V)for a dense graph we get( V2lg V2) = ( V2lg V) Kruskal ( G )1. Sort the edges E in non-decreasing weight2. T 3. For each v V create a set.4. repeat 5. {u,v} E, in order 6. ucomp find (u) 7. vcomp find (v) 8. ifucomp vcomp then9. add edge (v,u) to T10. union ( ucomp,vcomp )11.untilT contains |V | - 1 edges12. returntree T Cutler/Head