SHORTEST PATHS Lecture 19 CS2110 – Spring 2015 1.

Slides:



Advertisements
Similar presentations
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Advertisements

GRAPHS Lecture 18 CS2110 – Fall Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.
NL equals coNL Section 8.6 Giorgi Japaridze Theory of Computability.
1 Searching in a Graph Jeff Edmonds York University COSC 3101 Lecture 5 Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First.
Chapter 15 Graph Theory © 2008 Pearson Addison-Wesley. All rights reserved.
1 Spanning Trees Lecture 20 CS2110 – Spring
CSC 2300 Data Structures & Algorithms April 17, 2007 Chapter 9. Graph Algorithms.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
1 Teaching the development of algorithms Teach skill, not only facts David Gries Computer Science Department Cornell University November 2004 DrJava is.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
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.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Dijkstra's algorithm.
1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs , sec "This 'telephone' has too many shortcomings to be seriously.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Copyright © Cengage Learning. All rights reserved.
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.
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.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
Which of these can be drawn without taking your pencil off the paper and without going over the same line twice? If we can find a path that goes over all.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
CSE 024: Design & Analysis of Algorithms Chapter 9: NP Completeness Sedgewick Chp:40 David Luebke’s Course Notes / University of Virginia, Computer Science.
Graphs II Lecture 21: Shortest paths and spanning trees CS2110 – Spring Tokyo subway map.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
1 CS November 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
1 CS April 2010 binary search, linear search insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
1 Chapter 5-2 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
A Introduction to Computing II Lecture 16: Dijkstra’s Algorithm Fall Session 2000.
1 CS April 2009 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
1 CS November 2008 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
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.
1 CS April 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
SHORTEST PATHS Lecture 19 CS2110 – Fall Friday is Halloween. Why did I receive a Christmas card on Halloween?
SHORTEST PATHS READINGS? CHAPTER 28 Lecture 20 CS2110 – Spring
Graphs - II CS 2110, Spring Where did David leave that book? 2.
Week 7 - Monday CS 113.
1 Spanning Trees Lecture 22 CS2110 – Spring 2017.
CSC317 Shortest path algorithms
Dijkstra’s shortest path Algorithm
Shortest Path Algorithm
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Lecture 12 Algorithm Analysis
Shortest Path Algorithm
Shortest Path Problems
Programming Abstractions
Graphs Chapter 11 Objectives Upon completion you will be able to:
More Graphs Lecture 19 CS2110 – Fall 2013.
Shortest Path Problems
Yan Shi CS/SE 2630 Lecture Notes
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Artificial Intelligence
Route Inspection Which of these can be drawn without taking your pencil off the paper and without going over the same line twice? If we introduce a vertex.
DFS and Shortest Paths Lecture 18 CS2110 – Spring 2014.
Fundamental Structures of Computer Science II
Asymptotic complexity
Lecture 12 Algorithm Analysis
Searching and Sorting Hint at Asymptotic Complexity
Prims’ spanning tree algorithm
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

SHORTEST PATHS Lecture 19 CS2110 – Spring

Readings? Chapter 28 2

Do not write 2n = O(n) !!!!!! Please tell your AEW facilitator NOT to write: (1)2n = O(n) Instead, write: 2n is O(n) Formula (1) is a misuse of mathematical notation, and it can can lead to misconceptions and errors, as shown on the next slide. 3 Instead of “Jack is tall” do you write “Jack = tall”? Of course not!

Do not write 2n = O(n) !!!!!! We know that 2n is O(n) and 3n is O(n) Write these as 2n = O(n) and 3n = O(n) Then, we have: 2n = O(n) = 3n 4 So, using symmetry and transitivity of =, we have proved that 2n = 3n !!!!

Shortest Paths in Graphs Problem of finding shortest (min-cost) path in a graph occurs often  Find shortest route between Ithaca and West Lafayette, IN  Result depends on notion of cost Least mileage… or least time… or cheapest Perhaps, expends the least power in the butterfly while flying fastest Many “costs” can be represented as edge weights Every time you use googlemaps to find directions you are using a shortest-path algorithm 5

From Sid’s to David’s 6 Use googlemaps to find a route from Sid’s to David’s house. Gives three routes, depending on what is to be minimized. Miles? Driving time? Use of big highways? Scenic routes?

Shortest path? 7 Each intersection is a node of the graph, and each road between two intersections has a weight distance? time to traverse? …

Shortest path? 8 Fan out from the start node (kind of breadth- first search) Settled set: Those whose shortest distance is known. Frontier set: Those seen at least once but shortest distance not yet known

Shortest path? 9 Fan out from the start node (kind of breadth- first search). Start: Settled set: Frontier set: Choose the one in Frontier set with shortest distance from start

Shortest path? 10 Fan out from start node. Recording shortest distance from start seen so far Settled set: Frontier set: Now choose one in Frontier set with shortest distance from start

Shortest path? 11 Fan out from start, recording shortest distance seen so far Settled set: Frontier set: Now choose the one in Frontier set with shortest distance from start

Fan out from start, recording shortest distance seen so far Settled set: Frontier set: Shortest path? 12 1 Now choose the one in Frontier set with shortest distance

Fan out from start, recording shortest distance seen so far Settled set: Frontier set: Shortest path? Now choose the one in Frontier set with shortest distance A A

Fan out from start, recording shortest distance seen so far Settled set: Frontier set: Shortest path? A A 6 6 B B

15 Dijkstra’s shortest-path algorithm 15 Edsger Dijkstra, in an interview in 2010 (CACM): … the algorithm for the shortest path, which I designed in about 20 minutes. One morning I was shopping in Amsterdam with my young fiance, and tired, we sat down on the cafe terrace to drink a cup of coffee, and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a 20-minute invention. [Took place in 1956] Dijkstra, E.W. A note on two problems in Connexion with graphs. Numerische Mathematik 1, 269–271 (1959). Visit for all sorts of information on Dijkstra and his contributions. As a historical record, this is a gold mine.

16 Dijkstra’s shortest-path algorithm 16 Dijsktra describes the algorithm in English:  When he designed it in 1956 (he was 26 years old), most people were programming in assembly language!  Only one high-level language: Fortran, developed by John Backus at IBM and not quite finished. No theory of order-of-execution time —topic yet to be developed. In paper, Dijkstra says, “my solution is preferred to another one … “the amount of work to be done seems considerably less.” Dijkstra, E.W. A note on two problems in Connexion with graphs. Numerische Mathematik 1, 269–271 (1959).

NATO Conference on Software Engineering, Garmisch, Germany 17 Dijkstra Gries Term “software engineering” coined for this conference

NATO Conference on Software Engineering In Garmisch, Germany Academicians and industry people attended For first time, people admitted they did not know what they were doing when developing/testing software. Concepts, methodologies, tools were inadequate, missing The term software engineering was born at this conference. The NATO Software Engineering Conferences: Get a good sense of the times by reading these reports!

NATO Conference on Software Engineering, Garmisch, Germany 19

/69 NATO Conferences on Software Engineering Editors of the proceedings Edsger Dijkstra Niklaus Wirth Tony Hoare David Gries Beards The reason why some people grow aggressive tufts of facial hair Is that they do not like to show the chin that isn't there. a grook by Piet Hein

Dijkstra ’ s shortest path algorithm The n (> 0) nodes of a graph numbered 0..n-1. L[0] = 2 L[1] = 5 L[2] = 6 L[3] = 7 L[4] = 0 v Each edge has a positive weight. Some node v be selected as the start node. Use an array L[0..n-1]: for each node w, store in L[w] the length of the shortest path from v to w. weight(v1, v2) is the weight of the edge from node v1 to v2. Calculate length of shortest path from v to each node.

22 Dijkstra ’ s shortest path algorithm Develop algorithm, not just present it. Need to show you the state of affairs —the relation among all variables— just before each node i is given its final value L[i]. This relation among the variables is an invariant, because it is always true. Because each node i (except the first) is given its final value L[i] during an iteration of a loop, the invariant is called a loop invariant. L[0] = 2 L[1] = 5 L[2] = 6 L[3] = 7 L[4] = 0

23 1. For a Settled node s, L[s] is length of shortest v  s path. 2. All edges leaving S go to F. 3. For a Frontier node f, L[f] is length of shortest v  f path using only red nodes (except for f) 4. For a Far-off node b, L[b] = ∞ Frontier F Settled S Far off f f (edges leaving the black set and edges from the blue to the red set are not shown) 5. L[v] = 0, L[w] > 0 for w ≠ v The loop invariant v

24 1. For a Settled node s, L[s] is length of shortest v  r path. 2. All edges leaving S go to F. 3. For a Frontier node f, L[f] is length of shortest v  f path using only Settled nodes (except for f). 4. For a Far-off node b, L[b] = ∞. Theorem. For a node f in F with minimum L value (over nodes in F), L[f] is the length of the shortest path from v to f. Frontier F Settled S Far off f Theorem about the invariant fv g g Case 1: v is in S. Case 2: v is in F. Note that L[v] is 0; it has minimum L value L[g] ≥ L[f] 5. L[v] = 0, L[w] > 0 for w ≠ v.

25 1. For s, L[s] is length of shortest v  s path. 2. Edges leaving S go to F. S F Far off 3. For f, L[f] is length of shortest v  f path using red nodes (except for f). 4. For b in Far off, L[b] = ∞ 5. L[v] = 0, L[w] > 0 for w ≠ v For all w, L[w]= ∞; L[v]= 0; F= { v }; S= { }; Theorem: For a node f in F with min L value, L[f] is shortest path length v The algorithm Loopy question 1: How does the loop start? What is done to truthify the invariant?

26 When does loop stop? When is array L completely calculated? while { } 1. For s, L[s] is length of shortest v  s path. 2. Edges leaving S go to F. S F Far off 3. For f, L[f] is length of shortest v  f path using red nodes (except for f). 4. For b in Far off, L[b] = ∞ 5. L[v] = 0, L[w] > 0 for w ≠ v For all w, L[w]= ∞; L[v]= 0; F= { v }; S= { }; Theorem: For a node f in F with min L value, L[f] is shortest path length F ≠ {} The algorithm Loopy question 2:

27 How is progress toward termination accomplished? while { } f= node in F with min L value; Remove f from F, add it to S; 1. For s, L[s] is length of shortest v  s path. 2. Edges leaving S go to F. S F Far off 3. For f, L[f] is length of shortest v  f path using red nodes (except for f). 4. For b, L[b] = ∞ 5. L[v] = 0, L[w] > 0 for w ≠ v For all w, L[w]= ∞; L[v]= 0; F= { v }; S= { }; Theorem: For a node f in F with min L value, L[f] is shortest path length f F ≠ {} The algorithm Loopy question 3: f

28 How is the invariant maintained? while { } f= node in F with min L value; Remove f from F, add it to S; 1. For s, L[s] is length of shortest v  s path. 2. Edges leaving S go to F. S F Far off 3. For f, L[f] is length of shortest v  f path using red nodes (except for f). 4. For b, L[b] = ∞ 5. L[v] = 0, L[w] > 0 for w ≠ v For all w, L[w]= ∞; L[v]= 0; F= { v }; S= { }; Theorem: For a node f in F with min L value, L[f] is shortest path length F ≠ {} for each edge (f,w) { } if (L[w] is ∞) add w to F; if (L[f] + weight (f,w) < L[w]) L[w]= L[f] + weight(f,w); The algorithm Loopy question 4: f w w Algorithm is finished w

29 For all w, L[w]= ∞; L[v]= 0; F= { v }; S= { }; while F ≠ {} { f= node in F with min L value; Remove f from F, add it to S; for each edge (f,w) { if (L[w] is ∞) add w to F; if (L[f] + weight (f,w) < L[w]) L[w]= L[f] + weight(f,w); } Final algorithm 1. No need to implement S. 2. Implement F as a min-heap. 3. Instead of ∞, use Integer.MAX_VALUE. if (L[w] == Integer.MAX_VAL) { L[w]= L[f] + weight(f,w); add w to F; } else L[w]= Math.min(L[w], L[f] + weight(f,w)); SF

30 For all w, L[w]= ∞; L[v]= 0; F= { v }; while F ≠ {} { f= node in F with min L value; Remove f from F; for each edge (f,w) { if (L[w] == Integer.MAX_VAL) { L[w]= L[f] + weight(f,w); add w to F; } else L[w]= Math.min(L[w], L[f] + weight(f,w)); } Execution time SF n nodes, reachable from v. e ≥ n-1 edges n–1 ≤ e ≤ n*n O(n) O(n log n) O(e) O(n-1) O(n log n) O((e-(n-1)) log n) O(n) O(n + e) outer loop: n iterations. Condition evaluated n+1 times. inner loop: e iterations. Condition evaluated n + e times. Complete graph: O(n 2 log n). Sparse graph: O(n log n) O(n) O(1)