Dijkstra's algorithm CS3240. The author: Edsger Wybe Dijkstra "Computer Science is no more about computers than astronomy is about telescopes."

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
D IJKSTRA ' S ALGORITHM By Laksman Veeravagu and Luis Barrera Edited by: Manuela Caicedo, Francisco Morales, Rafael Feliciano, and Carlos Jimenez.
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.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
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)
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
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
D IJKSTRA ' S ALGORITHM By Laksman Veeravagu and Luis Barrera.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
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.
Introduction to Algorithms Jiafen Liu Sept
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.
Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Weighted Graphs Computing 2 COMP s1 Sedgewick Part 5: Chapter
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
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.
Lecture 13 Algorithm Analysis
Minimum Spanning Trees Text Read Weiss, §9.5 Prim’s Algorithm Weiss §9.5.1 Similar to Dijkstra’s Algorithm Kruskal’s Algorithm Weiss §9.5.2 Focuses on.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Graphs – Part III CS 367 – Introduction to Data Structures.
Shortest Path Dr. Yasir. Weighted Graphs In many applications, each edge of a graph has an associated numerical value, called a weight. Usually, the edge.
By Laksman Veeravagu and Luis Barrera
COMP108 Algorithmic Foundations Greedy methods
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Graphs Chapter 13.
Minimum Spanning Trees
Analysis and design of algorithm
CSE 373: Data Structures and Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Brad Karp UCL Computer Science
Slide Courtesy: Uwash, UT
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Graphs: Shortest path and mst
Presentation transcript:

Dijkstra's algorithm CS3240

The author: Edsger Wybe Dijkstra "Computer Science is no more about computers than astronomy is about telescopes."

Edsger Wybe Dijkstra - May 11, 1930 – August 6, Received the 1972 A. M. Turing Award, widely considered the most prestigious award in computer science. - The Schlumberger Centennial Chair of Computer Sciences at The University of Texas at Austin from 1984 until Made a strong case against use of the GOTO statement in programming languages and helped lead to its deprecation. - Known for his many essays on programming.

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. s w w"w' w" x x' xz z' w' Find shortest paths from source s to all other destinations

Applications of Dijkstra's Algorithm - Traffic Information Systems are most prominent use - Mapping (Map Quest, Google Maps) - Routing Systems

Applications of Dijkstra's Algorithm Another example: epidemiology You can use networks to model the spread of infectious diseases and design prevention and response strategies. Vertices represent individuals, and edges their possible contacts. It is useful to calculate how a particular individual is connected to others. Knowing the shortest path lengths to other individuals can be a relevant indicator of the potential of a particular individual to infect others.

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. Approach: Greedy (means at each point go for the best) 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

Dijkstra's algorithm - Pseudocode //STEP 1 INITIALIZATION dist[s] ← 0 (distance to source vertex is zero) path[s] = (); (path from s to s is nothing) for all v ∈ V–{s} { dist[v] ← ∞ (set all other distances to infinity) path[v] = null } (set path to v to empty) S ← ∅ (S, the set of visited vertices is initially empty) Q ← V (Q, the queue initially contains all vertices) while Q not Empty (while the queue is not empty) { //STEP 2: GET NOT VISITED VERTEX WITH MIN DISTANCE u ← mindistance(Q,dist)(select the element of Q with the min. distance) S ← S ∪ {u} (add u to list of visited vertices) for all v ∈ neighbors[u] //SETP 3: VISIT ALL NEIGHBORS UPDATE DISTANCE if dist[v] > dist[u] + w(u, v) (if new shortest path found to v) { d[v] = d[u] + w(u, v) (set new value of shortest path) path[v] = path[u] + Edge(u, v) } } return dist and path

Dijkstra Animated Example

path(A) = (); path(B) = Edge(A,B); path(C)=Edge(A,C)

Dijkstra Animated Example MINIMUM: take C

Dijkstra Animated Example path(B) = Edge(A,C) + Edge(C,B); path(D) = Edge(A,C) + Edge(C,D); path(E) = Edge(A,C) + Edge(C,E);

Dijkstra Animated Example MINIMUM: take E

Dijkstra Animated Example NO paths change as distances stay the same

Dijkstra Animated Example MINIMUM: take B

Dijkstra Animated Example path(D) = Edge(A,C) + Edge(C,B) + Edge(B,D);

Dijkstra Animated Example MINIMUM: take D and WE ARE DONE…no more vertices

Implementations and Running Times Implementations and Running Times The simplest implementation is to store vertices in an array or linked list. This will produce a running time of O(|V|^2 + |E|) For sparse graphs, or graphs with very few edges and many nodes, it can be implemented more efficiently storing the graph in an adjacency list using a binary heap or priority queue. This will produce a running time of O((|E|+|V|) log |V|)

Dijkstra's Algorithm - Why It Works As with all greedy algorithms, we need to make sure that it is a correct algorithm (e.g., it always returns the right solution if it is given correct input). A formal proof would take longer than this presentation, but we can understand how the argument works intuitively. If you can’t sleep unless you see a proof, see the second reference or ask us where you can find it.

To understand how it works, we’ll go over the previous example again. However, we need two mathematical results first: Lemma 1 : Triangle inequality If δ(u,v) is the shortest path length between u and v, δ(u,v) ≤ δ(u,x) + δ(x,v) Lemma 2 : The subpath of any shortest path is itself a shortest path. The key is to understand why we can claim that anytime we put a new vertex in S, we can say that we already know the shortest path to it. Now, back to the example… D IJKSTRA ' S A LGORITHM - W HY I T W ORKS

As mentioned, Dijkstra’s algorithm calculates the shortest path to every vertex. However, it is about as computationally expensive to calculate the shortest path from vertex u to every vertex using Dijkstra’s as it is to calculate the shortest path to some particular vertex v. Therefore, anytime we want to know the optimal path to some other vertex from a determined origin, we can use Dijkstra’s algorithm. D IJKSTRA ' S A LGORITHM - W HY USE IT ?

References Dijkstra’s original paper: E. W. Dijkstra. (1959) A Note on Two Problems in Connection with Graphs. Numerische Mathematik, MIT OpenCourseware, 6.046J Introduction to Algorithms. Accessed 4/25/09http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and- Computer-Science/6-046JFall-2005/CourseHome/ Meyers, L.A. (2007) Contact network epidemiology: Bond percolation applied to infectious disease prediction and control. Bulletin of the American Mathematical Society 44 : Department of Mathematics, University of Melbourne. Dijkstra’s Algorithm. Accessed 4/25/09http:// 261/dijkstra/dijkstra.html

25 A H B F E D C G Another Visualization Initialize array Kdvdv pvpv AF  BF  CF  DF  EF  FF  GF  HF  2

25 A H B F E D C G Start with G Kdvdv pvpv A B C D E F GT0  H 2

25 A H B F E D C G Update unselected nodes Kdvdv pvpv A B C D2G E F GT0  H3G 2

25 A H B F E D C G Select minimum distance Kdvdv pvpv A B C DT2G E F GT0  H3G 2

25 A H B F E D C G Update unselected nodes Kdvdv pvpv A B C DT2G E27D F20D GT0  H3G 2

25 A H B F E D C G Select minimum distance Kdvdv pvpv A B C DT2G E27D F20D GT0  HT3G 2

25 A H B F E D C G Update unselected nodes Kdvdv pvpv A7H B12H C DT2G E27D F20D GT0  HT3G 2

25 A H B F E D C G Select minimum distance Kdvdv pvpv AT7H B12H C DT2G E27D F20D GT0  HT3G 2

25 A H B F E D C G Update unselected nodes Kdvdv pvpv AT7H B12H C DT2G E27D F17A GT0  HT3G 2

25 A H B F E D C G Select minimum distance Kdvdv pvpv AT7H BT12H C DT2G E27D F17A GT0  HT3G 2

25 A H B F E D C G Update unselected nodes Kdvdv pvpv AT7H BT12H C16B DT2G E22B F17A GT0  HT3G 2

25 A H B F E D C G Select minimum distance Kdvdv pvpv AT7H BT12H CT16B DT2G E22B F17A GT0  HT3G 2

25 A H B F E D C G Update unselected nodes Kdvdv pvpv AT7H BT12H CT16B DT2G E22B F17A GT0  HT3G 2

25 A H B F E D C G Select minimum distance Kdvdv pvpv AT7H BT12H CT16B DT2G E22B FT17A GT0  HT3G 2

25 A H B F E D C G Update unselected nodes Kdvdv pvpv AT7H BT12H CT16B DT2G E19F FT17A GT0  HT3G

25 A H B F E D C G Select minimum distance Kdvdv pvpv AT7H BT12H CT16B DT2G ET19F FT17A GT0  HT3G Done

Order of Complexity Analysis o findMin() takes O(V) time o outer loop iterates (V-1) times  O(V 2 ) time Optimal for dense graphs, i.e., |E| = O(V 2 ) Suboptimal for sparse graphs, i.e., |E| = O(V)

Order of Complexity If the graph is sparse, i.e., |E| = O(V) –maintain distances in a priority queue –insert new (shorter) distance produced by line 10 of Figure 9.32  O(|E| log |V|) complexity

Negative Edge Weights Read § Dijkstra’s algorithm as shown in Figure 9.32 does not work! Why?

Acyclic Graphs Read § Combine topological sort with Dijkstra’s algorithm

All-Pairs Shortest Paths One option: run Dijktra’s algorithm |V| times  O(V 3 ) time A more efficient O(V 3 ) time algorithm is discussed in Chapter 10