CHAPTER 13 GRAPH ALGORITHMS

Similar documents
Graph Algorithms shortest paths, minimum spanning trees, etc.

Graph Algorithms shortest paths, minimum spanning trees, etc.

Graphs Weighted Graphs. Reading: 12.5, 12.6, 12.7

CHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW

Shortest Paths D E. Shortest Paths 1

Dijkstra s Algorithm

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

Prim & Kruskal Algorithm

Lecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler

Graphs ORD SFO LAX DFW. Data structures and Algorithms

CS2210 Data Structures and Algorithms

Minimum Spanning Trees

Weighted Graphs ( 12.5) Shortest Paths. Shortest Paths ( 12.6) Shortest Path Properties PVD ORD SFO LGA HNL LAX DFW MIA PVD PVD ORD ORD SFO SFO LGA

Outline and Reading. Minimum Spanning Tree. Minimum Spanning Tree. Cycle Property. Minimum Spanning Trees ( 12.7)

Routing & Congestion Control

22 Elementary Graph Algorithms. There are two standard ways to represent a

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

CHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW

Graphs ORD SFO LAX DFW. Graphs 1

Week 12: Minimum Spanning trees and Shortest Paths

22 Elementary Graph Algorithms. There are two standard ways to represent a

Lecture 6 Basic Graph Algorithms

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Artificial Intelligence

CHAPTER 13 GRAPH ALGORITHMS

Minimum Spanning Trees

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees

Minimum Spanning Trees

Graph Algorithms Introduction to Data Structures. Ananda Gunawardena 7/31/2011 1

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Week 11: Minimum Spanning trees

Shortest path problems

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Minimum Spanning Trees Shortest Paths

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

2 A Template for Minimum Spanning Tree Algorithms

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Minimum Spanning Trees My T. UF

Shortest Paths. Nishant Mehta Lectures 10 and 11

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Parallel Graph Algorithms

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Minimum Spanning Tree

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

Minimum Spanning Trees

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Shortest Paths. Nishant Mehta Lectures 10 and 11

Graphs 3/25/14 15:37 SFO LAX Goodrich, Tamassia, Goldwasser Graphs 2

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CS 310 Advanced Data Structures and Algorithms

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Minimum spanning trees

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Kruskal s MST Algorithm

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Lecture 11: Analysis of Algorithms (CS ) 1

Minimum spanning trees

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Single Source Shortest Path

Introduction: (Edge-)Weighted Graph

CS350: Data Structures Dijkstra s Shortest Path Alg.

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Parallel Graph Algorithms

CSE 100: GRAPH ALGORITHMS

Weighted Graph Algorithms Presented by Jason Yuan

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Lecture 18 Solving Shortest Path Problem: Dijkstra s Algorithm. October 23, 2009

Minimum Spanning Trees

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

Introduction to Algorithms. Lecture 11

Algorithms for Minimum Spanning Trees

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Review of course COMP-251B winter 2010

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

Algorithm Design (8) Graph Algorithms 1/2

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CS302 - Data Structures using C++

SHORTEST PATHS. Weighted Digraphs. Shortest paths. Shortest Paths BOS ORD JFK SFO LAX DFW MIA

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

Shortest Path Algorithms

CS 6783 (Applied Algorithms) Lecture 5

Lecture 19. Broadcast routing

Transcription:

CHAPTER 13 GRAPH ALGORITHMS SFO LAX ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 00) AND SLIDES FROM NANCY M. AMATO AND JORY DENNY 1

0 6 BOS 9 PVD 1 16 61 0 JFK 1 SFO 33 16 0 1391 1 BWI 1090 15 LAX 135 96 111 MIA MINIMUM SPANNING TREES 3

REMINDER WEIGHTED GRAPHS In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example: In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports SFO LGA PVD HNL LAX MIA 3

MINIMUM SPANNING TREE Spanning subgraph Subgraph of a graph G containing all the vertices of G Spanning tree Spanning subgraph that is itself a (free) tree Minimum spanning tree (MST) DEN 1 9 6 STL 10 3 PIT DCA Spanning tree of a weighted graph with minimum total edge weight 5 Applications Communications networks ATL Transportation networks

EXERCISE MST Show an MST of the following graph. SFO LGA PVD HNL LAX MIA 5

CYCLE PROPERTY Cycle Property: Let T be a minimum spanning tree of a weighted graph G Let e be an edge of G that is not in T and C let be the cycle formed by e with T For every edge f of C, weight f weight e Proof: By contradiction If weight f > weight(e) we can get a spanning tree of smaller weight by replacing e with f 6 6 f C f C 9 3 9 3 e Replacing f with e yields a better spanning tree e 6

PARTITION PROPERTY Partition Property: Consider a partition of the vertices of G into subsets U and V Let e be an edge of minimum weight across the partition There is a minimum spanning tree of G containing edge e Proof: Let T be an MST of G If T does not contain e, consider the cycle C formed by e with T and let f be an edge of C across the partition By the cycle property, weight f weight(e) Thus, weight f = weight e We obtain another MST by replacing f with e U f 9 5 e U f 9 5 e V 3 Replacing f with e yields another MST V 3

PRIM-JARNIK S ALGORITHM We pick an arbitrary vertex s and we grow the MST as a cloud of vertices, starting from s We store with each vertex v a label d v = the smallest weight of an edge connecting v to a vertex in the cloud At each step: We add to the cloud the vertex u outside the cloud with the smallest distance label We update the labels of the vertices adjacent to u

PRIM-JARNIK S ALGORITHM An adaptable priority queue stores the vertices outside the cloud Key: distance, D[v] Element: vertex v Q. replacekey i, k changes the key of an item We store three labels with each vertex v: Distance D[v] Parent edge in MST P[v] Locator in priority queue Algorithm PrimJarnikMST(G) Input: A weighted connected graph G Output: A minimum spanning tree T of G 1. Pick any vertex v of G. D v 0; P v 3. for each vertex u v do. D u ; P u 5. T 6. Priority queue Q of vertices with D[u] as the key. while Q. empty( ) do. u Q. removemin( ) 9. Add vertex u and edge P[u] to T 10. for each e u. incidentedges do 11. z e. opposite u 1. if e. weight( ) < D[z] 13. D z e. weight( ); P z e 1. Q. replacekey z, D[z] 15. return T 9

EXAMPLE B D C A F E 5 3 9 0 B D C A F E 5 3 9 0 5 B D C A F E 5 3 9 0 5 B D C A F E 5 3 9 0 5 10

EXAMPLE A 0 B 5 D 5 9 F C 3 E 3 A 0 B 5 D 5 9 F C 3 E 3 11

EXERCISE PRIM S MST ALGORITHM Show how Prim s MST algorithm works on the following graph, assuming you start with SFO Show how the MST evolves in each iteration (a separate figure for each iteration). SFO LGA PVD HNL LAX MIA 1

ANALYSIS Graph operations Method incidentedges is called once for each vertex Label operations We set/get the distance, parent and locator labels of vertex z O deg z times Setting/getting a label takes O 1 time Priority queue operations Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O log n time The key of a vertex w in the priority queue is modified at most deg w times, where each key change takes O log n time Prim-Jarnik s algorithm runs in O n + m log n time provided the graph is represented by the adjacency list structure Recall that Σ v deg v = m The running time is O m log n since the graph is connected 13

KRUSKAL S ALGORITHMS A priority queue stores the edges outside the cloud Key: weight Element: edge At the end of the algorithm We are left with one cloud that encompasses the MST A tree T which is our MST Algorithm KruskalMST(G) 1. for each vertex v G. vertices do. Define a cluster C v v 3. Initialize a priority queue Q of edges using the weights as keys. T 5. while T has fewer than n 1 edges do 6. u, v Q. removemin( ). if C v C(u) then. Add u, v to T 9. Merge C v and C u 10. return T 1

DATA STRUCTURE FOR KRUSKAL S ALGORITHM The algorithm maintains a forest of trees An edge is accepted it if connects distinct trees We need a data structure that maintains a partition, i.e., a collection of disjoint sets, with the operations: find u : return the set storing u union u, v : replace the sets storing u and v with their union 15

REPRESENTATION OF A PARTITION Each set is stored in a sequence Each element has a reference back to the set Operation find u takes O 1 time, and returns the set of which u is a member. In operation union u, v, we move the elements of the smaller set to the sequence of the larger set and update their references The time for operation union(u, v) is O min n u, n v, where n u and n v are the sizes of the sets storing u and v Whenever an element is processed, it goes into a set of size at least double, hence each element is processed at most log n times 16

ANALYSIS A partition-based version of Kruskal s Algorithm performs cluster merges as unions and tests as finds. Running time There will be at most m removals from the priority queue - O m log n Each vertex can be merged at most log n times, as the clouds tend to double in size - O n log n Total: O n + m log n 1

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 BWI 1090 15 LAX 135 96 3 111 MIA 1

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA 19

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA 0

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA 1

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA 3

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA 5

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA 6

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA 9

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 15 BWI 1090 LAX 135 96 3 111 MIA 30

EXAMPLE 0 6 BOS SFO 33 16 9 0 16 61 0 1391 PVD 1 1 JFK 1 BWI 1090 15 LAX 135 96 3 111 MIA 31

EXERCISE KRUSKAL S MST ALGORITHM Show how Kruskal s MST algorithm works on the following graph. Show how the MST evolves in each iteration (a separate figure for each iteration). SFO LGA PVD HNL LAX MIA 3

SHORTEST PATHS 0 A 1 B C D 5 3 9 5 E F 3 33

WEIGHTED GRAPHS In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example: In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports SFO LGA PVD HNL LAX MIA 3

SHORTEST PATH PROBLEM Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v. Length of a path is the sum of the weights of its edges. Example: Shortest path between Providence and Honolulu Applications Internet packet routing Flight reservations Driving directions HNL SFO LAX LGA PVD MIA 35

SHORTEST PATH PROBLEM If there is no path from v to u, we denote the distance between them by d v, u = What if there is a negative-weight cycle in the graph? SFO LGA PVD HNL LAX MIA 36

SHORTEST PATH PROPERTIES Property 1: A subpath of a shortest path is itself a shortest path Property : There is a tree of shortest paths from a start vertex to all the other vertices Example: Tree of shortest paths from Providence SFO LGA PVD HNL LAX MIA 3

DIJKSTRA S ALGORITHM The distance of a vertex v from a vertex s is the length of a shortest path between s and v Dijkstra s algorithm computes the distances of all the vertices from a given start vertex s (single-source shortest paths) Assumptions: The graph is connected The edges are undirected The edge weights are nonnegative Extremely similar to Prim-Jarnik s MST Algorithm We grow a cloud of vertices, beginning with s and eventually covering all the vertices We store with each vertex v a label D v representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices The label D v is initialized to positive infinity At each step We add to the cloud the vertex u outside the cloud with the smallest distance label, D v We update the labels of the vertices adjacent to u, in a process called edge relaxation 3

EDGE RELAXATION Consider an edge e = u, z such that s D[u] = 50 u D[y] = 0 e D[z] = 5 z u is the vertex most recently added to the cloud y z is not in the cloud The relaxation of edge e updates distance D z as follows: D z min D z, D u + e. weight s D[u] = 50 u D[y] = 0 e D[z] = 60 z y 39

1. Pull in one of the vertices with red labels EXAMPLE B A C 1 3 9 E F 5 0. The relaxation of edges updates the labels of LARGER font size D B A C 1 5 3 9 E F 5 0 D 3 B 0 A C 1 5 11 3 9 E F 5 D 3 0 A B C 1 D 5 3 9 5 E F 3 0

EXAMPLE B 0 A 1 C D 3 5 3 9 5 E F B 0 A 1 C D 3 5 3 9 5 E F 1

EXERCISE DIJKSTRA S ALGORITHM Show how Dijkstra s algorithm works on the following graph, assuming you start with SFO, i.e., s =SFO. Show how the labels are updated in each iteration (a separate figure for each iteration). SFO LGA PVD HNL LAX MIA

DIJKSTRA S ALGORITHM A locator-based priority queue stores the vertices outside the cloud Key: distance Element: vertex We store with each vertex: distance D v label locator in priority queue Algorithm Dijkstras_sssp G, s Input: A simple undirected weighted graph G with nonnegative edge weights and a source vertex s Output: A label D v for each vertex v of G, such that D u is the length of the shorted path from s to v 1. D s 0; D v for each vertex v s. Let priority queue Q contain all the vertices of G using D v as the key 3. while Q. empty do //O n iterations. //pull a new vertex u in the cloud 5. u Q. removemin //O log n 6. for each edge e u. incidentedges( ) do //O deg u iterations. //relax edge e. z e. opposite u 9. if D u + e. weight < D[z] then 10. D z D u + e. weight 11. Q. updatekey z //O log n 3

ANALYSIS Graph operations Method incidentedges is called once for each vertex Label operations We set/get the distance and locator labels of vertex z O deg z times Setting/getting a label takes O 1 time Priority queue operations Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O log n time The key of a vertex in the priority queue is modified at most deg w times, where each key change takes O log n time Dijkstra s algorithm runs in O n + m log n time provided the graph is represented by the adjacency list structure Recall that Σ v deg v = m The running time can also be expressed as O m log n since the graph is connected The running time can be expressed as a function of n, O n log n

EXTENSION Using the template method pattern, we can extend Dijkstra s algorithm to return a tree of shortest paths from the start vertex to all other vertices We store with each vertex a third label: parent edge in the shortest path tree Parents are all initialized to null In the edge relaxation step, we update the parent label as well 5

WHY DIJKSTRA S ALGORITHM WORKS Dijkstra s algorithm is based on the greedy method. It adds vertices by increasing distance. Proof by contradiction Suppose it didn t find all shortest distances. Let F be the first wrong vertex the algorithm processed. When the previous node, D, on the true shortest path was considered, its distance was correct. But the edge D, F was relaxed at that time! Thus, so long as D F > D D, F s distance cannot be wrong. That is, there is no wrong vertex. 0 s 1 B C D 5 3 9 5 E F 3 6

WHY IT DOESN T WORK FOR NEGATIVE-WEIGHT EDGES Dijkstra s algorithm is based on the greedy method. It adds vertices by increasing distance. If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud. B A -3 C 3 9 5 E F B A 0 0-3 C 5 11 D C s true distance is 1, but it is already in the cloud with D C =! 3 9 5 E F D 0

BELLMAN-F ALGORITHM Works even with negative-weight edges Must assume directed edges (for otherwise we would have negative-weight cycles) Iteration i finds all shortest paths that use i edges. Running time: O nm Can be extended to detect a negative-weight cycle if it exists How? Algorithm BellmanFord G, s 1. Initialize D s 0 and D v for all vertices v s. for i 1 n 1 do 3. for each e G. edges do. //relax edge e 5. u e. source ; z e. target 6. if D u + e. weight < D z then. D z D u + e. weight

BELLMAN-F EXAMPLE 0-1 3 9-5 Nodes are labeled with their D[v] values First round 0-1 - 3 9-5 Second round 5 0-1 - -1 3 9-5 1 9 Third round 5 0-1 - -1 3 9-5 1 9

EXERCISE BELLMAN-F S ALGORITHM Show how Bellman-Ford s algorithm works on the following graph, assuming you start with the top node Show how the labels are updated in each iteration (a separate figure for each iteration). 0-1 3 9-5 5 50

ALL-PAIRS SHORTEST PATHS Find the distance between every pair of vertices in a weighted directed graph G We can make n calls to Dijkstra s algorithm (if no negative edges), which takes O nm log n time. Likewise, n calls to Bellman-Ford would take O n m time. We can achieve O n 3 time using dynamic programming (similar to the Floyd-Warshall algorithm). Uses only vertices numbered i,, k i Uses only vertices numbered i,, j (compute weight of this edge) k j Uses only vertices numbered k,, j Algorithm AllPairsShortestPath G Input: Graph G with vertices labeled 1,, n Output: Distances D i, j of shortest path lengths between each pair of vertices 1. for each vertex pair i, j do. if i = j then 3. D 0 i, i 0. else if e = i, j is an edge in G then 5. D 0 i, j e. weight 6. else. D 0 i, j. for k 1 n do 9. for i 1 n do 10. for j 1 n do 11. D k i, j min D k 1 i, j, D k 1 i, k + D k 1 k, j 1. return D n 51