cs 261 reachability problems. the classic reachability algorithm findreachable (graph g, vertex...
TRANSCRIPT
CS 261
Reachability Problems
The classic Reachability algorithm
findReachable (graph g, vertex start) { create a set of reachable vertices, initially empty. call this r. create a container for vertices known to be reachable. call this c add start vertex to container c while the container c is not empty { remove first entry from the container c, assign to v if v is not already in the set of reachable vertices r { add v to the reachable set r add the neighbors of v to the container c } } return r}
Different containers different algorithms
• What is interesting about this algorithm is that if you use different containers
• You get different algorithms
Example Solving a Maze
Represented as a graph
Use a Stack: { 1 }
Use a Stack: { 2, 6 }
Use a Stack: { 7, 3, 6 }
Use a Stack: { 3, 6 } dead end
Try alternative: { 4, 8, 6 }
Keep plowing on: { 5, 9, 8, 6 }
Keep on: { 10, 9, 8, 6 }
Keep on: { 15, 9, 8, 6 }
Keep on: { 14, 20, 9, 8, 6 }
Note duplicate: { 9, 20, 9, 8, 6 }
Opps, 4 again: { 4, 20, 9, 8, 6 }
Pop, try alternative: { 20, 9, 8, 6 }
Keep going: { 19, 9, 8, 6 }
And going: { 24, 9, 8, 6 }
Finished: { 25, 9, 8, 6 }
Depth first search
• Keep going in one direction as long as possible
• When you get stuck, back up to last choice, try alternative
• Did anybody do a Corn Maze last fall? Probably used this technique.
What if we use a queue?
Initial queue: {1}
First neighbors: {2, 6}
Neighbors of 2: {6, 3, 7}
Neighbors of 6: {3, 7, 11}
Neighbors of 3: {7, 11, 4, 8}
Neighbors of 7: {11, 4, 8}
Neighbors of 11: {4, 8, 12, 16}
Neighbors of 4: {8, 12, 16, 5, 9}
Breadth-first search
• Notice how this search jumps all over the place
• It’s - you go that way, I’ll go this way (but with a very large group of friends)
• Or think about spilling ink at the start, and watching it seep through the entire maze
Questions you can ask
• Which is faster?
• Which is guaranteed to find a solution?
• What if the graph is infinite, but there is a finite solution - which is guaranteed to find a solution?
But there is more!
• If we have a weighted graph, and use a priority queue - then the same technique is called Dijkstra’s algorithm
• Idea: queue keeps shortest distance from some place you have been to someplace you might not have been
Essence of Dijkstra’s algorithm!
• Idea: queue keeps shortest distance from some place you have been to someplace you might not have been
• When you pull an item from the queue, if it is someplace you have seen, toss it out,
• If it is new, add to destinations, put neighbors into queue
What about Weighted Graphs?
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Dijkstra’s algorithm.Use a priority queue instead of a stack. Return a map of city, distance pairs. Pqueue orders values on shortest distance
2
4
3
3
4
3
5
410
8
5
2
Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map
Example: What is the distance from Pierre
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
------------Pierre: 0
2
4
3
3
4
3
5
410
8
5
2
Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map
Example: What is the distance from Pierre
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Pierre: 0------------ Pendeleton: 2
2
4
3
3
4
3
5
410
8
5
2
Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map
Example: What is the distance from Pierre
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Pierre: 0, Pendleton: 2------------Phoenix: 6, Pueblo: 10
Notice how the distances have been added
2
4
3
3
4
3
5
410
8
5
2
Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map
Example: What is the distance from Pierre
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Pierre: 0, Pendleton: 2, Phoenix: 6------------Pueblo: 9, Peoria: 10, Pueblo: 10, Pittsburgh: 16
Notice how values are stored in the Pqueue in distance order
2
4
3
3
4
3
5
410
8
5
2
Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map
Example: What is the distance from Pierre
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Pierre: 0, Pendleton: 2, Phoenix: 6, Pueblo: 9------------Peoria: 10, Pueblo: 10, Pierre: 13, Pittsburgh: 16
Pierre gets put in queue, although it is known to be reachable
2
4
3
3
4
3
5
410
8
5
2
Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map
Example: What is the distance from Pierre
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Pierre: 0, Pendleton: 2, Phoenix: 6, Pueblo: 9, Peoria: 10------------Pueblo: 10, Pierre: 13, Pueblo: 13, Pittsburgh: 15, Pittsburgh: 16Duplicates only removed when pulled out of queue
2
4
3
3
4
3
5
410
8
5
2
Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map
Depth first reachability, Breadth first Reachability,
Dijkstras Algorithm• All three are essentially the same
algorithm
• The only difference is the type of container they use
Bottom line
• So the key idea from this course is
• Selecting appropriate data structures is the key to programming
• You need to know what tools are available to make the best choices
• And with that, we end this course