1 / 9

Graph Algorithms Continued

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.

deborahwade
Download Presentation

Graph Algorithms Continued

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graph Algorithms Continued Kruskal’s Algorithm

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

More Related