Queue Project
Queue Project
Applications of Queues
Given that queues occur frequently in real life, it is not surprising that they are also
frequently used in simulations. To model customers using a bank, for example, you could
use a queue of waiting patrons. A simulation might want to ask questions such as the how
the average waiting time would change if you added or removed a teller position.
Queues are also used in any time collection where time of insertion is important. We have
noted, for example, that a printer might want to keep the collection of pending jobs in a
queue, so that they will be printed in the same order that they were submitted.
Imagine you are searching a maze, such as the one shown at right.
The goal of the search is to move from the square marked S, to the
square marked F.
S
To see the working of this algorithm in action, let us number of states of our maze from 1
to 25, as shown. There is only one cell reachable from the starting position, and thus after
the first step the queue contains only one element:
The solution is ultimately found in fifteen steps. The following shows the path to the
solution, with the cells numbered in the order in which they were
14 considered.
13 The strategy embodied in this code doggedly pursues a single path
12 11 10 until it either reaches a dead end or until the solution is found. When a
7 8 9 2 1 dead end is encountered, the most recent alternative path is
6 5 4 3 0
reactivated, and the search continues. This approach is called a depth-
first search, because it moves deeply into the structure before
examining alternatives. A depth-first search is the type of search a single individual might
perform in walking through a maze.
Suppose, on the other hand, that there were a group of people walking together. When a
choice of alternatives was encountered, the group might decide to split itself into smaller
groups, and explore each alternative simultaneously. In this fashion all potential paths are
investigated at the same time. Such a strategy is known as a breadth-first search.
As you might expect, a breadth-first search is more thorough, but may require more time
than a depth-first search. While the depth-first search was able to find the solution in 15
steps, the breadth-first search is still looking after 20. The
following shows the search at this point. Trace with your finger the 17 12 9 7
sequence of steps as they are visited. Notice how the search jumps all 18 13 4
over the maze, exploring a number of different alternatives at the
19 14 5 2
same time. Another way to imagine a breadth-first first is as what
15 10 3 1
would happen if ink were poured into the maze at the starting
location, and slowly permeates every path until the solution is 16 11 8 6 0
reached.
To create a maze-searching program you first need some way to represent the maze. A
simple approach is to use a two-dimensional integer array. The values in this array can
represent the type of room, as shown below. The values in this array can then tell you the
way in which it is legal to move. Positions in the maze can be represented by (I,j) pairs.
Use this technique to write a program that reads a maze description, and prints a sequence
of moves that are explored in either a depth-first or breadth-first search of the maze.
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15