Dijkstra’s Shortest Paths CS 312 Lecture 4. Announcements Project 1 comes out Friday –Min spanning trees and scheduling –due 2 weeks from Friday (1 week.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Chen, Lu Mentor: Zhou, Jian Shanghai University, School of Management Chen213.shu.edu.cn.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
Chapter 5 Shortest Paths: Label-Correcting Algorithms
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
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.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
1 Dijkstra’s Shortest Path Algorithm Gordon College.
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.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
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.
The Shortest Path Problem
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.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
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.
Dijkstra’s Algorithm. 2 Shortest-path Suppose we want to find the shortest path from node X to node Y It turns out that, in order to do this, we need.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 10.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
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.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Introduction to Algorithms Jiafen Liu Sept
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)
Prims Algorithm for finding a minimum spanning tree
CSE 421 Algorithms Richard Anderson Lecture 8 Optimal Caching Dijkstra’s algorithm.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.
Dijkstra animation. Dijksta’s Algorithm (Shortest Path Between 2 Nodes) 2 Phases:initialization;iteration Initialization: 1. Included:(Boolean) 2. Distance:(Weight)
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraph Algorithms.
Minimum Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Lecture 23 CSE 331 Oct 26, 2016.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Shortest Path Graph represents highway system Edges have weights
Minimum Spanning Trees
Autumn 2016 Lecture 9 Dijkstra’s algorithm
Autumn 2015 Lecture 9 Dijkstra’s algorithm
Richard Anderson Lecture 9 Dijkstra’s algorithm
Lecture 24 CSE 331 Oct 29, 2012.
Richard Anderson Lecture 9 Dijkstra’s algorithm
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
Fundamental Structures of Computer Science II
Slide Courtesy: Uwash, UT
Graph Algorithms: Shortest Path
Lecture 23 CSE 331 Oct 24, 2011.
Winter 2019 Lecture 9 Dijkstra’s algorithm
CSE 417: Algorithms and Computational Complexity
Prim’s algorithm for minimum spanning trees
Data Structures and Algorithm Analysis Lecture 8
Autumn 2019 Lecture 9 Dijkstra’s algorithm
Presentation transcript:

Dijkstra’s Shortest Paths CS 312 Lecture 4

Announcements Project 1 comes out Friday –Min spanning trees and scheduling –due 2 weeks from Friday (1 week later than current calendar) Quiz 1 is ready and waiting –Open book –30 min limit –get a copy on line before you start

Objectives Follow up –big-O, big-Theta and big-Omega –Prim’s algorithm: outside of class. Understand the shortest paths problem. Understand Dijkstra’s algorithm Analyze Dijkstra’s algorithm

Big-Letters

THE PROBLEM Given: a directed graph G=  N,A  each edge has a nonnegative length one designated source node Determine the length of the shortest path from the source to each of the other nodes of the graph. THE SOLUTION A greedy algorithm called Dijkstra’s Algorithm.

Dijkstra’s Algorithm: Overview Maintain a set S of vertices whose minimum distance from the source is known, a set C of candidate vertices (S  C=N, S  C=  ). Initially, S={s}. (the source) Keep adding vertices to S, choosing from C the node whose distance from the source is least. Eventually, S=N. Special path: a path from ‘s’ with all intermediate nodes in S. Maintain an array D with: If w  S, then D[w] is the length of the shortest path to w. If w  S, then D[w] is the length of the shortest special path from s to w.

Dijkstra’s algorithm L[i,j]: the weight along path from i to j. D[k]: length of the shortest special path from source to k. j in C : shortest path from source to j is not known. k in S : shortest path from source to k is known. Dijkstra (L[1…n, 1…n]) : array [2…n] array D [2…n] C  {2,3…n} for i  2 to n do D[i]  L[1,i] repeat n - 2 times v  some element of C minimizing D[v] C  C \ {v} for each w in C do D[w]  min (D[w], D[v] + L[v,w]) return D Inner loop

Step Init. v-v- C {2,3,4,5} D [50, 30, 100, 10] Choose node from C minimizing D (node 5)

Step Init. 1 v-5v-5 C {2,3,4,5} {2,3,4} D [50, 30, 100, 10] [50, 30, 20, 10] Compute D for another node in C

Step Init. 1 v-5v-5 C {2,3,4,5} {2,3,4} D [50, 30, 100, 10] [50, 30, 20, 10] Compute D for another node in C

Step Init. 1 v-5v-5 C {2,3,4,5} {2,3,4} D [50, 30, 100, 10] [50, 30, 20, 10] Compute D for another node in C

Step Init. 1 v-5v-5 C {2,3,4,5} {2,3,4} D [50, 30, 100, 10] [50, 30, 20, 10] Choose node from C minimizing D (node 4)

Step Init. 1 2 v-54v-54 C {2,3,4,5} {2,3,4} {2,3} D [50, 30, 100, 10] [50, 30, 20, 10] Compute D for another node in C

Step Init. 1 2 v-54v-54 C {2,3,4,5} {2,3,4} {2,3} D [50, 30, 100, 10] [50, 30, 20, 10] [40, 30, 20, 10] Compute D for another node in C

Step Init. 1 2 v-54v-54 C {2,3,4,5} {2,3,4} {2,3} D [50, 30, 100, 10] [50, 30, 20, 10] [40, 30, 20, 10] Choose node from C minimizing D (node 3)

Step Init v-543v-543 C {2,3,4,5} {2,3,4} {2,3} {2} D [50, 30, 100, 10] [50, 30, 20, 10] [40, 30, 20, 10] [35, 30, 20, 10] Compute D for another node in C (last one)

To find not only the length, but where they pass: add an array P[2..n], where P[v] contains v’s predecessor follow the pointers P backwards from destination to source. Modify the code: initialize P[i] to 1, for I=2,3,…,n replace the contents of the inner for loop by if D[w]>D[v] + L[v,w] thenD[w]  D[v]+L[v,w] P[w]  v

Special Path A path from the source to node x is special if all nodes in the path from the source to x belong to S S = {4,5} then path 1,5,4 is special and path 1,4,3 is special but path 1,3,2 is not.

We prove by mathematical induction that (a) If w  1 and w  S, then D[w] is the length of the shortest path to w from the source. (b) If w  S, then D[w] is the length of the shortest special path from s to w. Basis: Initially, only s  S, so (a) holds (vacuously). (b) is true because the only special path from s is the direct path and D is initialized accordingly. Induction hypothesis: Conditions (a) and (b) hold before a node v is added to S. Induction step for (a): For nodes already in S nothing changes, so (a) is true for them. Before adding v to S we must check that D[v] is the length of the shortest path from s to v. By the induction hypothesis, D[v] is the shortest special path, so we must verify that the shortest path from s to v does not go outside of S.

s v x The shortest path. The shortest special path. Suppose the contrary; the shortest path from s is not special, that is it goes outside S, and the first node encountered is x.

s v x The shortest path. The shortest special path. Since the path from 1…x is special, D[x] = length of shortest special path from 1 … x by (b). D[x], by induction hypothesis (b)

s v x So total distance from 1…x…v > 1…x (since … what?) and D[x] > D[v] since D[v] was chosen first. Therefore, D[x]+length(x…v) > D[v]. D[x], by induction hypothesis on (b)

s v x Total distance to v via x > D[x], (lengths are positive) D[x] > D[v], (the algorithm chose v before x)

s v x Suppose the contrary; the shortest path from s goes outside S, and the first node encountered is x. D[x], by induction hypothesis on (b) Total distance to v via x > D[x], (lengths are positive) D[x] > D[v], (the algorithm chose v before x)  Total distance to v via x > D[v], (transitivity) Contradiction! (The shortest path is longer than the shortest special path ???)

Dijkstra (L[1…n, 1…n]) : array [2…n] array D [2…n] C  {2,3…n} for i  2 to n do D[i]  L[1,i] repeat n - 2 times v  some element of C minimizing D[v] C  C \ {v} for each w in C do D[w]  min (D[w], D[v] + L[v,w]) return D Analysis

Dijkstra (L[1…n, 1…n]) : array [2…n] array D [2…n] C  {2,3…n} for i  2 to n do D[i]  L[1,i] repeat n - 2 times v  some element of C minimizing D[v] C  C \ {v} for each w in C do D[w]  min (D[w], D[v] + L[v,w]) return D Analysis Therefore: