Single-Source Shortest Path

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

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.
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
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,
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
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
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
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.
CS223 Advanced Data Structures and Algorithms 1 The Bellman-Ford Shortest Path Algorithm Neil Tang 03/11/2010.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
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.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Data Structure Lecture 10 Thursday, 28 Aug 2005.
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
ליאור שפירא, חיים קפלן וחברים
Graph Algorithms BFS, DFS, Dijkstra’s.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
CISC 235: Topic 10 Graph Algorithms.
Shortest Path Problems
Minimum Spanning Trees
SINGLE-SOURCE SHORTEST PATHS
Algorithms (2IL15) – Lecture 5 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
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
2018/12/27 chapter25.
Advanced Algorithms Analysis and Design
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Problems
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
The Bellman-Ford Shortest Path Algorithm Neil Tang 03/27/2008
Evaluation of the Course (Modified)
Algorithms Searching in a Graph.
Chapter 24: Single-Source Shortest Paths
Advanced Algorithms Analysis and Design
Shortest Paths.
Brad Karp UCL Computer Science
Shortest Path Problems
Graph Algorithms: Shortest Path
Chapter 24: Single-Source Shortest Paths
Negative-Weight edges:
Bellman Ford.
CS 3013: DS & Algorithms Shortest Paths.
Chapter 24: Single-Source Shortest-Path
Single-Source Shortest Path & Minimum Spanning Trees
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Topological Sorting Minimum Spanning Trees Shortest Path
Advanced Algorithms Analysis and Design
Presentation transcript:

Single-Source Shortest Path Jeff Chastine

How does Google Maps work? Jeff Chastine

Single-Source Shortest Path How many ways can I go from SPSU to KSU? How can I represent the map? What are vertices? What are edges? What are weights? Jeff Chastine

Single-Source Shortest Path For this problem Disallow cycles Still have weight function 𝑤:𝐸→𝑅 Calculate path p and shortest path 𝜕 where Note: Breadth works for non-weighted edges 𝑤 𝑝 = 𝑖=1 𝑘 𝑤( 𝑣 𝑖−1 , 𝑣 𝑖 ) 𝜕 𝑢,𝑣 = min 𝑤 𝑝 :𝑢 𝑝 𝑣 𝑖𝑓 ∃ 𝑝 ∞ Jeff Chastine

Single-Source Shortest Path Has optimal sub-structure. Why? Path is 𝑢 𝑝 𝑢𝑤 𝑥 𝑝 𝑥𝑤 𝑤 𝑝 𝑤𝑣 𝑣 is fastest Assume faster way between x and w Cut and paste faster way and you get optimal! Graph can’t have a negative cycle. Why? Can the graph have a positive cycle? Can the shortest path contain a positive cycle? Jeff Chastine

How it’s Done in General A node v has a predecessor node 𝜋(𝑣) Tells us how we got there NIL for first node End with a directed graph rooted at start node s Relaxation (pay attention!) Maintain an upper-bound cost for each node d[v] Initially, all nodes are marked as ∞ If we find a cheaper path to node v, relax (update) the cost 𝑑[𝑣] and predecessor 𝜋(𝑣) Jeff Chastine

Relaxation Example (using a breadth-first traversal) ∞ 2 5 a 11 d ∞ 99 3 c ∞ Original Graph Predecessor and Cost INITIALIZE-SINGLE-SOURCE(G, s) Jeff Chastine

Start with source node 𝑎 Relaxation Example b ∞ 2 5 a 11 d ∞ 99 3 c ∞ Original Graph Predecessor and Cost Start with source node 𝑎 Jeff Chastine

Update connected nodes Relaxation Example b 2 2 5 a 11 d ∞ 99 3 c 99 Original Graph Predecessor and Cost Update connected nodes Jeff Chastine

Relaxation Example b 2 a d ∞ c 99 Continue with 𝑏 Original Graph 5 a 11 d ∞ 99 3 c 99 Original Graph Predecessor and Cost Continue with 𝑏 Jeff Chastine

Relaxation Example b 2 a d 7 c 99 Original Graph Predecessor and Cost 5 a 11 d 7 99 3 c 99 Original Graph Predecessor and Cost Jeff Chastine

Relaxation Example b 2 a d 7 c 99 This guy can chill out… RELAX! 5 a 11 d 7 99 3 c 99 Original Graph Predecessor and Cost This guy can chill out… RELAX! Jeff Chastine

Relaxation Example b 2 a d 7 c 13 Original Graph Predecessor and Cost 5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost Jeff Chastine

Relaxation Example But wait! We’re not done! b 2 a d 7 c 13 5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost But wait! We’re not done! Jeff Chastine

Relaxation Example No relaxation b 2 a d 7 c 13 Original Graph 5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost No relaxation Jeff Chastine

Relaxation Example b 2 a d 7 c 13 Original Graph Predecessor and Cost 5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost Jeff Chastine

Relaxation Example Needs to relax again! b 2 a d 7 c 13 Original Graph 5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost Needs to relax again! Jeff Chastine

Relaxation Example Needs to relax again! b 2 a d 7 c 10 Original Graph 5 a 11 d 7 99 3 c 10 Original Graph Predecessor and Cost Needs to relax again! Jeff Chastine

Final Predecessor and Cost Relaxation Example b 2 2 5 a 11 d 7 99 3 c 10 Original Graph Predecessor and Cost Final Predecessor and Cost Jeff Chastine

How does Google Maps work? PWND! Jeff Chastine

Bellman-Ford Algorithm Works with negatively weighted edges Detects if negative cycle exists Consistently uses relaxation Runs in Θ(𝑉𝐸) Jeff Chastine

Bellman-Ford Algorithm BELLMAN-FORD(G, w, s) INITIALIZE-SINGLE-SOURCE(G, s) for i ← 1 to |V[G]| -1 foreach edge (u, v) ∈ E[G] RELAX(u, v, w) if d[v] > d[u] + w(u, v) then return FALSE return TRUE Jeff Chastine

Dijkstra’s Algorithm Is more efficient than Bellman-Ford Doesn’t work with negative edges Has a set S of vertices that it has already traversed. Heapifies V. In general Picks vertex u from V - S with minimum estimate Relaxes everything connected to u Adds u to S Repeats until V - S is the empty set Jeff Chastine

Dijkstra’s Algorithm DIJKSTRA (G, w, s) INITIALIZE-SINGLE-SOURCE(G, s) Q ← V[G] while Q ≠∅ do u ← EXTRACT-MIN(Q) S←S ∪ {u} foreach vertex v ∈ Adj[u] do RELAX(u, v, w) Jeff Chastine

Summary Both algorithms use Bellman-Ford (𝑂(𝐸 𝑉)) Dijkstra (𝑂(𝐸 𝑙𝑔𝑉)) A predecessor graph Costs to each node Relaxation Bellman-Ford (𝑂(𝐸 𝑉)) Works with negative weights Detects negative cycles Dijkstra (𝑂(𝐸 𝑙𝑔𝑉)) More efficient Doesn’t work with negative weights Jeff Chastine