CSC317 Shortest path algorithms

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

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.
* 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.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
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.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source 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.
Shortest Paths Definitions Single Source Algorithms
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.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Introduction to Algorithms Jiafen Liu Sept
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
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.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Graphs – Breadth First Search
Shortest Paths.
Chapter 22 Elementary Graph Algorithms
A vertex u is reachable from vertex v iff there is a path from v to u.
Shortest Paths and Minimum Spanning Trees
Algorithms and Data Structures Lecture XIII
Introduction to Graphs
Graph Search Algorithms
Shortest Path Problems
Shortest Path Problems
Shortest Path Graph represents highway system Edges have weights
Minimum Spanning Trees
Short paths and spanning trees
Graph Algorithm.
SINGLE-SOURCE SHORTEST PATHS
Shortest Paths.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Analysis and design of algorithm
Page 620 Single-Source Shortest Paths
Graphs.
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Problems
Floyd-Warshall Algorithm
Shortest Path Algorithms
Shortest Paths and Minimum Spanning Trees
Lecture 13 Algorithm Analysis
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Algorithms: Design and Analysis
Algorithms Searching in a Graph.
Chapter 24: Single-Source Shortest Paths
Shortest Path Problems
Text Book: Introduction to algorithms By C L R S
Advanced Algorithms Analysis and Design
Shortest Paths.
Shortest Path Problems
Shortest Paths.
Graph Theory Dijkstra's Algorithm.
Chapter 24: Single-Source Shortest Paths
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Directed Graphs (Part II)
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:

CSC317 Shortest path algorithms So far we only looked at unweighted graphs. But what if we need to account for weights (and on top of it negative weights)? Definition of a shortest path problem: We are given a weighted graph G = (V,E) with a weight function mapping edges to real-valued values. The weight w(p) of path is the sum of the weights of its constituent weights We define the shortest path weight d(u,v) from u to v by if there exists a path from u to v otherwise A shortest path from u to v is then defined as any path with weight w(p) = d(u,v) CSC317

Variants: Single destination shortest path problem: Shortest paths to a given destination vertex t from each vertex v (what happens when we reverse edges?) Single pair shortest path problem: Shortest path from u to v for given u, v All pairs shortest path problem: Shortest path from u to v for all u, v Optimal substructure of shortest paths Shortest-paths algorithms typically rely on the property that a shortest path between two vertices contains other shortest paths within it (does that sound familiar?). Optimal substructure is one key element of dynamic programming and greedy algorithms. Dijkstra algorithm is a greedy algorithm Floyd-Warshall algorithm is a dynamic programming algorithm CSC317

CSC317 Negative weights: (what is the problem?) Dijkstra’s algorithm only works on positive weights Floyd-Warshall allows negative weights CSC317

CSC317 Can a shortest path contain a cycle? As we have just seen, it cannot contain a negative-weight cycle. Nor can it contain a positive-weight cycle, since removing the cycle from the path produces a path with the same source and destination vertices and a lower path weight. If is a path and is a positive weight cycle so that then vi = vj and w(c) > p then the path has weight w(p‘) = w(p) – w(c) < w(p) and so p cannot be a shortest path. That leaves only 0-weight cycles. We can remove a 0-weight cycle from any path to produce another path whose weight is the same. Thus, if there is a shortest path from a source vertex s to a destination vertex v that contains a 0-weight cycle, then there is another shortest path from s to v without this cycle. CSC317

CSC317 Dijkstra’s shortest path algorithm This algorithm finds a single source shortest path on a directed graph, with weights for the edges (for instance, weights could be driving distances between locations). This is a greedy algorithm, and the weights must be non negative (otherwise, for instance if we want to represent positive profit and negative loss, we can use a “cousin” algorithm, Bellman Ford, which we do not discuss here, and uses dynamic programming). Remember that Breadth First Search (BFS) also finds a shortest path, but in the case of unweighted edges. Dijkstra can be seen as a generalization of BFS. CSC317

The main idea is that we maintain a set of vertices S whose final shortest path lengths have already been determined. In each time we consider not yet discovered vertices in the graph, and all edges going from a discovered vertex (u) to an undiscovered vertex (v). We choose an undiscovered vertex with an edge from u to v, that gives the shortest path length. The length from u to v for each vertex v, is given by the length of u, plus the weight between u and v. In the initialization step, we just include source node s in the set of discovered nodes, and set its length to 0. All other lengths are initially infinity. Then we keep expanding set S of discovered nodes in a greedy manner. CSC317

CSC317

CSC317

In the figure, the black vertices at each step are those vertices added to set S. Initially, only s is in set S. s can go to t (length 10) or y (length 5), and y yields the shortest path. Now both s and y are in set S. We consider all possibilities from set S (vertices s and y) to other vertices. We already have the s to t (length 10) stored and we also look at y to t (5 + 3 = 8) y to z (5 + 2 = 7) and y to x (5 + 9) – 14. The shortest greedy choice is y to z (length 7), so now z is added to our set of discovered nodes S. And so on see figure below. CSC317

CSC317 Run time: We run through each node once. For each node we look into it’s adjacency list. If there are n nodes and m edges, we have the usual (n+m) number of operations. However, each operation takes time since we need to find the minimum among all possible edges. This can be done by extracting the minimum from a queue with each minimum taking O(lg n) time. As a consequence we have O((n+m)lg n) complexity. CSC317

CSC317 Bellman-Ford algorithm Given a weighted, directed graph G = (V, E) with source s and weight function the Bellman-Ford algorithm returns a boolean value indicating whether or not there is a negative-weight cycle that is reachable from the source. If there is such a cycle, the algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortest paths and their weights. CSC317

CSC317

Run time: O(EV) Θ(V) Θ(EV) O(E) CSC317