Lecture 7 Shortest Path Shortest-path problems

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
Single Source Shortest Paths
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Shortest Paths Definitions Single Source Algorithms
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Lecture 8 Shortest Path Shortest-path problems
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.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
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.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
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.
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.
Lecture 13 Algorithm Analysis
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Shortest Paths.
Dijkstra’s Algorithm SSSP, non-neg Edge weights = w(x,y)
CSC317 Shortest path algorithms
Algorithm Analysis Fall 2017 CS 4306/03
Algorithms and Data Structures Lecture XIII
Shortest Path Problems
Minimum Spanning Trees
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
Shortest Paths.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Floyd-Warshall Algorithm
Shortest Path Algorithms
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Shortest Path Problems
All pairs shortest path problem
The Bellman-Ford Shortest Path Algorithm Neil Tang 03/27/2008
Chapter 24: Single-Source Shortest Paths
Text Book: Introduction to algorithms By C L R S
Advanced Algorithms Analysis and Design
Shortest Path Problems
Shortest Paths.
Chapter 24: Single-Source Shortest Paths
Lecture 21: Matrix Operations and All-pair Shortest Paths
Negative-Weight edges:
CS 3013: DS & Algorithms Shortest Paths.
Chapter 24: Single-Source Shortest-Path
COSC 3101A - Design and Analysis of Algorithms 12
Advanced Algorithms Analysis and Design
Presentation transcript:

Lecture 7 Shortest Path Shortest-path problems Single-source shortest path All-pair shortest path

Overview Shortest-path problems Single-source shortest path algorithms Bellman-Ford algorithm Dijkstra algorithm All-Pair shortest path algorithms Floyd-Warshall algorithm

Weighted graph Beijing Qingdao Lhasa Shanghai Single-source shortest path All-pair shortest path Guangzhou

Shortest Path

Cycles. Can a shortest path contains cycles? Optimal substructure: Subpaths of shortest paths are shortest paths. Cycles. Can a shortest path contains cycles? Negative weights.

Where are we? Shortest-path problems Single-source shortest path algorithms Bellman-Ford algorithm Dijkstra algorithm All-Pair shortest path algorithms Floyd-Warshall algorithm

Relaxing The process of relaxing an edge (u,v) consists of testing whether we can improve the shortest path to v found so far by going through u.

Shortest-path properties Notation: We fix the source to be s. λ[v]: the length of path computed by our algorithms from s to v. δ[v]: the length of the shortest path from s to v. Triangle property δ[v] <= δ[u] + w(u,v) for any edge (u,v). Upper-bound property δ[v] <= λ[v] for any vertex v. Convergence property If s⇒u→v is a shortest path, and if λ[u] = δ[u], then after relax(u,v), we have λ[v] = δ[v].

Bellman-Ford algorithm E = {(t,x),(t,y),(t,z),(x,t),(y,x),(y,z),(z,x),(z,s),(s,t),(s,y)} Θ(mn)

Negative cycle

Quiz Run Bellman-Ford algorithm for the following graph, provided by E = {(S,A),(S,G),(A,E),(B,A),(B,C),(C,D),(D,E),(E,B),(F,A),(F,E),(G,F)}

Correctness Correctness of Bellman-Ford If G contains no negative-weight cycles reachable from s, then the algorithm returns TRUE, and for all v, λ[v] = δ[v], otherwise the algorithm returns FALSE. Proof. No negative cycle. By the fact that the length of simple paths is bounded by |V| - 1. After i-th iteration of relax, λ[vi] = δ[vi]. s v v1 vj vi Negative cycle. Assume vj,…, vi, vj. is a negative cycle and assume Bellman-Ford returns TRUE. λ[vk+1] <= λ[vk] + w(vk,vk+1) Sum all the inequalities together, we will get controdictions.

Relax in topological order. Observation 1 If there is no cycle (DAG), ... Relax in topological order. Θ(m) s v v1 vj vi

Weighted DAG application seam

If (s, vi) is the lightest edge sourcing from s, then λ[vi] = δ[vi] Observation 2 If there is no negative edge, ... vj vi v1 s If (s, vi) is the lightest edge sourcing from s, then λ[vi] = δ[vi]

Dijkstra algorithm PV primitives Structural programming Distributed algorithms Edsger Wybe Dijkstra in 2002

Dijkstra algorithm

Correctness Assume u is chosen in Step 7, and 1. λ[u] > δ[u] 2. s ⇒ x → y ⇒ u is the shortest path δ[u] = δ[x] + w(x,y) + w(p2) = λ[x] + w(x,y) + w(p2) ≥ λ[x] + w(x,y) ≥ λ[y] ≥ λ[u]

Time complexity Θ(n2)

What is the time complexity? How about use d-heap?

Comparisons Array Binary heap d-ary heap Dijkstra O(n2) O(mlogn) O(dnlogdn + mlogdn)

Dense graph dense: m = n1+ε, ε is not too small. d-heap, d = m/n complexity: O(dnlogdn + mlogdn) = O(m)

Where are we? Shortest-path problems Single-source shortest path algorithms Bellman-Ford algorithm Dijkstra algorithm All-Pair shortest path algorithms Floyd-Warshall algorithm

All-pairs shortest path Define $d_{i,j}^{k}$ to be the length of a shortest path from $i$ to $j$ that does not pass any vertex in $\{k+1,k+2,\ldots,n\}$. Clearly \[d_{ij}^{k} = \left\{\begin{array}{ll} l[i,j] & {\rm if}\ k=0 \\ min\{d_{i,j}^{k-1},d_{i,k}^{k-1}+d_{k,j}^{k-1}\} & {\rm if}\ 1\le k\le n \end{array}\right.\]

Floyd-Warshall algorithm \left[ \begin{array}{ccccc} 0 & 10 & \infty & 5 & \infty\\ \infty & 0 & 1 & 2 & \infty\\ \infty & \infty & 0 & \infty & 4\\ \infty & 3 & 9 & 0 & 2\\ 2 & \infty & 6 & \infty & 0 \end{array} \right]

Floyd-Warshall algorithm Θ(n3)

Quiz Run Floyd-Warshall algorithm for the following graph:

Conclusion Dijkstra’s algorithm. Nearly linear-time when weights are nonnegative. Acyclic edge-weighted digraphs. Faster than Dijkstra’s algorithm. Negative weights are no problem. Negative weights and negative cycles. If no negative cycles, can find shortest paths via Bellman-Ford. If negative cycles, can find one via Bellman-Ford. All-pair shortest path. can be solved via Floyd-Warshall Floyd-Warshall can also compute the transitive closure of directed graph.