111111 The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.

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.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
© DEEDS 2008Graph Algorithms1 Remarks on Dijkstra  Determining the minimum marginal element (code line: v = the vertex in M 2 with minimum v.L; ) accounts.
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.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Introduction to Algorithms 6.046J/18.401J/SMA5503
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.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
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.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
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.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
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.
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.
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.
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.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
Lecture 13 Algorithm Analysis
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
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.
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.
Graphs – Breadth First Search
Cinda Heeren / Geoffrey Tien
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
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 .
Presentation transcript:

The Shortest Path Problem

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 22222

Shortest Path Problems Weighted graphs: 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 Goal: to find a smallest cost path Unweighted graphs: Input is an unweighted graph  i.e., all edges are of equal weight Goal: to find a path with smallest number of hops 3

Shortest Path Problems Single-source shortest path problem Given a weighted graph G=(V,E), and a source vertex s, find the minimum weighted path from s to every other vertex in G 4 s: source Weighted: Dijkstra’s algo Unweighted: Simple BFS Some algorithms:

Point to Point SP problem Given G(V,E) and two vertices A and B, find a shortest path from A (source) to B (destination). Solution: 1) Run the code for Single Source Shortest Path using source as A. 2) Stop algorithm when B is reached.

All Pairs Shortest Path Problem Given G(V,E), find a shortest path between all pairs of vertices. Solutions: (brute-force) Solve Single Source Shortest Path for each vertex as source There are more efficient ways of solving this problem (e.g., Floyd-Warshall algo).

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 Solution Detect presence of negative-weight cycles 7

Unweighted Shortest Paths No weights on edges Find shortest length paths Same as weighted shortest path with all weights equal Breadth-first search 8 source O(|E| + |V|)

Unweighted Shortest Paths 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 ) 9

Unweighted Shortest Paths 10 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 11 Solution: Ignore vertices that have already been visited by keeping only unvisited vertices (distance = ∞ ) on the queue. Running time: O(|E|+|V|) source Queue: v3v3 v 1, v 6 v 2, v 4 v 7, v 5

Unweighted Shortest Paths 12

Weighted Shortest Paths Dijkstra’s algorithm GREEDY strategy: Always pick the next closest vertex to the source Use priority queue to store unvisited vertices by distance from s After deleteMin v, update distances of remaining vertices adjacent to v using decreaseKey Does not work with negative weights 13

Dijkstra’s Algorithm 14

15 Dijkstra source

16 BuildHeap: O(|V|) DeleteMin: O(|V| log |V|) DecreaseKey: O(|E| log |V|) Total running time: O(|E| log |V|)

Why Dijkstra Works Hypothesis A least-cost path from X to Y contains least-cost paths from X to every city on the path to Y 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 17 A DC B This is called the “Optimal Substructure” property

Why Dijkstra Works PROOF BY CONTRADICTION: 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 18 X C Y P’

Printing Shortest Paths 19

What about graphs with negative edges? Will the O(|E| log|V|) Dijkstra’s algorithm work as is? -10 v1v1 v4v4 v2v2 v3v source deleteMinUpdates to dist V1V1 V 2.dist = 3 V2V2 V 4.dist = 4, V 3.dist = 5 V4V4 No change V3V3 No change and so v 4. dist will remain 4. Correct answer: v 4.dist should be updated to -5 Solution: Do not mark any vertex as “known”. Instead allow multiple updates.

Negative Edge Costs 21 Running time: O(|E|·|V|) v1v1 v4v4 v2v2 v3v source No updatesV4V4 V4V4 V3V3 V 4, V 3 V2V2 V1V1 Queue V 4.dist = -5V3V3 V 4.dist = 4, V 3.dist = 5V2V2 No updatesV4V4 V 2.dist = 3V1V1 Updates to distDequeue -10

Negative Edge Costs 22 Running time: O(|E|·|V|) Negative weight cycles?

Shortest Path Problems Unweighted shortest-path problem: O(|E|+|V|) Weighted shortest-path problem No negative edges: O(|E| log |V|) Negative edges: O(|E|∙|V|) Acyclic graphs: O(|E|+|V|) No asymptotically faster algorithm for single- source/single-destination shortest path problem 23

Course Evaluation Site in now Open! 92ad-cbc26bde3b53.srv 92ad-cbc26bde3b53.srv