Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
Shortest Paths Definitions Single Source Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
All-Pairs Shortest Paths
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
CS 146: Data Structures and Algorithms July 21 Class Meeting
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
Lecture 13 Algorithm Analysis
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
All pairs shortest path problem
Fundamental Structures of Computer Science II
CE 221 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Graph Algorithms: Shortest Path
CE 221 Data Structures and Algorithms
Presentation transcript:

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path

Cardinality of a Set “The number of elements in a set” Let A be a finite set  If A =  (the empty set), then the cardinality of A is 0  If A has exactly n elements, n a natural number, then the cardinality of A is n The cardinality of a set A is denoted by |A|

Notation in Graph A graph G = (V, E) consists of a set of vertices V and a set of edges E Example  V = {A, B, C, D, E, F, G}  E = {(A, B), (A, D), (B, C), (C, D), (C, G), (D, E), (D, F), (E, F)} Cardinality of Vertex Set denoted by |V|  |V| = 7 = number of vertices in set V Cardinality of Edge Set denoted by |E|  |E| = 8 = number of edges in set E

Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost Numerous applications  Map navigation  Flight itineraries  Circuit wiring  Network routing

Shortest Path Problems Input is a weighted graph where each edge (v i, v j ) has cost c i, j to traverse the edge Cost of a path v 1 v 2 …v N is  Weighted path cost Unweighted path length is N – 1, number of edges on path

Shortest-Path Problems (cont’d) Single-source shortest path problem  Given a weighted graph G = (V, E), and a distinguished start vertex, s, find the minimum weighted path from s to every other vertex in G  The shortest weighted path from v 1 to v 6 has a cost of 6 and v 1 v 4 v 7 v 6

Negative Weights Graphs can have negative weights E.g., arbitrage  Shortest positive-weight path is a net gain  Path may include individual losses Problem: Negative weight cycles  Allow arbitrarily-low path costs  Shortest path cost from v 5 to v 4 = 1 ? v 5 v 4 v 2 v 5 v 4 = - 5, still not shortest  Shortest path from v 1 to v 6 undefined negative-cost cycle Solution  Detect presence of negative-weight cycles

Shortest-Path Problems (cont’d) Unweighted shortest-path problem: O(|E| + |V|) Weighted shortest-path problem  No negative edges: O(|E| log |V|)  Negative edges: O(|E| x |V|)  poor time bound Acyclic graphs: O(|E| + |V|) in linear time No asymptotically faster algorithm for single- source/single-destination shortest path problem  No algorithms find the path from s to one vertex (one-to- one) any faster than finding the path from s to all vertices (one-to-many)

Unweighted Shortest Paths Problem: Find the shortest path from some vertex s to all other vertices  Input: s, the source/starting vertex  Output: minimum # of edges contained on the path  No weights on edges Find shortest length paths  Same as weighted shortest path with all weights equal  Start vertex is s = v 3  Shortest path from s to v 3 is 0 Breadth-First Search (BFS)  Process vertices in layers Closest to the start are evaluated first Then most distant vertices

Unweighted Shortest Paths (cont’d) For each vertex, keep track of  Whether we have visited it (known)  Its distance from the start vertex (d v )  Its predecessor vertex along the shortest path from the start vertex (p v )

Unweighted Shortest Paths (cont’d) Solution 1: Repeatedly iterate through vertices, looking for unvisited vertices at current distance from start vertex s Running time: O(|V| 2 )

Unweighted Shortest Paths (cont’d) Solution 2: Ignore vertices that have already been visited by keeping only unvisited vertices (distance = ∞) on the queue Running time: O(|E|+|V|) with adjacency lists Two groups of vertices based on currDist and currDist+1 known data member is not used

Unweighted Shortest Paths (cont’d)

Weighted Shortest Paths Dijkstra’s algorithm  Proceeds in stages just like the unweighted shortest- path algorithm  Select a vertex v, which has the smallest d v among all the unknown vertices and declares the shortest path from s to v is known  Use priority queue to store unvisited vertices by distance from s  After deleteMin v, update distance of remaining vertices adjacent to v using decreaseKey  Does not work with negative weights

Dijkstra’s Algorithm

Dijkstra’s Algorithm Implementation Priority queue such as binary heap Selection of a vertex v is deleteMin operation  Once unknown minimum vertex is found it is no longer unknown  Must be removed from future consideration Update of w’s distance (adjacent to v )  decreaseKey operation

BuildHeap: O(|V|) DeleteMin: O(|V| log |V|) DecreaseKey: O(|E| log |V|) Total running time: O(|E| log |V|) In unweighted case we set d w = d v + 1 if d w = infinity Here we lower the value of d w if vertex v offered a shorter path d w = d v + c v,w if the new value d w is an improvement

Dijkstra’s Adjacency List

Dijkstra’s Algorithm

Why Dijkstra Works Dijkstra’s algorithm is known as greedy algorithm  Solves a problem in stages by doing what appears to be the best thing at each stage Prove that it works: Hypothesis  A least-cost path from X to Y contains least-cost paths from X to every city on the path  E.g., if X  C1  C2  C3  Y is the least-cost path from X to Y, then X  C1  C2  C3 is the least-cost path from X to C3 X  C1  C2 is the least-cost path from X to C2 X  C1 is the least-cost path from X to C1 A DC B

Why Dijkstra Works Assume hypothesis is false  i.e., Given a least-cost path P from X to Y that goes through C, there is a better path P’ from X to C than the one in P Show a contradiction  But we could replace the subpath from X to C in P with this lesser- cost path P’  The path cost from C to Y is the same  Thus we now have a better path from X to Y  But this violates the assumption that P is the least-cost path from X to Y Therefore, the original hypothesis must be true X C Y P’

Printing Shortest Paths

Negative Edge Costs but No Cycles Running time: O(|E|·|V|) Negative weight cycles? Dijkstra’s algorithm does not work Vertex u is known but there may be a path from unknown vertex v back to u that is very negative Add a constant value to each edge cost? Solve this with the combination of unweighted and weighted algorithms // a bit can be set for each vertex to indicate presence in the queue Does not work for above graph, as it has negative-cost cycles

Shortest Path Algorithms Important graph problem with numerous applications Unweighted graph: O(|E| + |V|) Weighted graph  Dijkstra: O(|E| log |V|)  Negative weights: O(|E| x |V|) All-pairs shortest paths  Dijkstra: O(|V| x |E| log |V|) = O(|V| 3 log |V|)  Floyd-Warshall: O(|V| 3 )