CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercialShareAlike 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 A* and Dijkstra’s Close cousins ● ● ● ● 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. Dijkstra's Algorithm ● ● ● ● 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. A* Search A* on two points where the heuristic is slightly misleading due to a wall blocking the way A* starts with start node yellow, other nodes grey. A*: dequeue start node, turns green. 1+ 6? 1+ 6? 1+ 4? 1+ 6? A*: enqueue neighbors with candidate distance + heuristic distance as the priority value. 1+ 6? 1+ 6? 1 1+ 6? A*: dequeue min-priority-value node. 1+ 2+ 6? 5? 1+ 6? 1 2+ 3? 1+ 2+ 6? 5? A*: enqueue neighbors. 1+ 2+ 6? 5? 1+ 6? 1 2 1+ 2+ 6? 5? 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. 1+ 2+ 3+ 6? 5? 4? 1+ 6? 1 2 1+ 2+ 3+ 6? 5? 4? A*: enqueue neighbors—uh-oh, wall blocks us from continuing forward. 3+ 8? 3+ 4+ 5+ 6+ 7+ 8? 7? 6? 5? 4? 3+ 7+ 3 4 5 6 2 8? 2? 7+ 3+ 2 2 1 3 2? 8? 2 1 3+ 8? 2 3+ 8? 1 2 1 2 8 2 3 7 3 4 5 6 8+ 1? 7 3+ 4+ 5+ 6+ 7+ 8+ 8? 7? 6? 5? 4? 3? 8+ 3? A*: eventually figures out how to go around the wall, with some waste in each direction. But…compare to Dijkstras… 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 2 3 4 5 6 7 8 5 4 3 2 1 2 3 7 8 9? 4 3 2 1 1 2 8 5 4 3 2 1 2 3 7 8 9? 6 5 4 3 2 3 4 5 6 7 8 7 6 5 4 3 4 5 6 7 8 9? 8 7 6 5 4 5 6 7 8 9? 9? 9? For Comparison: What Dijkstra's Algorithm Would Have Searched Minimum Spanning Tree A spanning tree in an undirected graph is a set of edges with no cycles that connects all nodes. B 1 D 3 7 3 6 A C 3 E 3 How many distinct spanning trees are in this graph? A. 0-1 B. 2-3 C.4-5 F D. 6-7 E. >7 Edges: (A,B)=1 (A,C)=3 (B,C)=6 (B,D)=3 (C,E)=3 (D,E)=3 (D,F)=7 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 shortestpaths 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. B 1 D 3 7 3 6 A C 3 E 3 How many distinct MSTs are in this graph? A. 0-1 B. 2-3 C.4-5 F D. 6-7 E. >7 Edges: (A,B)=1 (A,C)=3 (B,C)=6 (B,D)=3 (C,E)=3 (D,E)=3 (D,F)=7 True or False? Having at least two edges with the same weight is SUFFICIENT for having more than one distinct minimum spanning tree. A. B. TRUE! FALSE! (i.e., “If at least two edges have the same weight, then there is more than one MST”) True or False? Having at least two edges with the same weight is NECESSARY for having more than one distinct minimum spanning tree. A. B. TRUE! FALSE! (i.e., “If there is more than one MST, then at least two edges have the same weight.”) 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 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 Kruskal’s algorithm 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 Prim’s algorithm The Good Will Hunting Problem “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) “homeomorphically irreducible” 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