union find data structure last time today next
play

Union-find Data Structure Last time Today Next Trees within - PowerPoint PPT Presentation

Union-find Data Structure Last time Today Next Trees within Graphs Trees within Graphs Final Spanning Trees More efficient END OF 15-122! Minimum Spanning Trees Reminders Final exam Review session Practice


  1. Union-find Data Structure

  2. Last time Today Next Trees within Graphs Trees within Graphs Final   Spanning Trees More efficient END OF 15-122!  Minimum Spanning Trees

  3. Reminders • Final exam • Review session • Practice exams • Practice written homework

  4. Spanning Tree A spanning tree is a s ubgraph of original graph that is a tree containing all vertices but only a minimal subset of the original edges 0 3 5 4 8 2 6 1 7

  5. Finding a minimum spanning tree Edge- centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T Greedy algorithm  Choice at each step will be part of the solution  No need for backtracking

  6. A-B (11) C-I (2) A-C (6) D-E (2) A-H (8) D-I (3) Boston A-I (5) F-H (3) 5 Erie Detroit B-C (7) F-I (9) 2 7 B-E (5) F-J (6) 2 2 3 C-D (2) G-H (1) 2 Indianapolis Columbus C-E (2) H-J (7) C-H(11) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  7. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  8. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  9. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  10. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  11. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  12. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  13. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  14. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  15. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  16. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  17. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  18. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  19. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  20. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  21. G-H (1) F-J (6) C-E (2) A-C (6) C-I (2) B-C (7) Boston D-E (2) H-J (7) 5 Erie Detroit C-D (2) A-H (8) 2 7 D-I (3) F-I (9) 2 2 3 F-H (3) C-H (11) 2 Indianapolis Columbus B-E (5) A-B (11) A-I (5) 11 5 6 9 11 Fort Worth Atlanta 8 6 3 7 Juarez Houston 1 Galveston

  22. 0 Spanning Tree 3 5 Complexity 4  General graph DFS/BFS 8 𝑃(𝑓) with adjacency lists 2 6  1 Running DFS/BFS on spanning tree 𝑃(𝑓) but 𝑓 = 𝑤 − 1  𝑃(𝑤) 7

  23. Finding a minimum spanning tree Edge- centric algorithm: Kruskal’s algorithm Sort edges O(e log e) T = graph_new O(1) For each edge (i,j) of G: e Are i and j already connected in T? O(v) Yes: Discard edge O(1) No: Add edge to T O(1) = O(ev + e log e)  O(ev) Where can we make it faster?

  24. Canonical Representatives Trying to speed up connectivity check 0 3 5 4 9 8 Relation of being connected is • reflexive 2 • symmetric 6 • transitive 1 7

  25. Canonical Representatives Trying to speed up connectivity check 0 3 5 4 9 8 Every connected component 2 is an equivalence class 6 1 7

  26. Union-find 1. Check connectivity, finding canonical representative 2. Create union 0 3 5 4 9 8 2 6 1 7

  27. Union-find data structure Basic Union-find Init 0 1 2 3 4 5 Edge order After (4,5) 0 1 2 3 4 4 (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1) After (3,5) 0 1 2 3 3 4 After (1,2) 0 1 1 3 3 4 After (2,3) 0 1 1 1 3 4 0 5 After (0,2) 1 1 1 1 3 4 2 3 0 5 1 4 2 3 How fast is the connectivity check? 1 4

  28. Union-find (UF) Tree 0 5 2 3 1 4

  29. Finding a minimum spanning tree Edge- centric algorithm: Kruskal’s algorithm Sort edges O(e log e) T = graph_new O(1) For each edge (i,j) of G: e * Are i and j already connected in T? O(v) Yes: Discard edge O(1) No: Add edge to T O(1) = O(ev + e log e)  O(ev) With our basic union-find, still O(v) because UF tree may not be balanced

  30. Union-find data structure Basic Union-find Init 0 1 2 3 4 5 Edge order After (4,5) 0 1 2 3 4 4 (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1) After (3,5) 0 1 2 3 3 4 After (1,2) 0 1 1 3 3 4 After (2,3) 0 1 1 1 3 4 0 5 After (0,2) 1 1 1 1 3 4 2 3 0 5 1 4 2 3 1 4

  31. Height Tracking How should we combine trees to control height? Merge shorter tree into taller tree by appointing the root of the taller tree as the canonical representative of the combined tree  height becomes the height of the bigger tree If trees have the same height, it does not matter which tree is merged into which  height increases by 1

  32. Union-find data structure 0 1 2 3 4 5 Basic Union-find Init -1 -1 -1 -1 -1 -1 Edge order After (4,5) -1 -1 -1 -1 -2 4 (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1) After (3,5) -1 -1 -1 -3 3 4 After (1,2) -1 -2 1 -3 3 4 After (2,3) -1 -4 1 1 3 4 0 5 After (0,2) 1 -4 1 1 3 4 2 3 0 5 1 4 2 3 Negative numbers: height 1 4 (used for canonical rep. only)

  33. Union-find data structure 0 1 2 3 4 5 Height-tracking Union-find Init -1 -1 -1 -1 -1 -1 Edge order After (4,5) -1 -1 -1 -1 -2 4 (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1) After (3,5) -1 -1 -1 4 -2 4 After (1,2) -1 -2 1 4 -2 4 After (2,3) -1 -3 1 4 1 4 0 5 After (0,2) 1 -3 1 1 1 4 2 3 0 5 1 4 2 3 Negative numbers: height 1 4 (used for canonical rep. only)

  34. Finding a minimum spanning tree Edge- centric algorithm: Kruskal’s algorithm Sort edges O(e log e) T = graph_new O(1) For each edge (i,j) of G: e * Are i and j already connected in T? O(log v) Yes: Discard edge O(1) No: Add edge to T O(1)

  35. Union-find with Height Tracking G-H (1) B C-E (2) A B C D E F G H I J 1 C-I (2) E D D-E (2) 0 1 2 3 4 5 6 7 8 9 4 3 C-D (2) D-I (3) I F-H (3) 2 8 C B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) 0 F A-H (8) A 5 F-I (9) 9 C-H (11) 7 A-B (11) J H 6 G

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend