Searching for Beepers

Download Report

Transcript Searching for Beepers

Searching in BeeperHunt
(Mostly minor variations on old slides)
26-Jul-16
Shortest path?


In the BeeperHunt assignment, there are two
“beepers” at all times
Your robot has to choose which beeper to go after




This may or may not be the closest beeper
In any case, once your robot has chosen a beeper, it
probably wants to find the shortest path to that beeper
The maze is multiply-connected, that is, it’s a graph
We have discussed two ways of finding a shortest path
in a graph


Breadth-first search
Dijkstra’s algorithm
Breadth-first searching (in a tree)

A
B
C

D
E
F
H
L
M
I
N
O
G
J
P
K
Q

A breadth-first search (BFS)
explores nodes nearest the root
before exploring nodes further
away
For example, after searching A,
then B, then C, the search
proceeds with D, E, F, G
Node are explored in the order
ABCDEFGHIJKLMNO
PQ

J will be found before N
How to do breadth-first searching





Put the root node on a queue;
while (queue is not empty) {
remove a node from the queue;
if (node is a goal node) return success;
put all children of node onto the queue;
}
return failure;
Just before starting to explore level n, the queue holds all the
nodes at level n-1
In a typical tree, the number of nodes at each level increases
exponentially with the depth
In BeeperHunt, however, it will always be less than the
number of cells in the maze—so this will not be a problem
Breadth-first search does not automatically give the path to the
goal—but that can be fixed
Searching a graph

With certain modifications, any tree search technique
can be applied to a graph


The difference is that a graph may have cycles



We don’t want to search around and around in a cycle
To avoid getting caught in a cycle, we must keep track
of which nodes we have already explored
There are two basic techniques for this:



This includes depth-first, breadth-first, depth-first iterative
deepening, and other types of searches
Keep a set of already explored nodes, or
Mark the node itself as having been explored
Either approach would work fine for BeeperHunt
Dijkstra’s algorithm

Dijkstra’s algorithm is designed to find the shortest path
in a graph when the edges have unequal costs




In a maze, an “edge” is a single step between adjacent maze
locations
Every step has the same cost
Dijkstra’s algorithm would work, but it’s more complicated
than we need
However, there is one idea in Dijkstra’s algorithm that
we can use:


Each time we find a (new shortest) path to a node, we set that
node to point to it’s predecessor (where we just came from)
This trick allows us to reconstruct our path
The End