Quiz 5

advertisement
CSCI 333 Fall 2004 Quiz 5
Name_____________________________
Section 1, Short Answer (5 pts each):
1. Describe how a backtracking algorithm works. When would you choose to use a
backtracking algorithm to solve a problem?
2. Trace the path of a depth first search on the tree below.
3. Explain how a Monte Carlo algorithm works. Why do we use a Monte Carlo
algorithm to estimate the time complexity of a backtracking algorithm?
4. The two high-level algorithms below use stack and queue ADTs. A queue ADT
provides a first-in-first-out (FIFO) ordering of its elements similar to a line at a movie
theater. A stack ADT, on the other hand, provides a last-in-first-out (LIFO) ordering of
its elements similar to a stack of trays at the cafeteria. Which of the algorithms below
performs a depth first search? Assume that both Stack_of_nodes and Queue_of_nodes
have global access and both are initialized to contain only the root node when mystery()
is first called.
void mystery1 () {
pop(Stack_of_nodes, v);
if ( promising(v) )
if (there is a solution at v)
write that solution
else
for (each child u of v)
push(Stack_of_nodes, u);
mystery1();
}
void mystery2 () {
dequeue(Queue_of_nodes, v);
if ( promising(v) )
if (there is a solution at v)
write that solution
else
for (each child u of v)
enqueue(Queue_of_nodes, u);
mystery2();
}
Section2: Problems (10 pts each)
1. Solving a Maze:
Write a high-level pseudo-code algorithm to solve the following problem using
backtracking.
9
0
Consider a maze, similar to the one in the diagram to
the left, where a player has to decide how to get from
room 0 to room 9, the max room number in the maze.
The player can move from room to room through the
corridors provided, but has no way of telling how
many corridors a room is connected to until he reaches
that room.
The maze can be thought of as a graph where the nine
rooms are nodes and corridors are edges. In this representation, adjacent nodes are rooms
that are directly connected by a corridor. A room is promising as long as it has not
already been visited. In writing your pseudo-code algorithm, assume that the adjacency
matrix representation of your maze is a global data structure.
Download