Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

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
 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)
Introduction to Algorithms 6.046J/18.401J/SMA5503
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.
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.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Shortest Paths Definitions Single Source Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
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.
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.
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.
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.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
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.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
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.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
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.
Master Method (4. 3) Recurrent formula T(n) = a  T(n/b) + f(n) 1) if for some  > 0 then 2) if then 3) if for some  > 0 and a  f(n/b)  c  f(n) for.
Introduction to Algorithms Jiafen Liu Sept
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
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.
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
Lecture 13 Algorithm Analysis
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
Shortest paths Given: A single source vertex (given s) in a weighted, directed graph. Want to compute a shortest path for each possible destination. (Single.
GRAPH THEORY Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS )
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Data Structures and Algorithms
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Page 620 Single-Source Shortest Paths
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
Chapter 24: Single-Source Shortest Paths
Advanced Algorithms Analysis and Design
Slide Courtesy: Uwash, UT
Chapter 24: Single-Source Shortest Paths
CS 3013: DS & Algorithms Shortest Paths.
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)

Graphs - Shortest Paths Application In a graph in which edges have costs.. Find the shortest path from a source to a destination Surprisingly.. While finding the shortest path from a source to one destination, we can find the shortest paths to all over destinations as well! Common algorithm for single-source shortest paths is due to Edsger Dijkstra

Dijkstra’s Algorithm - Data Structures For a graph, G = ( V, E ) Dijkstra’s algorithm keeps two sets of vertices: S Vertices whose shortest paths have already been determined V-S Remainder Also d Best estimates of shortest path to each vertex  Predecessors for each vertex

Predecessor Sub-graph Array of vertex indices,  [j], j = 1.. |V|  [j] contains the pre-decessor for node j j’ s predecessor is in  [  [j]], and so on.... The edges in the pre-decessor sub-graph are (  [j], j )

Dijkstra’s Algorithm - Operation Initialise d and  For each vertex, j, in V d j =   j = nil Source distance, d s = 0 Set S to empty While V-S is not empty Sort V-S based on d Add u, the closest vertex in V-S, to S Relax all the vertices still in V-S connected to u Initial estimates are all  No connections Add s first!

Dijkstra’s Algorithm - Operation Initialise d and  For each vertex, j, in V d j =   j = nil Source distance, d s = 0 Set S to empty While V-S is not empty Sort V-S based on d Add u, the closest vertex in V-S, to S Relax all the vertices still in V-S connected to u Initial estimates are all  No connections Add s first!

Dijkstra’s Algorithm - Operation The Relaxation process Relax the node v attached to node u relax( Node u, Node v, double w[][] ) if d[v] > d[u] + w[u,v] then d[v] := d[u] + w[u,v] pi[v] := u If the current best estimate to v is greater than the path through u.. Edge cost matrix Update the estimate to v Make v’s predecessor point to u

Dijkstra’s Algorithm - Full The Shortest Paths algorithm Given a graph, g, and a source, s shortest_paths( Graph g, Node s ) initialise_single_source( g, s ) S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w )

Dijkstra’s Algorithm - Initialise The Shortest Paths algorithm Given a graph, g, and a source, s shortest_paths( Graph g, Node s ) initialise_single_source( g, s ) S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w ) Initialise d, , S, vertex Q

Dijkstra’s Algorithm - Loop The Shortest Paths algorithm Given a graph, g, and a source, s shortest_paths( Graph g, Node s ) initialise_single_source( g, s ) S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w ) Greedy! While there are still nodes in Q

Dijkstra’s Algorithm - Relax neighbours The Shortest Paths algorithm Given a graph, g, and a source, s shortest_paths( Graph g, Node s ) initialise_single_source( g, s ) S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w ) Greedy! Update the estimate of the shortest paths to all nodes attached to u

Dijkstra’s Algorithm - Operation Initial Graph Distance to all nodes marked  Source Mark 0

Dijkstra’s Algorithm - Operation Initial Graph Source Relax vertices adjacent to source

Dijkstra’s Algorithm - Operation Initial Graph Source Red arrows show pre-decessors

Dijkstra’s Algorithm - Operation Source is now in S Sort vertices and choose closest

Dijkstra’s Algorithm - Operation Source is now in S Relax u because a shorter path via x exists Relax y because a shorter path via x exists

Dijkstra’s Algorithm - Operation Source is now in S Change u’s pre-decessor also Relax y because a shorter path via x exists

Dijkstra’s Algorithm - Operation S is now { s, x } Sort vertices and choose closest

Dijkstra’s Algorithm - Operation S is now { s, x } Sort vertices and choose closest Relax v because a shorter path via y exists

Dijkstra’s Algorithm - Operation S is now { s, x, y } Sort vertices and choose closest, u

Dijkstra’s Algorithm - Operation S is now { s, x, y, u } Finally add v

Dijkstra’s Algorithm - Operation S is now { s, x, y, u } Pre-decessors show shortest paths sub-graph

Dijkstra’s Algorithm - Proof Greedy Algorithm Proof by contradiction best Lemma 1 Shortest paths are composed of shortest paths Proof If there was a shorter path than any sub-path, then substitution of that path would make the whole path shorter

Dijkstra’s Algorithm - Proof Denote  (s,v) - the cost of the shortest path from s to v Lemma 2 If s ...  u  v is a shortest path from s to v, then after u has been added to S and relax(u,v,w) called, d[v] =  (s,v) and d[v] is not changed thereafter. Proof Follows from the fact that at all times d[v]   (s,v) See Cormen (or any other text) for the details

Dijkstra’s Algorithm - Proof Using Lemma 2 After running Dijkstra’s algorithm, we assert d[v] =  (s,v) for all v Proof (by contradiction) Suppose that u is the first vertex added to S for which d[u]    (s,u) Note v is not s because d[s] = 0 There must be a path s ...  u, otherwise d[u] would be  Since there’s a path, there must be a shortest path

Dijkstra’s Algorithm - Proof Proof (by contradiction) Suppose that u is the first vertex added to S for which d[u]    (s,u) Let s  x  y  u be the shortest path s  u, where x is in S and y is the first outside S When x was added to S, d[x]    (s,x) Edge x  y was relaxed at that time, so d[y]    (s,y)

Dijkstra’s Algorithm - Proof Proof (by contradiction) Edge x  y was relaxed at that time, so d[y]    (s,y)   (s,u)  d[u] But, when we chose u, both u and y where in V-S, so d[u]  d[y] (otherwise we would have chosen y ) Thus the inequalities must be equalities  d[y]    (s,y)   (s,u)  d[u] And our hypothesis ( d[u]    (s,u) ) is contradicted!

Dijkstra’s Algorithm - Time Complexity Dijkstra’s Algorithm Similar to MST algorithms Key step is sort on the edges Complexity is O( (|E|+|V|)log|V| ) or O( n 2 log n ) for a dense graph with n = |V|