Graph Searching, Reachability

Download Report

Transcript Graph Searching, Reachability

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
month? 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
Essense 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 old, toss out,
• If it is new, add to destinations, put
neighbors into queue
What about Weighted Graphs?
Pierre
2
Pendleton
Peoria
5
3
3
Pittsburgh
8
Pueblo
4
2
10
4
4
Princeton
Dijkstra’s algorithm.
Use a priority queue instead
of a stack. Return a map of
city, distance pairs. Pqueue
orders values on shortest
distance
3
Phoenix
5
Pensacola
Dijkstra (String startCity, Map[String, Map[String, double]] distances)
Make empty map of distances into variable reachable
Put (StartingCity, 0) into Pqueue
While 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 city
When done with loop, return reachable map
Example: What is the distance
from Pierre
Pierre
2
3
Pendleton
Peoria
3
Pittsburgh
8
Pueblo
4
-----------Pierre: 0
5
2
10
4
4
Princeton
3
Phoenix
5
Pensacola
Dijkstra (String startCity, Map[String, Map[String, double]] distances)
Make empty map of distances into variable reachable
Put (StartingCity, 0) into Pqueue
While 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 city
When done with loop, return reachable map
Example: What is the distance
from Pierre
Pierre
2
Pendleton
Pierre: 0
-----------Pendeleton: 2
Peoria
5
3
3
Pittsburgh
8
Pueblo
4
2
10
4
4
Princeton
3
Phoenix
5
Pensacola
Dijkstra (String startCity, Map[String, Map[String, double]] distances)
Make empty map of distances into variable reachable
Put (StartingCity, 0) into Pqueue
While 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 city
When done with loop, return reachable map
Example: What is the distance
from Pierre
Pierre
2
Pendleton
Pierre: 0, Pendleton: 2
-----------Phoenix: 6, Pueblo: 10
Peoria
5
3
3
Pittsburgh
8
Pueblo
4
2
10
4
4
Princeton
Notice how the distances
have been added
3
Phoenix
5
Pensacola
Dijkstra (String startCity, Map[String, Map[String, double]] distances)
Make empty map of distances into variable reachable
Put (StartingCity, 0) into Pqueue
While 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 city
When done with loop, return reachable map
Example: What is the distance
from Pierre
Pierre
2
Pendleton
Peoria
5
3
3
Pittsburgh
8
Pueblo
4
2
10
4
4
3
Phoenix
5
Pensacola
Princeton
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
Dijkstra (String startCity, Map[String, Map[String, double]] distances)
Make empty map of distances into variable reachable
Put (StartingCity, 0) into Pqueue
While 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 city
When done with loop, return reachable map
Example: What is the distance
from Pierre
Pierre
2
Pendleton
Peoria
5
3
3
Pittsburgh
8
Pueblo
4
2
10
4
4
3
Phoenix
5
Pensacola
Princeton
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
Dijkstra (String startCity, Map[String, Map[String, double]] distances)
Make empty map of distances into variable reachable
Put (StartingCity, 0) into Pqueue
While 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 city
When done with loop, return reachable map
Example: What is the distance
from Pierre
Pierre
2
Pendleton
Peoria
5
3
3
Pittsburgh
8
Pueblo
4
2
10
4
4
3
Phoenix
5
Pensacola
Princeton
Pierre: 0, Pendleton: 2,
Phoenix: 6, Pueblo: 9,
Peoria: 10
-----------Pueblo: 10, Pierre: 13,
Pueblo: 13, Pittsburgh: 15,
Pittsburgh: 16
Duplicates only removed
when pulled out of queue
Dijkstra (String startCity, Map[String, Map[String, double]] distances)
Make empty map of distances into variable reachable
Put (StartingCity, 0) into Pqueue
While 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 city
When 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