You are on page 1of 3

G51APS-E1

University of Nottingham
SCHOOL OF COMPUTER SCIENCE
AND INFORMATION TECHNOLOGY
A LEVEL 1 MODULE, AUTUMN SEMESTER 2006-2007
ALGORITHMIC PROBLEM SOLVING
(Course G51APS)
Solutions

Question 1:
a)

Assignment Expression Invariant?


n := n+5 n mod 4 no, 0 mod 4 6= 5 mod 4
n := n+2 even.n yes, even.n = even.(n+2)
m , n := m+2 , n+7 2×m − 7×n no, 2×0 − 7×0 6= 2×2 − 7×7
m , n := m+2 , n+7 14×m − 4×n yes, 14×m − 4×n = 14×(m+2) − 4×(n+7)
d , e := ¬e , ¬d d 6≡ e yes, (d 6≡ e) = (¬e 6≡ ¬d)

Table 1: Completed Entries

b) (This question is derived from the second coursework.)


Two strategies for taking the two slowest across are to take them seperately,
using the fastest to carry the torch, or to let them cross together, using the two
fastest to return the torch to the initial bank. In this case, it is better to
let them cross together. This gives the sequence: hp 1 ,p4 i, [p1 ] ,hp11 ,p12 i, [p4 ]
(Angle brackets indicate a crossing in one direction, square brackets a crossing in
the other direction.) After this sequence, just the two slowest have reached the
opposite bank, and the torch is at the initial bank. Now the two slowest are p 5
and p10 . For these, the optimal strategy is to let them cross separately, with the
fastest carrying the torch. This gives the sequence: hp 1 ,p10 i, [p1 ] ,hp1 ,p5 i, [p1 ]
The process is completed by p1 and p4 crossing together: hp1 ,p4 i .
The total time taken is 4+1+12+4+10+1+5+1+4 , i.e. 42 minutes.

G51APS-E1 Turn Over


2 G51APS-E1

Question 2:
a) See fig. 1 .

L L

W W W

L W W

W L L

W W W

Figure 1: Solution to 2(a)

0 1 2 3 4 5 6 7
b)
0 1 0 1 2 3 2 3
c)

G51APS-E1
3 G51APS-E1

Left Game Right Game “losing” or winning move


0 7 R5
3 0 L3
5 6 R5
7 9 L2
7 23 losing

Question 3 [Induction]
a) For n = 1 , it is clear that 0 comparisons are needed. For the induction step,
assume that n−1 comparisons are needed to find the lightest of n objects. To
find the lightest of n+1 objects, use n−1 comparisons to find the lightest of
n objects, then compare this object with the ( n+1 )th object. The lightest of
the two is the lightest of them all. Also, one extra comparison has been made,
making n in total.
b) For n = 2 , it is clear that 1 comparison is needed. For the induction step,
assume that 2n − 3 comparisons are needed to find the lightest and heaviest
of n objects. To find the lightest and heaviest of n+1 objects, use 2n − 3
comparisons to find the lightest and heaviest of n objects, then compare each
of these with the ( n+1 )th object. The lightest of the two is the lightest of
them all, and the heaviest of the two is the heaviest of them all. Also, two
extra comparisons have been made, making (2n − 3) + 2 , i.e. 2(n+1) − 3 in
total.
c) Compare A and C . The lightest of the two is the lightest of the four.
Compare B and D . The heaviest of the two is the heaviest of the four.
To weigh four objects, first compare A and B . Then, compare C and D .
d) For m = 1 , it is clear that 1 comparison is needed to find the lightest and
heaviest of 2 objects. And, 1 = 3×1 − 2 .
Consider now, that there are 2(m+1) objects. Select and compare any
two of the objects. Let the lightest be A and the heaviest B . By induction,
we can find the lightest and heaviest of the remaining 2m objects in 3m − 2
comparisons. Let these be C and D , respectively. We now have four objects,
A , B , C and D , such that A < B and C < D . By part (c), the lightest and
heaviest of these four can be found in 2 further comparisons. These are then
the lightest and heaviest of all 2(m+1) objects. And, the total number of
comparisons is 1 + (3m − 2) + 2 which equals 3(m+1) − 2 .

G51APS-E1 End

You might also like