Download presentation
Presentation is loading. Please wait.
1
Data Structures & Algorithms Graphs
Richard Newman based on book by R. Sedgewick and slides by S. Sahni 1 1 1 1 1
2
Tree Connected graph that has no cycles
n vertex connected graph with n-1 edges 2 2 2 2 2
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 3 3 3 3 3
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) 4 4 4 4 4
5
Minimum Cost Spanning Tree
2 3 8 10 1 4 5 9 11 6 7 Graph has 11 nodes, 13 edges Any spanning tree will have 10 edges 5 5 5 5 5
6
A Spanning Tree Spanning tree cost = 51
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? 6 6 6 6 6
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? 7 7 7 7 7
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 8 8 8 8 8
9
MST Algorithms Kruskal's Algorithm Prim's Algorithm
Boruvka's Algorithm 9 9 9 9 9
10
MST Algorithms Kruskal's Algorithm
Consider edges in increasing weight order Discard if edge creates a cycle Done when V-1 edges added 10 10 10 10 10
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 11 11 11 11 11
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 12 12 12 12 12
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), ... 13 13 13 13 13
14
Kruskal's Algorithm More edges….. 2 3 8 1 10 4 5 9 11 Done!
7 3 4 6 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 14 14 14 14 14
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 15 15 15 15 15
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 16 16 16 16 16
17
Prim's Algorithm 2 2 7 3 3 4 6 8 8 8 5 1 1 5 6 10 10 2 4 5 4 4 4 3 5 5 9 9 11 11 8 5 6 2 5 6 6 7 wt(6,5) < wt(2,5) wt(2,4) > wt(1,2) 7 7 Start at node 1... Grow by adding cheapest edge in fringe Add fringe edges 17 17 17 17 17
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 18 18 18 18 18
19
Boruvka's Algorithm 2 2 7 3 3 3 4 6 8 8 8 5 1 5 6 10 10 10 2 4 5 4 4 4 3 5 5 9 9 9 11 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 19 19 19 19 19
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) 20 20 20 20 20
21
Boruvka's Algorithm 2 2 7 3 3 3 4 6 8 8 8 5 1 5 6 10 10 10 2 4 5 4 4 4 3 5 5 9 9 9 11 11 8 5 6 2 5 6 6 6 7 7 7 7 Grow components in parallel... Done in two passes! 21 21 21 21 21
22
Boruvka's Algorithm Also known as Sollin’s algorithm
Grows components like Prim, but in parallel Adds edges connecting components like Kruskal 22 22 22 22 22
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 23 23 23 23 23
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 24 24 24 24 24
25
Performance Kruskal's Algorithm Prim'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) 25 25 25 25 25
26
MST Algorithms Kruskal's Algorithm Prim's Algorithm
Boruvka's Algorithm 26 26 26 26 26
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.