Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
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.
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 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.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
D IJKSTRA ' S ALGORITHM By Laksman Veeravagu and Luis Barrera.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
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.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Dijkstra's algorithm CS3240. The author: Edsger Wybe Dijkstra "Computer Science is no more about computers than astronomy is about telescopes."
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
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 in Weighted Graph : Dijkstra’s Algorithm.
D IJKSTRA ' S ALGORITHM. S INGLE -S OURCE S HORTEST P ATH P ROBLEM Single-Source Shortest Path Problem - The problem of finding shortest paths from a.
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)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Lecture 13 Algorithm Analysis
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
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,
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
By Laksman Veeravagu and Luis Barrera
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Minimum Spanning Tree Neil Tang 3/25/2010
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Analysis and design of algorithm
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Neil Tang 4/3/2008
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
Dijkstra’s Shortest Path Algorithm Neil Tang 3/2/2010
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Directed Graphs (Part II)
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1

Single-Source Shortest Path Problem Single-Source Shortest Path Problem - The problem of finding shortest paths from a source vertex v to all other vertices in the graph.

Applications - Maps (Map Quest, Google Maps) - Routing Systems

Dijkstra's algorithm Dijkstra's algorithm - is a solution to the single-source shortest path problem in graph theory. Works on both directed and undirected graphs. However, all edges must have nonnegative weights. Input: Weighted graph G={E,V} and source vertex v ∈ V, such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v ∈ V to all other vertices

Approach The algorithm computes for each vertex u the distance to u from the start vertex v, that is, the weight of a shortest path between v and u. the algorithm keeps track of the set of vertices for which the distance has been computed, called the cloud C Every vertex has a label D associated with it. For any vertex u, D[u] stores an approximation of the distance between v and u. The algorithm will update a D[u] value when it finds a shorter path from v to u. When a vertex u is added to the cloud, its label D[u] is equal to the actual (final) distance between the starting vertex v and vertex u. 5

Dijkstra pseudocode Dijkstra(v1, v2): for each vertex v: // Initialization v's distance := infinity. v's previous := none. v1's distance := 0. List := {all vertices}. while List is not empty: v := remove List vertex with minimum distance. mark v as known. for each unknown neighbor n of v: dist := v's distance + edge (v, n)'s weight. if dist is smaller than n's distance: n's distance := dist. n's previous := v. reconstruct path from v2 back to v1, following previous pointers. 6

7 Example: Initialization A GF B ECD ∞ ∞ ∞ ∞ Pick vertex in List with minimum distance. ∞∞ Distance(source) = 0 Distance (all vertices but source) = ∞

8 Example: Update neighbors' distance A GF B ECD ∞ ∞ 1 ∞∞ Distance(B) = 2 Distance(D) = 1

9 Example: Remove vertex with minimum distance Pick vertex in List with minimum distance, i.e., D A GF B EC D ∞ ∞ 1 ∞∞

Example: Update neighbors A GF B EC D Distance(C) = = 3 Distance(E) = = 3 Distance(F) = = 9 Distance(G) = = 5

11 Example: Continued... A GF B ECD Pick vertex in List with minimum distance (B) and update neighbors 95 Note : distance(D) not updated since D is already known and distance(E) not updated since it is larger than previously computed

12 Example: Continued... A GF B E CD No updating Pick vertex List with minimum distance (E) and update neighbors

13 Example: Continued... A GF B E C D Pick vertex List with minimum distance (C) and update neighbors Distance(F) = = 8

14 Example: Continued... A G F B ECD Distance(F) = min (8, 5+1) = 6 Previous distance Pick vertex List with minimum distance (G) and update neighbors

15 Example (end) A G F B ECD Pick vertex not in S with lowest cost (F) and update neighbors 65

Another Example

THE KNOWN CLOUD v P: Next shortest path from inside the known cloud v' Correctness :“Cloudy” Proof If the path to v is the next shortest path, the path to v' must be at least as long. Therefore, any path through v' to v cannot be shorter! Source Least cost node competitor When a vertex is added to the cloud, it has shortest distance to source.

26 Dijkstra’s Correctness We will prove that whenever u is added to S, d[u] =  (s,u), i.e., that d[u] is minimum, and that equality is maintained thereafter Proof – Note that  v, d[v] ≥   (s,v) – Let u be the first vertex picked such that there is a shorter path than d[u], i.e., that d[u]   (s,u) – We will show that this assumption leads to a contradiction

27 Dijkstra Correctness (2) Let y be the first vertex  V – S on the actual shortest path from s to u, then it must be that d[y] =  (s,y) because – d[x] is set correctly for y's predecessor x  S on the shortest path (by choice of u as the first vertex for which d is set incorrectly) – when the algorithm inserted x into S, it relaxed the edge (x,y), assigning d[y] the correct value

28 But if d[u] > d[y], the algorithm would have chosen y (from the Q) to process next, not u  Contradiction Thus d[u] =  (s,u) at time of insertion of u into S, and Dijkstra's algorithm is correct Dijkstra Correctness (3)

29 Dijkstra’s Pseudo Code Graph G, weight function w, root s relaxing edges

Time Complexity: Using List The simplest implementation of the Dijkstra's algorithm stores vertices in an ordinary linked list or array – Good for dense graphs (many edges) |V| vertices and |E| edges Initialization O(|V|) While loop O(|V|) – Find and remove min distance vertices O(|V|) Potentially |E| updates Update costs O(1) Total time O(|V 2 | + |E|) = O(|V 2 | )

31 Time Complexity: Priority Queue For sparse graphs, (i.e. graphs with much less than |V 2 | edges) Dijkstra's implemented more efficiently by priority queue Initialization O(|V|) using O(|V|) buildHeap While loop O(|V|) Find and remove min distance vertices O(log |V|) using O(log |V|) deleteMin Potentially |E| updates Update costs O(log |V|) using decreaseKey Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|) |V| = O(|E|) assuming a connected graph