Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
n) + 1
Substituting m = lg n gives :
T(2
m
) = T(2
m/2
) + 1
Let S(m) = T(2
m
). This gives the recurrence :
S(m) = S(m/2) + 1
By expansion or by the Master Theorem, it is easy to see that the solution of the above
recurrence is S(m) = (lg m). Changing back to n, we get :
T(n) = S(m) = (lg m) = (lg lg n)
Thus, T(n) = (lg lg n).
1.5 e
T(n) = T(n 1) +
1
n
By expansion, we have :
T(n) =
1
n
+
1
n 1
+ +
1
1
We know that the summation on the left is the nth Harmonic number, H
n
and that H
n
=
(lg n). Thus,
T(n) = (lg n)
1.6 f
T(n) = 4T(n 2) + 1
By expansion, we have :
T(n) = 1 + 4 + 4
2
+ + 4
n/2
=
4
n/2+1
1
4 1
Thus, we have T(n) = (4
n/2
) = (2
n
).
2
2 Problem 2
2.1 a
Let us assume that there are x good chips, where 2 x n. We divide the chips in three
groups. The rst group A consists of all the x good chips, the second group B consists of
any x bad chips, and the third group C consists of the remaining n 2 x bad chips. We
assume that the chips of group B mimic the corresponding chips in group A. More precisely,
any test carried out will have one of the following results :
A chip of group A is tested against another chip of group A. In this case, both the
chips mark the other one as good.
A chip of group A is tested against a chip of group B. In this case, both the chips
mark the other one as bad.
A chip of group B is tested against another chip of group B. In this case, both the
chips mark each other as good.
A chip from either group A or B is tested against another chip of group C. In this
case, both chips mark each other as bad.
Clearly, a chip in group A (a good chip) is indistinguishable from a chip in group B (a
bad chip). Given the results of the tests, it might as well be possible that the chips in group
B were good, and those in A were bad. Hence, it is not possible for an algorithm to surely
pinpoint a good chip.
Note that this argument fails when 2 x > n, since the good group cannot be mimicked
by a bad group of the same size.
2.2 b
The idea behind the algorithm is to test pairs of chips. We discard both chips as long as we
are sure that there is at least one bad chip amongst the pair discarded, or we discard any
one chip if it might be the case that both chips are good. More precisely, the algorithm is
as follows :
3
TESTCHIP()
1 while n > 1
2 make n/2 pairs for comparision with each other.
3 Leave the single chip(if n is odd) alone
4 for each pair {chip X, chip Y }
5 if x says y is good, and y says x is good, then discard one of the chips and
6 retain the other.
7 else discard both chips
8 if n is odd and the number of chips retained is odd, discard the unpaired chip
9 and retain it otherwise.
10 n = Number of retained chips.
11 return the remaining chip
We prove that the algorithm correctly returns a good chip if there are more than n/2
good chips at the start.
Proof. We use the loop invariant that the number of good chips at the start of any iteration
in line 1 is always more than n/2.
Initialization: By our assumption, we know that the invariant holds at the start.
Maintenance: It is easy to see that whenever we discard both the chips in a tested pair
in line 7, at least one of the chips is bad. Hence, after discarding both chips in all such
pairs, the number of good chips is still more than half the number of remaining chips. Out
of the remaining pairs of chips where both chips test the other one as good, let the number
of pairs where both chips are actually good be denoted by X, and those where both chips
are actually bad be denoted by Y .
Now we consider the following cases:
n is even. Clearly, X > Y , since otherwise, the number of bad chips would be at least
half the total number of chips. In this case, the number of good chips retained is X
whereas the number of bad chips retained is Y . Hence the invariant is maintained.
n is odd and (X + Y ) is odd. If Y > X, then even if the unpaired chip is good we
have at least half bad chips, which is a contradiction. Thus, we have X > Y . Since we
discard the unpaired chip, the invariant is maintained.
n is odd and (X +Y ) is even. Consider the following cases:
X < Y . This is not possible since in this case the number of bad chips is at least
half the total number of chips.
4
X = Y . Here, the unpaired chip must be good. Thus, the invariant will be
maintained since we include it.
X = Y + 1. This is not possible since X +Y is even.
If X > Y + 1 then irrespective of the type of the unpaired chip, the majority of
good chips is maintained.
Termination: On termination, there is clearly only one chip remaining which is good.
The other good chips can be identied by testing all other chips against this good chip.
There is an alternative solution. The idea behind the algorithm is to maintain groups of
chips, where we are guaranteed that all chips in the same group are of the same type (all
good, or all bad), and that the total number of good chips is strictly more than half the total
number of chips. Since all chips within a group are of the same type, a more precise measure
of the size of the problem state is the number of groups, rather than the actual number of
total chips. We will show that with n/2 pairwise tests, we can can halve the problem size.
The algorithm is given below :
TESTCHIP2()
1 Initialize n groups each containing 1 chip
2 groups = n
3 while groups > 1
4 make groups/2 pairs for comparision with each other.
5 Leave the single group(if groups is odd) alone
6 for each pair {group X, group Y }
7 pick one chip x from X and another chip y from Y and compare them
8 if x says y is good, and y says x is good, then merge the groups together
9 else if x says y is bad and y says x is good (or vice versa), discard the group saying good
10 else if x says y is bad and y says x is bad, throw as many chips from the bigger group
11 as the ones in the smaller group, and discard the smaller group. Discard the other
12 bigger group if it is now empty
13 groups = groups/2
14 return any chip from the only remaining group
We prove that the algorithm correctly returns a good chip if there are more than n/2
good chips at the start.
Proof. We use the following loop invariants that we mentioned above:
5
Each group consists of chips of the same type
The total number of good chips is strictly more than half the total number of chips
Initialization:
Initially each group contains a single good chip of a bad chip
We know that the number of good chips initially is more than half the total number
of chips
Maintenance:
A group is merged only in line 8. We know that both chips call each other good if and
only if both are good, or both are bad. Thus the merged group contains the same type
of chips
In line 9, we will surely discard only a bad group. The discarded group marks the
other group as good, and the other group marks the discarded group as bad. This
results in a contradiction if the discarded group was good. In line 10, an equal number
of chips from both groups are discarded, thus maintaining the majority for the good
chips. This is because at least one out of the two concerned groups have to be bad
since both groups are marked as bad by the other group.
Termination: On termination, there is clearly only one group remaining which has chips
of the same type. These have to be good chips, since good chips are always present in
majority.
The above algorithm clearly makes n/2 comparisons per iteration of the loop in line
3, and the number of groups after one iteration halves. As mentioned above, the number of
groups is a more precise measure of the size of the problem.
2.3 c
The above algorithm nds a good chip in O(n) time.
Proof. We have the following recurrence :
T(n) = T(n/2) + n/2
By expansion, we get T(n) = n/2 +n/4 + +n/2
k
, where 2
k
is the biggest power of 2
which is at most n. Thus we get T(n) = n(1/2 + 1/4 + + 1/2
k
). The right hand since is
clearly at most n, and thus T(n) = O(n).
6
Once we nd this good chip, we can nd all the good chips by testing the good chip
against every other chip. This needs another n 1 tests, and the total number of tests used
to identify all good chips is O(n).
7
Problem 3:
(i) What is the probability that the guess in the ith step is correct?
(ii) Show that the expected total number of points that you gain using the random strategy is (logn)
Solution:
Probability that the guess in the ith step is correct :
1
(ni+1)
At the rst step, any ordering is possible, so the probability that the guess is correct on the rst step is
1
n
.
At the second step, we know that the element cannot be the one that was picked in the earlier step since all
the cards are distinct, so there are only n 1 elements to choose from. Hence the probability of making a
correct guess in this step is
1
(n1)
. Similarly, the probability of making a correct guess on the third step is
1
(n2)
, fourth step is
1
(n3)
and so on. Thus probability of making a correct guess on the ith step is
1
(ni+1)
.
We can calculate the expected number of total points that one can gain using the random strategy by
setting up an indicator random variable X
i
for the ith guess:
X
i
=
i=1
X
i
]
=
n
i=1
E[X
i
]
=
n
i=1
1. Pr(guess is correct) + 0. Pr(guess is incorrect)
=
n
i=1
1.
1
n i + 1
= 1 +
1
2
+
1
3
+ ... +
1
n 2
+
1
n 1
+
1
n
= (logn)
The nal summation is the harmonic series, and it is known that it is (logn)
1
Problem 4
1. The Randomized QuickSort algorithm as given in the textbook is
PARTITION(A, p, r)
1 x A[r]
2 i p - 1
3 for j p to r - 1
4 do if A[j] x
5 then i i + 1
6 exchange A[i] A[j]
7 exchange A[i + 1] A[r]
8 return i + 1
RANDOMIZED-PARTITION(A, p, r)
1 i RANDOM(p, r)
2 exchange A[r] A[i]
3 return PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, r)
1 if p < r
2 then q RANDOMIZED-PARTITION(A, p, r)
3 RANDOMIZED-QUICKSORT(A, p, q - 1)
4 RANDOMIZED-QUICKSORT(A, q + 1, r)
When all elements in the array A are equal, we can observe that in any call to PARTITION,
A|]] = x, v p ] < r and hence the pivot is always placed at A|r]. Thus, the right partition is
always empty and the left partition contains r -p elements. The running time for any input is
independent of the random choices made and is given by:
I(n) = I(n -1) +0(n) = 0(n
2
)
And the expected running time is:
I
is compaieu to z
]
uuiing an execution of RANB0NIZEB -Q0ICKS0RTS ]
And,
X
]
= I { z
is compaieu to z
]
uuiing an execution of RANB0NIZEB -Q0ICKS0RT ]
Then,
E|X
]
i
] = Pi _
z
_ +
Pi _
z
]
is the fiist pivot chosen fiom Z
]
anu no othei pivot
pieviously chosen has the same value as z
]
_
Pi|z
. Let z
, such that z
< z. Let
w = |y
m
y
. Let us
consider all the possible relations between y
m
and y
:
1. y
> y
m
: We know from our strategy S that there are at least k wells, k n/2, with
y-coordinates y
i
y
m
. All these oil wells are distance w farther away from the main
pipeline in S
wells, k
n/2
with y-coordinates y
i
> y
m
. These wells are distance w nearer to the main pipeline in
S
z = w(k k
). As k k
, we get z
z 0 or z
z,
a contradiction.
2. y
< y
m
: We know from our strategy S that there are at least k wells, k n/2, with
y-coordinates y
i
y
m
. All these oil wells are distance w farther away from the main
pipeline in S
wells, k
n/2
with y-coordinates y
i
< y
m
. These wells are distance w nearer to the main pipeline in
S
z = w(k k
) 0 or z
z, a contradiction.
3. If y
= y
m
then z
= z, again a contradiction.
Hence our strategy S is optimal.
1