Graph Algorithms: Shortest Path

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
CS138A Single Source Shortest Paths Peter Schröder.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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 Path Problems
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
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.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.
Dijkstra's algorithm.
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.
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
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.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
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)
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
Single-Source Shortest Paths
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Shortest Path Problems
Unweighted Shortest Path Neil Tang 3/11/2010
Minimum Spanning Tree Neil Tang 3/25/2010
SINGLE-SOURCE SHORTEST PATHS
Announcement 2: A 2 hour midterm (open book) will be given on March (Tuesday) during the lecture time. 2018/12/4.
Page 620 Single-Source Shortest Paths
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
2018/12/27 chapter25.
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Dijkstra’s Shortest Path Algorithm Neil Tang 03/25/2008
Shortest Path Problems
Shortest path algorithm
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Neil Tang 4/3/2008
Slide Courtesy: Uwash, UT
Single-source shortest paths
Lecture 13 Algorithm Analysis
All pairs shortest path problem
Fundamental Structures of Computer Science II
Dijkstra’s Shortest Path Algorithm Neil Tang 3/2/2010
CSE 373 Data Structures and Algorithms
Brad Karp UCL Computer Science
Slide Courtesy: Uwash, UT
CSE 417: Algorithms and Computational Complexity
CS 3013: DS & Algorithms Shortest Paths.
Chapter 9: Graphs Shortest Paths
Chapter 9 Graph algorithms
Minimum-Cost Spanning Tree
Presentation transcript:

Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E  R mapping edges to real valued weights. 1 5 10 1 8 3 2 4 3 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E  R mapping edges to real valued weights. 1 The weight of a path p = (v , v , . . ., v ) is  w(v , v ). 5 10 1 1 2 k k-1 8 3 2 4 3 i i+1 i=1 1 1 3 The weight of the path along the red edges is 1 + 6 + 1 + 4 = 12. 1 4 6 6 5 2

Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E  R mapping edges to real valued weights. 1 The weight of a path p = (v , v , . . ., v ) is  w(v , v ). 5 10 1 1 2 k k-1 8 3 2 4 3 i i+1 i=1 1 Single source shortest path problem: given a vertex s, for every vertex v  V find a shortest path from s to v. 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Single source shortest path problem: given vertex s, for every vertex v  V find a shortest path from s to v. 1 5 10 1 Dijkstra’s algorithm solves this problem efficiently for the case in which all weights are nonnegative (as in the example graph). 8 3 2 4 3 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. 1 It also maintains, for each vertex v not in S, an upper bound d[v] on the weight of a shortest path from source s to v. 5 10 1 8 3 2 4 3 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. 1 It also maintains, for each vertex v not in S, an upper bound d[v] on the weight of a shortest path from source s to v. 5 10 1 8 3 2 4 3 1 The algorithm repeatedly selects the vertex u  V – S with minimum bound d[v], inserts u into S, and relaxes all edges leaving u. 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 Set S = { } initially. 5 10 1 v 1 2 3 4 5 6 8 3 2 4 3 d      1 1 3 1 4 6 Also initialize a queue with all the vertices and their upper bounds. 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 S = { } 5 10 1 v 1 2 3 4 5 6 8 3 2 4 3 d      1 Select the vertex u  V – S with minimum bound d[v], insert u into S, . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 (here we keep the bound with the vertex in S) S = 5 10 1 v 2 3 4 5 6 8 3 2 4 3 d      1 . . . remove it from the queue, insert it in S,. . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 S = 5 10 1 v 2 3 4 5 6 8 3 2 4 3 10 1 5  d  1 . . . and relax the edges from this vertex. 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 S = 5 10 1 v 3 4 2 5 6 8 3 2 4 3 1 5 10  d  1 . . . reordering the queue according to the new upper bounds. 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 S = 5 10 1 v 3 4 2 5 6 8 3 2 4 3 1 5 10  d  1 Repeat . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 S = 5 1 10 1 v 4 2 5 6 8 3 2 4 3 5 10  d  1 . . . remove from queue and put in S 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 S = 5 1 10 1 v 4 2 5 6 8 3 2 4 3 4 9 2 d  1 . . . relax the edges . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 S = 5 1 10 1 v 5 4 2 6 8 3 2 4 3 d 2 4 9  1 . . . and reorder the queue . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 S = 5 1 10 1 v 5 4 2 6 8 3 2 4 3 d 2 4 9  1 Repeat . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 5 S = 5 1 2 10 1 v 4 2 6 8 3 2 4 3 4 9 d  1 . . . remove from queue and put in S 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 5 S = 5 1 2 10 1 v 4 2 6 8 3 2 4 3 4 9 d 4 1 . . . relax the edges . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 5 S = 5 1 2 10 1 v 4 6 2 8 3 2 4 3 d 4 4 9 1 . . . and reorder the queue . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 5 S = 5 1 2 10 1 v 4 6 2 8 3 2 4 3 d 4 4 9 1 Repeat . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 4 5 S = 5 1 4 2 10 1 v 6 2 8 3 2 4 3 d 4 9 1 . . . remove from queue and put in S 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 4 5 S = 5 1 4 2 10 1 v 6 2 8 3 2 4 3 d 4 9 1 . . . relax the edges (no change in this case) . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 4 5 S = 5 1 4 2 10 1 v 6 2 8 3 2 4 3 d 4 9 1 . . . and reorder the queue (no change in this case) . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 1 3 4 5 S = 5 1 4 2 10 1 v 6 2 8 3 2 4 3 d 4 9 1 Repeat . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 6 1 3 4 5 S = 5 1 4 2 4 10 1 v 2 8 3 2 4 3 d 9 1 . . . remove from queue and put in S 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 6 1 3 4 5 S = 5 1 4 2 4 10 1 v 2 8 3 2 4 3 d 9 1 . . . relax the edges (no change in this case) . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 6 1 3 4 5 S = 5 1 4 2 4 10 1 v 2 8 3 2 4 3 9 d 1 Repeat . . . 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s 1 6 1 2 3 4 5 S = 5 9 1 4 2 4 10 1 v 8 3 2 4 3 d 1 Done! 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. Suppose vertex 1 is the source. s v 1 6 1 2 3 4 5 S = 5 d 9 1 4 2 4 10 1 8 3 2 4 3 The result is the bottom row which contains the length of the shortest path from s to the vertex above it. 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path v 1 6 1 2 3 4 5 S = 5 d 9 1 4 2 4 10 1 p 3 1 3 3 5 8 3 2 4 3 To compute the corresponding paths, we augment the data structure with an additional attribute, p(v), which is the vertex that precedes v in a shortest path. 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path v 1 6 1 2 3 4 5 S = 5 d 9 1 4 2 4 10 1 p 3 1 3 3 5 8 3 2 4 3 Using the predecessor attribute, p(v), it’s easy to construct the path to v, working backwards from v to s. 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path v 1 6 1 2 3 4 5 S = 5 d 9 1 4 2 4 10 1 p 3 1 3 3 5 8 3 2 4 3 E.g., v = 6. 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path v 1 6 1 2 3 4 5 S = 5 d 9 1 4 2 4 10 1 p 3 1 3 3 5 8 3 2 4 3 E.g., v = 6. p(6) = 5, 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path v 1 6 1 2 3 4 5 S = 5 d 9 1 4 2 4 10 1 p 3 1 3 3 5 8 3 2 4 3 E.g., v = 6. p(6) = 5, p(5) = 3, 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path v 1 6 1 2 3 4 5 S = 5 d 9 1 4 2 4 10 1 p 3 1 3 3 5 8 3 2 4 3 E.g., v = 6. p(6) = 5, p(5) = 3, p(3) = 1. 1 1 3 1 4 6 6 5 2

Graph Algorithms: Shortest Path v 1 6 1 2 3 4 5 S = 5 d 9 1 4 2 4 10 1 p 3 1 3 3 5 8 3 2 4 3 E.g., v = 6. p(6) = 5, p(5) = 3, p(3) = 1. 1 1 3 1 4 6 6 5 Path: 1, 3, 5, 6. Weight: 4. 2

Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. Delete_min executed how many times?

Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. Delete_min executed |V| times.

Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. Delete_min executed |V| times. Decrease_key executed how many times?

Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. Delete_min executed |V| times. Decrease_key executed |E| times. Total time = |V| T + |E| T Delete_min Decrease_key

Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. Delete_min executed |V| times. Decrease_key executed |E| times. Total time = |V| T + |E| T Delete_min Decrease_key Priority queue T T Total time Array Binary heap Decrease_key Delete_min

Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. Delete_min executed |V| times. Decrease_key executed |E| times. Total time = |V| T + |E| T Delete_min Decrease_key Priority queue T T Total time Array O(|V|) O(1) Binary heap Decrease_key Delete_min

Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. Delete_min executed |V| times. Decrease_key executed |E| times. Total time = |V| T + |E| T Delete_min Decrease_key Priority queue T T Total time Array O(|V|) O(1) O(|V| ) Binary heap O(log |V|) O(log |V|) Decrease_key Delete_min 2

Graph Algorithms: Shortest Path Computing time analysis: look at different implementations of the priority queue, with different costs for the queue operations. Delete_min executed |V| times. Decrease_key executed |E| times. Total time = |V| T + |E| T Delete_min Decrease_key Priority queue T T Total time Unsorted Array O(|V|) O(1) O(|V| ) Binary heap O(log |V|) O(log |V|) O(E log |V|) Decrease_key Delete_min 2