You are on page 1of 4

Data Structures, Homework 6, Solutions

1. Carrano, Chapter 5, Self-Test Exercise 1: Consider a Four Queens Prob-


lem, which has the same rules as the Eight Queens problem but uses a 4 by
4 board. Find all solutions to this new problem by applying backtracking
by hand. Show your work.
Solution: The possible answers are

. Q . . . Q . .
Q . . . . . . Q
. . . Q Q . . .
. Q . . . . Q .

To find these answers, we apply our algorithm to place queens in each


column in succession, starting at the top row in each column and back-
tracking when necessary. So here are some partial steps
Q . . .
. . . .
. Q . .
. . . .
There is no open slot in the third column, so we backtrack to the second
column, place a queen in the unguarded slot in the third column to find
Q . . .
. . Q .
. . . .
. Q . .
Now there is no spot available in the last column, and no other spot in
the next-to-last. Since we are at the bottom row in the 2nd column, we
backtrack to the first row. Then we fill in to find the solution
. . Q .
Q . . .
. . . Q
. Q . .

To find more solutions, we backtrack from this one: its easy to see there
are no more possibilities by considering columns 2,3,4. So we backtrack
to the first column and fill in so
. Q . .
. . . Q
Q . . .
. Q . .
This is a solution. Its also clear in this case that any backtracking to find
new solutions must go all the way back to the first column. Then we have
possibilities
. Q . .
. . . .
. . Q .
Q . . .
Now we must backtrack to the second column to find
. . . .
. Q . .
. . . .
Q . . .

This doesnt lead to a solution and there are no more ways to backtrack.
Thus we have found all the solutions to the Four Queens Problem.
2. Carrano, Chapter 6, Exercise 14: Execute the HPAir algorithm with the
map in Figure 6-17 for the following requests. Show the state of the stack
after each step.

In the solutions below, we use the HPAir algorithm by visiting cities from
a given one in alphabetical order.

(a) Fly from A to F .


Solution: Successful route found: A,B,D,F. Here is the stack:
C
I I I
E E E E E F
D D D D D D D D
B B B B B B B B B
A A A A A A A A A A
Note we must backtrack at C because there are no unvisited cities
available from C.
(b) Fly from D to A.
Solution: No route found: Here is the stack:
G
H H H
B B B B B
C C C C C C C
I I I I I I I I I
E E E E E E E E E E E F
D D D D D D D D D D D D D D D
Since the algorithm ends with the empty stack, there is no route.
(c) Fly from A to G.
Solution: Route found: A,B,D,F,G. Here is the stack:
C
I I I G
E E E E E F F
D D D D D D D D D
B B B B B B B B B B
A A A A A A A A A A A
(d) Fly from I to G.
Solution: Route found: I,C,B,D,F,G. Here is the stack:
G
E F F
D D D D D
B B B B B B
C C C C C C C
I I I I I I I I
(e) Fly from F to H.
Solution: Route found: F,G,C,B,D,H. Here is the stack:
I
E E E H
D D D D D D
B B B B B B B
C C C C C C C C
G G G G G G G G G
F F F F F F F F F F
3. If you add the integers 2,4,6,8 in sequence to a queue and then remove
them, in what order will they be deleted from the queue? See e.g. Self-Test
Exercise 1, Chapter 7, Carrano, for a similar problem.
Solution: 2,4,6,8. First in, first out
4. What do the initially empty queues queue1 and queue2 look like after
the following sequence of operations?
queue1.enqueue(3)
queue1.enqueue(2)
queue2.enqueue(9)
queue2.enqueue(4)
queue1.dequeue()
queue2.getFront(queueFront)
queue1.enqueue(queueFront)
queue1.enqueue(7)
queue2.dequeue(x)
queue2.enqueue(x)

See e.g. Self-Test Exercise 2, Chapter 7, 4th edition of Carrano, for a


similar problem.
Solution:
queue1 queue2 queueFront x
queue1.enqueue(3) 3
queue1.enqueue(2) 3,2
queue2.enqueue(9) 3,2 9
queue2.enqueue(4) 3,2 9,4
queue1.dequeue() 2 9,4
queue2.getFront(queueFront) 2 9,4 9
queue1.enqueue(queueFront) 2,9 9,4 9
queue1.enqueue(7) 2,9,7 9,4 9
queue2.dequeue(x) 2,9,7 4 9 9
queue2.enqueue(x) 2,9,7 4,9 9 9
5. Carrano, Chapter 7, Exercise 3 (slightly corrected)
What is the output of the following pseudocode, where num1, num2, and
num3 are integer variables?

num1 = 5
num2 = 1
num3 = 4
aQueue.enqueue(num2)
aQueue.enqueue(num3)
aQueue.dequeue()
aQueue.enqueue(num1 - num2)
aQueue.dequeue(num1)
aQueue.dequeue(num2)
cout << num2 << " " << num1 << " " << num3 << endl

Solution:

4 4 4

You might also like