1 / 98

CS106X – Programming Abstractions in C++

CS2 in C++ Peer Instruction Materials by  Cynthia Bailey Lee  is licensed under a  Creative Commons Attribution- NonCommercial - ShareAlike 4.0 International License . Permissions beyond the scope of this license may be available at  http://peerinstruction4cs.org.

heidi-sosa
Download Presentation

CS106X – Programming Abstractions in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org. CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz

  2. Graphs Note about representation of graphs in Trailblazer assignment(compared to our two canonical graph representation forms: adjacency matrix and adjacency list)

  3. Representing Graphs: Adjacency Matrix vs. Trailblazer

  4. Breadth-First Search Graph algorithms

  5. Breadth-First Search A B C D E F G H I J K L

  6. Breadth-First Search A B C D A B C D THINGS TO NOTICE: We used a queue What’s left is kind ofa subset of the edges, in the form of ‘parent’ pointers If you follow the parent pointers from the desired endpoint, you will get back to the start point, and it will be the shortest way to do that. E F G H E F G H I J K I J K L L

  7. Breadth-First Search A B C D A B C D THINGS TO NOTICE: (4) We now have the answer to the question “What is the shortest path to you from F?” forevery single node in the graph!! E F G H E F G H I J K I J K L L

  8. But wait!! • What you calculated will find the shortest path in terms of fewest number of road segments but doesn’t at all take into account how long each road segment is! 

  9. But wait!! • This might be useful if you are a fugitive and want to go from one city to another passing through the fewest other cities on the way… • But what if we want to save gas/fewest miles?

  10. Dijkstra’s Shortest Paths (Like Breadth-first Search, but takes into account weight/distance between nodes)

  11. EdsgerDijkstra 1930-2002 • THE multiprogramming system (operating system) • Layers of abstraction!! • Complier for a language that can do recursion • Dining Philosopher’s Problem (resource contention and deadlock) • Dijkstra’s algorithm • “Goto considered harmful” (title given to his letter) This file is licensed under the Creative CommonsAttribution-Share Alike 3.0 Unported license. http://en.wikipedia.org/wiki/File:Edsger_Wybe_Dijkstra.jpg

  12. The Shortest Path Problem Suppose that you have a graph representing different locations Each edge has an associated cost (or weight, length, etc) We'll assume costs are nonnegative* Goal: Find the least-cost (or lowest-weight, lowest-length, etc) path from some node u to a node v * else use the Bellman–Ford algorithm

  13. A A B B C C 6 3 3 1 1 D D E E F F 1 7 9 4 7 G G H H I I 2 5

  14. Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority 0. While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L. If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L. Dijkstra's Algorithm

  15. A A 0? B B C C 6 3 3 1 2 1 D D E E F F 1 7 9 4 4 7 G G H H I I 2 5

  16. A A 0? B B C C 6 3 A 0? 3 1 2 1 D D E E F F 1 7 9 4 4 7 G G H H I I 2 5

  17. A A 0? B B C C 6 3 A 0? 3 1 2 1 D D E E F F 1 7 9 4 4 7 G G H H I I 2 5

  18. A A 0? B B C C 6 3 3 1 2 1 D D E E F F 1 7 9 4 4 7 G G H H I I 2 5

  19. A A 0 B B C C 6 3 3 1 2 1 D D E E F F 1 7 9 4 4 7 G G H H I I 2 5

  20. A A 0 B B 6? C C 6 3 3 1 2 1 D D 3? E E F F 1 7 9 4 4 7 G G H H I I 2 5

  21. A A 0 B B 6? C C 6 3 D 3? 3 1 2 1 B 6? D D 3? E E F F 1 7 9 4 4 7 G G H H I I 2 5

  22. A A 0 B B 6? C C 6 3 D 3? 3 1 2 1 B 6? D D 3? E E F F 1 7 9 4 4 7 G G H H I I 2 5

  23. A A 0 B B 6? C C 6 3 D 3? 3 1 2 1 B 6? D D 3? E E F F 1 7 9 4 4 7 G G H H I I 2 5

  24. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 D D 3? E E F F 1 7 9 4 4 7 G G H H I I 2 5

  25. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 D D 3 E E F F 1 7 9 4 4 7 G G H H I I 2 5

  26. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 D D 3 E E 4? F F 1 7 9 4 4 7 G G 12? H H I I 2 5

  27. A A 0 B B 6? C C 6 3 3 1 2 1 B 6? D D 3 E E 4? F F 1 7 9 4 4 7 G G 12? H H I I 2 5

  28. A A 0 B B 6? C C 6 3 E 4? 3 1 2 1 B 6? D D 3 E E 4? F F G 12? 1 7 9 4 4 7 G G 12? H H I I 2 5

  29. A A 0 B B 6? C C 6 3 E 4? 3 1 2 1 B 6? D D 3 E E 4? F F G 12? 1 7 9 4 4 7 G G 12? H H I I 2 5

  30. A A 0 B B 6? C C 6 3 E 4? 3 1 2 1 B 6? D D 3 E E 4? F F G 12? 1 7 9 4 4 7 G G 12? H H I I 2 5

  31. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 G 12? D D 3 E E 4? F F 1 7 9 4 4 7 G G 12? H H I I 2 5

  32. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 G 12? D D 3 E E 4 F F 1 7 9 4 4 7 G G 12? H H I I 2 5

  33. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 G 12? D D 3 E E 4 F F 11? 1 7 9 4 4 7 G G 12? H H 8? I I 2 5

  34. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 D D 3 E E 4 F F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  35. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 H 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  36. A A 0 B B 6? C C 6 3 B 6? 3 1 2 1 H 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  37. A A 0 B B 5? C C 6 3 B 6? 3 1 2 1 H 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  38. A A 0 B B 5? C C 6 3 B 5? 3 1 2 1 H 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  39. A A 0 B B 5? C C 6 3 B 5? 3 1 2 1 H 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  40. A A 0 B B 5? C C 6 3 B 5? 3 1 2 1 H 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  41. A A 0 B B 5? C C 6 3 B 5? 3 1 2 1 H 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  42. A A 0 B B 5? C C 6 3 H 8? 3 1 2 1 F 11? D D 3 E E 4 F F 11? G 12? 1 7 9 4 4 7 G G 12? H H 8? I I 2 5

  43. A A 0 B B 5 C C 6 3 H 8? 3 1 2 1 F 11? D D 3 E E 4 F F 11? G 12? 1 7 9 4 4 7 G G 12? H H 8? I I 2 5

  44. A A 0 B B 5 C C 8? 6 3 H 8? 3 1 2 1 F 11? D D 3 E E 4 F F 11? G 12? 1 7 9 4 4 7 G G 12? H H 8? I I 2 5

  45. A A 0 B B 5 C C 8? 6 3 H 8? 3 1 2 1 D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  46. A A 0 B B 5 C C 8? 6 3 H 8? 3 1 2 1 C 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  47. A A 0 B B 5 C C 8? 6 3 H 8? 3 1 2 1 C 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

  48. A A 0 B B 5 C C 8? 6 3 H 8? 3 1 2 1 C 8? D D 3 E E 4 F F 11? F 11? 1 7 G 12? 9 4 4 7 G G 12? H H 8? I I 2 5

More Related