280 likes | 402 Views
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.
E N D
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
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. Update v's priority in the priority queue to d + L. 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. Update v's priority in the priority queue to d + L + h(v, t). A* Search
A* on two points where the heuristic is slightly misleading due to a wall blocking the way
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 + 6? 2 + 5? 1 + 6? 1 2 + 3? 2 + 5? 1 + 6? A*: enqueue neighbors.
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.
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.
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…
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
A spanning tree in an undirected graph is a set of edges with no cycles that connects all nodes.
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
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.
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
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”)
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.”)
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
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
“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