Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Graphs Lecture 19 CS2110 – Fall 2013.

Similar presentations


Presentation on theme: "More Graphs Lecture 19 CS2110 – Fall 2013."— Presentation transcript:

1 More Graphs Lecture 19 CS2110 – Fall 2013

2 Readings? This lecture is based on chapter 28
Homework: (a simple self-test question): Suppose you were doing your own version of Google maps. You are writing code that tells the user how to get from Ithaca to Miami South Beach. Would you start by running Dijkstra’s, Prim’s, or Kruskel’s algorithm?

3 Representations of Graphs
1 2 4 3 List Matrix Danaus Park 1 2 3 4 2 3 4 1

4 Adjacency Matrix or Adjacency List or “Park”?
Danaus is a kind of graph In A3 and A5 we’ve simply captured it into a 2-D array What graph would Danaus look like if you instead wanted to draw a picture of it as a graph? Each tile would be a node Each single move in a flyable path would be an edge Edges present if you can get from [x][y] to [x’][y’] Should the edges be weighted? In A6 wind effects might argue for a weighted graph! Which representation you choose should depend on what operations on the graph are to be performed and also on whether the graph is sparse or dense.

5 Representing one thing two ways
In computer science we often build and use multiple representations of the same data For A5 this isn’t really necessary, but in A6 (coming soon!) you’ll need to work with both explicit graph representations of the park and with the 2-D form in order to have a high quality solution For a lower quality solution this won’t be needed Best solutions might be 100x or more faster…

6 Shortest Paths in Graphs
Finding the shortest (min-cost) path in a graph is a problem that occurs often Find the shortest route between Ithaca and West Lafayette, IN Result depends on our 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 A butterfly that optimizes to fly in bright sunshine, or to most efficiently collect a list of flowers, is optimizing over possible path lengths that are computed using one or perhaps multiple such factors: machine learning How do we find a shortest path?

7 Dijkstra’s shortest-path algorithm
7 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. Here’s a bit of history about Dijkstra’s shortest path algorithm. Dijkstra was 26 at the time. Dijkstra died of cancer at the age of 72. His wife, Ria, the young fiance he mentions, died just 6 months ago, in October 2012.

8 Dijkstra’s shortest-path algorithm
8 Dijsktra describes the algorithm in English: When he designed it in 1956, 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, Dijsktra 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). Gries had only one course on programming or computer science: a course in 1959 on an assembly language for a fake computer. We, too, could not test our program! There were about 20 students in the course, so the professor didn’t have TOO much grading to do –no TAs. After graduating in June 1960, he worked for two years for the US Naval Weapons Lab (as a civilian), as a mathemataician-programmer. He was taught Fortran in 1 week, and then started programming in both Fortran and the IBM 7090 assembly language.

9 Dijkstra’s shortest path algorithm
The n (> 0) nodes of a graph numbered 0..n-1. Each edge has a positive weight. weight(v1, v2) is the weight of the edge from node v1 to v2. Some node v be selected as the start node. Calculate length of shortest path from v to each 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. L[0] = 2 L[1] = 5 L[2] = 6 L[3] = 7 L[4] = 0 This will be a high-level description, not mentioning what the graph representation is. 4 1 2 3 4 2 1 3 v

10 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 Showing an algorithm and then presenting its correctness proof is not as compelling as showing the development of program and proof hand-in-hand, with the proof ideas leading the way. If possible, one should show how to develop the invariant from the pre- and post-conditions, but that is not always possible. Here, we describe the loop invariant first, not saying how it came about.

11 The loop invariant Frontier F Settled S Far off f
(edges leaving the black set and edges from the blue to the red set are not shown) f 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) f 4. For a Far-off node b, L[b] = ∞ Settled node information is always in red, Frontier information in blue, and Far off info in black. 5. L[v] = 0, L[w] > 0 for w ≠ v 4 2 1 3 v

12 Theorem about the invariant
Settled S Frontier F Far off Theorem about the invariant f v g L[g] ≥ L[f] f 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] = ∞. 5. L[v] = 0, L[w] > 0 for w ≠ v . Note that the theorem is proved not in terms of executing things but in terms of a state of affairs. This makes it easier to state and to prove. The case v is in F is here only to make the algorithm a bit shorter. We could start with v in S, but that would require more work in order to truthify the invariant later on. 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. Case 1: v is in S. Case 2: v is in F. Note that L[v] is 0; it has minimum L value

13 The algorithm For all w, L[w]= ∞; L[v]= 0; S F Far off
F= { v }; S= { }; v 1. For s, L[s] is length of shortest v s path. 2. Edges leaving S go to F. 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 To truthify invariant initially, set all L values to infinity and L[v] to 0. Then make F contain v and s empty. It is easy to see that each part of the invariant is true. Loopy question 1: Theorem: For a node f in F with min L value, L[f] is shortest path length How does the loop start? What is done to truthify the invariant?

14 The algorithm For all w, L[w]= ∞; L[v]= 0; S F Far off
F= { v }; S= { }; while { } F ≠ {} 1. For s, L[s] is length of shortest v  s path. 2. Edges leaving S go to F. 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 The loop can stop when l is empty. Then, v has to be in S, there are no edges leaving S, so all nodes are in S. Loopy question 2: Theorem: For a node f in F with min L value, L[f] is shortest path length When does loop stop? When is array L completely calculated?

15 The algorithm For all w, L[w]= ∞; L[v]= 0; S F Far off
F= { v }; S= { }; while { } F ≠ {} f f 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. 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 To make progress add a node to the settled set. The theorem tells us which one to add. Note that moving f to S may gives new branches to both F and Far Off. Something must Be done to truthify the invariant. Loopy question 3: Theorem: For a node f in F with min L value, L[f] is shortest path length How is progress toward termination accomplished?

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

17 About implementation 1. No need to implement S.
2. Implement F as a min-heap. 3. Instead of ∞, use Integer.MAX_VALUE. S F 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); } 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));

18 Execution time n nodes, reachable from v. e ≥ n-1 edges n–1 ≤ e ≤ n*n
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)); O(n) O(1) O(n) O(n + e) outer loop: n iterations. Condition evaluated n+1 times. inner loop: e iterations. n + e times. O(n) O(n log n) O(e) O(n-1) O(n log n) In processing the graph, each edge is processed exactly once, when its source is put in S. Thus, the body of the loop is executed e times but the loop condition is evaluated n+e times, since it has to be found false. The if-statement is evaluated e time; its condition is true n-1 times. O((e-(n-1)) log n) Complete graph: O(n2 log n). Sparse graph: O(n log n)

19 Dijkstra’s Algorithm dijkstra(s) { // Note: weight(s,t) = cost of the s,t edge if present // Integer.MAX_VALUE otherwise D[s] = 0; D[t] = weight(s,t), t ≠ s; mark s; while (some vertices are unmarked) { v = unmarked node with smallest D; mark v; for (each w adjacent to v) { D[w] = min(D[w], D[v] + weight(v,w)); }

20 Dijkstra’s Algorithm 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 2.4 1.5

21 Dijkstra’s Algorithm X 2.4 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5

22 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 4.6

23 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 4.6

24 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 4.6

25 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 2.5

26 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 2.5

27 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 2.5

28 Shortest Paths for Unweighted Graphs – A Special Case
Use breadth-first search Time is O(n + m) in adj list representation, O(n2) in adj matrix representation S B A C D E F


Download ppt "More Graphs Lecture 19 CS2110 – Fall 2013."

Similar presentations


Ads by Google