Shortest Paths.

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
§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.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Data Structures Week 9 Towards Weighted BFS So, far we have measured d s (v) in terms of number of edges in the path from s to v. Equivalent to assuming.
Graph (II) Shortest path, Minimum spanning tree GGuy
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
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.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs – Part III CS 367 – Introduction to Data Structures.
Shortest Paths.
Network Flow.
Shortest Paths C B A E D F Shortest Paths
Graphs Representation, BFS, DFS
Shortest Paths and Minimum Spanning Trees
Dijkstra’s Algorithm SSSP, non-neg Edge weights = w(x,y)
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Lecture 13 Shortest Path.
CSC317 Shortest path algorithms
Shortest Paths C B A E D F Shortest Paths
Network Flow.
Minimum Spanning Trees and Shortest Paths
Cinda Heeren / Geoffrey Tien
Algorithms and Data Structures Lecture XIII
Shortest Paths C B A E D F Shortest Paths
Shortest Path Problems
Shortest Paths C B A E D F
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Shortest Paths.
Shortest paths & Weighted graphs
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
CSE 373: Data Structures and Algorithms
Graphs.
Chapter 13 Graph Algorithms
Algorithms and Data Structures Lecture XIII
Shortest Path Problems
Shortest Path Algorithms
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
Floyd’s Algorithm (shortest-path problem)
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Route Inspection Which of these can be drawn without taking your pencil off the paper and without going over the same line twice? If we introduce a vertex.
All pairs shortest path problem
Strongly Connected Components
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Algorithms: Design and Analysis
Presented by-Kapil Kumar Cse-iii rd year
Shortest Path Problems
Text Book: Introduction to algorithms By C L R S
Network Flow.
Shortest Paths.
Dijkstra’s Shortest Path Algorithm
CSE 373 Data Structures and Algorithms
CSE 417: Algorithms and Computational Complexity
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Network Flow.
Directed Graphs (Part II)
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Shortest Paths

Basic Categories Single source vs. all-pairs Weighted vs. unweighted Single Source Shortest Path: SSSP All-pairs Shortest Path: APSP Weighted vs. unweighted Can edges be negative? Can there be negative cycles? Often, modeling the graph is the biggest issue SSSP on Unweighted Graph Just use BFS

SSSP on Weighted Graph: Dijkstra’s Algorithm Basic idea: Priority Queue showing shortest vertex reachable so far (and possibly what vertex it is reachable from) Pull off the next shortest, add to found list For all edges from that vertex, relax the edge See if the edge creates a new shortest distance to the next vertex; if so, reduce the weight of that vertex Traditional: queue is a heap with a decrease_key option This is not supported in the STL Implementing with STL priority queue Reducing priority is not supported Instead, if the new edge is less than min so far, just enqueue a new vertex Creates duplicates, but can use STL implementation Must check each vertex when pulled off to see if it’s already been added (simple to do)

Negative Weights Negative weight non-cycle Can be solved using the variant of Dijkstra’s that keeps enqueuing data Each time a node is pulled off, check whether it’s at a shorter distance than previously found, if so, keep adding in Negative weight cycle: need to detect Bellman-Ford algorithm Also solves the SSSP problem, but less efficient unless there’s a negative cycle Relax every edge, V-1 times Start with dist[A] = 0 Relax all edges, in any order If at the end, any edge can still be relaxed, there’s a negative cycle So, check by trying each edge one last time

All Pairs Shortest Path: Floyd Warshall A dynamic programming approach to solving Dist(A,B,k) = Distance from A to B going through only nodes 1..k Dist(A,B,k) = 0 if A=B Dist(A,B,-1) = weight(A,B) for edge from A to B (and infinity if no such edge) Dist(A,B,k) = min of: Dist(A,B,k-1) Dist(A,k,k-1) + Dist(k,B,k-1) Super-easy to implement (4 lines of code if adjacency matrix is used!) Use on a graph with ~400 or fewer vertices – even if just SSSP!

Floyd-Warshall extensions Can modify Floyd-Warshall to compute other things (see book): Finding shortest path itself add a “parent” matrix that’s updated along the way Transitive closure (which vertices reachable from which others) Initialize matrix with 1 and 0 based on edges, use bitwise-or in Floyd-Warshall SCC Use transitive closure, then check whether i->j and j->i both true Diameter of graph Find the maximum distance between any pair Minimax/Maximin Check max( dist(A,k,k-1), dist(k,B,k-1) ) instead of sum Cheapest cycle Initialize dist(A, A) to very high value in initial adjacency matrix If, after algorithm is run, dist is less than that val, there is a cycle. Smallest value is the minimum cycle length (or IDs a negative cycle).

Graph Modeling (one example) Sometimes, need to understand how to model a graph Example: Cities (vertices) each with a price for fuel. Roads with lengths connecting cities. Fuel tank capacity. Best route from A to B SSSP on just city-road graph is not sufficient Create an extended graph (state space graph): Node is (City, Fuel Tank level) – 1 for each unit of fuel tank capacity Edges: 0 length from (X, Fuel_x) to (Y, Fuel_x – length(X,Y) ) Fuel cost length from (X, Fuel_x) to (X, Fuel_x+1) SSSP on this new graph will solve the problem Route is (A,0) to (B,0)