270 likes | 295 Views
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
E N D
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 • If original graph has n vertices, the spanning tree has n vertices and n-1 edges
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)
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
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?
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?
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
MST Algorithms • Kruskal's Algorithm • Prim's Algorithm • Boruvka's Algorithm
MST Algorithms • Kruskal's Algorithm • Consider edges in increasing weight order • Discard if edge creates a cycle • Done when V-1 edges added
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
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
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), ...
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
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
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
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
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
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
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)
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!
Boruvka's Algorithm • Also known as Sollin’s algorithm • Grows components like Prim, but in parallel • Adds edges connecting components like Kruskal
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
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
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)
MST Algorithms • Kruskal's Algorithm • Prim's Algorithm • Boruvka's Algorithm