Professional Documents
Culture Documents
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)
Question 2:
a) See fig. 1 .
L L
W W W
L W W
W L L
W W W
0 1 2 3 4 5 6 7
b)
0 1 0 1 2 3 2 3
c)
G51APS-E1
3 G51APS-E1
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