Data Structures & Algorithms Graphs

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chen, Lu Mentor: Zhou, Jian Shanghai University, School of Management Chen213.shu.edu.cn.
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Tree Algorithms
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Minimum Spanning Trees
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
COSC 2007 Data Structures II Chapter 14 Graphs III.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Prim's Algorithm This algorithm starts with one node. It then, one by one, adds a node that is unconnected to the new graph to the new graph, each time.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Minimum- Spanning Trees
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Lecture ? The Algorithms of Kruskal and Prim
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees
Lecture 22 Minimum Spanning Tree
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CISC 235: Topic 10 Graph Algorithms.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree Neil Tang 3/25/2010
Connected Components Minimum Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
CSE 373 Data Structures and Algorithms
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Minimum-Cost Spanning Tree
Kruskal’s Minimum Spanning Tree Algorithm
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Tree Neil Tang 4/3/2008
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Greedy Algorithms Comp 122, Spring 2004.
Minimum Spanning Trees (MSTs)
Lecture 12 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Minimum Spanning Trees
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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