1 / 26

Data Structures & Algorithms Graphs

Data Structures & Algorithms Graphs. Richard Newman based on book by R. Sedgewick and slides by S. Sahni. Tree. Connected graph that has no cycles n vertex connected graph with n-1 edges. Spanning Tree. Subgraph that includes all vertices of the original graph Subgraph is a tree

pgoins
Download Presentation

Data Structures & Algorithms Graphs

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. Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

  2. Tree • Connected graph that has no cycles • n vertex connected graph with n-1 edges

  3. Spanning Tree • Subgraph that includes all vertices of the original graph • Subgraph is a tree • If original graph has n vertices, the spanning tree has n vertices and n-1 edges

  4. Minimum Cost Spanning Tree • Weighted, undirected graph G • Spanning tree T of G • Cost(T) = sum of edge weights • MST = Spanning tree T of G such that no other spanning tree T' of G has cost(T') < cost(T)

  5. 2 4 3 8 8 1 6 10 2 4 5 4 4 3 5 9 8 11 5 6 2 7 6 7 Minimum Cost Spanning Tree Graph has 11 nodes, 13 edges Any spanning tree will have 10 edges

  6. A Spanning Tree 2 4 3 8 8 1 6 10 2 4 5 4 4 3 5 9 8 11 5 6 2 7 6 7 Spanning tree cost = 51 Is there a cheaper spanning tree?

  7. Minimum Cost Spanning Tree 2 4 3 8 8 1 6 10 2 4 5 4 4 3 5 9 8 11 5 6 2 7 6 7 Spanning tree cost = 41 Is there any cheaper spanning tree?

  8. Greedy Method • Next optimal thing to do is also globally optimal thing to do • Never revisit a decision • Always make progress • Greedy method works for MST

  9. MST Algorithms • Kruskal's Algorithm • Prim's Algorithm • Boruvka's Algorithm

  10. MST Algorithms • Kruskal's Algorithm • Consider edges in increasing weight order • Discard if edge creates a cycle • Done when V-1 edges added

  11. MST Algorithms • Prim's Algorithm • Start at any node v • Add non-tree node w whose edge from tree is least cost • Done when all nodes added

  12. MST Algorithms • Boruvka-Sollin Algorithm • Start with all nodes, no edges • For each tree in forest, add each to connect to nearest neighbor tree • If edge weights distinct, all good • Otherwise, must detect cycles and delete edges as needed

  13. Kruskal's Algorithm 2 3 4 8 8 1 6 10 2 4 5 4 4 3 5 9 11 8 5 6 2 6 7 7 Edge order: (1,4),(5,6),(10,11), ...

  14. Kruskal's Algorithm More edges….. 7 2 3 6 4 8 8 5 1 5 6 10 2 4 5 4 4 3 5 9 11 8 5 6 2 Done! have V-1 edges 5 6 7 7 Edge order: (1,4),(5,6),(10,11), ... (2,4) discarded since it makes a cycle

  15. Kruskal's Algorithm • Sort edges by weight • O(E lg E) time • Determine if next edge makes cycle • Iff connects two nodes in same component • Use fast Union-Find • Stop when V-1 edges are added

  16. Prim's Algorithm • Start at any node v – set T = {v} • Set Fringe F = {edges from v} • Set tree edges M = {} • While |T| < V • Add least cost edge in F to M • Add its endpoint w to T • Add all edges from w to u in V-T to F whose weight is less

  17. Prim's Algorithm 7 2 2 3 3 6 4 8 8 8 5 1 1 5 6 10 10 2 4 5 4 4 4 3 5 5 9 11 9 11 8 5 6 2 5 6 6 7 wt(2,4) > wt(1,2) wt(6,5) < wt(2,5) 7 7 Start at node 1... Grow by adding cheapest edge in fringe Add fringe edges

  18. Boruvka's Algorithm • Start with each node a component • Start with tree edges M = {} • While |M| < V-1 • For each component add least cost edge to distinct component • Merge components

  19. Boruvka's Algorithm 2 7 2 3 3 3 6 4 8 8 8 5 5 1 6 10 10 10 2 4 5 4 4 4 3 5 5 9 9 11 9 11 8 5 6 2 5 6 6 6 7 7 7 7 Merge components All nodes are tiny trees....each with a color Grow each by adding nearest neighbor Two components may pick the same edge

  20. Boruvka's Algorithm • Note that while the animation showed one edge added at a time, in fact each component can add an edge in parallel • Makes for nice parallel MST algorithm (Sollin)

  21. Boruvka's Algorithm 2 7 2 3 3 3 6 4 8 8 8 5 1 5 6 10 10 10 2 4 5 4 4 4 3 5 5 9 9 11 9 11 8 5 6 2 5 6 6 6 7 7 7 7 Grow components in parallel... Done in two passes!

  22. Boruvka's Algorithm • Also known as Sollin’s algorithm • Grows components like Prim, but in parallel • Adds edges connecting components like Kruskal

  23. Boruvka's Algorithm • Each node is a component at start • All nodes have all adjacent edges as their fringe at start • Always pick least cost edge in a component's fringe (keep sorted) • Need to deal with component merge • Union-Find to discard internal edges • Merge fringes and sort

  24. Faster Boruvka's Algorithm • Each node is a component at start • All edges are viable at start • While there are viable edges • If edge makes cycle discard • If edge costs less to connect two distinct components, remember it is viable • For each component, add cheapest fringe edge if no cycle

  25. Performance • Kruskal's Algorithm • O(V + E lg E) time using Union-Find • Prim's Algorithm • O(V2) using Dijkstra-like approach • O(E + V lg V) Fibonacci heap • Boruvka-Sollin Algorithm • Halve number of trees per pass • O(E lg V lg*E)

  26. MST Algorithms • Kruskal's Algorithm • Prim's Algorithm • Boruvka's Algorithm

More Related