Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.

Similar presentations


Presentation on theme: "Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni."— 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 Minimum Cost Spanning Tree Graph has 11 nodes, 13 edges Any spanning tree will have 10 edges 2 3 8 10 1 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 8 2

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

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

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 Edge order: (1,4),(5,6),(10,11),... 4 8 6 6 7 5 2 4 4 5 3 8 2 6 7 2 4 5 3 1 8 9 10 11

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

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 Start at node 1... Grow by adding cheapest edge in fringe 4 8 6 6 7 5 2 4 4 5 3 8 2 6 7 2 4 5 3 1 8 9 10 11 7 5 6 5 5 6 7 2 4 5 3 1 8 9 10 11 wt(2,4) > wt(1,2) Add fringe edges wt(6,5) < wt(2,5)

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 All nodes are tiny trees....each with a color Grow each by adding nearest neighbor 4 8 6 6 7 5 2 4 4 5 3 8 2 6 7 2 4 5 3 8 9 10 11 7 5 6 5 5 6 7 2 4 3 9 10 6 5 9 3 8 7 11 10 Merge components 1 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 Grow components in parallel... Done in two passes! 4 8 6 6 7 5 2 4 4 5 3 8 2 6 7 2 4 5 3 1 8 9 10 11 7 5 6 5 5 6 7 2 4 3 9 10 6 5 9 3 8 7 11 10

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(V 2 ) 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


Download ppt "Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni."

Similar presentations


Ads by Google