1 / 28

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.

wray
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 Animation slides by Keith Schwarz

  2. A* and Dijkstra’s Close cousins

  3. 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

  4. 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 h(s, t). 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+ h(v, t). 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 + h(v, t). A* Search

  5. A* on two points where the heuristic is slightly misleading due to a wall blocking the way

  6. A* starts with start node yellow, other nodes grey.

  7. A*: dequeue start node, turns green.

  8. 1 + 6? 1 + 6? 1 + 4? 1 + 6? A*: enqueue neighbors with candidate distance + heuristic distance as the priority value.

  9. 1 + 6? 1 + 6? 1 1 + 6? A*: dequeue min-priority-value node.

  10. 1 + 6? 2 + 5? 1 + 6? 1 2 + 3? 2 + 5? 1 + 6? A*: enqueue neighbors.

  11. 1 + 6? 2 + 5? 1 + 6? 1 2 2 + 5? 1 + 6? A*: dequeue next lowest priority value node. Notice we are making a straight line right for the end point, not wasting time with other directions.

  12. 1 + 6? 2 + 5? 3 + 4? 1 + 6? 1 2 2 + 5? 1 + 6? 3 + 4? A*: enqueue neighbors—uh-oh, wall blocks us from continuing forward.

  13. 3 + 8? 5 + 6? 7 + 4? 6 + 5? 4 + 7? 3 4 5 6 3 + 8? 7 + 2? 2 2 7 + 2? 3 + 8? 2 1 3 3 + 8? 1 1 2 8 2 1 3 8 + 1? 3 + 8? 2 7 2 3 + 8? 3 4 6 7 5 2 8 + 3? 3 + 8? 7 + 4? 5 + 6? 6 + 5? 8 + 3? 4 + 7? A*: eventually figures out how to go around the wall, with some waste in each direction. But…compare to Dijkstras…

  14. 8 7 6 5 4 5 6 7 8 9? 7 6 5 4 3 4 5 6 7 8 9? 6 5 4 3 3 4 5 6 7 8 9? 2 5 4 3 2 7 8 9? 2 1 3 4 3 1 1 2 8 2 5 4 3 1 3 8 9? 2 7 2 6 5 4 3 3 4 6 7 8 9? 4 5 2 7 6 5 4 3 4 5 6 7 8 9? 8 7 6 5 4 5 6 7 8 9? For Comparison: What Dijkstra's Algorithm Would Have Searched

  15. Minimum Spanning Tree

  16. A spanning tree in an undirected graph is a set of edges with no cycles that connects all nodes.

  17. B D F 3 7 1 3 6 Edges: (A,B)=1 (A,C)=3 (B,C)=6 (B,D)=3 (C,E)=3 (D,E)=3 (D,F)=7 3 A C E 3 How many distinct spanning trees are in this graph? • 0-1 • 2-3 • 4-5 • 6-7 • >7

  18. Minimum Spanning Tree A minimum spanning tree (or MST) is a spanning tree with the least total cost. Note: This may sound similar to the shortest-paths tree that Dijkstra’s made, but an MST may not be a shortest-paths tree, and a shortest-paths tree may not be an MST.

  19. B D F 3 7 1 3 6 Edges: (A,B)=1 (A,C)=3 (B,C)=6 (B,D)=3 (C,E)=3 (D,E)=3 (D,F)=7 3 A C E 3 How many distinct MSTs are in this graph? • 0-1 • 2-3 • 4-5 • 6-7 • >7

  20. True or False? • Having at least two edges with the same weight is SUFFICIENT for having more than one distinct minimum spanning tree. • TRUE! • FALSE! (i.e., “If at least two edges have the same weight, then there is more than one MST”)

  21. True or False? • Having at least two edges with the same weight is NECESSARY for having more than one distinct minimum spanning tree. • TRUE! • FALSE! (i.e., “If there is more than one MST, then at least two edges have the same weight.”)

  22. Kruskal’s algorithm • Remove all edges from graph • Place all edges in a PQ based on length/weight • While !PQ.isEmpty(): • Dequeue edge • If the edge connects previous disconnected nodes or groups of nodes, keep the edge • Otherwise discard the edge

  23. Kruskal's with Clusters (i.e. how to know “If the endpoints of the edge aren't already connected to one another”) • Place every node into its own cluster • Place all edges into a priority queue • While there are two or more clusters remaining: • Dequeue an edge from the priority queue • If its endpoints are not in the same cluster: • Merge the clusters containing the endpoints • Add the edge to the resulting spanning tree

  24. Kruskal’s algorithm

  25. Prim’s algorithm • Choose a start node from the graph, which becomes our starting tree of size 1 (doesn’t matter which one you pick) • While tree.size() < graph.size(): • Examine all outgoing edges from the tree (edges that connect a node in the tree with a node not in the tree) • Choose the shortest edge and add it (and the node it connects to) to the tree

  26. Prim’s algorithm

  27. The Good Will Hunting Problem

  28. “Draw all the homeomorphically irreducible trees with n=10.” • In this case “trees” simply means graphs with no cycles • “with n = 10” (i.e., has 10 nodes) • “homeomorphicallyirreducible” • No nodes of degree 2 • For this problem, nodes of degree 2 are useless in terms of tree structure—they just act as a blip on an edge—and are therefore banned • Have to be actually different • Ignore superficial changes in rotation or angles of drawing

More Related