Algorithms Detour - Shortest Path Lesson 2-7 AP Computer Science Principles
Objectives Students will be able to: Reason about a general solution (algorithm) for a problem while examining specific instances of it. Follow a pseudocode algorithm for the single source shortest path problem. Informally analyze an algorithm to state how “long” it takes to run relative to the size of the problem.
Minimal Spanning Tree vs. Shortest Path Last time you developed an algorithm for the Minimum Spanning Tree (MST) problem. The MST is useful for knowing the most cost- effective way to build or connect a network together. But today we’re going to look at a different problem -- the “Shortest Path Problem” -- which is also a widely studied problem in computer science.
Shortest Path Problem Given a graph of nodes and edges, where the weights of the edges represent some cost (distance, time, money, etc.), find the lowest-cost path between any two nodes.
Shortest Path Problem Typically, we think of the shortest path problem like a map with roads between towns or different points in a big city and we’re trying to find the shortest/fastest way to get from one place to another. This is what map apps that give you driving directions do. But the shortest path problem doesn’t have to represent traveling anywhere at all. You could use a graph of a social network to figure out how closely connected you are to some other person, for example, how many friend connections separate me from the president of the United States?
Shortest Path Wroksheet Look at the first chart. What is the shortest path from A→C? Just because A is connected to C doesn’t mean that is the shortest path!
Shortest Path Worksheet Look at the most direct path: A→C has a cost of 7 Look at other connections A→B→C has a cost of 5 Consider other options A→B→D→C has a cost of 6 A→B→D→E→C has a cost of 6
Your turn… Find the shortest path from A→C on each of the remaining charts. ...but you should be thinking... Remember: in computer science, “solving a problem” doesn’t mean finding an answer to an instance of a problem; it means finding an algorithm that might be able to solve any instance of that problem. As you look at the graphs on the next page, your brain is working to find a solution. You might think you’re just trying “random stuff” but you’re not. You are using your human intelligence to help you. Think about your own thinking process. Could you express what you are thinking to solve this problem as an algorithm?
Your turn… Now that you have the shortest path from A→C, go back to each chart and find the shortest paths from: A→B A→D A→E
What’s Your Algorithm? What algorithm did you use to find the shortest paths? What makes the algorithm harder/easier than minimum spanning tree?
Shorest Path and Routers The reason we have routers is because we want to send messages from our router to lots and lots of different locations. So a more interesting problem on the Internet is finding not just the path from my router to one other router, but the path from my router to EVERY other router! This is known as the Single Source Shortest Path Problem (SSSP). Today we’re going to look at and analyze a famous algorithm that solves this problem.
Dijkstra’s Shortest Path Algorithm You team will be given the algorithm and a graph. Together you will act as the computer, interpreting the instructions and trying to trace out the algorithm and follow its steps. Part of analyzing an algorithm is trying it out on many different inputs. So, each group will have a different node to start with on the graph and we will test the algorithm to see if it works and then see what we think about it, in terms of its correctness and efficiency.
Compare Your Results Find your corresponding group Graph A with Graph E Graph B with Graph F Graph C with Graph H Graph D with Graph G
Compare Your Results As a group: Compare the shortest path diagrams; these form a tree extending from the source node. Are the shortest path trees from two different sources the same? What about the path between the two source nodes? If so, why? If not, why not? Based on your experience, would this algorithm find the shortest path for any graph of nodes and edges? Is there a way to stop early? That is, is there a way to stop processing the algorithm because you know you’ve found the shortest path tree? Can you guarantee that you could always stop early? Why or why not?
Stage 7 Complete Stage 7