Clrs Solution Collection
Clrs Solution Collection
Clrs Solution Collection
CMPSC 465
Exercise 2.1-3
Here, we work with the linear search, specified as follows:
LINEAR-SEARCH(A, v)
Input: A = <a1, a, , an>; and a value v.
Output: index i if there exists an i in 1..n s.t. v = A[i]; NIL, otherwise.
Maintenance:
For i Z s.t. 1 i A.length, consider iteration i. By the loop invariant, at the start of iteration i, A[1..i-1] doesnt
contain v. The loop body is only executed when A[i] is not v and we have not exceeded A.length. So, when the ith
iteration ends, A[1..i] will not contain value v. Put differently, at the start of the (i+1)st iteration, A[1..i-1] will once again
not contain value v.
Termination:
There are two possible ways the loop terminates:
If there exists an index i such that A[i] == v, then the while loop will terminate at the end of the ith iteration.
The loop invariant says A[1..i-1] doesnt contain v, which is true. And, in this case, i will not reach A.length + 1,
so the algorithm returns i s.t. A[i] = v, which is correct.
Otherwise, the loop terminates when i = n + 1 (where n = A.length), which implies n = i - 1. By the loop
invariant, A[1..i-1] is the entire array A[1..n], and it doesnt contain value v, so NIL will correctly be returned by
the algorithm.
Note: Remember a few things from intro programming and from Epp:
Remember to think about which kind of loop to use for a problem. We dont know how many iterations the linear search loop will run until its
done, so we should use an indeterminate loop structure. (If we do, the proof is cleaner.)
As noted in Epp, the only way to get out of a loop should be by having the loop test fail (or, in the for case, the counter reach the end). Dont
return or break out of a loop; proving the maintenance step becomes very tricky if you do.
3 9 26 38 41 49 52 57
3 26 41 52 9 38 49 57
3 41 26 52 38 57 9 49
3 41 52 26 38 57 9 49
The algorithm consists of merging pairs of 1-item sequence to form sorted sequences of length 2, merging pairs of sequences
of length 2 to form sorted sequences of length 4, and so on, until two sequences of length n/2 are merged to form the final
sorted sequence of length n.
Exercise 4.4-1
The recurrence is T(n) = 3T( ) + n. We use a recurrence tree to determine the asymptotic upper bound on this recurrence.
Because we know that floors and ceilings usually do not matter when solving recurrences, we create a recurrence tree for the
recurrence T(n) = 3T(n/2) + n. For convenience, we assume that n is an exact power of 2 so that all subproblem sizes are
integers.
n n
lgn
n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 (3/2)2n
.
.
.
. . .
T(1) T(1) T(1) T(1) T(1) T(1)
Total: O( )
Because subproblem sizes decrease by a factor of 2 each time when go down one level, we eventually must reach a boundary
condition T(1). To determine the depth of the tree, we find that the subproblem size for a node at depth i is n/2i. Thus, the
subproblem size hits n = 1 when n/2i = 1 or, equivalently, when i = lgn. Thus, the tree has lgn + 1 levels (at depth 0, 1, 2, 3,
, lgn).
Next we determine the cost at each level of the tree. Each level has 3 times more nodes than the level above, and so the
number of nodes at depth i is 3i. Because subproblem sizes reduce by a factor of 2 for each level when go down from the
root, each node at depth i, for i = 0, 1, 2, 3, , lgn 1, has a cost of n/2i. Multiplying, we see that the total cost over all
nodes at depth i, for i = 0, 1, 2, 3, , lgn 1, is 3i * n/2i = (3/2)in. The bottom level, at depth lgn, has = nodes, each
contributing cost T(1), for a total cost of T(1), which is ( ), since we assume that T(1) is a constant.
= + by using Sigma to sum all the elements except the last one
Thus, we have derived a guess of T(n) = O for our original recurrence T(n) = 3T( ) + n. Now we can use the
inductive proof to verify that our guess is correct.
To prove:
For all integers n s.t. n 1, the property P(n):
The closed form T(n) , for some constants d and c s.t. d > 0 and c > 0,
matches the recurrence T(n) = 3T( ) + n.
Proof:
We will reason with strong induction.
Since were only proving a bound and not an exact running time, we dont need to worry about a base case.
Inductive Step:
Let k Z s.t. k 1 and assume i Z s.t. 1 i k, P(i) is true. i.e. T(i) matches the recurrence.
[inductive hypothesis]
Consider T(k+1):
T(k + 1) = 3T( )+k+1 by using the recurrence definition (as k 1 implies
k + 1 2, so we are in the recursive case)
3d (k +1) + k + 1 by subs. from inductive hypothesis,
(k + 1)/2, and d c + 1
(k +1) + k + 1 by laws of exp.
(k +1) + k + 1 by laws of exp. and log.
(k +1) as long as c 2, (k +1) (k + 1) (k +1)
So P(k + 1) is true.
So, by the principle of strong mathematical induction, P(n) is true for all integers n s.t. n 1, and constant d = 3, c = 2.
Solution:
For this recurrence, we have a = 2, b = 4, f (n) = 1, and thus we have that = . Since f (n) = 1 = O( ),
where = 0.2, we can apply case 1 of the master theorem and conclude that the solution is T(n) = ( ) = ( ) =
( ).
b) Use the master method to give tight asymptotic bounds for the recurrence T(n) = 2T(n/4) + .
Solution:
For this recurrence, we have a = 2, b = 4, f (n) = , and thus we have that = = = . Since f (n) = ( ),
we can apply case 2 of the master theorem and conclude that the solution is T(n) = ( ).
c) Use the master method to give tight asymptotic bounds for the recurrence T(n) = 2T(n/4) + n.
Solution:
For this recurrence, we have a = 2, b = 4, f (n) = n, and thus we have that = . Since f (n) = ( ) , where
, we can apply case 3 of the master theorem if we can show that the regularity condition holds for f (n).
To show the regularity condition, we need to prove that af (n/b) cf (n) for some constant c < 1 and all sufficiently large
n. If we can prove this, we can conclude that T(n) = (f (n)) by case 3 of the master theorem.
d) Use the master method to give tight asymptotic bounds for the recurrence T(n) = 2T(n/4) + n2.
Solution:
For this recurrence, we have a = 2, b = 4, f (n) = n2, and thus we have that = . Since f (n) =( ) , where
, we can apply case 3 of the master theorem if we can show that the regularity condition holds for f (n).
Exercise 6.1-1
Problem: What are the minimum and maximum numbers of elements in a heap of height h?
Since a heap is an almost-complete binary tree (complete at all levels except possibly the lowest), it has at most
1+2+22+23++2h=2h+1-1 elements (if it is complete) and at least 2h-1+1=2h elements (if the lowest level has just 1 element
and the other levels are complete).
Exercise 6.1-3
Problem: Show that in any subtree of a max-heap, the root of the subtree contains the largest value occurring anywhere in
that subtree.
To prove:
For any subtree rooted at node k of a max-heap A[1, 2, ... , n], the property P(k):
The node k of the subtree rooted at k contains the largest value occurring anywhere in that subtree.
Proof:
Base Case:
When k n / 2 + 1, n , k is a leaf node of a max-heap since n / 2 is the index of the last parent, and the
subtree rooted at k just contains one node. Thus, node k contains the largest value in that subtree.
Inductive Step:
Let k s.t. k is an internal node of a max-heap, and assume s.t. k < i n, P(i) is true. i.e.
The node i of the subtree rooted at i contains the largest value occurring anywhere in that subtree.
[inductive hypothesis]
So, we can conclude that node k contains the largest value in the subtree rooted at k.
Thus, by the principle of strong mathematical induction, P(k) is true for all nodes in a max-heap.
Exercise 6.1-4
Problem: Where in a max-heap might the smallest element reside, assuming that all elements are distinct?
The smallest element can only be one of leaf nodes. If not, it will have its own subtree and is larger than any element on that
subtree, which contradicts the fact that it is the smallest element.
1
23
2 3
17 14
4 5 6 7
6 13 10 1
8 9 10
5 7 12
So, this array isn't a max-heap. As shown in the figure above, the value of node 9 is greater than that of its parent node 4.
Exercise 6.4-2
We argue the correctness of HEAPSORT using the following loop invariant:
At the start of each iteration of the for loop of lines 2-5, the subarray A[1..i] is a max-heap containing the i smallest
elements of A[1..n], and the subarray A[i+1..n] contains the n-i lagest elements of A[1..n], sorted.
Proof:
We need to show that this invariant is true prior to the first step, that each iteration of the loop maintains the invariant. It
provides the property to show correctness of HEAPSORT.
Initialization:
Prior to the first iteration of the loop, i = n. The subarray A[1..i] is a max-heap due to BUILD-MAX-HEAP. The subarray
A[i + 1..n] is empty, hence the claim about it being sorted is vacuously true.
Maintenance:
For each iteration, By the loop invariant, A[1..i] is a max-heap containing the i smallest elements of A[1..n], so the A[1]
contains the (ni+1)st largest element. The execution of line 3 exchanges A[1] with A[i], so it makes the subarray A[i..n]
contain the (ni+1) largest elements of A[1..n], sorted. The heap size of A is decreased in to heap-size(A) = i 1. Now
the subarray A[1..i1] is not a max-heap, since the root node violates the map-heap property. But the children of the root
maintain the max-heap property, so we can restore the max-heap property by calling MAX-HEAPIFY(A, 1), which leaves a
max-heap in A[1..i1]. Consequently, the loop invariant is reestablished for the next iteration.
Termination:
At termination, i = 1. By the loop invariant, the subarray A[2.. n] contains the n 1 largest elements of A[1..n], sorted, and
thus, A[1] contains the smallest element of A[1..n]. A[1..n] is a sorted array.
Problem: What is the running time of HEAPSORT on an array A of length n that is already sorted in increasing order? What
about decreasing order?
The running time of HEAPSORT on an array of length that is already sorted in increasing order is (n lgn), because even
though it is already sorted, it will be transformed back into a heap and sorted.
The running time of HEAPSORT on an array of length that is sorted in decreasing order will be (n lgn). This occurs because
even though the heap will be built in linear time, every time the element is removed and HEAPIFY is called, it could cover the
full height of the tree.
Exercise 6.5-1
Problem: Illustrate the operation of HEAP-EXTRACT-MAX on the heap A = <15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1>.
Exercise 7.1-3
Problem: Give a brief argument that the running time of PARTITION on a subarray of size n is (n).
Since each iteration of the for loop involves a constant number of operations and there are n iterations total, the running time
is (n).
To prove:
For all integers n s.t. n 1, the property P(n):
The closed form T(n) and T(n) , for some constants c and d, s.t. c > 0 and d > 0, matches the recurrence
T(n) = T(n1) + (n).
Proof:
We will reason with strong induction.
Base case:
Let n = 1. T(1) = T(0) + (1) = (1) , if c is large enough. T(1) = T(0) + (1) = (1) , if d is small
enough. So P(1) holds.
Inductive Step:
Let k Z s.t. k 1 and assume i Z s.t. 1 i k1, P(i) is true. i.e.
T(i) and T(i) matches the recurrence. [inductive hypothesis]
So P(k) is true.
So, by the principle of strong mathematical induction, P(n) is true for all integers n s.t. n 1, and constants c > c1, 0 < d
c1/2.
Since T(n) = (n2) and T(n) = O(n2), we obtain that T(n) = (n2).
INSERTION-SORTs running time on perfectly-sorted input runs in (n) time. So, it takes almost (n) running time to sort an
almost-sorted input with INSERTION-SORT. However, QUICKSORT requires almost (n2) running time, recalling that it takes
(n2) time to sort perfectly-sorted input. This is because when we pick the last element as the pivot, it is usually the biggest
one, and it will produce one subproblem with close to n 1 elements and one with 0 elements. Since the cost of PARTITION
procedure of QUICKSORT is (n), the recurrence running time of QUICKSORT is T(n) = T(n 1) +(n). In another problem,
we, use the substitution method to prove that the recurrence T(n) = T(n 1) +(n) has the solution T(n) = (n2). So we
use INSERTION-SORT rather than QUICKSORT in this situation when the input is almost sorted.
Exercise 8.4-2
Problem: Explain why the worst-case running time for bucket sort is (n2). What simple change to the algorithm preserve its
linear average-case running time and makes its worst-case running time O(n lgn)?
The worst case for bucket sort occurs when the all inputs fall into a single bucket, for example. Since we use INSERTION-
SORT for sorting buckets and INSERTION-SORT has a worst case of (n2), the worst case run time for bucket sort is (n2).
By using an algorithm like MERGE-SORT with worst case run time time of O(n lgn) instead of INSERTION-SORT for sorting
buckets, we can ensure that the worst case of bucket sort is O(n lgn) without affecting the average case running time.
Exercise 8.4-3
Problem: Let X be a random variable that is equal to the number of heads in two flips of a fair coin. What is E[X2]? What is
E2[X]?
Suppose that a dynamic set S is represented by a direct-address table T of length m. Describe a procedure that finds the
maximum element of S. What is the worst-case performance of your procedure?
Solution:
FindMax (T, m)
{
max =
for i = 1 to m
{
if T[i] != NIL && max < T[i]
max = T[i]
}
return max
}
In the worst-case searching the entire table is needed. Thus the procedure must take O (m) time.
Suppose we use a hash function h to hash n distinct keys into an array T of length m. Assuming simple uniform hashing,
what is the expected number of collisions? More precisely, what is the expected cardinality of {{k, l}: k l and h (k) = h (l)}?
Solution:
For each pair of keys k, l, where k l, define the indicator random variable Xlk = I {h (k) = h (l)}. Since we assume simple
uniform hashing, Pr { Xlk = 1} = Pr { h (k) = h (l)} = 1 / m, and so E[Xlk] = 1 / m.
Now define the random variable Y to be the total number of collisions, so that . The expected number of collisions
is
E[Y ] = E[ X kl ] since Y = X kl
kl kl
= E[ X kl ] by linearity of expectation
kl
1 1
= since E[ X kl ] =
kl m m
n 1
= by choosing k and l out of n keys
2 m
n(n 1) 1 n n! n n! n(n 1)
= by = r!(n r)! , = 2!(n 2)! =
2 m r 2 2
n(n 1)
=
2m
Exercise 11.2-2
Demonstrate what happens when we insert the keys 5, 28, 19, 15, 20, 33, 12, 17, 10 into a hash table with collisions resolved
by chaining. Let the table have 9 slots, and let the hash function be h (k) = k mod 9.
Solution: omitted.
Solution:
Successful searches: (1 + ), which is identical to the original running time. The element we search for is equally likely
to be any of the elements in the hash table, and the proof of the running time for successful searches is similar to what
we did in the lecture.
Unsuccessful searches: 1/2 of the original running time, but still (1 + ), if we simply assume that the probability that
one element's value falls between two consecutive elements in the hash slot is uniformly distributed. This is because the
value of the element we search for is equally likely to fall between any consecutive elements in the hash slot, and once
we find a larger value, we can stop searching. Thus, the running time for unsuccessful searches is a half of the original
running time. Its proof is similar to what we did in the lecture.
Insertions: (1 + ), compared to the original running time of (1). This is because we need to find the right location
instead of the head to insert the element so that the list remains sorted. The operation of insertions is similar to the
operation of unsuccessful searches in this case.
Deletions: (1 + ), same as successful searches.
Solution:
First, we observe that we can generate any permutation by a sequence of interchanges of pairs of characters. One can prove
this property formally, but informally, consider that both heapsort and quicksort work by interchanging pairs of elements and
that they have to be able to produce any permutation of their input array. Thus, it suffices to show that if string x can be
derived from string y by interchanging a single pair of characters, then x and y hash to the same value.
Let xi be the ith character in x, and similarly for yi. We can interpret x in radix 2p as , and interpret y as . So
Suppose that x and y are identical strings of n characters except that the characters in positions a and b are interchanged:
xa = yb and ya = xb. (1)
Without loss of generality, let a > b. We have:
Since 0
h(x), h(y) < 2p 1, we have that (2p 1) < h(x) h(y) < 2p 1. If we show that (h(x) h(y)) mod (2p 1) = 0, then
h(x) = h(y). To prove (h(x) h(y)) mod (2p 1) = 0, we have:
= ((xa2ap + xb2bp) (ya2ap + yb2bp)) mod (2p 1) as x and y are identical strings
of n characters except that
chars. in positions a and
b are interchanged
= ((xa2ap + xb2bp) (xb2ap + xa2bp)) mod (2p 1) as xa = yb, xb = ya see (1)
= ((xa xb)2ap + (xb xa)2bp) mod (2p 1) by combining like terms
= ((xa xb)2ap (xa xb)2bp) mod (2p 1) as (xb xa) = (xa xb)
= ((xa xb)(2ap 2bp)) mod (2p 1) by factoring out (xa xb)
= ((xa xb)(2ap(2bp/2bp) 2bp)) mod (2p 1) by multiplication by
2bp/2bp = 1
= ((xa xb)2bp(2(a b)p 1)) mod (2p 1) by factoring out 2bp
= ((xa xb)2bp( )(2p 1)) mod (2p 1) by substituting [2(a b)p 1]2
1
Consider the congruence relation: (m o m ) mod n = ((m mod n) o (m mod n)) mod n, where o is +, , or *
1 2 1 2
2 Consider the equation = (geometric series) and multiplying both sides by 2p 1 to get
Examples of applications:
A dictionary which contains words expressed by ASCII code can be one of such example when each character of the
dictionary is interpreted in radix 28 = 256 and m = 255. The dictionary, for instance, might have words "STOP," "TOPS,"
"SPOT," "POTS," all of which are hashed into the same slot.
Exercise 11.3-4
Consider a hash table of size m = 1000 and a corresponding hash function for . Compute
the locations to which the keys 61, 62, 63, 64, and 65 are mapped.
Solution:
Solution:
Linear Probing
With linear probing, we use the hash function h(k, i) = (h'(k) + i) mod m = (k + i) mod m. Consider hashing each of the
following keys:
1) Hashing 10:
h(10, 0) = (10 + 0) mod 11= 10. Thus we have T[10] = 10.
2) Hashing 22:
h(22, 0) = (22 + 0) mod 11 = 0. Thus we have T[0] = 22.
3) Hashing 31:
h(31, 0) = (31 + 0) mod 11 = 9. Thus we have T[9] = 31.
4) Hashing 4:
h(4, 0) = (4 + 0) mod 11 = 4. Thus we have T[4] = 4.
5) Hashing 15:
h(15, 0) = (15 + 0) mod 11 = 4, collision!
h(15, 1) = (15 + 1) mod 11 = 5. Thus we have T[5] = 15.
6) Hashing 28:
h(28, 0) = (28 + 0) mod 11 = 6. Thus we have T[6] = 28.
7) Hashing 17:
h(17, 0) = (17 + 0) mod 11 = 6, collision!
h(17, 1) = (17 + 1) mod 11 = 7. Thus we have T[7] = 17.
8) Hashing 88:
h(88, 0) = (88 + 0) mod 11 = 0, collision!
h(88, 1) = (88 + 1) mod 11 = 1. Thus we have T[1] = 88.
9) Hashing 59:
h(59, 0) = (59 + 0) mod 11 = 4, collision!
h(59, 1) = (59 + 1) mod 11 = 5, collision!
h(59, 2) = (59 + 2) mod 11 = 6, collision!
h(59, 3) = (59 + 3) mod 11 = 7, collision!
h(59, 4) = (59 + 4) mod 11 = 8. Thus we have T[8] = 59.
Quadratic Probing
With quadratic probing, and c1 = 1, c2 = 3, we use the hash function h(k, i) = (h'(k) + i + 3i2) mod m = (k + i + 3i2) mod m.
Consider hashing each of the following keys:
1) Hashing 10:
h(10, 0) = (10 + 0 + 0) mod 11 = 10. Thus we have T[10] = 10.
2) Hashing 22:
h(22, 0) = (22 + 0 + 0) mod 11 = 0. Thus we have T[0] = 22.
3) Hashing 31:
h(31, 0) = (31 + 0 + 0) mod 11 = 9. Thus we have T[9] = 31.
4) Hashing 4:
h(4, 0) = (4 + 0 + 0) mod 11 = 4. Thus we have T[4] = 4.
5) Hashing 15:
h(15, 0) = (15 + 0 + 0) mod 11 = 4, collision!
h(15, 1) = (15 + 1 + 3) mod 11 = 8. Thus we have T[8] = 15.
6) Hashing 28:
h(28, 0) = (28 + 0 + 0) mod 11 = 6. Thus we have T[6] = 28.
7) Hashing 17:
Page 6 of 7 PSU CMPSC 465 Spring 2013
h(17, 0) = (17 + 0 + 0) mod 11 = 6, collision!
h(17, 1) = (17 + 1 + 3) mod 11 = 10, collision!
h(17, 2) = (17 + 2 + 12) mod 11 = 9, collision!
h(17, 3) = (17 + 3 + 27) mod 11 = 3. Thus we have T[3] = 17.
8) Hashing 88:
h(88, 0) = (88 + 0 + 0) mod 11 = 0, collision!
h(88, 1) = (88 + 1 + 3) mod 11 = 4, collision!
h(88, 2) = (88 + 2 + 12) mod 11 = 3, collision!
h(88, 3) = (88 + 3 + 27) mod 11 = 8, collision!
h(88, 4) = (88 + 4 + 48) mod 11 = 8, collision!
h(88, 5) = (88 + 5 + 75) mod 11 = 3, collision!
h(88, 6) = (88 + 6 + 108) mod 11 = 4, collision!
h(88, 7) = (88 + 7 + 147) mod 11 = 0, collision!
h(88, 8) = (88 + 8 + 192) mod 11 = 2. Thus we have T[2] = 88.
9) Hashing 59:
h(59, 0) = (59 + 0 + 0) mod 11 = 4, collision!
h(59, 1) = (59 + 1 + 3) mod 11 = 8, collision!
h(59, 2) = (59 + 2 + 12) mod 11 = 7. Thus we have T[7] = 59.
Doubling Hashing
With double hashing, we use the hash function:
h(k, i) = (h'(k) + ih2'(k)) mod m = (k + i(1 + (k mod (m 1)))) mod m.
Consider hashing each of the following keys:
1) Hashing 10:
h(10, 0) = (10 + 0) mod 11 = 10. Thus we have T[10] = 10.
2) Hashing 22:
h(22, 0) = (22 + 0) mod 11 = 0. Thus we have T[0] = 22.
3) Hashing 31:
h(31, 0) = (31 + 0) mod 11 = 9. Thus we have T[9] = 31.
4) Hashing 4:
h(4, 0) = (4 + 0) mod 11 = 4. Thus we have T[4] = 4.
5) Hashing 15:
h(15, 0) = (15 + 0) mod 11 = 4, collision!
h(15, 1) = (15 + 1 * h2'(15)) mod 11 = 10, collision!
h(15, 2) = (15 + 2 * h2'(15)) mod 11 = 5. Thus we have T[5] = 15.
6) Hashing 28:
h(28, 0) = (28 + 0) mod 11 = 6. Thus we have T[6] = 28.
7) Hashing 17:
h(17, 0) = (17 + 0) mod 11 = 6, collision!
h(17, 1) = (17 + 1 * h2'(17)) mod 11 = 3. Thus we have T[3] = 17.
8) Hashing 88:
h(88, 0) = (88 + 0) mod 11 = 0, collision!
h(88, 1) = (88 + 1 * h2'(88)) mod 11 = 9, collision!
h(88, 2) = (88 + 2 * h2'(88)) mod 11 = 7. Thus we have T[7] = 88.
9) Hashing 59:
h(59, 0) = (59 + 0) mod 11 = 4, collision!
h(59, 1) = (59 + 1 * h2'(59)) mod 11 = 3, collision!
h(59, 2) = (59 + 2 * h2'(59)) mod 11 = 2. Thus we have T[2] = 59.
Disclaimer: This is a draft of solutions that has been prepared by the TAs and the instructor makes no guarantees that
solutions presented here contain the level of detail that would be expected on an exam. Any errors or explanations you find
unclear should be reported to either of the TAs for corrections first.
Exercise 13.1-1
In the style of Figure 13.1(a), draw the complete binary search tree of height 3 on the keys {1, 2, , 15}. Add the NIL leaves
and color the nodes in three different ways such that the black-heights of the resulting red-black trees are 2, 3, and 4.
Solution:
Solution:
If the node with key 36 is inserted and colored red, the red-black tree becomes:
36
If the node with key 36 is inserted and colored black, the red-black tree becomes:
36
Proof:
In the longest path, at least every other node is black. In the shortest path, at most every node is black. Since the two paths
contain equal numbers of black nodes, the length of the longest path is at most twice the length of the shortest path.
Exercise 13.2-1
Solution:
RIGHT-ROTATE (T, x)
y = x.left //set y
x.left = y.right // turn ys right subtree into xs left subtree
if y.right NIL
y.right.p = x
y.p = x:p // link xs parent to y
if x.p == NIL
T.root = y
else if x == x.p.right
x.p.right = y
else
x.p.left = y
y.right = x //put x on ys right
x.p = y
Solution:
Since the exercise asks about binary search trees rather than the more specific red-black trees, we assume here that leaves are
full-fledged nodes, and we ignore the sentinels.
Taking the books hint, we start by showing that with at most n1 right rotations, we can convert any binary search tree into
one that is just a right-going chain.
The idea is: Let us define the right spine as the root and all descendants of the root that are reachable by following only right
pointers from the root. A binary search tree that is just a right-going chain has all n nodes in the right spine.
As long as the tree is not just a right spine, repeatedly find some node y on the right spine that has a non-leaf left child x and
then perform a right rotation on y:
(In the above figure, note that any of , , and can be an empty subtree.)
Observe that this right rotation adds x to the right spine, and no other nodes leave the right spine. Thus, this right rotation
increases the number of nodes in the right spine by 1. Any binary search tree starts out with at least one node the root
in the right spine. Moreover, if there are any nodes not on the right spine, then at least one such node has a parent on the right
spine. Thus, at most n1 right rotations are needed to put all nodes in the right spine, so that the tree consists of a single right-
going chain.
If we knew the sequence of right rotations that transforms an arbitrary binary search tree T to a single right-going chain T,
then we could perform this sequence in reverse turning each right rotation into its inverse left rotation to transform T
back into T.
Therefore, here is how we can transform any binary search tree T1 into any other binary search tree T2. Let T be the unique
right-going chain consisting of the nodes of T1 (which is the same as the nodes of T2). Let r = <r1, r2, ..., rk> be a sequence of
right rotations that transforms T1 to T, and let r = <r1, r2, ..., rk> be a sequence of right rotations that transforms T2 to T.
We know that there exist sequences r and r with k, k n1. For each right rotation ri, let li be the corresponding inverse
left rotation. Then the sequence <r1, r2, ..., rk, lk, lk-1, , l2, l1> transforms T1 to T2 in at most 2n2 rotations.
Solution:
Insert 12
Insert 19
Insert 8
Figure 13.5
Figure 13.6
Solution:
In Figure 13.5, nodes A, B, and D have black-height k + 1 in all cases, because each of their subtrees has black-height k and a
black root. Node C has black-height k + 1 on the left (because its red children have black-height k + 1) and black-height k + 2
on the right (because its black children have black-height k + 1).
In Figure 13.6, nodes A, B, and C have black-height k + 1 in all cases. At left and in the middle, each of As and Bs subtrees
has black-height k and a black root, while C has one such subtree and a red child with black-height k + 1. At the right, each of
As and Cs subtrees has black-height k and a black root, while Bs red children each have black-height k + 1.
Disclaimer: This is a draft of solutions that has been prepared by the TAs and the instructor makes no guarantees that
solutions presented here contain the level of detail that would be expected on an exam. Any errors or explanations you find
unclear should be reported to either of the TAs for corrections first.
Exercise 18.1-1
Why dont we allow a minimum degree of t = 1?
Solution:
According to the definition, minimum degree t means every node other than the root must have at least t 1 keys, and every
internal node other than the root thus has at least t children. So, when t = 1, it means every node other than the root must have
at least t 1 = 0 key, and every internal node other than the root thus has at least t = 1 child.
Thus, we can see that the minimum case doesn't exist, because no node exists with 0 key, and no node exists with only 1 child
in a B-tree.
Exercise 18.1-2
For what values of t is the tree of Figure 18.1 a legal B-tree?
Solution:
According to property 5 of B-tree, every node other than the root must have at least t1keys and may contain at most 2t1
keys. In Figure 18.1, the number of keys of each node (except the root) is either 2 or 3. So to make it a legal B-tree, we need
to guarantee that
t 1 2 and 2 t 1 3,
Exercise 18.1-3
Show all legal B-trees of minimum degree 3 that represent {1, 2, 3, 4, 5}.
Solution:
We know that every node except the root must have at least t 1 = 2 keys, and at most 2t 1 = 5 keys. Also remember that
the leaves stay in the same depth. Thus, there are 2 possible legal B-trees:
3
1, 2, 3, 4, 5
1, 2 4, 5
Solution:
# of
depth nodes
2t 0 1
1
2
t
2t
2t
1
1 2t
1
2 2
t t
2t
2t 2t
2t 2 (2t)2
1 1 1 1
2 2 2 2
t t t t
2t
2t 2t
2t 2t
2t 2t 3
(2t)3
2t
1 1 1 1 1 1 1 1
We can see that each node contains 2t 1 keys, and at depth k, the tree at most has (2t)k nodes. The total nodes is therefore
the sum of (2t)0, (2t)1, (2t)2, (2t)3, , (2t)h. Let MaxKeyNum(t, h) as a function that returns the maximum number of keys in
a B-tree of height h and the minimum degree t. We can get that:
MaxKeyNum(t, h) = (2t 1)[(2t)0 + (2t)1 + (2t)2 + (2t)3 + + (2t) h] as [keys per node] * [total # of nodes]
= (2t 1) by using Sigma to sum up total # of nodes
Exercise 18.1-5
Describe the data structure that would result if each black node in a red-black tree were to absorb its red children,
incorporating their children with its own.
Solution:
After absorbing each red node into its black parent, each black node may contain 1, 2 (1 red child), or 3 (2 red children) keys,
and all leaves of the resulting tree have the same depth, according to property 5 of red-black tree (For each node, all paths
from the node to descendant leaves contain the same number of black nodes). Therefore, a red-black tree will become a B-
tree with minimum degree t = 2, i.e., a 2-3-4 tree.
Solution:
Finding the minimum in a B-tree is quite similar to finding a minimum in a binary search tree. We need to find the left most
leaf for the given root, and return the first key.
B-TREE-FIND-MIN(x)
//PRE: x is a node on the B-tree T. The top level call is B-TREE-FIND-MIN(T.root).
//POST: FCTVAL is the minimum key stored in the subtree rooted at x.
{
if x == NIL //T is empty
{
return NIL
}
else if x.leaf //x is leaf
{
return x.key1 //return the minimum key of x
}
else
{
DISK-READ(x.c1)
return B-TREE-FIND-MIN(x.c1)
}
}
Finding the predecessor of a given key x.keyi is according to the following rules:
If x is not a leaf, return the maximum key in the i-th child of x, which is also the maximum key of the subtree rooted
at x.ci
If x is a leaf and i > 1, return the (i1)st key of x, i.e., x.keyi1
Otherwise, look for the last node y (from the bottom up) and j > 0, such that x.keyi is the leftmost key in y.cj; if j = 1,
return NIL since x.keyi is the minimum key in the tree; otherwise we return y.keyj1.
B-TREE-FIND-PREDECESSOR(x, i)
//PRE: x is a node on the B-tree T. i is the index of the key.
//POST: FCTVAL is the predecessor of x.keyi.
{
if ! x.leaf
{
DISK-READ(x.ci)
return B-TREE-FIND-MAX(x.ci)
}
else if i > 1 //x is a leaf and i > 1
return x.keyi1
else //x is a leaf and i = 1
{
z=x
while (1)
{
if z.p == NIL //z is root
return NIL // z.keyi is the minimum key in T; no predecessor
y = z.p
j=1
DISK-READ(y.c1)
if j == 1
z=y
else
return y.keyj1
}
}
}
B-TREE-FIND-MAX(x)
//PRE: x is a node on the B-tree T. The top level call is B-TREE-FIND-MAX(T.root).
//POST: FCTVAL is the maximum key stored in the subtree rooted at x.
{
if x == NIL //T is empty
{
return NIL
}
else if x.leaf //x is leaf
{
return (x, x.n) //return the maximum key of x
}
else
{
DISK-READ(x.cx.n+1)
return B-TREE-FIND-MAX(x.cx.n+1)
}
}
Exercise 18.2-6
Suppose that we were to implement B-TREE-SEARCH to use binary search rather than linear search within each node. Show
that this change makes the CPU time required O(lg n), independently of how t might be chosen as a function of n.
Solution:
As in the TREE-SEARCH procedure for binary search trees, the nodes encountered during the recursion form a simple path
downward from the root of the tree. Thus, the B-TREE-SEARCH procedure needs O(h) = O(logt n) CPU time to search along
the path, where h is the height of the B-tree and n is the number of keys in the B-tree, and we know that h . Since
the number of keys in each node is less than 2t 1, a binary search within each node is O(lg t). So the total time is:
O(lg t * logt n) = O(lg t * ) by changing the base of the logarithm
= O(lg n)
Disclaimer: This is a draft of solutions that has been prepared by the TAs and the instructor makes no guarantees that
solutions presented here contain the level of detail that would be expected on an exam. Any errors or explanations you find
unclear should be reported to either of the TAs for corrections first.
Exercise 22.1-1
Given an adjacency-list representation of a directed graph, how long does it take to compute the out-degree of every vertex?
How long does it take to compute the in-degrees?
Solution:
Given an adjacency-list representation Adj of a directed graph, the out-degree of a vertex u is equal to the length of Adj[u],
and the sum of the lengths of all the adjacency lists in Adj is |E|. Thus the time to compute the out-degree of every vertex is
(|V| + |E|).
The in-degree of a vertex u is equal to the number of times it appears in all the lists in Adj. If we search all the lists for each
vertex, the time to compute the in-degree of every vertex is (|V||E|).
(Alternatively, we can allocate an array T of size |V| and initialize its entries to zero. Then we only need to scan the lists in
Adj once, incrementing T [u] when we see u in the lists. The values in T will be the in-degrees of every vertex. This can be
done in (|V| + |E|) time with (|V|) additional storage.)
Solution:
1
2 3
4 5
6 7
An adjacency-list representation of this tree is shown below:
1 2 3
2 1 4 5
3 1 6 7
4 2
5 2
6 3
7 3
1 2 3 4 5 6 7
1 0 1 1 0 0 0 0
2 1 0 0 1 1 0 0
3 1 0 0 0 0 1 1
4 0 1 0 0 0 0 0
5 0 1 0 0 0 0 0
6 0 0 1 0 0 0 0
7 0 0 1 0 0 0 0
Show the dist and pred values that result from running breadth-first search on the directed graph below, using vertex 3 as the
source. 1 2 3
Solution: 4 5 6
3.pred 3.pred
1 2 :NIL 1 2 :NIL
0 0
Q 3 Q 4
0 2
2 1
1
4 5 6 4.pred 5.pred 6.pred
:5 :3 :3
(a) (d)
1 1 3
2 1 1
1
1
4 5.pred 6.pred 4.pred 5.pred 6.pred
:3 :3 :5 :3 :3
(b) (e)
(c) (f)
The procedure of the breadth-first search is shown above. From the result, we can see that:
Show the dist and pred values that result from running breadth-first search on the undirected graph below, using vertex u as
the source. r s t u
v w x y
Solution:
Exercise 22.3-1
Make a 3-by-3 chart with row and column labels WHITE, GRAY, and BLACK. In each cell (i, j), indicate whether, at any
point during a depth-first search of directed graph, there can be an edge from a vertex of color i to a vertex of color j. For
each possible edge, indicate what edge types it can be. Make a second such chart for depth-first search of an undirected graph.
Solution:
Directed graph:
WHITE GRAY BLACK
WHITE tree, back, forward, and cross back and cross cross
GRAY tree and forward tree, forward, back tree, forward, and cross
BLACK back and cross tree, forward, back and cross
Undirected graph:
WHITE GRAY BLACK
WHITE tree and back tree and back
GRAY tree and back tree and back tree and back
BLACK tree and back tree and back
Exercise 22.3-2
Show how depth-first search works on the graph of Figure 22.6. Assume that the for loop of lines 57 of the DFS procedure
considers the vertices in alphabetical order, and assume that each adjacency list is ordered alphabetically. Show the discovery
and finishing times for each vertex, and show the classification of each edge.
The discovery and finishing times for each vertex are shown in the figure below:
1,16 17,20
10,11
Tree edges: (q, s), (s, v), (v, w), (q, t), (t, x), (x, z), (t, y), (r, u)
Back edges: (w, s), (z, x), (y, q)
Forward edges: (q, w)
Cross edges: (r, y), (u, y)
Exercise 22.3-3
Show the parenthesis structure of the depth-first search of Figure 22.4.
Solution:
Exercise 22.3-7
Rewrite the procedure DFS, using a stack to eliminate recursion.
Solution:
time = time + 1
s.td = time
s.color = GRAY
PUSH(S, s)
while S != EMPTY
{
t = TOP(S)
if v V(G).Adj[t] s.t. v.color == WHITE // vs adjacency list hasnt been fully examined
{
v.pred = t
time = time + 1
v.td = time
v.color = GRAY
PUSH(S, v)
}
else // vs adjacency list has been fully examined
{
t = POP(S)
time = time + 1
v.tf = time
v.color = BLACK
}
}
Exercise 22.4-1
Show the ordering of vertices produced by TOPOLGICAL-SORT when it is run on the dag
of Figure 22.8, under the assumption of Exercise 22.3-2.
Solution:
According to the assumption of Exercise 22.3-2, the for loop of lines 57 of the DFS procedure
2/5 23/24
6/19
9/18 12/13
15/16
Ordering of vertices:
Exercise 22.4-2
Give a linear-time algorithm that takes as input a directed acyclic graph G = (V, E) and two vertices s and t , and returns the
number of simple paths from s to t in G. For example, the directed acyclic graph of Figure 22.8 contains exactly four simple
paths from vertex p to vertex v: pov, poryv, posryv, and psryv.
(Your algorithm needs only to count the simple paths, not list them.)
Solution:
Add a field to the vertex representation to hold an integer count. Initially, set vertex ts count to 1 and other vertices count to
0. Start running DFS with s as the start vertex. When t is discovered, it should be immediately marked as finished (BLACK),
without further processing starting from it. Subsequently, each time DFS finishes a vertex v, set vs count to the sum of the
counts of all vertices adjacent to v. When DFS finishes vertex s, stop and return the count computed for s.
Disclaimer: This is a draft of solutions that has been prepared by the TAs and the instructor makes no guarantees that
solutions presented here contain the level of detail that would be expected on an exam. Any errors or explanations you find
unclear should be reported to either of the TAs for corrections first.
Exercise 23.1-2
Professor Sabatier conjectures the following converse of Theorem 23.1. Let G = (V, E) be a connected, undirected graph with
a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G,
let (S, V S) be any cut of G that respects A, and let (u, v) be a safe edge for A crossing (S, V S). Then, (u, v) is a light edge
for the cut. Show that the professors conjecture is incorrect by giving a counterexample.
Solution:
That is false. A safe edge (with respect to the set of edges A) is defined to be any edge such that when we add it to A, we still
have a subset of some minimum spanning tree. This definition does not reference light edges in any way. In the situation
from a counterexample, there may be multiple safe edges from S to V S, not all of them have to be light.
A counterexample is shown below. Let S = {a, b, c}, A = {(a, b), (b, c}, we can see that (a, d), (b, e), (c, f) are all safe edges
for A crossing (S, V S), but only (b, e) is a light edge for the cut.
1 1
a b c
3 1 5 4
d 5
e
f
Exercise 23.1-3
Show that if an edge (u, v) is contained in some minimum spanning tree, then it is a light edge crossing some cut of the graph.
Solution:
Suppose (u, v) T, a minimal spanning tree. Let A = {T (u, v)}. A contains two trees: A = Tu Tv. Tu is the tree in which
vertex u appears, and Tv is the tree in which vertex v appears. Moreover, Vu Vv = V, where Vu contains the vertices of Tu
and Vv contains the vertices of Tv. That is, (Vu, Vv) is a cut, which is crossed by (u, v) and which respects A. Any edge crossing
the cut rejoins the two trees. If there is a crossing edge (x, y) with w(x, y) < w(u, v), then T = Tu Tv (x, y) is a spanning
tree with weight:
w(T) = w(Tu Tv) + w(x, y) as T = Tu Tv (x, y)
= w(Tu Tv) + w(u, v) + [w(x, y) w(u, v)] by adding 0, that is w(u, v) w(u, v)
= w(T) [w(u, v) w(x, y)] as T = Tu Tv (u, v)
< w(T)
This contradicts with that (u, v) is contained in some minimum spanning tree. So, w(u, v) < w(x, y) for all (x, y) crossing the
cut (Vu, Vv). That is, (u, v) is a light edge crossing the cut.
Exercise 23.2-1
Kruskals algorithm can return different spanning trees for the same input graph G, depending on how it breaks ties when the
edges are sorted into order. Show that for each minimum spanning tree T of G, there is a way to sort the edges of G in
Kruskals algorithm so that the algorithm returns T.
We would start by sorting the edges in of G in non-descending order. In addition, we would want that among the edges of
same weight, the edges which are contained in T are placed in first positions.
Exercise 23.2-8
Professor Borden proposes a new divide-and-conquer algorithm for computing minimum spanning trees, which goes as
follows. Given a graph G = (V, E), partition the set V of vertices into two sets V1 and V2 such that |V1| and |V2| differ by at
most 1. Let E1 be the set of edges that are incident only on vertices in V1, and let E2 be the set of edges that are incident only
on vertices in V2. Recursively solve a minimum-spanning-tree problem on each of the two subgraphs G1 = (V1, E1) and G2 =
(V2, E2). Finally, select the minimum-weight edge in E that crosses the cut (V1, V2), and use this edge to unite the resulting
two minimum spanning trees into a single spanning tree.
Either argue that the algorithm correctly computes a minimum spanning tree of G, or provide an example for which the
algorithm fails.
Solution:
We argue that the algorithm fails. Consider the graph G below. We partition G into V1 and V2 as follows: V1 = {A, B}, V2 = {C,
D}. E1 = {(A, B)}. E2 = {(C, D)}. The set of edges that cross the cut is Ec = {(A, C), (B, D)}.
1
A
B
G1
2 3
G2
C D
8
Now, we must recursively find the minimum spanning trees of G1 and G2. We can see that in this case, MST(G1) = G1 and
MST(G2) = G2. The minimum spanning trees of G1 and G2 are shown below on the left.
The minimum weighted edge of the two edges across the cut is edge (A, C). So (A, C) is used to connect G1 and G2. This is
the minimum spanning tree returned by Professor Bordens algorithm. It is shown below and to the right.
1 1
A
B
A
B
G1
2 3 2 3
G2
C D C D
8
8
We can see that the minimum-spanning tree returned by Professor Bordens algorithm is not the minimum spanning tree of G,
therefore, this algorithm fails.
Disclaimer: This is a draft of solutions that has been prepared by the TAs and the instructor makes no guarantees that
solutions presented here contain the level of detail that would be expected on an exam. Any errors or explanations you find
unclear should be reported to either of the TAs for corrections first.
Exercise 24.3-1
Run Dijkstras algorithm on the directed graph below, first using vertex s as the source and then using vertex z as the source.
In the style of Figure 24.6, show the d and values and the vertices in set S after each iteration of the while loop.
Solution:
Omitted
Exercise 24.3-3
Suppose we change line 4 of Dijkstras algorithm to the following:
line 4: while |Q| > 1
This change causes the while loop to execute |V| 1 times instead of |V| times. Is this proposed algorithm correct?
Solution:
Yes, the algorithm still works. Let u be the leftover vertex that does not get extracted from the priority queue Q. If u is not
reachable from s, then d[u] = (s, u) = . If u is reachable from s, there is a shortest path p = s ~ x u. When the node x was
extracted, d[x] = (s, x) and then the edge (x, u) was relaxed; thus, d[u] = (s, u).
I-1 Let T (n) = M for n M for some constant M 1 independent of n, and 2T (n) = 2T (n/2) + 3T (n/3) + n
otherwise. Show that T (n) = (n log n).
As a clarification, we assume that the recurrence is defined for all positive reals. To show T (n) = (n log n),
we show separately that T (n) = O(n log n) and T (n) = (n log n). All logarithms used are 2-based.
For the O direction, we show by induction that T (n) n log n + cn = (n log n) for n 1/3 for some
c > 0.
1. Case 1/3 n M: Its easy to see that n log n is bounded from below by some constant c0 . Now, if
we choose any c 3(M c0 ), we have that T (n) = M c0 + (1/3)c n log n + cn.
2. Induction step n > M: assume that the claim holds for n/2 and n/3. Note that, since M 1, the
smallest n for which the claim is assumed to hold is 1/3, which is proven in the base case. Under these
assumptions, we have that
2T (n) = 2T (n/2) + 3T (n/3) + n
2( 2n log n2 + n2 c) + 3( 3n log n3 + n3 c) + n induction assumption
= n(log n2 + log 3n + 1) + 2nc
= n(2 log n log 6 + 1) + 2nc properties of logarithm
2(n log n + nc) log2 6 > 1
holds regardless of our choice of c.
For the direction, we show by induction that T (n) cn log n = (n log n) for some c > 0.
1. Case n M: by choosing c 1/ log M, we have that T (n) = M cM log M cn log n.
2. Induction step n > M: assume that the claim holds for n/2 and n/3. Then,
2T (n) = 2T (n/2) + 3T (n/3) + n
2(c 2n log n2 ) + 3(c n3 log n3 ) + n induction assumption
= 2cn log n + n(1 c log 6) properties of logarithm
2cn log n
where the last inequality holds if 1 c log 6 0 c 1/ log 6.
Since we want both of steps 1 and 2 to hold, we can choose c = min(1/ log M, 1/ log 6).
I-2 (CLRS 2.3-7) Describe a (n log n)-time algorithm that, given a set S of n integers and another integer x,
determines whether or not there exist two elements in S whose sum is exactly x.
Sort the set S using merge sort. Then for each y S separately use binary search to check if integer x y
exists in S. Sorting takes time (n log n). Binary search takes time O(log n) and is executed n times. The
total time is thus (n log n).
If S is sorted, the problem can also be solved in linear time by scanning the list S at the same time forward
and backward directions:
Input: list S sorted in ascending order, x
Output: true if there exist two elements in S whose sum is exactly x, false otherwise
1 i 1, j n
2 while i j do
3 if S[i] + S[ j] = x then
4 return true
5 if S[i] + S[ j] < x then
6 i i+1
7 else
8 j j1
9 return false
1
Note: The above solutions assume that the two elements can actually be the same element (for examples, if
S = {1, 2, 5} and x = 4, the algorihms return true since 2 + 2 = 4). If this is not allowed, then small changes
to algorithms are needed. (In the first solution skip y if 2y = x. In the second solution replace by <.)
I-3 (CLRS 2-4 Inversions) Let A[1..n] be an array of n distinct numbers. If i < j and A[i] > A[ j], then the pair
(i, j) is called an inversion of A.
I-4 (CLRS 4.2-6) How quickly can you multiply a knn matrix by an nkn matrix, using Strassens algorithms
as a subroutine? Answer the same question with the order of input matrices reversed.
Let the input be A = [AT1 ATk ]T and B = [B1 Bk ], where Ai and Bi are n n submatrices. The product
AB is a kn kn matrix
A1 B1 A1 Bk
AB = ... .. .. ,
. .
Ak B1 Ak Bk
where each product Ai B j can be computed in (nlog2 7 )
time using Strassens algorithm. There are k2 such
2 log 7
products, so the total time requirement is (k n 2 ).
The product BA is a n n matrix BA = ki=1 Ai Bi . There are k products requiring (nlog2 7 ) time and k 1
summations requiring (n2 ) time. The total time is thus (knlog2 7 ).
2
I-5 (CLRS 4.2-7) Show how to multiply the complex numbers a + bi and c + di using only three multiplications
of real numbers. The algorithm should take a, b, c, and d as input and produce the real component ac bd
and the imaginary component ad + bc separately.
Compute the products ac, bd and (a + b)(c + d). Now the real component is ac bd and imaginary com-
ponent is (a + b)(c + d) ac bd.
Alternatively, compute e.g. a(c d), d(a b) and b(c + d). Now the real component is obtained as the sum
of the first two, and the imaginary component as the sum of the last two.
Both solutions use 3 multiplications and 5 additions/substractions.
3
Design and Analysis of Algorithms, Fall 2014
Exercise II: Solutions
II-1 Where in the matrix multiplication-based DP algorithm for the all-pairs shortest paths problem do we need
the associativity of matrix multiplication?
The algoritm computes the product W n1 with the exception that instead of the usual definition, the product
of matrices A and B is defined by
A B = C, where Ci j = min{Aik + Bk j }.
k
The slow version of the algorithm uses a recurrence W i+1 = W i W , which gives the correct result. The fast
version uses repeated squaring: W 2i = W i W i . We do not know a priori that the second recurrence computes
W 2i correctly. However, from the associativity of multiplication it follows that W i W i = W 2i1 W , that is,
the fast version works correctly.
II-2 Give an O(n2 )-time algorithm to find the maximum length of monotonically increasing subsequences of a
given sequence of n numbers. For instance, if given (3, 1, 2, 5, 2, 6, 8, 6, 7, 3, 5) as input, the algorithm should
output 6 (the length of the subsequence (1, 2, 5, 6, 6, 7)). (Side note: Even an O(n log n)-time algorithm
exists.)
We write MIS short for monotonically increasing subsequence. Let A[i] denote the ith number in the
sequence. For all 1 i n we define L[i] as the length of the longest MIS that ends in A[i]. Then L[1] = 1
and for all i > 1 we have that
In other words, L[i] is found by finding the longest MIS among the preceding numbers that can be continued
by A[i]. Assuming the values L[k] for 1 k i 1 are already computed, L[i] is easily computed in linear
time. This yields a simple dynamic programming algorithm that computes all L[i] in increasing order of i in
O(n2 ) time. The solution is then obtained as max{L[i] : 1 i n}. To find the actual longest MIS (instead
of just the length), the algorithm should also keep track of the maximizing indices k, which can then be used
to reconstruct the MIS.
The problem can also be solved in O(n log n) time by formulating the dynamic programming in a different
manner. The algorithm initializes an array E and performs n iterations, maintaining the following invariant:
After the ith iteration, each E[`] for 1 ` i contains the smallest number that ends a MIS of length exactly
` among the first i elements of A, or if no such subsequence exists.
Assuming the invariant holds, its easy to see that E is always increasing: If theres a MIS of length ` ending
at A[i], theres also a MIS of length ` + 1 ending at A[ j] for A[i] < A[ j]. Therefore A[i] couldnt be the
smallest number that ends a MIS of length `.
To maintain the invariant, for iteration i the algorithm uses binary search to find the largest E[`] A[i]. Since
E[`] ends a MIS of length `, it is extended by A[i] to produce a MIS of length ` + 1, and A[i] is clearly the
smallest number ending such a MIS so far. Hence, the algorithm updates E[` + 1] = A[i]. If no E[`] A[i]
exists, the algorithm updates E[1] = A[i]. It turns out no other changes are required.
Therefore, for each of the n iterations the algorithm performs a O(log n) amount of work, giving the claimed
time complexity. After all iterations, the answer is the largest index k for which L[k] < . Again, slight
modifications are required to find the actual MIS.
1
II-3 (CLRS 15-3 Bitonic euclidean traveling-salesman problem) The euclidean traveling-salesman problem
is the problem of determining the shortest closed tour that connects a given set of n points in the plane.
Figure 15.11(a) shows the solution to a 7-point problem. The general problem is NP-complete, and its
solution is therefore believed to require more than polynomial time (see Chapter 34).
J. L. Bentley has suggested that we simplify the problem by restricting our attention to bitonic tours, that is,
tours that start at the leftmost point, go strictly left to right to the rightmost point, and then go strictly right
to left back to the starting point. Figure 15.11(b) shows the shortest bitonic tour of the same 7 points. In
this case, a polynomial-time algorithm is possible.
Describe an O(n2 )-time algorithm for determining an optimal bitonic tour. You may assume that no two
points have the same x-coordinate. (Hint: Scan left to right, maintaining optimal possibilities for the two
parts of the tour.)
The first step is to sort the points according to x-coordinate. Let the sorted points be 1, . . . , n and let d(i, j)
be the distance between points i and j.
A bitonic tour starts at the leftmost point and ends at the rightmost point. It consists of two paths, the upper
and lower (imaging a line connecting the starting and end points), such that each point is visited by at least
one of the paths. We describe a dynamic programming algorithm which uses partially constructed bitonic
tours.
For i, j {1, . . . , n} and i, j on separate paths, let B[i, j] be the minimum total cost of two paths. Now the
length of the optimal bitonic tour is given by B[n, n]. Since B[i, j] = B[ j, i], we are only interested in pairs
i, j with 1 i j n. The base case is B[1, 1] = 0. For the rest of the cases we compute B[i, j] as follows
(the idea is deciding the predecessor of point j):
Case 1: If i < j 1, then the path ending in j must also visit j 1, because the other path cannot visit
j 1 and then backtrack to i. Thus we have
Case 2: If i = j 1 or i = j, then the optimal solution must have a path which ends in j and comes
from some node k with 1 k < j. The optimal solution is therefore given by selecting the optimal
predecessor by setting
B[i, j] = min {B[i, k] + d(k, j)}.
1k< j
where B[i, k] can be replaced by B[k, i] (already computed), since B[i, j] = B[ j, i].
There are O(n2 ) entries that fall under the first case, and each of these takes a constant time to handle. The
second case occurs only O(n) times, with each taking O(n) time. Therefore the total running time is O(n2 ).
To get the actual optimal bitonic tour, we modify the algorithm to keep track of the optimal predecessor
chosen in the case 2 in a separate table. The optimal tour can then be constructed from this information.
II-4 (CLRS 15-4 Printing neatly) Consider the problem of neatly printing a paragraph on a printer. The input
text is a sequence of n words of lengths l1 , l2 , . . . , ln , measured in characters. We want to print this paragraph
neatly on a number of lines that hold a maximum of M characters each. Our criterion of neatness is as
follows. If a given line contains words i through j, where i < j, and we leave exactly one space between
j
words, the number of extra space characters at the end of the line is M j + i k=i lk , which must be non-
negative so that the words fit on the line. We wish to minimize the sum, over all lines except the last, of the
cubes of the numbers of extra space characters at the ends of lines. Give a dynamic-programming algorithm
to print a paragraph of n words neatly on a printer. Analyze the running time and space requirements of
your algorithm.
Let
j
s(i, j) = M j + i + 1 lk
k=i+1
2
be the number of trailing spaces required when the words i + 1, . . . , j are put on a same line. Observe that is
possible to put these words on a line only if s(i, j) 0. We define the cost of putting the words from i + 1
to j on a same line as
,
if s(i, j) < 0
c(i, j) = 0, if j = n and s(i, j) 0
s(i, j)3 , otherwise.
The dynamic programming is now straightforward to formulate. Let C[ j] be the optimal cost of printing the
words from 1 to j, such that word j ends a line. Then we have
C[0] = 0
C[ j] = min {C[i] + c(i, j)} for j > 0.
0i< j
Computing C takes (n2 ) time, as c(i 1, j) can be computed from c(i, j) in constant time. The space
requirement is (n). A straightforward modification gives us O(Mn) time, as there cannot be more than
(M + 1)/2 words on a single line.
Again, to actually get the optimal line lengths and print the paragraph, we need to keep track of minimizing
values i, which indicate the last word on the previous line, given that the last word on the current line is j.
II-5 (CLRS 25.1-9) Modify Faster-All-Pairs-Shortest-Paths so that it can determine whether the graph contains
a negative-weight cycle.
We observe that if the graph contains a negative-weight cycle, then there is a negative-weight path of length
k n from some vertex i to i. This means that in the matrix-multiplication-based all-pairs shortest path
algorithm we will get a negative value on the diagonal of the matrix W m for all m k. Therefore we can
detect the existence of negative cycles simply by checking whether there are negative values on the diagonal
dlog ne
of the matrix W 2 2 . Note that we have to modify the algorithm to compute matrices up to at least W n
instead of W n1 , as the shortest negative weight cycle might have length n.
3
Design and Analysis of Algorithms, Fall 2014
Exercise III: Solutions
III-1 Show that the subset-sum problem is solvable in polynomial time if the target value t is expressed in unary.
Let Si , . . . , Sn be the sequence of positive integers. We want to find out if there is a (multi-)subset of S that
sums up to t > 0. For all 0 i n and 0 p t, we define L[i, p] to be true if there is a subset of S1 , . . . , Si
that sums up p, and false otherwise. Then L[n,t] is the solution to the problem. Observe that L[i, 0] is true
for all, i and L[0, p] is false for all p > 0. For i 1 and p 1, there is a subset of S1 , . . . , Si summing up to
p if and only if there is a subset of S1 , . . . , Si1 summing to either p or p Si . Thus, we get
true if p = 0
L[i, p] = false if p > 0 and i = 0
L[i 1, p] L[i 1, p Si ] otherwise.
The term L[n,t] is easily evaluated in O(nt) time using dynamic programming. Let b be the size of the
input in bits. If t is expressed in binary, as is common, then b = O(log2 t) and the running time is O(2b n).
However, if we express t in unary, then b = O(t) and the running time is O(nb), which is polynomial in the
input b.
III-2 (CLRS 34.1-5) Show that if an algorithm makes at most a constant number of calls to polynomial-time
subroutines and performs an additional amount of work that also takes polynomial time, then it runs in
polynomial time. Also show that a polynomial number of calls to polynomial-time subroutines may result
in an exponential-time algorithm.
Let k be the number of subroutines. The algorithm starts out with an input data of size n. Each subroutine
takes (some of) the available data as an input, performs some steps, then returns some amount of data as an
output. Every time a subroutine returns, the output accumulates the amount of data the algorithm has access
to. Any or all of this data may then be given as an input to the next subroutine. Since each subroutine runs
in polynomial time, the output must also have a size polynomial in the size of the input. Let d be an upper
bound on the degree of the polynomials. Then there is a function p(n) = nd + c, where c is a constant, such
that p(n) is an upper bound for the size of the output of any subroutine when given an input of size n.
Let n0 = n and ni = ni1 + p(ni1 ) for all 1 i k. We show by induction that ni is an upper bound for
the amount of data available to the algorithm after the ith subroutine call. The base case is trivial. Assume
the claim holds for i 1 and let n0 be the exact amount of data available before the ith call. Then we have
ni n0 + p(n0 ), since the ith call accumulates the amount of the data by at most p(n0 ). Since p is increasing,
by assumption we have n0 ni1 and p(n0 ) p(ni1 ), from which the claim follows.
We use induction again to show that each ni is polynomial in n. The base case is again trivial. Assume ni1
is polynomial in n. Since the composition of two polynomials is also polynomial, we have that p(ni1 ) is
polynomial in n. Since also the sum of two polynomials is polynomial, we have that ni1 + p(ni1 ) = ni is
polynomial in n. Therefore nk , which is an upper bound for the amount of data after the final subroutine, is
also polynomial in n, and the time must also be polynomial.
For the second part, observe that if we have a subroutine whose output is always twice the size of its input,
and we call this subroutine n times, starting with input of size 1 and always feeding the previous output back
into the subroutine, the final output will have size 2n . This means that the algorithm will take exponential
time.
III-3 (CLRS 34.5-8) In the half 3-CNF satisfiability problem, we are given a 3-CNF formula with n variables
and m clauses, where m is even. We wish to determine whether there exists a truth assignment to the
variables of such that exactly half the clauses evaluate to 0 and exactly half the clauses evaluate to 1.
Prove that the half 3-CNF satisfiability problem is NP-complete. (You may assume that the 3-CNF formula
has at most 3 literals per clause, not necessarily exactly 3.)
First observe that given a 3-CNF formula and an assignment, it is easy to check in polynomial time if the
assignment satisfies exactly half of the clauses. Therefore the half 3-CNF satisfiability is in NP.
1
We show NP-completeness by reduction from 3-CNF satisfiability. Let be a 3-CNF-SAT formula with
n variables and m clauses. We construct a 3-CNF-SAT formula such that exactly half of the clauses in
can be satisfied if and only if can be satisfied. Suppose that yi and zi for i = 1, . . . , m + 1 as well as p
are variables that do not appear in . We add to by all the clauses of , m(m + 1) + 1 distinct clauses
of form {q, q, p}, where q can be any variable (we call these type 1 clauses) and clause {yi , z j , p} for all
i, j {1, . . . , m + 1} (we call these type 2 clauses). Constructing clearly takes polynomial time.
We observe that all type 1 clauses are always satisfied. Since there are total of 2(m + 1)2 clauses, we have
to satisfy precisely m other clauses to satisfy half of the clauses of . If we tried to satisfy any type 2 clause
{yi , z j , p}, we would also satisfy all type 2 clauses with variable yi or all type 2 clauses with variable z j . This
means that we would satisfy at least m + 1 additional clauses, that is, total of at least (m + 1)2 + 1 clauses.
Thus the only way to satisfy exactly (m + 1)2 clauses in is to satisfy all the clauses of . This implies that
exactly half of the clauses in can be satisfied if and only if can be satisfied.
Thus, given a polynomial-time algorithm for the half 3-CNF-SAT problem, we could solve 3-CNF-SAT in
polynomial time. Since we know 3-CNF-SAT to be NP-complete, it follows that the half 3-CNF-SAT is
NP-complete as well.
III-4 (CLRS 34.4-6) Suppose someone gives you a polynomial-time algorithm to decide formula satisfiability.
Describe how to use this algorithm to find satisfying assignments in polynomial time.
Let be the input SAT formula that is satisfiable and contains n variables. Let |xi =0 and |xi =1 be the
simplified SAT formulas obtained by replacing variable xi by values 0 and 1 respectively and eliminating
constants 0 and 1 by partially evaluating the formula. These can be computed in polynomial time. The
results are SAT formulas containing n 1 variables. For example, if = ((x1 x2 ) ((x1 x3 )
x4 )) x2 then
Clearly is satisfiable by assignment containing xi = c if and only if |xi =c is satisfiable. The algorithm
now takes any variable xi and asks the oracle whether |xi =0 is satisfiable. If the answer is yes, then the
algorithm sets xi = 0 and recursively repeats the procedure with |xi =0 . Otherwise |xi =1 must be satisfiable,
so the algorithm sets xi = 1 and recursively repeats the procedure with |xi =1 . Once all variables have been
assigned a value, this assignment satisfies the original SAT formula. The algorithm takes n polynomial time
steps and thus works in polynomial time.
Instead of reducing the formula, one may alternatively augment it with conjunctions, producing formulas of
form xi and xi . Again, one consults the oracle and recurses on a satisfiable formula until for each
variable either the variable or its negation has been added, yielding a satisfying assignment.
III-5 (CLRS 34.5-6) Show that the hamiltonian-path problem is NP-complete. (You may assume that you know
that HAM-CYCLE is NP-complete.)
Again, observe that given a sequence of vertices it is easy to check in polynomial time if the sequence is a
hamiltonian path, and thus the problem is in NP.
We reduce from the hamiltonian cycle problem. Let G = (V, E) be a graph. The reduction transforms graph
G into G0 as follows. We pick an arbitrary vertex v V and add a new vertex v0 that is connected to all the
neighbors of v. We also add new vertices u and u0 so that u is adjacent to v and u0 is adjacent to v0 . This
reduction clearly takes a polynomial time.
To complete the proof, we have to show that G has a hamiltonian cycle if and only if G0 has a hamiltonian
path. Now if there is a hamiltonian cycle (v, v2 , . . . , vn , v) in G, then (u, v, v2 , . . . , vn , v0 , u0 ) is a hamiltonian
path in G0 . On the other hand, if there is a hamiltonian path in G0 , its endpoints have to be u and u0 ,
because these have only on neighbor and thus cannot be in a middle of the path. Thus, the path has form
(u, v, v2 , . . . , vn , v0 , u0 ) and we have that (v, v2 , . . . , vn , v) is a hamiltonian cycle in G.
2
Design and Analysis of Algorithms, Fall 2014
Exercise IV: Solutions
IV-1 (CLRS 17.1-1) If the set of stack operations included a M ULTIPUSH operation, which pushes k items onto
the stack, would the O(1) bound on the amortized cost of stack operations continue to hold?
No. The time complexity of such a series of operations depends on the number of pushes (pops vise versa)
could be made. Since one M ULTIPUSH needs (k) time, performing n M ULTIPUSH operations, each with
k elements, would take (kn) time, leading to amortized cost of (k).
IV-2 (CLRS 17.1-3) Suppose we perform a sequence of n operations on a data structure in which the ith operation
costs i if i is an exact power of 2, and 1 otherwise. Use aggregate analysis to determine the amortized cost
per operation.
In a sequence of n operations there are blog2 nc + 1 exact powers of 2, namely 1, 2, 4, . . . , 2blog2 nc . The total
blog nc
cost of these is a geometric sum i=0 2 2i = 2blog2 nc+1 1 2log2 n+1 = 2n. The rest of the operations are
cheap, each having a cost of 1, and there are less than n such operations. The total cost of all operations is
thus T (n) 2n + n = 3n = O(n), which means O(1) amortized cost per operation.
IV-3 (CLRS 17.2-2 and CLRS 17.3-2) Redo the previous exercise using (a) an accounting method of analysis
and (b) a potential method of analysis.
Now an operation, which is an exact power of 2 uses all previously accumulated credit plus one unit
of its own amortized cost to pay its true cost. It then assigns the remaining unit as credit. On the other
hand, if i is not an exact power of 2, then the operation uses one unit to pay its actual cost and assigns
the remaining two units as credit. This covers the actual cost of the operation. We still have to show
that there will always be enough credit to pay the cost of any power-of-2 operation. Clearly this is
the case for the first operation (i = 1), which happens to be an exact power of two. Let now j > 0.
After 2 j1 :th operation there is one unit of credit. Between operations 2 j1 and 2 j there are 2 j1 1
operations none of which is an exact power of 2. Each assigns two units as credit resulting to total of
1 + 2 (2 j1 1) = 2 j 1 accumulated credit before 2 j th operation. This, together with one unit by
its own, is just enough to cover its true cost. Therefore the amount of credit stays nonnegative all the
time, and the total amorized cost is an upper bound for the total actual cost.
(b) We define the potential function as follows:
(D0 ) = 0 and (Di ) = 2i 2blog2 ic+1 + 1 for i > 0.
(Note that (Di ) actually equals to the amount of credit after ith operation in previous exercise.)
This potential is always nonnegative, so we have (Di ) 0 = (D0 ) for all i. Now
For i, which is an exact power of 2, the potential difference is
(Di ) (Di1 ) = (2i 2i + 1) (2(i 1) i + 1) = 2 i
Note that this also holds for i = 1. Thus, the amortized cost of ith operation is
ci = ci + (Di ) (Di1 ) = i + 2 i = 2.
1
For i, which is not an exact power of 2, the potential difference is
(Di ) (Di1 ) = (2i i + 1) (2(i 1) i + 1) = 2.
Thus, the amortized cost of ith operation is
ci = ci + (Di ) (Di1 ) = 1 + 2 = 3.
As seen above, the amortized cost of each operation is O(1).
IV-4 (CLRS 17.3-4) What is the total cost of executing n of the stack operations P USH, P OP, and M ULTIPOP,
assuming that the stack begins with s0 objects and finishes with sn objects?
Let be the potential function that returns the number of elements in the stack. We know that for this po-
tential function, we have amortized cost 2 for P USH operation and amortized cost 0 for P OP and M ULTIPOP
operations.
The total amortized cost is
n n
ci = ci + (Dn ) (D0 ).
i=1 i=1
Using the potential function and the known amortized costs, we can rewrite the equation as
n n
ci = ci + (D0 ) (Dn )
i=1 i=1
n
= ci + s0 sn
i=1
2n + s0 sn ,
which gives us the total cost of O(n + (s0 sn )). If sn s0 , then this equals to O(n), that is, if the stack
grows, then the work done is limited by the number of operations.
(Note that it does not matter here that the potential may go below the starting potential. The condition
(Dn ) (D0 ) for all n is only required to have ni=1 ci ni=1 ci , but we do not need for that to hold in
this application.)
IV-5 (CLRS 17.4-3) Suppose that instead of contracting a table by halving its size when its load factor drops
below 1/4, we contract it by multiplying its size by 2/3 when its load factor drops below 1/3. Using the
potential function (T ) = |2 N(T ) S(T )|, show that the amortized cost of a TABLE -D ELETE that uses
this strategy is bounded above by a constant. Here N(T ) and S(T ) denote the number of items stored in
table T and the size of T , respectively.
Let ci denote the actual cost of the ith operation, ci its amortized cost and ni , si and i the number of items
stored in the table, the size of the table and the potential after the ith operation, respectively. The potential
i cannot get negative values and we have 0 = 0. Therefore i 0 for all i and the total amortized cost
provides an upper bound on the actual cost.
Now if the ith operation is TABLE -D ELETE, then ni = ni1 1. Consider first the case when the load factor
ni
does not drop below 1/3, i.e. si1 31 . Then the table isnt contracted and si = si1 . We pay ci = 1 for
deleting one item. Thus
ci = ci + i i1
= 1 + |2ni si | |2ni1 si1 |
= 1 + |2ni1 si1 2| |2ni1 si1 | ni = ni1 1, si = si1
1 + |(2ni1 si1 2) (2ni1 si1 )| (reverse) triangle inequality
= 1+2 = 3
2
Now consider the case when the load factor drops below 1/3, i.e.
ni 1 ni1 2
< 2ni < si1 2ni + 2. (1)
si1 3 si1 3
Now we contract the table and have
2 2
si = b2/3 si1 c si1 1 si si1 . (2)
3 3
By combining (1) and (2) we get 2ni 1 si 2ni + 2 and thus |2ni si | 2. Furthermore, from (1) we
get si1 > 3ni and thus |2ni1 si1 | = 2ni1 + si1 ni1 . We pay ci = ni + 1 for deleting one item and
moving the remaining ni items into the contracted table. Then,
ci = ci + i i1
= (ni + 1) + |2ni si | |2ni1 si1 |
(ni + 1) + 2 ni = 3
In both cases the amortized cost is at most 3 and thus bounded above by a constant.
3
!"#$&%
'
(*),+-/.!01+3254768.:9<;=0?>A@CB/DEDEF
GIHAJK^CGMLl|N*hOQq}_?PS]QRUlUTW^{VXvpJZ`~Y\[\`{]Qcr]b^=_1^=`7"aAa1bbQcd`~a*Wef^=ctstsgQ^{hjuq}ikgcdlU`k_?aAccd_1a1mZ`{non]Q`{g,a1^paAa1_<qsx rEctstsmu]^=_1qsa1qwv=`=xzySlU^{v=`
G - Jj-/p+ eGwi\GwJ\
J3fGwi,Gs\ 9<JI>|J8z-/aA*b`{Cg-U.:i,>Gs-/\*J*{
f;pGMe01> Gwu\JI J@zxu. z 0?7eGs\ -/J~ 0E7i, zGs\eJGw\ 9<J*>-<H
Cf-/G}.!eGs>\-/JI"J {z ;p0?01K> . 01@>!0u 9 -/>:e0fGw-|\J3p-U.!Cf>:-<GwQi,{Gs\JIJ ;p01>
9U& 01-/<C01+>1@ -| 6?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-< f.0 0B9<. |9d W+ .
..!9|-/;p. 0E GwN \01Jkf 0? ;=0?9<> |-5 9E.!>> 6 >!9E @Z. 9j0u.!0-U{.I.A.:@0?&> 0 9U.
; j9 0f6 ;d .:9X>:-E9 68 .0 5-<C9U+ 0?> T
9<>-<+dX0?.>k.:.!9 0?>: ;K.!>4-<68>.u+ -C-/> . . ) 0|09 79 .@C;-Ud.:.5>:-<03E 0*: -<6.!9<C-<+ ;p 0;d. .! >-/-<.6.505-/0?9<<01+
9U -/0?C> T +39<>6?+d9<0? > .:.!01-<>!E3. z: D 9< C>6 < .0 !-/. Gw\;=J0 . -UGs.
|. o 0?>:J 09<-< >!0k -u pz-U.!C0>:>:-<01\6{C>!>:0?C;=6?0?0>
>!&0107-/ . 9<=u.!+d>:9{0 Z .&01R <Fd0?Qfu-/9EE>:0+d01 >01 .:9 0 01 9< > C >!0
. R/-/V5.&G . 9< . 0 01> >! . TW0< @ -/ wp+ BUI +d0 HU 9E\?J 01@ >! 9p9 @
&03 z. 0?>:01X >!0|. -U. KR/V 9<C- @&&0 -E0. -U.
Gw\ J9U CGw3))po9 0J .9< -U . 9E> R -<CR +SF z0 C -"<0 )9<>:C9U>"<01;C
- . 006?>:- 0 0< . 9E> R F
GB J Gs J Gw oJ 9<
z9EKE.0-< > 010u -. !> 0 . .zTW . --< C0&+ > 9E >\ ! . +d. 0fTW 9 -/.!fC9
+ GI;=HA!0J +d.!>:09C0 9|QG fB J 9<@ . >: 0 0E0z. 6@ 9E-< B< .:. -/G1 Xw0u. 6 9<BU >! 9E.E-/HX.=. 9 k . J 0z>!9E9EE E.SGs -<9 > k. . J 0
.P{ C060k>!01 f - -U .* 0.:0?-<>:E . ";p0Gs 0? XJz-U . < 0EGs@ 0<o@&.J 9<-U. H X @p &0k-< >!09E>:01 -< GsR F -UR E J C z. -U.
.9{9E 9E0 -)=;p 01-<6 +j
63XH {. {Z 9<;p0 01>!> 01GsR {-/01>:U0?R ZF ;=JS.0?>zG -|f. 6)=-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@ 0A +.: 96 I)>:059U<{0 k9U. -U.
FIKJML NL PORQ
SB C
>.!0A-/0>:>>:-< . TW p-/@ p+ ! +0 { <9EE 0 8 . * 0*U9U-<> 0? -/E;0?>A0 @d. C 69<9fp<+ 0 ..* 9E> + 9 . 9/ .0k47+df0?)=@C0?;=p01+d6?0?-<C 6? 0<0 @
A G
W6
@6
-<6.!KC-<0|-f .7 .!-<.!0 9 !!f 9U)0?>A. -/. 0 Gs- E 07J. -/ . Gw z J& )C>!Gs9{ 9 9{d9<J\
! G d JZ-<>1 @p; d.
6 6
= Gw I G {dJuJ DC@QG d05Ju - K +>! 9E) -/ . 0 G 0u .! 01>!B -<J C+o69E GC61C +d05. -/G d. JIJ Gw P{ JuC6 0
( G G
E
G
G B G J$ B DC J$@ 0Ez@ -/ .
B &0 >:01 -<D5 zN -/0A-/X>:. >:-<* Gs C @<J"& 0 0? 0* . -/z. .* 01.!>:9<0
GsVXJKGMLON
P3V/T F J b` "Bq 5^=cgQgQch r<rEq" g[ n D `XlU_ _1HEcdx aAqs_A|Mm` aAbg``lUaA`XbrE`[\l=lU`{`{gg`XrE`lUcda1q}g
6 ; ;
; ; ZY [ \)
) ] E]
- . 0.$9 01> 0 -/p+ ! 9U . -U.z. 0 > . 0?.!. 0?>: 0?I.Iu. -Up.6 @p "> ) . C5 9<d9/..
Abc) $b Gd fefg ] d
<
Fb Gd hd Vdb Gd $d b d
6.!C-<<@=. C 9/.
B 4C
;@j ;@j ; ;
jG XJ E jG XJ
:;
g
;
:;
g
;
:;
g
;
:;
g
;
d idb d id 3b d 0d ] d ] d (d ] d ] d
8 8
;{ f. >! 010 ) -E0A6 6 9<pk+ . 0SU-/;{> -<; 0 Bd O . 0? .10 9{> 9<. | -U.z. ;{0 oT .!0?H >:@d -/C"+5 01. )C -/0S>-UU.:-/0?> <-/Q;K0 0
< < < <
k2 U2 l2
<0?. U2 d
3G {J 3G {JGIH& J { G J :;
g
; ;
d idb Gd md b Gd m] nd G] i] nd
8 8 8
jG XJ jG XJ
;
;
g
:;
d idb d id b d 0] d i] d ] d
P01{X CC6-<0 }@,- +d0 ! >!0A +Fd @ G <01
0?>-/H M@C-/C9E+ 09<C+ H @{-.< 0S0 .! 9 0S.$-<69u.!9<0> d)H*>: 0 :! 9E -/>: 0
x xy x xy x xy
)=kC0?G 6> 9< >:. -U. -| J?)CG -<f>I. -< > -<J 6. 9<. -U .+d016? 9<@ fH)= 9 J ! . 9E .!9 kC+KG 6 9< .J?-/G X. ,J -<C+ -<C+
md md
Gd $x 8 Gd ~x y 8 k Vd $d Gd x y Gd (x k
01~69E;=C 9/+|. 01XCI -U+d. 0 9E 9 J . z 0 01>!0 > 9<.>:0<0AE@ p -U. 9<o;{ H @-/C+ I -/>: 9<>. 0
; < ; q 8Ktu
e g
* x{d efg xKy d D
< 8ktv u <
Vx{d
b d :; x x y nd
| Ex{d x4y ds}
-
+ 0 !
:
> 1
0
+
G + J3ySlU^{vp`faAbcdaEZW^Cl D F ,l^=[QghQ`{haA^|aAb`fgQ`{cdlU`X_1aSqsg,a1`{=`XlX,qs_ ;
;
;
<
-<9<6..1@{.$9f0 01--<E>!0 0 ! . 9U "X.!u0?E. 0? > -U .z J . 01>!0 9f-/; < .$,<. -U. @{. -U.. 01>!07-/>:0
x
q
;
G F JK^pGMLO_1N
q <Pf`oV RUT _1B cJ a1kqs_ bQ`5`X_jlU [QgGs\gQJ7qsgQ aAqsnGs` B JQGs\Jz ^p "cCugbQc`Xts=l`{^pcl_~qsa1bQa1bQn `olU[^=gQgogq}cggq}g\aAqs]n[`a
] Vx t 8 tv 0x ] 0x
r q w
Y
X GwGw\J3UVX^=Jcg cCWts^p=l^ClU_Aqw^paAbnn `or<^= g^p_1a1gcg,ca g=qsx g]Q[aobQcd^pa|q}_1_q <a1`bQ`t}_AcdcdlUa1pq}`{_ _1a~`{_qsg, a1`{ =Gs\`XJ|l
3
_1B [Qrb9<a1bQ cdVa q}9<_ c_1 mZ@ no 9 ]Qa1 ^p a1q}r<ct}ftwmGw Wc_?aA`XljaAJub cC9<g> 9E0 P{ C60 V D Gs )p@,0A6 0 6? -<-E<0 @
/
V
P
9E9E> Gw\ J B @J . z01>! 0j -/ @d>:05;{u. . >! 010 0~)=9- : I.:0?; > z . 0 01 9<>:0?B 3@ 9<Gw\ J@B Gw 9< J 9<>uBo
h 84 D
<9E> -/U-< P{0 C 690z 0 -<C>!6 0 E. .:0?-/>:.0 B .!0A+ 9< - -/C-<+ >!E0C- >: )p 9 !- ! ;CXf0E@ )d.: 9/. . 610?-/>:0 -<>! 0z- -/.!{0? >. X .!-<0 T
H
H
H
9Ef -<0 68.A/@ B D@ 9 9E Gw\@ JS 0<@ H kGw V J V z S Gs\@{. J7 01 d Gs Gw\J!fJSGs dGs J&J7 9<>
7 6 0 7 684
V V
G B J
G B J V 2
H
6
z @ 9. & 0?0>:0z0?-<0A>!+0 X9/.!0?.fE60?9<> I +d 0?>kH .S 05p6)= 9 :. ! -U; . .$Kd>:. -/ .f- B { f)d9<.!9<. 6?-/9E> B - .!01>9E. -<
H H
z \0>!6? 9< f- ) dX.f-U.)d .:9E9/. -/61;=-/9U< 0 !- .:9U0? >z . . -/-U .&. 0 f-<>!E0 V . C W ! <U-/0*9 f C6 . -U. .L 1 1 +
.! 0 .">: 60 < 0A+. ;{@d>!0 -3 69E. 01 6. >:9<9< 9 -<X-/| T 6;C9<-E+K; 6 C-U ) . 9E |z9 6 &) 0u @616-/9<C+9<. X-CE9U0 ;=0? 019< >
V)
;C -E0>!+07!6. 9U -U) ." -< 0kG}9*z+d. 9 0?-U>:=.0U-<0>! 0 -61<-/ 9E0-/9<0k.Q.!;CC0 -<01 +36.0 >:6!0 -/ ) > . S {. -/9U.7 9E > +d0 >:0 . . -U<.0? 0 C-+ E 610-U-<.!0
;C C-E6?+6?06 :I ) < 01E@= 9< > 0?0+-<>:0 )C 0E@. . f0& .:0 . f ~ 9| . 0f ;=@/0 )= 0?.> &9<0u>:6?-<0A+
s
33
a1^=`{g_1`a1_^=[k_AgQqsgQ`{cdlUa1twmbQ`3bhc`Xt}vZq}a1r<bQ`3`cd_AlUq `3E`=_1x|[O -<r<;pq}`X01g,a. a10f^K6 lC`{ ) hQ [rE`j@ a1bQ`|H<]Q lU^ Its`{n fKa1^ 0 ; )
-/>:>)-)= 9 0C0 6 -. E 0-U. -/ -<H E9< > . . 0 >:0 . GG {9<J > J 6 9 >: 0.:I >: u-~.:.$9&9.!0 T 01 .fE.: >! @
G
p 2 9
s2
33
;
.$.-/ p-U95+ .
6 . B 0?) ,3 @C -/. >:. 0 010-<>:. 0 < 0 9{9d .*-<+689 @ .
95. I-/.
05f6? -<9<~>:.:09f) . .!6 0 -< .
G} --/&J . 9 . 0 . 0? >:0?> 0k.-/>:9E-/0S>:00<9u<9{4
f9d. 9<+j >:0?07>: . 0A -/-< 0< @
3 ;
-/p0+69E+ !6? -<+d>:01+ >" 6 79{. 9 !0? )C- 0 > : 9 01 -E66 )>!01 )p@X9E.:0>I. . . 0*. 9< . 01 01> -/E - < 9{9d .+9< z0S-/ 9<0A. - <010 >1 @
- - Eu0
-/. 010k>-/) < 9E0> 9 . 6 G 7 ) J @= C 6 &0.!0 .S>!0A6> I <01EK9/. 0?>S9<>+ @=0
E H fzC +0 0| f)d9E.>!07-<>!<>9{-9d +|6 C ) .Q. ;= 0-<|9<;C -ET +0?f )d.$E@U-/C+ . 01E.:> 0 .
Zssk G
2H D qs
B f H B
p3
3
G
,~,
H<H BH `{ghQqs B
1
H1R`{ghQW^Cl
,
HH,~,
H
HH 2 `{W^Cgl hQqs H .:9 0?<. s BU
K
B/B DH qs B H H
P
=
+ -
BB<VF`{6>:g01hQ-/.!W^C0Sl -/>:>:- H
V BB lU`E aA[\lUg
5+s G ,
3
,
G J G J
-/,0 6 .: -/) >! .k z. 0 )qs >:9{.!0 6? 01.+d9<C5>!05 C0 . H F )Cd. 0?IdH<)p 0A68.!0AI+5 ;@f. 01f-< )p 9E>I~.-/. X . -U.G 69< . I.:0 +d 0?. >
Z+k
33
+
>:-/p+ . 0. - . -/;C<-E9E+> @/. . 9<9E|d. . . >:J 0? 0k6 ) @d9 G k 6 J. 0 -/> -< .zE9<.$&> 9u. -<>!07 <9{ 9d6 +
9dz+ + 0 07C-/+ >: 0*60 . @ 0 ! CC+6?0 60 . 09 0?. X .:07> 60 C ) . 0? 6 < 01 0 p + - 6?<0 0 -/9/>:.z0~<0?-/.z
+ u 6?-/-/>:<>+d0A+0A+W .J
V
; d01.*- 0S.:9<9 01."0z. . 0k-/.+d0?. .:- 61-/f;=0*+d9<0 f. f0 Gw\J @< 01>!0 ~ 0?C/. @ s .
0-69{- 9< ) . { U-/-/. > -<~X. 9<>: 9<0S>. . -<0 W ^p-/l 99{ 9<). 9<06 C) 0 9/K.7fC+-<0?>!01E+Z01+ @&0j-<-/>!>:005< 9{ 9d<+01
/B &C
.. f-U.0 .C6>: 0?.!f>:0?07X.:01)=+9<@E . 01 . 0 -/ . 0?-U> . 9EZ3 >:. 0 .:0 >: .:-/01.!+ 01f01X .z-/- C +f.!>:C906 . -<0S<- 0 .
;{+d! 0Af6-U>:)01.S-f 9<019 +~; . ;X0?>:H < 0zBz@. C-U7. 0. .. & 0f0. {fp- .! >:X;p.:0
010?>7C. -<9 C-/ .60 fCf9<.:>:0?-<0")Z>!E@Z. 01-<+-/C +E@9{ 9{-U-/.7+K .!96 01 >! . ) 07C+d-U6 01.C6? 9E>!) Z0A - @ 019/+ 0.
uP -/)C>:)p<90A+ 0|. -/-/>:.107@<-/9{.9d+ 0 H Fd@0?-<68.: i 9 . 036 ) p+d0d01+;{. 0j T
F
u C-/+d>:0<d0A0A+|+K01;{X.!> . 0 059 C u-/H >:<01+B 01E .:B> 0 -/9 >:0k<9{H9d+@=-/B Cw+ 0?B -<68-/.:>: 0f ;C-<9 + . Gs0 6 9/C. )60
=
33
G9Es>Gw-/J|J{ii ;p 0?@ 9<.$K&> 010? 9d+H +3-<C-/+C+w. BU0kE6 @ C ) CB Q+d05dH0A+| ;{ Cf-<>!E01 +E@9X. 9d +0?. 0z6 C )
.. C +d00 0W d^p. 0A l + T 9{;{9E)u9<-/B >:< 0A+u0 H 0?H X-<2 .:C> + 0- 9 B !-/H >:0A0&+@/;=09/B . w-E 68E .!BU9X 9diC+-/ 9<>:B">0
9 ;p< 9<9{. . 9d +0z;C@d6 -/-ECp+ ) + Qz0? dC -E+d 680? .:{@/0A + ?;{01 B
K 3
+
9-/>:0j. ;C0-<+6 ) 9< >:p0?+d9U0Ed0?01>A+@ I;{ Co603. .0 0 . W^C T l C9{u9E-/)>:<u01+-/>:0? X .!> 9< 0 09 9 . 0H >!01uB - BC
f
3
33
-/>:0 u-<>!E01+~ 01XB.!> 0 ;C-<+ B -U.k 0A-<6 B .:0?>-U. u 9E-/Z>:@\<- 0A +5.:0?0?> X.: . > 0 - >:0?6u9<f- )0.:C01 +Kz. 010? >:0
GSs.Gw J|J0ii-U .IBkB .:0? >Q6???- BkBd0<@ 9E@UK-z> .: 9/.9d-/+<+39 Z-/GCiC+| B . J8 u0kH 6 6 ) ) ZC+d C0?+d{0?0A{+j0A+S;X ;{7C \u -/>:<<9{019d+7+01 E.:> 0
, , , o
99 -/-<>:>!00<;C9{-E9d++@1 P{ C0?6?>:010 - Gw i-z B.!9<J.:-<<Hf9 C? G}Bz16 B J ) Z HC+d 0?{10A +Bd;{@,C0 u. -/ >:< 01-+7<001Ef.:> 9< 0>:0
&
iC<P 9{9dB)C+)p 9 6 GM0 ? ) iB JZB. -/H G};p?-<B+J3
oH . z0 9E0?>!f 0?> 6?B- 0< @Q0 3. 0?>B iC B Gwi B GMJ?B JG}? B H Ju9< >
6B CGM+d? ) 0QdB J0A p+
+d;{0Hjdo01+ ;{9duk++-/>:@ <u 0A9 +o-/>:0?<X0A .:+>* 0?0 X .:>f9 0 -< >!E9 01 -<+ >!0|5-<<>! 9{09d;Cz+-E @+ P{ -~01C>!.:6?0A9/0- . iC-/ "-~B9 .: 9/.:i?-< zBB9 @U679<CC? )6?B0
, ,
K $
.0?-/XX-U.:- >. 0 i Bj09 -EG}0?-< f>!B 0JQ9EE>!0"9X9dEH +9X 9d@z+f 6 0?01 >!) 0A-U - .. 0A-U-/- .uu . f;CiC-E9 + B|. P6G}1 )) B )=7J9 p0<+d@EH 09<df6 01+K. ) ;{0*u9/ C. +d0?0u>d0A-/+>:-<<C;{0A++ @
{
-/d)C. -< >I0*. 0?6?X.:> -/ >0 Gs9 C0dC)p+d0A680?.:0A01++ @ J 6&9<0 0A-<E+fC0?uC6?-/0 >:<0A +. -<-/d.
. & 0*0 01 E-.:<> 0 0 ,9U u@E-<.>!E0101+
00-E-E6868.!.!5DD9 9 . . 00j6 6C ) ) & C p+d+d0?d0d01+|01+;Xu;XCuCf-/>:-<<>!01E+01+0?X01.!E> .:0 > 0 9 9 -<>!07-/>:<059{9d;C+-<+Z-<@C+-
s
.6z9E 0X .!>-/-<. .I+.!01068>1.>: 019E6?u> 9 ! <. 0 06?-< -E<68.!.9 . -U.. 0?>:0 ! 9<+uS;=09Ef 9E >!0&0*B 9 .-< 61060? )9<. >: f- 0?Z> . )-<d .
l
C
; -k-EC+9<9ET0?0 f @d)d .$5 -<6 >!j>- )f>:9E016>!0 0?9 u . 9 0 001E0?X-/.!> 0 )d .Ap +d05E9X9d+56 C ) . -/ C+0
kZ+k
-/-/)C) 6?)-<d;.
0 9 Q !01 >!10E0 =G <f01 ! & .S 0?>:0f -/B )E) z 6? -/0;Cu0E@- & .:00?>z&. 9E 0?9<+ >:0? -E0 .:. 9| E 0?>9/ .
J i
+sk
.P{ C-U6. 0|e. GwC\Jf-/. + u016?+. . 0.:9o69E+dC9K+ . 9<. 9<d{.5eGs{/9U Jj 0?eGs-<\68.:Jo 9<> 9E-Uf. 0 9<>: . H 0
G
-/pC+6. 9<p +d0?e> T 0 .: -/. E u0 -/@ . .3 9< + . ;=0K- 0A{- f! )d01>5.!9<.!. 9 6?-/+d9 9E +>j0?X . 6?p-<-/0 .! > . 6f-/.!9 0 9UJ E0?> T
.)C -<-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,P*2WH_7_
P*uWHTVWHWHuQXYbmXUdQ_7y\WQ7N
Wx_Ud%Psts1P2m,| o~}4}7}p!MD IK)M2fr*)
q/ 8 I*)(6q/26r3I46q)73.)7f8H22f2*2)M/ Ixq*)*6/2623
)K6)43)4f78^r]q4)B)=26=M 8 o r p 8^/25H0HI/2'qr
D
r I3)7 8 zo r p 8fq/[B)M*)4)/2*I,*6/102)B1rrIqI3)7 +zo
r 8 pHGM *)[)7=(q*) vxN
yVTW I/f/S/2I*) VrII(/2q*),6q
q6q/'*2x*) \X I/f/ o; q4) m| o> 8*q/ hB*/rM)K6)43)4fq)
o /?*)6~I/H/2 GS ?*)/2q*)76~ri8qK)28
/2z*)6~q
q6q/'*2S*) \
X I/H/2^8 8 o 8B)~t0)[qr m| 8
8q/x*)~)7=Iq*)
vxNyTVW /H/2]/2I*) <2< VrII(33)7h1)4IS/2*)~6=qq*6/2'*Sq*)
\
X /H/2 G
M )46q*) vxN
yVTW I/f/b/xI*)7 r << t02)B)463.Irq)7^8B)t0)1
m,| 8 .O r m,| 2+ OdG -(/1*)46)r6)//=I5*IqI)7 G q) yVY
q)/II*) < h'*q'44)7k'*!8*)4S)K)K'/4)72q)7Mq*) s1WQUds1u
I6q'qI/2d8Bq/)26q)S/2) G -(/rq)Sk/6x'q)5)7/1qr78*I42q)28
FodG
bq),)7=hq'7K)7=k'*8B),qB2 m,| 8 2 m,| 2 8*)4?K6)43)4f 8 2
)K6)43)4f G( H'(q*)76q).r6)[I4)7F E 8 G< o rI H [<.p
q'q1 m,| E 2 m| H J dG, *)7K HF L ErM E 8 H G
26=I4'*I2678k/26*)6)43x2*)46/r*).*6q/26r3).t0)~q1 H2
8 E G -/2q)k/6('q)[5)7/1q178Ir6qqhK'*hr68 p k/6Mq),6)43xrI)76
/rq)M*6q/26r3 G -(/1*)M/2*I,t[1 m| E /6 m,| H MI*)702)76b5)M3/102)
Iq)6)43xrI)76/2q).6q/26r3I8^1[=/3.)./2I8i)6q)2*) yVY
q)/2?I*) < M*)7 8 H/26 E G (/1)702)7678?qI[72q) 8 HF
/26 E 8 8^6)7q)0)4I28^q/*) yVY q)7=VrII Gx H'*)4q*)76 m,| E */26
m,| H MId)402)765)[3/102) Gb *)4*) vxNyTVW )7=/2I*) VrIIM)4*)702)46
E8r*) vN
yVTW q)~/?I) << V2hM*)7*)40)46 8 H Gx f' 8 I
*)702)46IK6)43)7)7) H p 82 I*)40)46M)746q)73)4fq)2=% E ]oG
; >CKYMowpHQ7N
WHuMvxN
Wfu . jad0c /21q`badcfegheigkjlQWfstyVu
PQ7WH_o?wpf
x26= ; :15 > 8f)(q*/1B)7q178H/q)4631qI/2d8 .Fo /6 pHG op 8
I)7*)46(72q) xpHG
; > CKYopHQ7N
WHuvxN
Wfu . jad0c /21=`baceigheigkjlQ7Wfs1yVu
PQWf_WfRWstWHTk
WHWHuQ?XYm,| o~}7}4}!2,y_zTWH_7_?Q7N
P*uXsWHiUPTQXwWfRWfst{WHTVWHWHuQ]XY
m,|I;~<>}7}4}px )xKhrI3q1q*).k/I/1M=1)43)4fI6q').r)70 J
)46])4)7K'*qI/2F/rI*) < O N02)46)76qz/r m| o~}7}4}4; 8 <> I,)q[q2
/26[)f'r%/ 8
)402)76q])4fq6]/2 m|;I,<t>}4}7}p h[26)7rq)46r/6[)7f'r
q/ 8 m| 8 2 m| 2G ; -/rI4),1[B)26q) PQSR%4I23I*1
qI=rq)73.)7fIbq6'*) 5)42'=)M*)7)7^ 5)2 M*)7x)M5)4.q) \X
I/f/^8r/*,/2I*) < G >~ )M6q/10)1b*I1q)73)4fIbI/H/2H01r6hrf
I*)['q'r^t
T CKu
ykQ7yP*TVy_7P*Q7yVXuGd *)6=do3 )B)6)7[I*) < 8*)m,)7| f= m| 8 d
Bq'7K)7=k'*
k/26)40)46 8 8 8^2*).q) 2
='4K)q=k'*(k/26)702)76q wOf p 85*'q*)]q)7= m,| 8 2
m,| 2 B)46)'*q'44)7k'* G
T P*yuQ7WHuPuWGF@ /2qh)466q)2*I*I*) < ]r q)76,*)6=,qI3) G
&()7*/r)5H 8VUXW Y 2 UOW Y q)01rI'*)7,/r 8 2 *)h2=[qI3)B)
6q)2*)I*) < G 1B)?t02)]6)7*)7I*) < r23.)r
qr 8VUOW Y UOW Y 2B))76=k/6q3)zq*)=rF/FI*) < : G *)7
rI)76qI)7~/r m,|<}4}7}7; 8ZUXW Y <> 26q)xI)7[r/6~)7f'2/ 2
rI)7fq6)~/r m,|I; UOW Y <>}7}4}qp7 26q)6q)1)46~r/26.)7f'rq/ d
5*'.V28=I4)B)qB2*) m,| 8VUOW Y r m,| UOW Y 8b)40)42B)4 J
q6)./r m|<}7}4} 8VUXW Y r6)I)7,r/26.)7f'2Bq/ 22)76qI)7
[
r/ m,| UXW Y }4}7}=p r6).)qrz/26)7f'2%q/ dG ?q*)x3/f[6)7K)7f
)K)74'qI/2/rb*) vxN
yVTW I/f//I*) r << 8)[k/'*S1
q*)~q)7= m| 8
2q'7K)7=k'*ik/26)40)46 8VUOW Y <~ 8 d 8 82
q*))7= m| f BBq'7K)7=k'*k/26M)702)46 xFfi UXW Y < 85*'
q*)q)7= m,| 8 2 m,| 2 VrI) Gb H'bB)Mt02)M3xrIf2*)
q*)[IH01r6I2 G
T]\ G
Wfs1yuPQyVXu_G ^ /6bq*)*6/226r3q/[)463.Irq)28)(3,m,'| o~=}4}7}7t; 0) 8 < >
*)7~*)/H/2H01r6hrf%t^1%)40)46[)4fq6[/r 8
I~)q~q2F/26~)'rb/ r)402)76q])4fq6]/2 m|;I<>}7}4}p I
26)7rq)46qr/26x)'rq/ 8BMI) m,| 8 ` r m| 2G
8 w 8q*h[3)72q1 m,| 2 8
q/S)40)46)4fq6]/2 m| o,}4}4}!r I
)4q*)76,r)76q]/r m,| o~}4}7}7; 8 <> /26 m| 2 8)44)xI[I)7r/26
)7f'2iq/ 8M*hIS'*6qSI)7qr/26M)f'ri/x)40)46)4f6q/r
m,|I;BF<t>}4}7}p!G 8 8fq*)7)40)46)7fq6/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! .$' !. 6 .. + !/. rB !r. 6 !/!/+\! '
! ! ' .. 0 ! x ' ". !/. '+ . ' .! ! ! . '+\!,. !
! u ' ..
a! ' !,. ! / ' .!/!P' !$ '. 0 g ' A ! ' ! *A
-!/! -` '+ . ' 0 !<! r r , ! 6 " '+ . '
B !. > "B , ! 6 "3' !S*-" ' a ! -!/ !
! )! -` ! ! ' r ! I ! '. ! P .. ' !! . ! ' !r. 6
.. 4 "'.<! + !S0 'r. .. !,.!$i%"23 B i#" @ 5 130 ! !
'. ! .. 'S" -!\:#%"2" @ " 5 130 !c! . ! ' > !r. 6
.. .! ! ! ' r 0 . r ' !( -!/' !! E ! '"
.! #%"2SG&' ! r < ! .!' ' !,. ! !/ 0 ' !
-!/ '" w $ !,-! 2#%"S 7? ! !/.! .! y ' 6 -"! -!,
4 ' ! '.! $[A !/!/+\! r- ! ' { 5 ,//# [ 7 0 r !,)!/. ' 6 -"! -!/
+$ u ' / ".' w ! 6 ' !z.! r -r!! 0 !g . ! { ! 6 ' .!,!
4 a ' A .! r -"! ' ! .!\.!,+\'*)! 7( ! u .! "A
-r!z! )S! ? ! 4 - . 7 .!,!z' ! P +\' 6 # ' ! 0
! r )S! y ! ! S ' { ! ! 6 ' .!/! ! 6 ' y ? !
! ! S ' ! ! 6 ' .!,! ! +\! ! '. u A ! r+$-!/.<'
'S+ . ' 0 ! 'r ! ' ' r ' < # [w4[{7
y !,+ " ' . !' ' 4 # [{7S #
' [rr . ' !
! `+ ' c '.+$" ' d 4 ' 7 0-" '`! E '. 0
'. ! '' " .! ' yr ' 4 ' 7 +\! r < !/.!
.! ' 6 B r ' ' 4 7 '
'. / ! . !<.! / r -3 + !/. ' 6 B r
-` . !/.\' ! ! ! .S0 !+ "+\! r z ! !S
' $ '. 1"0 -" 5 80 &( '. + ! ' ! . 1 ' ' .'-"!/5 + 0'" .
5 0
!
'
4 7
!, 3 !/)S! ' < 'S. 5 y ! rE )S! ' 4 # [{7S #
' [
4 # [{7 ' 4 # [{7 # [ ' 4m[{ 7 # [} # 4 7[ ' [ )S! I !
rS 5 0 !!/..'. !,.+ 'r -! 4m[{7 0 ' 4[{7
I ' 6 -"! ' ' ! ! .! .! " " ! " '. !' ? . "
r .' + ' g4 u ! ' uy ' !,3"! !+! ' ! -'*)! 7 0`-"
B !/
c' ' .'*) ! ! '' " + !,. . r+!
!(- ' A
+ !,'.!/+x0
# [ # [ 5 5 4u5T57
+ ! " ' ' r : 4 # #[ 5 # 7
" !
. r .,0 : 1 $ [ 0 r i 1 $ Q[
y +\! B < r V 0 1",//,# [ y
4 # %[ T57 # [ [ # [ #
'' :y # [e ' 4 # &[ 57 ? !z' 4 # '[ 57 4[{7 0 ! .!
!
4 7}C{M>H`OYL,MGNLbnmZ{LOPHK`hmK`oK`UL,JyNW*K_HU>JK`_D>L,nmRKnmU}LM>KJ,HWL,K`]gHW*]>K3W
N"UG]8_SHo K$Z:W*Ho HF>FIHJnL,KPNWW*NXEJSbL,MGK`UgL,MGK3Xgo8DGJ,L|IK\_HoFGN"WK`]j
? ' ! r ( !P'. r .. .!) ( /,/,* ( B ) + /,/, + r
r ( r +.- .! ' ! )! ! 'S . ! 'S. !/.$< 6 " !
.. c ! ! .0 !+ " ' ! ( +*- ? " ' ! '
'S+ . ' + !-! u !,! ( r +*- ? ! '+ . "P !, !,+\! 0
' !/ . rB ( 'S./+*-B0 '0+*- \ ! +\! z 'S+ . r z '1( 4 6 !
( Br +*- .! ' ! )S! 7 0 r + . 'S. '+ . " !,!,+\! 0
' !/. r ( 'S.2+*-B0 '3( 0 ! '' ! +\! .' S ! ! / A
' .!,!0 r8 ! !z.! ! +\! ' !0 < !\. 4'S ".+\!/. " A
'S. +' ! u ' 4 '. ! r7 .. ) ( /,/. ( , + - * ( ,///* ( r
) + /,/, + - * ( . + - /,/, + y < r )S! !6! 5! ' > " (
B + - !$'S 0 ) r B 'S..! '. ! } .. S ? !
' . ' 0 !/.!+P u !,. r )!-!/! '+\! 'S+ . ' + !
-! u !/! (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' 0 ! )! r
8
! ' .'S+
4 !/! ' Y5 X 2\'
U ! ! 7 r ( !$. " " +!
' ;c{>{3 ' wB ' ! " w[ 4[{$7 T
\4 7 0 !/.!
" I! ! ! " +$ ] -!/\.< ' ^ ! I . ! ! I +\!S ! ? ! < Z ! H4 ' e6 5*7 [[ .[! [{7G B! r +\ !!0 ' ! 6r )S !
r
\4 7 6a
` _
? ! F D L3bcdL r! G ! ' r ' ' ! .!/) ' . . 0 ! r )S!
r
_ [
a
' ' -'*)S! ? ! \ 4 ' 7 \4 7 0 ! 6 r )! r
kjmlYnYo"o"n@pr! qY.su tu" vC "w xz y|{% }+\~,! lYn2 "lY4m)[{nY7 o"i)TYo$4
YlY |\4 l u 7 0 @' G ! tzu}h r4 )S}! h @4 tY}N} !(lY!/" oG @! tz }!
"lY)|~,l6pYonYo"| ,~.| o~.2/x>Y)4tY},YN4}:6lY"o>r ro"o"N>Yo"|o) | op,oY~oE)r~.
YrlY"|o~,|lYYp.|krM4tz},erM4}/xz6lY"o
|z|)Yo
4~,%xzp,u@ GtY}.G@4},e4tz}@
~lYlYnzo")o*~,)lYe Ylz")l@6Y*p4
~,l/xzp4utY}@ }4tz}>/@x@<tz}u@ x}u@ tz}&xz
. " " +\! \4m[{7 !}' !,. rB 0 ' ! r \ ! ' F '
H 1r J7y ! ! `-
L L L 1
F N4 L7
1r L 1
<!"+'*1"{:+*7*
b
c d
3 d 2 1
O
30 2"{|*B"+<)"@"{'*'*,552"2U:{*1|h<8'y*y*,!@~'2625:{*B"{<~"{'*](*,&"{2
"{'*C~'*" z 2:+OpLP~<|*"+'*7:+*3:+*%)"y*,!#" :{*|h<8'y*y*,4*! F "{<* *7P"{:h')*7y*76]*,~5"
<!"{298*8'y*%"{298*@6]*7:+*,(~E"{29~P}D&25!{"+25~G;E"{<*C'*,55"x!)"Uy*,5!"xy25 O
P$<!"{<*B(*7&"+M!%y!{2j)"%y*!#"3y25 O
!" #!$
%'&)(*%+(,-(/.102(4357698:8<;
=?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_ 7,4( ( VC7K1 : (-h(,-& (/ x>o .(45 7( , 7 3&, :(/
=>v@k .(57oJebr)b 7 3&))) .t( =a@-
= 6 @BbZJO!O_CEILNJY)L1IN!Y:X2I=[^z>@O_CFHDL/E[f ? {tw 8 44/-?{tiN!IKQI
f w fhiF[V wc m1bN!CLNJYhLiLNJIuCbI<cu:FHI<DLtX2I<u:L/CK[ /4/- l yC2V
d=af2@kw l l f fMx^xjf f)
vb
u:Y)DRkIu:C2T]O!ZJL/IGF[DL/FHTcIB=[b/@-m (y0 9 (4)(,M (, . & 9 9
( 3 )&7, vb =afyxf)@ 5<( , i: , P=HQ_ ( ,4, 3h) (W 9 &)3 9
9) 3 < , @9 3( ) (/ .t( =ab@_+9 ,4(:5 ) (4(435 79 5) k(, . & (
( & W 3 h&7, P= | .t ( (43.i @ ,-(:5 (/(, 3(/, :(43 (
3 )&7, j < 7( (/3. .t (/0 ?) (h? 5 , (,
&), B0<_3 0 (/. >=H (13( h(43o 7 & :(/3 a 3o ?( 7Q (, ) :
, . & ( (gh(4 .t Q 3&7) 3, @
W: ( 7Q 5r k( 7 :(1 M[ 3o, . & (/ ( 3 )&7, yi=af2@'wh b f r z
4/zj fz^ l & &7 02( a 3( ( :j (43.B5 (/b5 y|e3 0 (4. > 5 (
3 )&7, 7, &7h) 7Q (43.
r
i=af@-=afxjf @ewc rf z = rx f @f z=?x7l/f @
&7e(+, M, . & ( (+07 3 h&7, k a: W ! ( 3:&).t(/ e (43(W (
33 t 8 /4 ) ?( (4 3M [f ? >42w}f 7 H 6 2w
yabo2J = @
> ,-3( 9 (o:(, 3
6 >
VC7Kyw 8 ix>
)W) r
; VCK1!wx> B>
r x : >-
*
I !
D J
G
V CK
'
l
x7l : >-
Q
K <
I /
L J
Z Q
K D
> 8 IDJG!VC7K
(y3( h(/3 ) & , (/, 7Qi[ (, (-M,4(/ :(, 3 9 r l =[ftxf @?
: < 3 a 3 ( & (43 VCK1<b_ L/I<KQTcF[D!YhL/FHCD 5 W h 7Q
(g, (4i,4(/ (/, 3 :J (0) 3 h&7, 79 k(y /e 7,-( ())(43
VC7K (4h(/,-& (/' =a@ .t(/ a 3W( , (-h(/,4& :! ( & (43 V CK:h (
& (43 VC7K1: (-h(/,4& ( .t(/ ) 7(4:(/3t7 3&7, :( .t( =>v@ 5h(
7 :( 7Q ya'J 7
3&)7)7 .(+ =[b@-= [ , 5: e( )
79 + , & 9 3&)))) .t( =a @ 5)0)& ( )(/(/ (43( @
e(4 ) &) ( : 3 . 02( ,h :9 :W &)7, 5 &7
, 33) & t ( 3 a 3 3^h( j9o) 0)&7 & d=af2@ =ab/@
)) 9 .t(
bJ J = @
>^B (/) 7x>
6 ya'J = @
,-3( 9 (o:(, 3
VC7K!w>'t
;
oJebr)b = >-a@ >-
VCKow 8 ipx>
* >
FHV w
=af x^f @
>8 ID!GJFHV
>:> ID!GJVCK
> 6 VCKow 8 ipx>
> z H 6
> ID!GJVCK
> ; IDJG!VC7K
> KQI<L/ZJKvD
= y-7r^ ( 9 3 .h(,4302(/e3 0 (/. >'W: ( 79' )(
;^ & ))
(P, (-M,4(/ (/, 3 :' ( & 9 (/ i:' (0)
3 )&7, , . & (/ 0 :(y0 fx >- 5h ( 5 fMxjf _ (g(4 3 >4 7 (
(47102(/, &7(( 3( 3 ) ( (/3. s , oJr)r
9 r3 h&7,4(/ @ (M 3( h(43g ) & |, (, Q (, (-M,4(/ :(/, 3
9 l b p: 3 ya 3 ( & (43 VCKM<b
L/I<KQTcF[D!YhLF[C2D 5 h 7Qt7: ) (, (-M,-(4 (/, 3 9 (h(?3(
.t :H 99 ())(/3 V CKW: !(-h(,-& ( .t(/ a 3!(/:(/3(4(,-&
9 ( & (43 VCKk:b ( & (43 VCK_: (-h(/,4& ( .t(/ 9 7r5(-),4( hra 3 (
?) (, 9J ya'J = , 3&)7' .t(t =[b@@ 5r(4(437 3&7,
:(/ .t( =?>@e &7 ( 9 3 . 7 3&)))) .t( =ab@
Solution Manual for:
Introduction to ALGORITHMS (Second Edition)
by T. Cormen, C. Leiserson, and R. Rivest
John L. Weatherwax
Acknowledgments
Special thanks to Avi Cohenal for finding typos in these notes. All comments were (and are)
much appreciated.
wax@alum.mit.edu
1
Chapter 1 (The Role of Algorithms in Computing)
1.1 (Algorithms)
Sorting is done in all sorts of computational problems. It is especially helpful with regard
to keeping data in a understood ordering so that other algorithms can then work easily
and efficiently on the underlying sorted items. One such example of such an algorithm is
searching for a specific key in a sequence of elements. When the elements are sorted searching
can be done more efficiently.
Selecting the optimal order to multiply matrices can occur in programs/algorithms that
update their state through linear transformations. When this is the case, and the trans-
formations can be cashed, in other words they dont need to be performed immediately, then
computing an optimal ordering in which to calculate the individual matrix products could
radically reduce the total computational time.
Finding the convex hull occurs in many graphics programs where the convex hull finding
algorithm needs to determine the largest box required to contain all the given data points.
Other common measures of efficiency used to compare algorithms could be anything that
might be constrained in a real world setting. Examples of this are memory constraints
both disk and random access memory (RAM), the number of memory accesses, determinism
as opposed to a randomize algorithm, number of files created, number of sockets opened,
number of Internet connections established etc.
A common data structure often used is a linked list. Such a data structure can easily insert
items into any location within the data structure once the desire insertion point is known. A
linked list structure cannot locate new elements or locations quickly since it must effectively
look at each element one at a time until the desired one is found.
Exercise 1.1-4 (shortest-path v.s. traveling-salesman problems)
In the shortest-path problem the path through the network is often only one way. It is not
required to end up at the starting location, but just to end at the last destination desired.
In the traveling-salesman problem the algorithm must start and end at the same location
while visiting all other destinations en-route. This requirement that we start and end at the
same location while visiting all intermediate locations makes the problem more difficult.
There are relatively few situations in real life where only the optimal will do. This is because
normally in formulating a physical problem into a framework that an algorithm can solve
involves approximations and simplifications and using an approximate algorithm (assuming
that it is not way off of optimal) does not introduce errors greater than have already been
introduced in the approximations up to that point.
Chapter 28 (Matrix Operations)
These two facts are a simple consequences of the definition of a transpose: the (i, j)th
element in M T is the (j, i)th element in M. For A + B we have that the (i, j)th element in
(A + B)T is the (j, i)th element in A + B, which is the sum of the (j, i)th elements in A and
B individually, which is also the sum of the (i, j)th elements in AT and B T individually. So
we have that the (i, j)th element in (A + B)T is the same as the sum of the (i, j)th element
from AT and B T . Since A and B are symmetric we have that
(A + B)T = AT + B T = A + B
and the matrix A + B is symmetric. The proof for the difference of A and B is done in the
same way.
To prove that (AB)T = B T AT we begin by looking at the components of the product AB.
For simplicity assume A and B are n by n. Then the (i, j)th entry of AB is given by
n
X
(AB)i,j = Ai,k Bk,j .
k=1
Note that the components of A in the above can be expressed in term of As transpose since
Aj,k = (AT )k,j . The same can be said for B and when this substitution is done we have
n
X n
X
((AB)T )i,j = (AB)j,i = (AT )j,k (B T )i,k = (B T )i,k (AT )j,k .
k=1 k=1
Where in the first summation we have replace Aj,k with (AT )k,j (similarly for B) and in the
second summation we just placed the term B T before the term involving AT . The above can
be recognized as the (i, j)th element of the product B T AT as expected.
Using the fact that (AB)T = B T AT (proven above), and that (AT )T = A, for the product
AT A we have that
(AT A)T = (A)T (AT )T = AT A .
thus since AT A when transposed equals itself we have that it is a symmetric matrix as
requested.
Exercise 28.1-3 (uniqueness of a matrix inverse)
If we assume (to reach a contradiction) that both B and C are inverses of A then we must
have that
AB = I and BA = I
AC = I and CA = I ,
Multiplying the equation AB = I on the left by C gives CAB = C. Using the fact that
CA = I, the above simplifies to B = C, showing that the inverse of a matrix must be unique.
We will prove that the product of two lower triangular matrices is lower triangular by in-
duction. We begin with n = 2 for which we have
l11 0 m11 0 l11 m11 0
=
l21 l22 m21 m22 l21 m11 + l22 m21 l22 m22
which is lower triangular. Assume the product of two lower triangular matrices of size nn
is also lower triangular and consider two lower triangular matrices of size n + 1. Performing
a bordered block partitioning of the two lower triangular matrices we have that
Ln 0 Mn 0
Ln+1 = and Mn+1 =
lT ln+1,n+1 mT mn+1,n+1
where the single subscripts denote the order of the matrices. With bordered block decom-
position of our two individual matrices we have a product given by
Ln Mn 0
Ln+1 Mn+1 = .
lT Mn + ln+1,n+1mT ln+1,n+1 mn1 ,n+1
Since by the induction hypotheses the product Ln Mn is lower triangular we see that our
product Ln+1 Mn+1 is lower triangular.
The fact that the determinant of a triangular matrix is equal to the product of the diagonal
elements, can easily be proved by induction. Lets assume without loss of generality that
our system is lower triangular (upper triangular systems are transposes of lower triangular
systems) and let n = 1 then |G| = g11 trivially. Now assume that for a triangular system
of size n n that the determinant is given by the product of its n diagonal elements and
consider a matrix G of size (n + 1) (n + 1) partitioned into a leading matrix G11 of size
n n.
G11 0
G= .
hT gn+1,n+1
Now by expanding the determinant of G along its last column we see that
n
Y n+1
Y
|G| = gn+1,n+1|G11 | = gn+1,n+1 gii = gii ,
i=1 i=1
proving by induction that the determinant of a triangular matrix is equal to the product of
its diagonal elements.
We will prove that the inverse of a lower triangular matrix L (if it exists) is lower triangular
by induction. We assume that we are given an L that is non-singular and lower triangular.
We want to prove that L1 is lower triangular. We will do this by using induction on n the
dimension of L. For n = 1 L is a scalar and L1 is also a scalar. Trivially both are lower
triangular. Now assume that if L is non-singular and lower triangular of size n n, then
L1 has the same property. Let L be a matrix of size (n + 1) (n + 1) and partition L as
follows
L11 0
L= .
L21 L22
Where L11 and L22 are both lower triangular matrices of sizes less than n n, so that we
can apply the induction hypothesis. Let M = L1 and partition M con formally i.e.
M11 M12
M= .
M21 M22
We want to show that M12 must be zero. Now since ML = I by multiplying the matrices
above out we obtain
L11 0 M11 M12
LM =
L21 L22 M21 M22
L11 M11 L11 M12 I 0
= =
L21 M11 + L22 M21 L21 M12 + L22 M22 0 I
Equating block components gives
L11 M11 = I
L11 M12 = 0
L21 M11 + L22 M21 = 0
L21 M12 + L22 M22 = I.
By the induction hypothesis both L11 and L22 are invertible. Thus the equation L11 M11 = I
gives M11 = L1 11 , and the equation L11 M12 = 0 gives M12 = 0. With these two conditions
the equation L21 M12 + L22 M22 = I becomes L22 M22 = I. Since L22 is invertible we compute
that M22 = L1 22 . As both L11 and L22 are lower triangular of size less than n n by the
induction hypothesis their inverse are lower triangular and we see that M itself is then lower
triangular since 1
L11 0
M= .
M21 L1 22
Thus by the principle of induction we have shown that the inverse of a lower triangular
matrix is lower triangular.
From the definition of a permutation matrix (a matrix with only a single one in each
row/column and all other elements zero) the product P A can be computed by recognizing
that each row of P when multiplied into A will select a single row of A and thus produces a
permutation of the rows of A. In the same way the product AP will produce a permutation
of the columns of A. If we have two permutation matrices P1 and P2 , then P1 acting on P2
will result in a permutation of the rows of P2 . This result is a further permutation matrix,
since it is the combined result of two permutations, that from P2 and then that from P1 . If
P is a permutation, then it represents a permutation of the rows of the identity matrix. The
matrix P representing the permutation which reorders the rows of P back into the identity
matrix would be the inverse of P and from this argument we see that P is invertible and has
an inverse that is another permutation matrix. The fact that P 1 = P T can be recognized
by considering the product of P with P T . When row i of P is multiplied by any column of
P T not equal to i the result will be zero since the location of the one in each vector wont
agree in the location of their index. However, when row i of P is multiplied by column i the
result will be one. Thus we see by looking at the components of P P T that P P T = I and
P T is the inverse of P .
Lets assume (without loss of generality) that j > i, then we will let M be the elementary
matrix (of type 1) that produces A from A i.e. it adds row i to j and the sum replaces row
j. Then since AB = I multiplying this system by M on the left we obtain (since MA = A )
that A B = M. From this we see that by multiplying this expression above by M 1 on
the left we obtain A BM 1 = I, showing that the inverse of A is BM 1 . Now the matrix
M is like the identity by with an additional one at position (j, i) rather than a zero there.
Specifically we have
1
1
..
.
M = 1 .
1
..
1 .
1
Where the one in the above matrix is at location (j, i). In this case the inverse of M is then
easy to compute; it is the identity matrix with a minus one at position (j, i), specifically we
have
1
1
..
.
M 1 = 1 .
1
. ..
1
1
Now multiplying B by this matrix on the left will operate on the columns of B, thus B =
BM 1 is the same matrix as B but with the jth and the negative of the ith column added
together and placed in column j. That is we are subtracting the ith column from the jth
column and placing it in column j.
Lets begin by assuming that every entry of A is real and then show that every entry of A1
is real. The easiest way to see that all entries of A1 must be real is to recall the adjoint
theorem from linear algebra. The adjoint theorem gives the inverse of a matrix in terms of
the adjoint of that matrix. Specifically, the adjoint theorem states that
1
A1 = CT ,
det(A)
where C is the matrix of cofactors of A. This cofactor matrix C is the matrix such that its
(i, j) element is given by the cofactor of the element aij or
Where A[ij] is the ijth minor of the matrix A, i.e. the submatrix that is produced from
A by deleting its ith row and its jth column. Because the determinants of the minors of
A only involve additions and multiplications, if A has only real elements then all cofactors
and determinants of A must be real. By the adjoint theorem above, A1 can have only real
elements. The other direction, where we argue that if A1 has only real elements then A
must have only real elements can be seen by applying the above arguments to the matrix
A1 .
Let A be symmetric and invertible. Then by the definition of the inverse, A1 satisfies
AA1 = I. Now taking the transpose of both sides and remembering that the transpose of a
product is the product of the transposes but in the opposite order we have (A1 )T AT = I T
which simplifies to (A1 )T A = I, since both A and I are symmetric. By multiplying both
sides on the left by A1 we have that
(A1 )T = A1
That the product BAB T is symmetric is just an exercise in taking transposes. We have
(BAB T )T = (B T )T AT B T = BAB T ,
Lets assume that A has full column rank and that Ax = 0. Note that Ax = 0 is equivalent
to the statement that a linear combination of the columns of A sums to zero. Specifically if
vi represents the ith column of A then Ax = 0 is equivalent to
n
X
xi vi = 0 .
i=1
Since A is full column rank its columns are linearly independent (this is the definition of full
column rank). Because of this fact, the only way these columns can sum to zero is if x = 0
and we have proven one direction. Now lets assume that Ax = 0 implies that x = 0. This
statement is equivalent to the fact that the columns of A are linearly independent which
again implies that A is of full column rank.
rank(AB) rank(A) .
rank(AB) rank(B) .
When these two results are combined the rank of the product AB must be less than the
smaller of the two rank(A) and rank(B) giving the requested inequality of
To show that rank(AB) rank(A), we first note that every vector in the column space of
AB is in the column space of A. This is true since AB is the action of the matrix A on every
column of the matrix B, and the action of a matrix (A) on a vector is a linear superposition
of the columns of that matrix (A). Thus the number of linearly independent columns of the
product matrix AB cannot be greater than that of the number of linear independent columns
in the multiplying matrix (A) and we recognize the desired result of
rank(AB) rank(A) .
To show the second inequality rank(AB) rank(B), we apply the expression proved above
on the matrix (AB)T . Since this matrix equals B T AT we have that
But since rank((AB)T ) = rank(AB) replacing the first term in the above gives
rank(AB) rank(B) ,
showing the second of the desired inequalities. If A or B is invertible, it must be square
and its rank is equal to its number of columns (equivalently the number of rows). With out
loss of generality assume that A is invertible. By the arguments above when we multiply B,
the dimension of the column space of AB cannot change from the dimension of the column
space of B. Thus we have that
rank(AB) = rank(B) .
The same argument applied to (AB)T (as outlined in the inequality proof above) can be
used to show that if B is invertible that
rank(AB) = rank(A) .
Following the hint given, we will multiply column i by x0 and add it to column i + 1 for
i = n 1, n 2, , 1. This action will not change the value of the determinant but will
make it simpler to evaluate as we will soon see. We begin by first multiplying column n 1
by x0 and adding to column n. We find that
1 x0 2 n2 n1
x 0 x0 x0
1 x1 2 n2 n1
x 1 x1 x1
det(V (x0 , x1 , x2 , , xn3 , xn2 , xn1 )) = 1 x2
x22 x2n2 x2n1
.. .. .. .. ..
. . . . .
n2
1 xn1 x2n1 xn1 n1
xn1
1 x0 2 n2
x 0 x0 0
1 x1 2 n2 n2
x1 x1 (x1 x0 )x1
1 x2 2 n2 n2
= x 2 x2 (x 2 x )x
0 2 .
.. .. .. .. ..
. . . . .
n2
1 xn1 x2n1 xn1 n2
(xn1 x0 )xn1
Where we see that this action has introduced a zero in the (1, n) position. Continuing, we
now multiply column n 2 by x0 and add it to column n 1. We find that the above
determinant now becomes
1 x0
x20 0 0
1 x1 2 n3 n2
x 1 (x1 x )x
0 1 (x1 x )x
0 1
1 x2 2 n3 n2
x 2 (x2 x 0 )x2 (x2 x 0 )x2 .
.. .. .. .. ..
. . . . .
2 n3 n2
1 xn1 xn1 (xn1 x0 )xn1 (xn1 x0 )xn1
Where we see that this action has introduced another zero this time at the (1, n1) position.
Continuing in this manner we will obtain the following determinant where the first row has
only one non-zero element
1 0 0 0 0 0
1 x1 x0
(x1 x0 )x1 (x1 x0 )x21 (x1 x0 )x1n3 (x1 x0 )x1n2
1 x2 x0 (x2 x0 )x2 (x2 x0 )x22 (x2 x0 )x2n3 (x2 x0 )x2n2 .
.. .. .. .. .. ..
. . . . . .
1 xn1 x0 (xn1 x0 )xn1 (xn1 x0 )x2n1 n3 n2
(xn1 x0 )xn1 (xn1 x0 )xn1
We can expand this determinant about the first row easily since there is only a single nonzero
element in this row. After this reduction we see that we can factor x1 x0 from the first
row of our reduced matrix, factor x2 x0 from the second row, x3 x0 from the third row,
and so on til we get to the n 1th row of our reduced matrix where we will factor xn1 x0 .
This gives us
1 x1 2 n3 n2
x 1 x1 x1
n1 1 x2 2 n3 n2
Y x 2 x2 x2
det(V (x0 , x1 , x2 , , xn2 , xn1 )) = (xi x0 ) .. .. .. .. .. .
. . . . .
i=1
2 n3 n2
1 xn1 xn1 xn1 xn1
Note that the remaining determinant is of size (n 1) (n 1) and is of exactly the same
form as the previous determinant. Thus using the same manipulations performed earlier we
see that
n1
Y
det(V ) = (xi x0 ) det(V (x1 , x2 , , xn1 ))
i=1
n1
Y n1
Y
= (xi x0 ) (xj x1 ) det(V (x2 , x3 , , xn1 ))
i=1 j=2
n1
Y n1
Y n1
Y
= (xi x0 ) (xj x1 ) (xk x2 )det(V (x3 , , xn1 ))
i=1 j=2 k=3
n1
Y n1
Y n1
Y n1
Y
= (xi x0 ) (xj x1 ) (xk x2 ) (xl xn3 ) (xn1 xn2 ) .
i=1 j=2 k=3 l=n2
t1 = ad
t2 = bc
t3 = (a + b)(c d) ,
which involve only three real multiplications. Note that the product obtained by computing
t3 is algebraically equivalent to
where we have grouped specific terms to help direct the manipulations we will perform below.
Note also that the sums t1 + t2 and t3 + t1 t2 are given by
t1 + t2 = ad + bc
t3 + t1 t2 = ac bd .
so that our full complex product can be written in terms of these sums as
x1 = 3
x2 = 14 4x1 = 14 12 = 2
x3 = 7 + 6x1 5x2 = 7 + 18 10 = 1 .
Since A is positive definite we must have for all non-zero xs the condition xT Ax > 0. Let
x = ei , a vector of all zeros but with a one in the i-th spot. Then xT Ax = eTi Aei = aii > 0,
proving that the diagonal elements of a positive definite matrix must be positive.
Chapter 31 (Number-Theoretic Algorithms)
Exercise 31.1-1
Following the hint, given a sequence of increasing primes p1 , p2 , , pk we can form another
prime p by computing the product plus one or p = p1 p2 pk + 1. Note that this new
number p is prime since none of p1 , p2 , , pk divide it. If one did then it would also have to
divide the number p p1 p2 pk = 1 which is impossible. Thus the newly formed number
is prime and as we can repeat this procedure an infinite number of times there must be an
infinite number of primes.
Exercise 31.1-2
If a|b then we have that there exists an integer k1 such that b = k1 a. Since b|c we have that
there exists an integer k2 such that c = k2 b. Using the first of these two relationships into
the second we have
c = k2 b = k2 (k1 a) = k1 k2 a ,
showing that a|c.
Exercise 31.1-3
Assume that gcd(k, p) = d 6= 1. Then d|k and d|p. Since p is prime if d|p then d = 1 or
d = p. As we assumed that d 6= 1 we must have d = p. The fact that d|k means that |d| |k|
or |p| |k|. This last fact is a contraction to the problem assumption that k < p. Thus the
assumption that d 6= 1 must be false.
Exercise 31.1-4
Since gcd(a, n) = 1 then by Theorem 31.2 there must exist integers x and y such that
1 = ax + ny. If we multiply this equation by b we get
b = abx + nbx .
Note that the right-hand-side of the above is divisible by n since it is a linear combination
of two things (the terms abx and nbx) both of which are divisible by n. We know that abx is
divisible by n since ab is and nbx is divisible by n since n is a factor. Since the right-hand-side
equals b this shows that b is divisible by n.
Exercise 31.1-5
(a + b)p = ap + bp (mod p) .
Exercise 31.1-6
as we were to show. Next if we assume that x y (mod b) then taking the mod a operator
to both sides of this expression and using the just derived result we get
as we were to show.
Exercise 31.1-8
Recall that Theorem 31.2 states that that gcd(a, b) is the smallest positive element of the set
{ax + by : x, y Z}. Since this set definition of the greatest common divisor is symmetric
in a and b we see that
gcd(a, b) = gcd(b, a) .
and because x and y are arbitrary integers that
We also have that gcd(a, 0) is the smallest element in the set {ax : x Z} which would be
when x = 1 such that the product of ax is positive or |a|. Next we have that gcd(a, ka) is
the smallest element of the set {ax + kay : x, y Z} = {a(x + ky) : x, y Z}. This smallest
element would be when x + ky = 1 in order that a(x + ky) = |a|.
Exercise 31.1-9
Let d = gcd(a, gcd(b, c)) i.e. the left-hand-side of the equation we wish to show. Then d|a
and d| gcd(b, c). This second statement d| gcd(b, c) means that d|b and d|c. Since d divides
a and b by the above this means that d| gcd(a, b). Thus d is a number that gcd(a, b) and c.
Lets show that d is the largest number that divides both gcd(a, b) and c. Then if so we have
that d = gcd(gcd(a, b), c) and we have shown the desired equivalence. To show this assume
that there is another integer d such that d > d that divides gcd(a, b) and c. This means
that d |a and d |b, and d |c so that d | gcd(b, c). Thus since d divides a and gcd(b, c) it must
be smaller than or equal to the greatest common divisor of a and gcd(b, c) or
giving a contradiction to the initial assumption that d > d. Thus means d must be the
largest integer already and so d = gcd(gcd(a, b), c).
Recall that a mod b = a ab b i.e. a mod b is the remainder when a is divided by b. This
5 mod 10 = 5
2 mod 5 = 2 .
The point of these comments is to give some understanding to Euclids algorithm for finding
the greatest common divisor when the inputs are EUCLID(a, b) where a < b then in this
case the first recursive call performed is
import fractions
print fractions.gcd(30,21) # the example from the book gives 3
At the time of writing the source code of this function shows that its in fact using Euclids
algorithm for its implementation.
Exercise 31.2-1
Let d = gcd(a, b) then d must have a prime factorization in terms of the same primes as a
and b namely
d = pg11 pg22 pgrr .
Since d is a divisor of a and b we must have d|a and d|b which means that
gi = min(ei , fi ) ,
as we were to show.
Exercise 31.2-2
See the output in Table 1 where we follow the output format given in this section of the
notes where as we move down the table we are moving in increasing stack depth. From that
output we see that gcd(899, 493) = 29 and that 29 = 899(6) + 493(11).
Exercise 31.2-3
One way to see that these two expressions are equal is to use one step of EUCLIDs algorithm
to evaluate both of them and see that after the first step both of these algorithms are
stack level a b a/b d x y
0 899 493 1 29 -6 11
1 493 406 1 29 5 -6
2 406 87 4 29 -1 5
3 87 58 1 29 1 -1
4 58 29 2 29 0 1
5 29 0 - 29 1 0
computing the same thing. For example, EUCLID to compute gcd(a, n) would first call
(assuming n 6= 0)
EUCLID(n, a mod n) .
The first step of EUCLIDs algorithm on the second expression gcd(a + kn, n) would call
Since (a + kn) mod n = a mod n we have that the two expressions will evaluate to the same
number and are therefore the same. If n = 0 the we can see that both sides are equal.
Exercise 31.2-4
def iterative_euclid(a,b):
while b != 0 :
a, b = b, a % b # swap in place
return a
Here we have assigned b to a and a mod b to b in one step using pythons tuple assignment.
Exercise 31.2-5
Exercise 31.2-7
One can show that the function returns the same answer independent of the order of the
arguments by induction. It is easiest to see how to write
gcd(a0 , a1 , a2 , , an ) = a0 x0 + a1 x1 + a2 x2 + + an xn ,
by looking at an example. Consider
gcd(a0 , a1 , a2 , a3 ) = gcd(a0 , gcd(a1 , a2 , a3 )) = gcd(a0 , gcd(a1 , gcd(a2 , a3 ))) .
We can now use Theorem 31.2 to write gcd(a2 , a3 ) as a2 x2 + a3 x3 for two integers x2 and x3 .
This would give
gcd(a0 , a1 , a2 , a3 ) = gcd(a0 , gcd(a1 , a2 x2 + a3 x3 )) .
We can again use use Theorem 31.2 to write gcd(a1 , a2 x2 + a3 x3 ) as
x1 a1 + y1 (a2 x2 + a3 x3 ) = x1 a1 + y1 x2 a2 + y1 x3 a3 .
for two integers x1 and y1 . Thus we have shown that
gcd(a0 , a1 , a2 , a3 ) = gcd(a0 , x1 a1 + y1 x2 a2 + y1 x3 a3 ) .
One more application of Theorem 31.2 gives
gcd(a0 , a1 , a2 , a3 ) = x0 a0 + y2 (x1 a1 + y1 x2 a2 + y1 x3 a3 )
= x0 a0 + y2 x1 a1 + y1 y2 x2 a2 + y1 y2 x3 a3 .
As each coefficient in front of ai is the product of integers it itself is an integer. This gives the
desired representation when n = 3. In general, we use the definition of the greatest common
divisor for n > 1 to nest gcd function calls until we get a function call with only two
arguments gcd(an1 , an ). We can then use Theorem 31.2 to write this as an1 xn1 + an xn
for two integers xn1 and xn . We can then unnest the gcd function calls each time using
Theorem 31.2 to get the desired result.
Exercise 31.2-8 (the least common multiple)
We can first compute the greatest common divisor of the given n integers
d = gcd(a1 , a2 , . . . , an ) .
This can be done using the recursive definition of the gcd for more than two arguments given
in a previous problem. Then since d is a divisor of each of ai it is a factor of each of them
(by definition). Once we have this number then the least common multiple is given by
a a a
1 2 n
lcm(a1 , a2 , . . . , an ) = d .
d d d
In the right-hand-side
of the above expression by grouping d with a given term in parenthesis
we have that d adi = ai and thus the above number is a multiple of ai for each i.
Appendix C (Counting and Probability)
The proof of the lower bound on the binomial coefficients given in the book, requires that
n1 n
,
k1 k
for 1 k n. We can show this expression is true, by starting with it and seeing if we can
convert it using reversible transformations to another expression known to be true. If this
can be done, since each transformation is reversible, the original expression must also be
true. For this expression we can demonstrate it is true by using some simple manipulations.
We have
n1 n
k1 k
k(n 1) n(k 1)
kn k nk n
k n
n k
Which is known to be true. Using the same manipulations we can show show that
n2 n
.
k2 k
This inductive process can continue, subtracting one each time from the numerator and
denominator. As a check we can verify the correctness of the final inequality which is given
by
nk+1 n
.
1 k
Using the same approach as above
nk+1 n
1 k
kn k 2 + k n
n(k 1) k(k 1) 0
(k 1)(n k) 0
and the last equation is true. Given this sequence of results we can derive the lower bounds
on the binomial coefficients as
n n(n 1) . . . (n k + 1)
=
k k(k 1) . . . 1
n n 1
nk+1
=
k k1 1
n n n
k k k
n k
=
k
Assuming k < n, then the first substring occupies the locations 1, 2, . . . , k, the second sub-
string occupies the locations 2, 3, . . . , k + 1, and so on. The last possible substring would
occupy the locations n k + 1, n k, . . . , n. Thus we have in total
nk+1
substrings of size k in a string of size n.
To calculate how many substrings of size k a string of size n has, we from the above formula
that we have n size one substrings (k = 1), n 1 size two substrings (k = 2), n 2 size
three substrings, etc. Continuing this pattern the total number of substrings is the sum of
all these numbers given by
n n
X X n(n + 1)
(n k + 1) = l= .
k=1 l=1
2
Exercise C.1-2 (counting Boolean functions)
Each Boolean function is defined by how it maps its inputs to outputs. For an n-input,
1-output Boolean function we have a total of 2n possible inputs, to which we can assign
a TRUE or a FALSE output. Thus for each possible unique input specification we have 2
possible output specifications. So we have for the number of possible inputs (counting the
number of possible outputs for each input)
2 22 2.
With one factor of 2 for each of the possible input specification. Since there are 2n possible
input specifications, this gives a total of
n
22 ,
For a n-input m-output we have 2m possible choices for each possible output, so using the
same logic as before we have
n n
(2m )2 = 2m2 ,
possible n-input m-output Boolean functions.
Let the number of such ways our professors can sit be denoted by Nt , with t for table. Then
for each one of these seatings we can generate all permutations of n objects in the following
way. First consider a specific table ordering of professors say abcd. Then all the other
equivalent table orderings are given by circularly shifting the given ordering i.e. producing
bcda, cdab, dabc. This circular shifts produce n different. Since by performing this operation
on each of the Nt table orderings we get all possible permutations of which there are n!, so
in equation form we have that nNt must equal n!, which when we solve for Nt gives
n!
Nt = = (n 1)! .
n
Exercise C.1-4 (an even sum from distinct subsets of size three)
Our set is given by S = 1, 2, . . . , 100 and we want three distinct numbers that sum to an
even number. The total number of distinct size 3 subsets
(independent of the order of the
100
elements in the subset) drawn from S is given by . Half of these will sum to an even
3
number and the other half will sum to an odd number. Thus the total number is given by
1 100
= 80850 .
2 3
Another way to obtain this same number is to recognize that to have an even sum I must
pick either three even numbers or one even number and two odd numbers. Since these two
outcomes are mutually exclusive using the rule of sum the total number of ways to pick a
even summable subset is the sum of the number of ways to select each of the previous sets.
This number is
50 50 50
+ ,
3 1 2
where the first combination is the number of ways to select three even numbers and the
second combination product is the number of ways to select a single odd number and then
two even numbers. One can check that the sum above reduces to 80850 also.
Considering the group of n objects with one object specified as distinguished or special.
Then the number of ways to select k objects from n can be decomposed into two distinct
occurrences. The times when this special object is selected in the subset of size k and the
times when its not. When it is not selected in the subset ofsize k we are specifying our k
n1
subset elements from the n 1 remaining elements giving total subsets in this
k
case. When it is selected into the subset
of size
k we have to select k 1 other elements from
n1
the n1 remaining elements, giving additional subsets in this case. Summing the
k1
counts from these two occurrences we have that factorization can be written as the following
n n1 n1
= + .
k k k1
Exercise C.1-8 (Pascals triangle)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
0
Note that the top row only has one element 1. The second row has two elements
0
1 1
= 1, and = 1. Subsequent rows are obtained by beginning and ending with a
0 1
one and summing the values of the two binomial coefficients in the row above.
Expressing each side of this expression, we have that the left hand side is given by
n
X n(n + 1)
i=
i=1
2
n
Exercise C.1-10 (maximum of as a function of k)
k
As a combinatorial proof, the number of ways to select j + k items from a set of n will
be smaller than if we first select j items from n, and for each of those sets select k items
from n j. That the former is a larger number can be seen by the following example where
equality does not hold. Consider the case n = 4, k = 3, and j = 1, then
4 4 3
=1< = 4.
4 1 3
There the number of ways to select 4 items from 4 is only one which is smaller than if we are
allowed to select 1 of the 4 items first (of which there are four ways) and then combine this
with the number of ways to select three remaining elements (of which there is only one).
Assuming that 00 = 1, we can show that k = 0 satisfies this easily since it reduces to 1 1.
However, to begin with amore
realistic case, to anchor our induction proof to, we start with
n
k = 1. This value gives = n for the left hand side of this expression, while the right
1
hand-side evaluates to
nn
.
(n 1)n1
This can be simplified as follows
nn nn1
= n
(n 1)n1 (n 1)n1
n1
n
= n
n1
n1
1
= n
1 n1
1
Now since we assume that n > 1, the expression 1 n
is bounded by
1
0<1 <1
n
and more specifically
1
1< 1 .
1 n
Taking positive powers of this expression (i.e. to the n 1 power) can only increase its value
and we have that n1
1
n<n
1 n1
which provides the anchoring point from which to begin mathematical induction. Having
shown that this inequality is true for k = 1 we next proceed to assume it is true for k < k
and show that it is true for k < k + 1. To do this consider the following
n n!
=
k+1 (n k 1)!(k + 1)!
n! nk
=
(n k)!k! k + 1
n nk
=
k k+1
n
nn
n nk
k = .
k (n k)nk k + 1 k k (k + 1)(n k)nk1
Here on the last step we have used the induction hypothesis. Our induction proof will be
finished if we can show that the last expression above is less than the following
nn
.
(k + 1)k+1(n k 1)nk1
Towards this end we will show this by assuming that it is true and then through reversible
transformations reduce this expression to one that is known to be true. Since all transfor-
mation are reversible the original inequality must be true. So to begin we assume that
nn nn
k k (k + 1)(n k)nk1 (k + 1)k+1(n k 1)nk1
(k + 1)k (n k)nk1
kk (n k 1)nk1
k nk1
1 1
1+ 1+
k nk1
The above expression becomes the following functional relationship, which if we can prove
is true, will complete the inductive proof
f (k) f (n k 1) .
2.6
2.4
2.2
1.8
f
1.6
1.4
1.2
1
0 1 2 3 4 5 6 7 8 9 10
x
Figure 1: Graphical plot showing the monotonicity of f (x) required for the inductive proof
in Exercise C.1-12.
Further assuming that f has a monotone inverse (to be discussed below) then we can apply
the inverse to this expression to obtain the region of validity for this inequality as
k nk1
or k n1
2
. For all the statements above to be valid (and our induction proof to be complete)
all one must do now is to show that f (x) has an inverse or equivalently that f (x) is monotonic
over its domain. This could be done with derivatives but rather than that Figure 1 shows a
plot of f (x) showing its monotonic behavior.
Note also from the symmetry relationship possessed by the binomial coefficients, i.e,
n n
=
k nk
we can extend our result above to all 0 k n.
ln(x)
lg(x) =
ln(2)
lg(x) 1
= .
dx x ln(2)
Using this expression we can take the derivative of the binary entropy function giving
(1 )
H () = lg() + lg(1 ) +
ln(2) (1 ) ln(2)
= lg() + lg(1 ) .
When we set this equal to zero looking for an extrema we have that
lg() + lg(1 ) = 0
which has as its solution = 21 . We can check that this point is truly a maximum and not
a minimum by computing the second derivative of H(). We have that
1 1
H () = ,
ln(2) (1 ) ln(2)
1
which is negative when = 2
implying a maximum. We also have that
1 1 1
H( ) = + = 1 .
2 2 2
Taking the derivative of this expression with respect to x gives the following
n
n1
X n
n(x + y) = kxk1 y nk ,
k
k=0
For this problem we can explicitly enumerate our sample space and then count the number
of occurrences where professor Rosencrantz obtains more heads than professor Guildenstern.
Denoting the a triplet of outcomes from the one coin flip by Rosencrantz and the two coin
flips by Guildenstern as (R, G1 , G2 ), we see that the total sample space for this experiment
is given by
(H, H, H) (H, H, T ) (H, T, H) (H, T, T )
(T, H, H) (T, H, T ) (T, T, H) (T, T, T )
From which the only outcome where professor Rosencrantz obtains more heads than professor
Guildenstern is the sample (H, T, T ). Since this occurs once from eights possible outcomes,
Rosencrantz has a 1/8 probability of winning.
There are 10 9 8 = 720 possible ways to draw three cards from ten when the order of the
drawn cards matters. To help motivate how to calculate then number of sorted three card
draws possible, well focus how the number of ordered draws depends on the numerical value
of the first card drawn. For instance, if a ten or a nine is drawn as a first card there are no
possible ways to draw two other cards and have the total set ordered. If an eight is drawn
as the first card, then it is possible to draw a nine for the second and a ten for the third
producing the ordered set (8, 9, 10). Thus if an eight is the first card drawn there is one
possible sorted hand. If a seven is the first card drawn then we can have second and third
draws consisting of (8, 9), (8, 10), or a (9, 10) and have an ordered set of three cards. Once
the seven is drawn we now are looking for the number of ways to draw two sorted cards from
{8, 9, 10}. So if an seven is drawn we have three possible sorted hands. Continuing, if a six
is drawn as the first card we have possible second and third draws of: (7, 8), (7, 9), (7, 10),
(8, 9), (8, 10), or (9, 10) to produce an ordered set. Again we are looking for a way to draw
two sorted cards from a list (this time {7, 8, 9, 10}).
After we draw the first card an important subcalculation is to compute the number of ways
to draw two sorted cards from a list. Assuming we have n ordered items in our list, then
we have n ways to draw the first element and n 1 ways to draw the second element giving
n(n1) ways to draw two elements. In this pair of elements one ordering result in a correctly
sorted list (the other will not). Thus half of the above are incorrectly ordered what remains
are
n(n 1)
N2 (n) =
2
The number of total ways to draw three sorted cards can now be found. If we first draw an
eight we have two cards from which to draw the two remaining ordered cards in N2 (2) ways.
If we instead first drew a seven we have three cards from which to draw the two remaining
ordered cards in N2 (3) ways. If instead we first drew a six then we have four cards from
which to draw two ordered cards in N2 (4) ways. Continuing, if we first draw a one, we have
nine cards from which to draw two ordered cards in N2 (9) ways. Since each of these options
is mutually exclusive the total number of ways to draw three ordered cards is given by
9 9
X X n(n 1)
N2 (3) + N2 (4) + N2 (5) + + N2 (9) = N2 (n) = = 120 .
n=2 n=2
2
Finally with this number we can calculate the probability of drawing three ordered cards as
120 1
= .
720 6
Following the hint, since a < b, the quotient a/b is less than one. If we express this number
in binary we will have an (possibly infinite) sequence of zeros and ones. The method to
determine whether to return a biased heads (with probability a/b) or a biased tails
(with probability (ba)/b) is best explained with an example. To get a somewhat interesting
binary number, lets assume an example where a = 76 and b = 100, then our ratio a/b is 0.76.
In binary that has a representation given by (see the Mathematica file exercise C 2 4.nb)
0.76 = 0.11000010100011110112
We then begin flipping our unbiased coin. To a head result we will associate a one and to
a tail result we shall associate a zero. As long as the outcome of the flipped coin matches
the sequence of ones and zeros in the binary expansion of a/b, we continue flipping. At the
point where the flips of our unbiased coins diverges from the binary sequence of a/b we stop.
If the divergence produces a sequence that is less than the ratio a/b we return this result by
returning a biased head result. If the divergence produces a sequence that is greater than
the ratio a/b we return this result by returning a biased tail result. Thus we have used
our fair coin to determine where a sequence of flips falls relative to the ratio of a/b.
The expected number of coin flips to determine our biased result will be the expected number
of flips required until the flips from the unbiased coin dont match the sequence of zeros and
ones in the binary expansion of the ratio a/b. If we assume that a success is when our
unbiased coin flip does not match the digit in the binary expansion of a/b. The for each
flip a success can occur with probability p = 1/2 and a failure can occur with probability
q = 1 p = 1/2, we have that the number of flips n needed before a success is a geometric
random variable with parameter p = 1/2. This is that
P {N = n} = q n1 p .
So that the expected number of flips required for a success is then the expectation of the
geometric random variable and is given by
1
E[N] = = 2,
p
which is certainly O(1).
Using the definition of conditional probability given in this section we have that
P {A B}
P {A|B} = ,
P {B}
so that the requested sum is then given by
P {A B} P {A B}
P {A|B} + P {A|B} = +
P {B} P {B}
P {A B} + P {A B}
=
P {B}
Now since the events A B and A B are mutually exclusive the above equals
P {(A B) (A B)} B}
P {(A A) P {B}
= = = 1.
P {B} P {B} P {B}
This result follows for the two set case P {A B} = P {A|B}P {B} by grouping the sequence
of Ai s in the appropriate manner. For example by grouping the intersection as
A1 A2 An1 An = (A1 A2 An1 ) An
we can apply the two set result to obtain
P {A1 A2 An1 An } = P {An |A1 A2 An1 } P {A1 A2 An1 } .
Continuing now to peal An1 from the set A1 A2 An1 we have the second probability
above equal to
P {A1 A2 An2 An1 } = P {An1|A1 A2 An2 }P {A1 A2 An2 } .
Continuing to peal off terms from the back we eventually obtain the requested expression
i.e.
Note: As requested by the problem I was not able to find a set of events that are pairwise
independent but such that no subset k > 2 of them are mutually independent. Instead I
found a set of events that are all pairwise independent but that are not mutually independent.
If you know of a solution to the former problem please contact me.
Consider the situation where we have n distinct people in a room. Let Ai,j be the event
that person i and j have the same birthday. We will show that any two of these events are
pairwise independent but the totality of events Ai,j are not mutually independent. That is
we
desire
to show that the two events Ai,j and Ar,s are independent but the totality of all
n
events are not independent. Now we have that
2
1
P (Ai,j ) = P (Ar,s ) = ,
365
since for the specification of either one persons birthday the probability that the other person
will have that birthday is 1/365. Now we have that
1 1 1
P (Ai,j Ar,s ) = P (Ai,j |Ar,s )P (Ar,s ) = = .
365 365 3652
This is because P (Ai,j |Ar,s ) = P (Ai,j ) i.e. the fact that persons r and s have the same
birthday has no effect on whether the persons i and j have the same birthday. This is true
even if one of the people in the pairs (i, j) and (r, s) is the same. When we consider the
intersection of all the sets Ai,j , the situation changes. This is because the event (i,j) Ai,j
(where the intersection is over all pairs (i, j)) is the event that every pair of people have the
same birthday, i.e. that everyone considered has the same birthday. This will happen with
probability n1
1
,
365
while if the events Ai,j were independent the required probability would be
n n(n1)
1 1
2
Y 2
P (Ai,j ) = = .
365 365
(i,j)
n
Since 6= n 1, these two results are not equal and the totality of events Ai,j are not
2
independent.
Consider the situation where we have to coins. The first coin C1 is biased and has probability
p of landing heads when flipped where p > 1/2. The second coin C2 is fair and has probability
of landing heads 1/2. For our experiment one of the two coins will be selected (uniformly and
at random) and presented to two people who will each flip this coin. We will assume that
the coin that is selected is not known. Let H1 be the event that the first flip lands heads,
and let H2 be the event that the second flip lands heads. We can show that the events H1
and H2 are not independent by computing P (H1 ), P (H2 ), and P (H1 , H2 ) by conditioning
on the coin selected. We have for P (H1 )
or simplifying this result equality only holds if p = 1/2 which we are assuming is not true.
Now let event E denote the event that we are told that the coin selected and flipped by both
parties is the fair one, i.e. that event C2 happens. Then we have
1 1
P (H1 |E) = and P (H2 |E) =
2 2
1
P (H1 , H2 |E) = .
4
Since in this case we do have P (H1 , H2 |E) = P (H1|E)P (H2|E) we have the required condi-
tional independence. Intuitively, this result makes sense because once we know that the coin
flipped is fair we expect the result of each flip to be independent.
Once we have selected a curtain we are two possible situations we might find ourself in. The
first situation A, is where we have selected the curtain with the prize behind it or situation
B where we have not selected the curtain with the prize behind it. The initial probability
of event A is 1/3 and that of event B is 2/3. We will calculate our probability of winning
under the two choices of actions. The first is that we choose not to switch curtains after the
emcee opens a curtain that does not cover the prize. The probability that we win, assuming
the no change strategy, can be computed by conditioning on the events A and B above as
Under event A and the fact that we dont switch curtains we have P (W |A) = 1 and
P (W |B) = 0, so we see that P (W ) = 1/3 as would be expected. If we now assume that
we follow the second strategy where by we switch curtains after the emcee revels a curtain
behind which the prize does not sit. In that case P (W |A) = 0 and P (W |B) = 1, since in
this strategy we switch curtains. The the total probability we will is given by
2 2
P (W ) = 0 + 1 = ,
3 3
since this is greater than the probability we would win under the strategy were we dont
switch this is the action we should take.
I will argue that X has obtained some information from the guard. Before asking his question
the probability of event X (X is set free) is P (X) = 1/3. If prisoner X is told that Y (or Z
in fact) is to be executed, then to determine what this implies about the event X we need
to compute P (X|Y ). Where X, Y , and Z are the events that prisoner X, Y , or Z is to be
set free respectively. Now from Bayes rule
P (Y |X)P (X)
P (X|Y ) = .
P (Y )
We have that P (Y ) is given by
1 1 2
P (Y ) = P (Y |X)P (X) + P (Y |Y )P (Y ) + P (Y |Z)P (Z) = +0+ = .
3 3 3
So the above probability then becomes
1(1/3) 1 1
P (X|Y ) = = > .
2/3 2 3
Thus the probability that prisoner X will be set free has increased and prisoner X has
learned from his question.
proof of the cumulative summation formula for E[X] (book notes page 1109)
expressing this in terms of the the complement of the cumulative distribution function
P {X i} gives E[X] equal to
X
i(P {X i} P {X i + 1}) .
i=0
Using the theory of difference equations we write the difference in probabilities above in
terms of the operator defined on a discrete function f (i) as
giving
X
E[X] = ii P {X i} .
i=0
No using the discrete version of integration by parts (demonstrated here for two discrete
functions f and g) we have
X
X
f (i)g(i) = f (i)g(i)|
i=1 f (i)g(i) ,
i=0 i=1
Table 2: The possible values for the sum (the first number) and the maximum (the second
number) observed when two die are rolled.
which is the desired sum. To see this another way we can explicitly write out the summation
given above and cancel terms as in
X
E[X] = i(P {X i} P {X i + 1})
i=0
= 0 + P {X 1} P {X 2}
+ 2P {X 2} 2P {X 3}
+ 3P {X 3} 3P {X 4} + . . .
= P {X 1} + P {X 2} + P {X 3} + . . . ,
We will begin by computing all possible sums and maximum that can be obtained when we
roll two die. These numbers are computed in table 2, where the row corresponds to the first
die and the column corresponds to the second die. Since each roll pairing has a probability
of 1/36 of happening we see that the expectation of the sum S is given by
1 2 3 4 5 6
E[S] = 2 +3 +4 +5 +6 +7
36 36 36 36 36 36
5 4 3 2 1
+ 8 +9 + 10 + 11 + 12
36 36 36 36 36
= 6.8056 .
Since the array elements are assumed random the maximum can be at any of the n indices
with probability 1/n. Thus if we define X to be the random variable representing the location
of the maximum of our array we see that the expectation of X is given by
1 1 1
E[X] = 1 +2 + +n
n n n
n
1X 1 n(n + 1)
= k=
n k=1 n 2
n+1
= .
2
The same is true for the expected location of the minimum.
Define X to be the random variable denoting the payback from one play of the carnival
game. Then the payback depends on the possible outcomes after the player has guessed a
number. Let E0 be the event that the players number does not appear on any die, E1 the
event that the players number appears on only one of the three die, E2 the event that the
players number appears on only two of the die, and finally E3 the event that the players
number appears on all three of the die. The the expected payback is given by
E[X] = P (E0 ) + P (E1 ) + 2P (E2 ) + 3P (E3 ) .
We now compute the probability of each of the events above. We have
3
5
P (E0 ) = = 0.5787
6
2
1 5
P (E1 ) = 3 = 0.3472
6 6
1 1 5
P (E2 ) = 3 = 0.0694
6 6 6
1
P (E3 ) = 3 = 0.0046 .
6
Where the first equation expresses the fact that each individual die will not match the
players selected die with probability of 5/6, so the three die will not match the given die
with probability (5/6)3 . The other probabilities are similar. Using these to compute the
expected payoff using the above formula gives
17
E[X] = = 0.0787 .
216
To verify that these numbers are correct in the Matlab file exercise C 3 3.m, a Monte-Carlo
simulation is developed verifying these values. This function can be run with the command
exercise C 3 3(100000).
Exercise C.3-4 (bounds on the expectation of a maximum)
We have for E[max(X, Y )] computed using the joint density of X and Y that
XX
E[max(X, Y )] = max(x, y)P {X = x, Y = y}
x y
XX
(x + y)P {X = x, Y = y} ,
x y
since X and Y are non-negative. Then using the linearity of the above we have
XX XX
E[max(X, Y )] xP {X = x, Y = y} + yP {X = x, Y = y}
x y x y
X X
= xP {X = x} + yP {Y = y}
x y
= E[X] + E[Y ] ,
P {X = x, Y = y} = P {X = x}P {Y = y} .
But by definition of the random variable X has a realization equal to x then the random
variable f (X) will have a realization equal to f (x). The same statement hold for the random
variable g(Y ) which will have a realization of g(y). Thus the above expression is equal to
(almost notationally)
Defining the random variable F by F = f (X) (an instance of this random variable f ) and
the random variable G = g(Y ) (similarly and instance of this random variable g) the above
shows that
P {F = f, G = g} = P {F = f }P {G = g}
or
P {f (X) = f, g(Y ) = g} = P {f (X) = f }P {g(Y ) = g} ,
showing that f (X) and g(Y ) are independent as requested.
To prove that
E[X]
P {X t} ,
t
is equivalent to proving that
E[X] tP {X t} .
To prove this first consider the expression for E[X] broken at t i.e.
X
E[X] = xP {X = x}
x
X X
= xP {X = x} + xP {X = x} .
x<t xt
P
But since X is non-negative we can drop the expression x<t xP {X = x} from the above
and obtain a lower bound. Specifically we have
X
E[X] xP {X = x}
xt
X
t P {X = x}
xt
= tP {X t} ,
At = {s S : X(s) t}
Bt = {s S : X (s) t} .
Then lets consider an element s Bt . Since s is in Bt we know that X ( s) > t. From the
assumption on X and X we know that X( s) X (
s) t, and s must also be in At . Thus
the set Bt is a subset of the set At . We therefore have that
X X
P {X t} = P {s} P {s} = P {X t} ,
sAt sBt
From the calculation of the variance of the random variable X we have that
since the square in the expectation is always a positive number. Expanding this square we
find that
E[X 2 ] E[X]2 > 0 .
Which shows on solving for E[X 2 ] that
E[X 2 ] > E[X]2 ,
or that the expectation of the square of the random variable is larger than the square of
the expectation. This make sense because if X where to take on both positive and negative
values in computing E[X]2 the expectation would be decreased by X taking on instances of
both signs. The expression E[X 2 ] would have no such difficulty since X 2 is aways positive
regardless of the sign of X.
All the possible outcomes for the sum on two die are given in table 2. There one sees that the
sum of a 7 occurs along the right facing diagonal (elements (6, 1), (5, 5), (4, 3), (3, 4), (2, 5), (1, 6)),
while the sum of 11 occurs for the elements (6, 5), (5, 6). The the probability we roll a 7 or
an 11 is given by
6 2 8 2
p= + = = .
36 36 36 9
if we take this as the probability of success then the remaining results from the book follow.
The geometric distribution gives the probability our first success occurs at trail numbered k
when each trial has p as a probability of success. It has a distribution function given by
P {X = k} = p(1 p)k1 = pq k1 .
Where we have defined q = 1 p. If we sum this probability distribution for all possible
location of the first success we obtain
X pX k
p(1 p)k1 = q
k=1
q k=1
!
p X
= qk 1
q k=0
p 1
= 1
q 1q
p 1
= 1
q p
p 1p
= = 1,
q p
as we were to show.
Exercise C.4-2 (average number of times to obtain three heads and tails)
Let one trial consist of flipping six coins. Then we consider a success when we have obtained
6
three head and three tails. Thussince there are 2 possible outcomes of six flips when the
6
flips are ordered there are then the probability of success (p) for this experiment
3
is given by
6
3 20
p= 6
= = 0.3125 .
2 64
Since the number of trials needed to obtain a success is a geometric distribution we have
that the average number of times we must perform this experiment is given by the average
for a geometric distribution with probability of success p or
1 1
= = 3.2 .
p 0.3125
Exercise C.4-3 (an equality with the binomial distribution)
From the discussion in the text the binomial coefficient has a maximum at the integer k
that lies in the range np q < k < (n + 1)p. To evaluate the approximate maximum of the
binomial coefficient we will evaluate it at k = np which is certainly between the two limits
np q and (n + 1)p. Our binomial coefficient evaluated at its approximate maximum k = np
is given by
n
b(np; n, p) = pnp (1 p)nnp
np
n!
= pnp (1 p)nq
(n np)!(np)!
n!
= pnp q nq .
(nq)!(np)!
where we have introduced q = 1 p into the above to simplify the notation. Using Stirlings
approximation for n! given by
n n
1
n! = 2n 1 + ( ) ,
e n
we can simplify the factorial expression above (and dropping the order symbols ) we have
nq np
n! n n 1 e 1 e
2n
(nq)!(np)! e 2nq nq 2np np
1/2
nn
1 n
=
2 (nq)(np) (nq)nq (np)np
1/2 n
1 1 1
= .
2 nqp q q pp
upon multiplying the above by pnp q nq we find that
1/2
1
b(np; n, p) ,
2npq
as we were to show.
Using the limiting argument above we have this probability equal to e1 as n goes to infinity.
For a total of 2n flips we have 22n = 4n possible sequences of heads and tails in 2n flips.
Let the first n of these correspond to the flips of professor Rosencrantz and the remaining
n corresponds to those of professor Guildenstern. Following the hint in the book, we will
call a success for professor Rosencrantz when his flip lands heads and a success for professor
Guildenstern when his flip lands tails. Then both professors will obtain the same number
of heads if say professor Rosencrantz has k successes while professor Guildenstern has n k
successes. Thus the number of sequences (from our 4n ) that have the same number of heads
for both professors is an equivalent problem to selecting k + (n k) = n total locations
from among 2n possible and declaring these to be the locations of the successes for both
professors. This means that if a success is placed before the n + 1th flip it is considered to be
a success for professor Rosencrantz and denotes a head (the remaining flips for Rosencrantz
are then all tails). If a success falls after the nth flip it is considered to be a success for
professor Guildenstern and is considered to be a tail (the remaining flips for Guildenstern
are considered
to be all heads). The number of sequences with n total successes is given by
2n
and so the probability of obtaining the same number of heads is given by
n
2n
n
,
4n
as claimed. Using the result from Exercise C.1-13 which derives the approximate
4n
2n 1
= 1 + O( ) .
n n n
we can simplify the probability of obtaining the same number of heads and find that is is
approximately equal to
1 1
1 + O( ) .
n n
Another way to approach this problem is to explicitly sum the probability that professor
Rosencrantz has k successes while professor Guildenstern has nk successes. The probability
that both these events happen (since they are independent) is given by the product of the
appropriate binomial distribution i.e.
So the total probability of that the two professors get the same number of heads is given by
the sum of that probability for all possible ks (k = 0 to k = n) or
n
X
b(k; n, 1/2)b(n k; n, 1/2) .
k=0
as claimed.
From the binomial bound derived in Appendix C.1 (also see the notes above that go with
that section) we have that
n n
2nH( k ) .
k
Using this expression we can immediately bound b(k; n, 1/2) as
k nk
n 1 1
b(k; n, 1/2) =
k 2 2
n
n 1
=
k 2
n
n 1
2nH( k )
2
n
= 2nH( k )n ,
Since X is a random variable representing the total number of success in n Bernoulli trials
(with each trial having pi as its probability of success) we can explicitly express X as the
sum of n indicator random variables Ii as
n
X
X= Ii .
i=1
Here where Ii is one if the ith Bernoulli trial is a success and zero otherwise. Then since
the definition of P {X < k} means the sum of the probability that X takes the values
0, 1, . . . , k 1 or
k1
X
P {X < k} = P {X = i} ,
i=0
the probability we are attempting to bound is given in terms of equality probabilities i.e.
expressions like P {X = i}. Now for the random variable X to be exactly i means that only
i of the Bernoulli trials were a success (no more and no less) and the remaining trials were a
failure. Thus if we select a subset of size i from the n total Bernoulli random variables, the
above probability is expressed as the sum over all such subsets of size i i.e.
X
P {X = i} = pl1 pl2 pli (1 pli+1 )(1 pli+2 ) (1 pln ) . (5)
Here the sum is over all possible subsets of size i from n and the indices li select which of
the i Bernoulli indicator variables from n were successful. Now since pi p it follows that
1 pi 1 p and thus using these for each factor in the product we have that the term in
the sum above is bounded as
We can work this exercise in much the same way as the previous exercise C.4-8. Here we
can still write P {X = i} in the form as given by Equation 5. Rather than use a single value
of p such that p pi for all i we now have several pi where pi pi and thus Equation 6 in
this case becomes
pl1 pl2 pli (1 pli+1 )(1 pli+2 ) (1 pln ) pl1 pl2 pli (1 pli+1 )(1 pli+2 ) (1 pln ) .
Thus
X
P {X = i} = pl1 pl2 pli (1 pli+1 )(1 pli+2 ) (1 pln )
X
pl1 pl2 pli (1 pli+1 )(1 pli+2 ) (1 pln ) = P {X = i} .
We know that the probability of obtaining n failures when flipping a fair coin is given by
n
1
2
This is to be compared to the probability of flipping fewer than n heads when we flip our
fair coin 4n times or
n1
X
b(i; 4n, 1/2)
i=0
From Corollary C.5 we have that (with X the number of success obtained in our 4n flips)
that
n1
X
P {X < n} = b(i; 4n, 1/2) < b(n; 4n, 1/2) .
i=0
Since the numerator in the above has n factors we can break this factor up into n products
(each of which is less than 4) like the following
4n 4n 1 4n 2 4n 3 3n + 2 3n + 1
.
n n1 n2 n3 2 1
Therefore since each term in the above is bounded by 4 and there are n of them we can
further bound b(n; 4n, 1/2) as follows
4n n
n 1 1
b(n; 4n, 1/2) < 4 = .
2 4
Thus since
n1 n n
X 1 1
b(i; 4n, 1/2) < < ,
i=0
4 2
we see that obtaining fewer than n heads in 4n flips of a fair coin is less likely then obtaining
no heads when flipping a fair coin n times.
Exercise C.5-2 (bounds on the right tail of the binomial distribution)
We begin by proving Corollary C.6 which claims that given a sequence of n Bernoulli trials
each with probability p of success that
n
X (n k)p
P {X > k} = b(i; n, p) < b(k; n, p) .
i=k+1
k np
To prove this lets first consider the ratio b(i + 1; n, p)/b(i; n, p) which is given by
n
pi+1 (1 p)ni1
b(i + 1; n, p) i+1
=
b(i; n, p) n
pi (1 p)ni
i
ni p
= .
i+1 1p
Thus (since we assume that i is taken between k and n i.e. that k < i < n) we have
b(i + 1; n, p) nk p
.
b(i; n, p) k+1 1p
Defining x to be the above expression we see that x < 1 through the following arguments
n np p
x <
np + 1 1p
n(1 p)p np
= =
(np + 1)(1 p) np + 1
np np
< < = 1.
np + 1 np
Now using the ratio above and the definition of x we have that b(i + 1; n, p) is bounded by
b(i; n, p) as
b(i + 1; n, p) < xb(i; n, p) .
In the same way, b(i + 2; n, p) is bounded by b(i; n, p) as
b(i + 2; n, p) < xb(i + 1; n, p) < x2 b(i; n, p) .
Continuing this iteration scheme for i + 3, i + 4, we see that the probability X > k (the
right tail probability) is bounded above by
Xn
b(i; n, p) < xb(k; n, p) + x2 b(k; n, p) + + xn b(k; n, p) .
i=k+1
We next prove Corollary C.7 which is the statement that given a sequence of n Bernoulli
trials each with probability p of being a success and a specific number of success k in the
right tail of our distribution ( np+n
2
< k < n) that the probability of obtaining more than k
success is less than one half the probability of obtaining more than k 1 success. This is
really a statement that as we require having more and more successes from our n Bernoulli
trials the probabilities decrease geometrically (with rate 1/2). We will begin by showing that
the coefficient of b(k; n, p) in Corollary C.6 is less than one. We have since np+n
2
<k<n
that
n np+n
nk 2
p < np+n p = p < 1.
k np 2
np
Thus from Corollary C.6 we have that
Xn
P {X > k} = b(i; n, p) < b(k; n, p) ,
i=k+1
or equivalently that
b(k; n, p)
Pn > 1.
i=k+1 b(i; n, p)
Now consider the ratio of P {X > k} to P {X > k 1}
Pn
P {X > k} b(i; n, p)
= Pi=k+1n
P {X > k 1} i=k b(i; n, p)
Pn
i=k+1 b(i; n, p)
=
b(k; n, p) + ni=k+1 b(i; n, p)
P
1
= b(k;n,p)
1 + Pn b(i;n,p)
i=k+1
1 1
< = ,
1+1 2
and the desired result is proven.
a
Let a > 0 be given, and define p = a+1
< 1, then
a 1
q = 1p = 1 = .
a+1 a+1
n
Now consider the pq product found in the binomial coefficient b(i; n, p) = pi q ni , i.e.
i
pi q ni . With the definition given above for p and q we have
i ni
ai
i ni a 1
pq = = .
a+1 a+1 (a + 1)n
From this we see that ai = (a + 1)n pi q ni and thus our desired sum is given by
k1 k1
X n i n
X n
a = (a + 1) pi q ni .
i i
i=0 i=0
Which can be seen as the left tail of the binomial distribution for which we have developed
bounds for in the text. Specifically if X is a binomial random variable (with parameters
(n, p)) then using the bounds that
k1
X kq
P {X < k} = b(i; n, p) < b(k; n, p) ,
i=0
np k
k1 k1
X n i ni
X
pq = b(i; n, p) ,
i
i=0 i=0
which by Theorem C.4 is bounded above by
kq
b(k; n, p) .
np k
From Lemma C.1 we can further bound the individual binomial coefficient b(k; n, p) as
np k nq nk
b(k; n, p) .
k nk
To show the second identity, we will apply Corollary C.9 to the random variable Y , but
in this case the probability of success for each Bernoulli trial is a constant p. Thus the
probability of failure in each Bernoulli trial is also a constant q = 1 p. We thus obtain
(since E[Y ] = nq) nqe r
P {Y nq r} .
r
Again using the fact that Y = n X to write the above expression in terms of X we find
nqe r
P {n X nq r} ,
r
or nqe r
P {n(1 q) X r} ,
r
or nqe r
P {np X r} ,
r
the desired result.
as suggested in the text. If anyone has such a proof please contact me.
Using the notation in the text we have that (by using Markovs inequality)
P {X r} E[e(X) ]er .
Since X is the number of success in n independent Bernoulli trials X = ni=1 Xi the expec-
P
tation of the exponential can be written as the product of the individual indicator random
variables Xi as
Yn
(X)
E[e ]= E[e(Xi pi ) ] .
i=1
The explicit expectation of each term in the product is easily computed using the definition
of expectation and gives
E[e(Xi pi ) ] = pi eqi + qi epi .
2 /2
If we now assume that pi eqi + qi epi e , then the product expectation can be written
as n
2 /2 2 /2
Y
(X)
E[e ] e = en .
i=1
To compute the tightest possible upper bound on this probability we can minimize the
right hand side of this expression with respect to . This is equivalent to maximizing the
expression r n2 /2 with respect to . Taking the derivative of this expression and
setting it equal to zero gives
r n = 0 ,
which has = r/n as its solution. The second derivative of r n2 /2 with respect to
begin negative imply that this value of corresponds to a maximum. The value of this
quadratic at this maximal is given by
r n r2 r2
r = .
n 2 n2 2n
This result intern implies that we have an upper bound on P {X r} given by
r2
P {X r} e 2n ,
P {X r} exp(e r) .
If we consider the right-hand side of this expression to be an function of , we can find the
that minimizes this function by taking the first derivative and setting it equal to zero. We
find that the first derivative is given by
d
exp(e r) = exp(e r) (e r) .
d
Which when we set this equal to zero and solve for gives
r
= ln( ) .
We can check that this point is indeed a minimum of our right-hand side by computing the
sign of the second derivative at that point. The second derivative is given by
d2
2
exp(e r) = exp(e r)(e r)2 + exp(e r)(e )
d
= exp(e r)(2e2 2re + r 2 + e )
= exp(e r)(2e2 (2r 1)e + r 2 ) .
Evaluating this expression at = ln(r/) (and ignoring the exponential factor which does
not affect the sign of the second derivative) gives
2
2 r
(2r 1)r + r 2 = r 2 2r 2 + r + r 2 = r > 0 .
2
Part (a): We have b choices for the location of the first ball, b choices for the location of
the second ball, b choices for the location of the third ball and so on down to b choices for
the n-th ball. Since the balls are distinguishable each of these placements represent different
configurations. Thus the total number of possible configurations in this case is bn .
Part (b): Following the hint, since we have a total of n distinguishable balls and b bins
we can imagine the requested problem as equivalent to that of counting the number of
configurations of n balls and b 1 sticks. The b 1 sticks represents the internal bin edges
and the linear ordering of the balls between sticks represents the ball ordering within the
bin. Now to count this number we note that we can order n + b 1 distinguishable objects
in (n + b 1)! ways, but since the b 1 sticks are indistinguishable we need to divide by the
number of unique orderings of b 1 objects giving a total count of
(n + b 1)!
.
(b 1)!
Part (c): If the balls in each bin are in fact identical in the same manner in which we
needed to divide (n + b 1)! by the number of unique orderings of distinguishable internal
sticks (b 1)! we need to divide the result above by the number of unique orderings of these
n balls or n!. This gives
(n + b 1)! n+b1
= .
(b 1)!n! n
Part (d): We assume in this part that we have more bins than balls or b > n. If we assume
for the moment that the balls are not identical then we have b choices for the location of the
first ball, b 1 choices for the location of the second ball, b 2 choices for the location of
the third ball and so on down to b n + 1 choices for the n-th ball. This gives
b!
b(b 1)(b 2) (b n + 2)(b n + 1) = ,
(b n)!
Ways to place n unique balls. Since the balls are in fact identical this number over counts
the total possible by n!. So we have
b! b
= ,
(b n)!n! n
placements of identical balls where no more than one ball can go in any given bin.
Part (e): We assume in this part that we have more balls than bins or n > b. Consider all
of our n indistinguishable balls lined up in a row and note that our problem is equivalent
to that of counting the number of ways we can select b 1 spaces (which will represent
the b 1 internal bin boundaries) from all n 1 possible spaces in our linear ordering of the
balls. The number of ways to select b 1 things from a set of n 1 is given by
n1
,
b1
or the claimed number.
CS161: Design and Analysis of Algorithms Summer 2004
If you use pseudocode in your assignment, please either actually use psuedocode or
include a written explanation of your code. The TA does not want to parse through
C or JAVA code with no comments.
If you fax in your problem set, please make sure you write clearly and darkly. Some
problem sets were very difficult to read. Also, make sure that you leave enough margins
so that none of your work is cut off.
The version of the Master Theorem that was given in lecture is slightly different from
that in the book. We gave case 2 as
On exams, using the Master Theorem is normally quicker than other methods. But,
remember that cases 1 and 3 only apply when f (n) is polynomially smaller or larger,
which is different from asymptotically smaller or larger.
In addition, asymptotic bounds for Stirlings formula are helpful in ranking the
expressions with factorials:
n! = (nn+1/2 en )
lg(n!) = (n lg n)
(lg n)! = ((lg n)lg n+1/2 e lg n )
Each term gives a different equivalence class, where the > symbol means .
n+1 n
22 > 22 > (n + 1)! > n! > en >
nlg lg n
n 2n > 2n > ( 32 )n > > (lg n)! >
(lg n)lg n
n2 n lg n n
n3 > > > > ( 2)lg n >
4lg n lg(n!) 2 lg n
2 lg n
2 > lg2 n > ln n > lg n > ln ln n >
lg (lg n) 1
n
lg(lg n)
2lg > > >
lg n n 1/ lg n
In general, if you have multiple recursive calls, the sum of the arguments to those
calls is less than n (in this case n/2 + n/4 + n/8 < n), and f (n) is reasonably
large, a good guess is T (n) = (f (n)).
(f) [2 points] T (n) = T (n 1) + 1/n.
Answer: T (n) = (lg n). We solve this problem by algebraic substitution.
T (n) = T (n 1) + 1/n
= T (n 2) + 1/(n 1) + 1/n
...
n
X
= (1) + 1/i
i=1
= ln n + (1)
Problem Set #1 Solutions 5
(h) [2 points] T (n) = nT ( n) + n.
Answer: T (n) = (n lg lg n). We solve this problem by algebraic substitution.
T (n) = nT ( n) + n
= n1/2 (n1/4 T (n1/4 ) + n1/2 ) + n
= n3/4 T (n1/4 ) + 2n
= n3/4 (n1/8 T (n1/8 ) + n1/4 ) + 2n
= n7/8 T (n1/8 ) + 3n
...
k k
= n11/2 T (n1/2 ) + kn
k
When, n1/2 falls under 2, we have k > lg lg n. We then have T (n) = n11/ lg n T (2)+
n lg lg n = (n lg lg n).
Here, a and c are the higher order bits, and b and d are the lower order bits of u and v
respectively. Multiplications are done recursively, except multiplication by a power of
2, which is a simple bitshift and takes (n) time for an n-bit number. Addition and
subtraction also take (n) time.
(a) [4 points] Write a recurrence for the running time of this algorithm as stated.
Solve the recurrence and determine the running time.
Problem Set #1 Solutions 6
Answer: Insertion sort is stable. We will prove this using the following loop
invariant: At the start of each interation of the for loop of lines 1-8, if A[a] =
A[b], a < b j 1 and distinct, then A[a] appeared before A[b] in the initial
array.
Initialization: Before the first loop iteration, j = 2 so there are no distinct
Problem Set #1 Solutions 7
Answer: Mergesort is stable. We prove this by induction on the fact that Merge-
sort is stable.
Problem Set #1 Solutions 8
Base Case: When we call merge-sort with indices p and r such that p 6< r (there-
fore p == r), we return the same array. So, calling mergesort on an array of size
one returns the same array, which is stable.
Induction: We assume that calling merge-sort on an array of size less than n
returns a stably sorted array. We then show that if we call merge-sort on an array
of size n, we also return a stably sorted array. Each call to mergesort contains two
calls to mergesort on smaller arrays. In addition, it merges these two subarrays
and returns. Since we assume that the calls to mergesort on smaller arrays return
stably sorted arrays, we need to show that the merge step on two stably sorted
arrays returns a stable array. If A[i] = A[j], i < j in the initial array, we need
f (i) < f (j) in the new array, where f is the function which gives the new positions
in the sorted array. If i and j are in the same half of the recursive merge-sort
call, then by assumption, they are in order when the call returns and they will
be in order in the merged array (since we take elements in order from each sorted
subarray). If i and j are in different subarrays, then we know that i is in the left
subarray and j is in the right subarray since i < j. In the merge step, we take
the elements from the left subarray while the left subarry element is less than or
equal to the right subarray element (line 13). Therefore, we will take element i
before taking element j and f (i) < f (j), the claim we are trying to prove.
Therefore, Mergesort is stable.
(c) [8 points] Quicksort: pseudocode given on page 146 of CLRS.
QUICKSORT(A, p, r)
1 if p < r
2 then q PARTITION(A, p, r)
3 QUICKSORT(A, p, q 1)
4 QUICKSORT(A, q + 1, r)
PARTITION(A, p, r)
1 x A[r]
2 i p1
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
Answer: Quicksort is not stable. To make it stable, we can add a new field to
each array element, which indicates the index of that element in the original array.
Then, when we sort, we can sort on the condition (line 4) A[j] < x OR (A[j] == x
AND index(A[j]) < index(x)). At the end of the sort, we are guaranteed to have
all the elements in sorted order, and for any i < j, such that A[i] equals A[j], we
will have index(A[i]) < index(A[j]) so the sort will be stable.
Problem Set #1 Solutions 9
// base cases
if (n == 1) return 1;
if (n == 0) return 0;
Sketch a recursion tree for this algorithm. During the computation of Fib(n), how
many times is Fib(n-1) called? Fib(n-2)? Fib(n-3)? Fib(2)? Use formula (3.23)
on page 56 of CLRS to conclude that this algorithm takes time exponential in n.
i i 1+ 5 1 5
Fi = , where = and = (1)
5 2 2
Answer:
F(n)
F(n1) F(n2)
We call F ib(n 1) one time during the recursion, F ib(n 2) 2 times, F ib(n 3)
3 times, F ib(n 4) 5 times, . . ., F ib(2) Fn1 times. Each node in the recurrence
Problem Set #1 Solutions 10
tree takes constant time to evaluate, so we need to calculate the number of nodes
in the tree. T (n) = ( ni=1 Fi ). Using formula 3.23 in the book, we get
P
n
X i i
T (n) = ( )
i=1 5
As n grows large, the i term goes to zero, so we are left with
n
i
) = (n )
X
T (n) = (
i=1 5
So, the algorithm takes time exponential in n.
(b) [5 points] Notice that our recursion tree grows exponentially because the same
computation is done many times over. We now modify the naive algorithm to only
compute each Fib(i) once, and store the result in an array. If we ever need Fib(i)
again, we simply reuse the value rather than recomputing it. This technique is
called memoization and we will revisit it when we cover dynamic programming
later in the course.
Prune your recursion tree from part (a) to reflect this modification. Considering
the amount of computation done at each node is (1), what is the total amount
of computation involved in finding Fib(n)?
Answer:
Fib(n)
Fib(n1) Fib(n2)
Fib(n2) Fib(n3)
Fib(n3) Fib(n4)
... Fib(n5)
As in part (a), the time to calculate the nth Fibonacci number Fn is equal to the
time to calculate Fn1 , plus the time to calculate Fn2 , plus the time to add Fn1
to Fn2 . However, because of memoization, the time to calculate Fn2 is constant
once Fn1 has already been calculated. Therefore, the recurrence for this modified
algorithm is T (n) = T (n 1) + (1). It is easy to use the algebraic substitution
method to verify that the solution to this recurrence is T (n) = (n).
Problem Set #1 Solutions 11
for n = 1, 2, 3, . . . .
Hint: The last line of the question should immediately suggest what proof tech-
nique would be useful here.
Answer: We prove the statement by mathematical induction. For the base case
of n = 1, we get
!1 !
1 1 F2 F1
= (2)
1 0 F1 F0
which certainly holds. Now, we assume the statement holds for n 1. Then,
!n !n1 !
1 1 1 1 1 1
=
1 0 1 0 1 0
! !
Fn Fn1 1 1
=
Fn1 Fn2 1 0
!
Fn + Fn1 Fn
=
Fn1 + Fn2 Fn1
!
Fn+1 Fn
=
Fn Fn1
(a) [10 points] Design a (n lg n)-time algorithm that, given an array A of n integers
and another integer x, determines whether or not there exist two (not necessarily
distinct) elements in A whose sum is exactly x. This is problem 2.3-7 on page 37
of CLRS, slightly reworded for the sake of clarity.
Answer: We first sort the elements in the array using a sorting algorithm such
as merge-sort which runs in time (n lg n). Then, we can find if two elements
exist in A whose sum is x as follows. For each element A[i] in A, set y = A[i] x.
Using binary search, find if the element y exists in A. If so, return A[i] and y.
If we cant find y for any A[i], then return that no such pair of elements exists.
Each binary search takes time (lg n), and there are n of them. So, the total
time for this procedure is T (n) = (n lg n) + (n lg n) where the first term comes
from sorting and the second term comes from performing binary search for each
of the n elements. Therefore, the total running time is T (n) = (n lg n).
An alternate procedure to find the two elements in the sorted array is given below:
SUM-TO-X(A)
1 Merge-Sort(A)
2 i1
3 j length(A)
4 while i j
5 if A[i] + A[j] equals x
6 return A[i], A[j]
7 if A[i] + A[j] < x
8 then i i + 1
9 if A[i] + A[j] > x
10 then j j 1
We set counters at the two ends of the array. If their sum is x, we return those
values. If the sum is less than x, we need a bigger sum so we increment the bottom
counter. If the sum is greater than x, we decrement the top counter. The loop
does (n) iterations, since at each iteration we either increment i or decrement j
so j i is always decreasing and we terminate when j i < 0. However, this still
runs in time (n lg n) since the running time is dominated by sorting.
(b) [16 points] The majority element of an array A of length n is the element that
appears strictly more than bn/2c times in the array. Note that if such an
element exists, it must be unique.
Design a (n)-time algorithm that, given an array A of n objects, finds the
majority element or reports that no such element exists. Assume that two objects
can be compared for equality, read, written and copied in (1) time, but no other
operations are allowed (for instance, there is no < operator). Explain why
your algorithm is correct (no formal proof necessary).
Hint: It is possible to find a candidate solution using only one pass through the
array, with the help of an auxiliary data structure such as a stack or a queue.
Then with one more pass you can determine whether this candidate is indeed the
majority element.
Answer: We will give an algorithm to solve this problem which uses an auxiliary
Problem Set #1 Solutions 13
stack and a proof of why it is correct. The following is the pseudocode for the
algorithm.
FIND-MAJORITY-ELEMENT(A)
1 for i 1 to length[A]
2 if stack is empty
3 then push A[i] on the stack
4 else if A[i] equals top element on the stack
5 then push A[i] on the stack
6 else pop the stack
7 if stack is empty
8 then return NoMajority
9 candidate top element on the stack
10 counter 0
11 for i 1 to length[A]
12 if A[i] equals candidate
13 then counter counter + 1
14 if counter > blength[A]/2c
15 then return candidate
16 return NoMajority
The procedure first generates a candidate object from the array. It finds this
element using the stack by looping over the array. If the stack is empty or the
current element is the same as the top object on the stack, the element is pushed
onto the stack. If the top object of the stack is different from the current element,
we pop that object off the stack.
Claim: If a majority element exists, it will be on the stack at the end of this part
of the procedure.
Proof: (by contradiction) Call the majority element x and say it appears i >
bn/2c times. Each time x is encountered, it is either pushed on the stack or an
element (different from x) is popped off the stack. There are n i < i elements
different from x. Assume x is not on the stack at the end of the procedure. Then,
each of the i elements of x must have either popped another element off the stack,
or been popped off by another element. However, there are only n i < i other
elements, so this is a contradiction. Therefore, x must be on the stack at the end
of the procedure.
Notice that this proof does not show that if an element is on the stack at the end
of the procedure that it is the majority element. We can construct simple coun-
terexamples (A = [1, 2, 3]) where this is not the case. Therefore, after obtaining
our candidate majority element solution, we check whether it is indeed the major-
ity element (lines 9-16). We do this by scanning through the array and counting
the number of times the element candidate appears. We return candidate if it
appears more than bn/2c times, else we report that no majority element exists.
This procedure scans through the array twice and does a constant amount of
computation (pushing and popping elements on the stack take constant time) at
each step. Therefore the running time is T (n) = cn = (n).
CS161: Design and Analysis of Algorithms Summer 2004
Remember you can only split an expectation of a product into the product of the
expectations if the two terms are independent. Many students did this on question
3c either without justification or justifying it as linearity of expectation, which is
incorrect.
For the skip list question 4b, many students found the expected number of nodes
between lef t[i + 1].below and right[i + 1].below by saying the expected number of
nodes at level i + 1 is npi+1 and the expected number of nodes at level i is npi . Then,
if you divide the number of nodes at i by the number at i + 1, you get an average
interval of 1/p. While this gives the correct answer, it is not a valid argument as it
stands. For example, lets say you always propagate the first nodes in level i - you will
get an interval of length 1 with probability 1/p and an interval of length npi (1 p)
with probability 1 1/p, which clearly gives you a different expectation. You need to
use properties other than just the expected number of nodes to calculate the expected
interval length.
(a) [5 points] Do problem C.2-9 on page 1106 of CLRS. Assume that initially, the
prize is placed behind one of the three curtains randomly.
Answer: The answer is not 1/2, because now the two curtains are no longer
equally likely to hide the prize. In fact, the original 3 cases are still equally likely,
but now in two of the cases the prize is behind the curtain that is not revealed or
chosen. Therefore, the probability of winning if you stay is 1/3, if you switch is
2/3.
(b) [5 points] Do problem C.2-10 on page 1106 of CLRS.
Answer: This is essentially the same problem, except now there is no option
to switch. The prisoners are the curtains, freedom is the prize, and the guards
revelation is equivalent to the emcees revelation in the previous question. Thus,
the probability that X will go free is 1/3.
Problem Set #2 Solutions 2
(c) [8 points] Consider the following game using a standard 52-card deck (with 26
red cards and 26 black cards):
i. the deck D is shuffled randomly.
ii. one at a time, the top card of D is removed and revealed until you say stop
or only one card remains.
iii. if the next card is red, you win the game. if the next card is black, you lose
the game.
Consider the following algorithmic strategy for playing this game:
i. count the number of black cards (b) and red cards (r) revealed thus far.
ii. as soon as b > r, say stop.
What is the probability of success for this strategy? Is there a better strategy?
Can you prove a useful upper bound on the probability of success for any legal
strategy?
Hint: Consider the following modification to the game: after you say stop,
rather than looking at the top card of D, we look at the bottom card.
Answer: For any given strategy S, let P rS {(b, r)} be the probability that we
stop after we have seen b black cards and r red cards. Note that some of these
may be 0, for example, using the strategy in question we will only stop when
b = r + 1 or (b, r) = (25, 26).
Since we assume all permutations of the deck to be equally likely, the remaining
52 b r cards are in random order, so the probability of success (top remaining
card is red) is just
26 r
P rS {success | (b, r)} = .
52 b r
Thus, we can write
X 26 r
P rS {success} = P rS {(b, r)} .
52 b r
(b,r)
Now notice that (26 r)/(52 b r) is also the probability that the bottom card
is red given that we stop at (b, r). Thus, we can say
X
P rS {success} = P rS {(b, r)} P r{bottom = red | (b, r)}.
(b,r)
The argument applies for any legal strategy, thus, regardless of the strategy, we
succeed with probability 1/2.
If you are uncomfortable with some of the probabilistic manipulation and
reasoning in this solution, please review Appendix C in CLRS, particularly section
C.2 and Bayess theorem.
Problem Set #2 Solutions 3
(a) [3 points] Write a recurrence for the worst-case running time T (n) of this
algorithm.
Answer: Our partition guarantees that there are at least n elements in each
subarray. In the worst case, all the remaining elements will go to one subarray.
Then, we will need to solve one subproblem
of the size n and one of the size
n n. Also, we need to sort the 2 n + 1 elements to find the pivot. Using
insertion sort, this is a worst case running time of ((2 n + 1)2 ) = (n). Then,
we find the median of the sorted sample, a (1) step. In addition, we need to
partition the elements at each step, a(n) operation.
So in the worst-case, the
recurrence relation will be T (n) = T ( n) + T (n n) + (n).
(b) [2 points] Draw the recursion tree for the expression you came up with in part
(a). Label the work in the top 3 levels of the tree.
Answer:
=n
n
1/2 1/2
n nn =n
(c) [6 points] How many levels does it take before the shortest branch reaches a
leaf? the longest branch (an asymptotic upper bound is sufficient)?
Hint: Consider writing a recurrence
p for
the height of the longest branch. You
may find the approximation n n n 1/2 helpful in guessing a solution
for this recurrence.
Answer: The shortest branch is the one where everytime we call T ( n). At each
i
level i, we will call T (n1/2 ). We assume T (n) is a constant for small values of n,
i
so we find the value of i for which n1/2 2. This is true for i lg lg n. Thus,
Problem Set #2 Solutions 4
h(n) = h(n n) + 1
q
c n n+1
c( n 1/2) + 1
= c n c/2 + 1
c n
which is true for c 2. Therefore, we know that the longest branch is O( n).
(d) [1 point] Using your answers from parts (b) and (c), give a big-O bound on T (n).
Answer: T (n) = O(n3/2 ). Looking at the recurrence tree from part (b), we see
that we do (n) work on each level. We showed
in part (c) that the longest branch
of the recurrence tree had a height of O( n) levels. So, we know the recurrence
is bounded by T (n) = O(n3/2 ).
Because Xq is independent from the time is takes to do the recursive calls, we can
split the expectation of the product into the product of expectations.
n
X
E[T (n)] = E[Xq ] E[T (q 1) + T (n q) + (n)]
q=1
Substituting in the value for E[Xq ] that we solved in part (a) and again changing
the expectation of the sum into a sum of expectations, we have
n
X 1
E[T (n)] = (E[T (q 1)] + E[T (n q)] + E[(n)])
q=1
n
n
1X
= (E[T (q 1)] + E[T (n q)] + (n))
n q=1
n n n
1 X X X
= ( E[T (q 1)] + E[T (n q)] + (n))
n q=1 q=1 q=1
1
= ((E[T (0)] + E[T (1)] + + E[T (n 1)]) +
n
(E[T (n 1)] + E[T (n 2)] + + E[T (0)]) + n(n))
n1
1 X
= (2 E[T (q)]) + (n)
n q=2
Answer:
n1 dn/2e1 n1
X X X
k lg k = k lg k + k lg k
k=2 k=2 k=dn/2e
dn/2e1 n1
X X
k lg(n/2) + k lg n
k=2 k=dn/2e
dn/2e1 n1
X X
= lg(n/2) k + lg n k
k=2 k=dn/2e
(n/2 1)(n/2) (n 1)(n) (n/2 1)(n/2)
= lg(n/2) 1 + lg(n)
2 2 2
2
2 2
(n/2) n (n/2)
(lg n lg 2) + (lg n)
2 2 2
2 2 2 2
n n n n
= (lg n) + (lg n) (lg n)
8 8 2 8
1 2 1 2
= n lg n n
2 8
(e) [2 points] Do problem 7-2(e) on page 161 of CLRS. Note that you are actually
showing E[T (n)] = O(n lg n), but the bound is trivial.
Answer: We will show this using the guess-and-check method. First, we guess
that E[T (q)] cq lg q for 2 q < n. Then, we show that the same is true for
E[T (n)].
n1
2X
E[T (n)] = E[T (q)] + kn
n q=2
n1
2X
cq lg q + kn
n q=2
2 1 2 1 2
c n lg n n + kn
n 2 8
1 1
= 2c n lg n n + kn
2 8
1
= cn lg n cn + kn
4
cn lg n
for c 4k.
4. [19 points] Skip List Analysis
In this problem, you will analyze the behavior of the skip list data structure introduced
in lecture. Please see the Skip List Summary handout for more information, including
detailed descriptions of the FIND, INSERT, and DELETE operations.
Problem Set #2 Solutions 7
Throughout this problem, we will consider a skip list with L levels and n elements, in
which elements are propagated to the next level with probability p, 0 < p < 1.
(a) [5 points] What is the expected value of L in terms of n and p? For simplicity
of analysis, consider levels that you expect to contain less than 1 cell to be
nonexistent. In practice, how might you enforce this assumption?
Answer: E[L] = (log1/p n). We can define the indicator variable Ii,k to be
equal to 1 if and only if the element A[i] is present in the k th level of the skip list.
Then,
Xn
E[L] = min(E[ Ij,k ] 1)
k
j=1
the expected number of levels in the skip list is equal to the minimum level where
the expected number of elements in the list is 1. By linearity of expectation,
we can move the summation outside of the expectation:
n
X
E[L] = min( E[Ij,k ] 1)
k
j=1
n
X
E[L] = min( pk 1)
k
j=1
n
X
k
= min(p 1 1)
k
j=1
= min(npk 1)
k
The value npk falls below 1 when k log1/p n. So, we expect to have E[L] =
(log1/p n).
(b) [8 points] Consider the work done by the SEARCH operation at level i, where
i < L. We must examine, in the worst case, all cells between lef t[i + 1].below and
right[i + 1].below (since lef t[L] and right[L] are not actually defined, we can say
that in level L 1 we look between dummy and NULL). Thus, we examine all
cells immediately after some given cell in level i that have not been propagated
to level i + 1. What is the expected number of such cells? What is the expected
running time of the SEARCH operation in terms of n and p?
Problem Set #2 Solutions 8
We found the expected number of levels in part (c), names E[levels] = 1/(1 p).
So,
n
X
E[num cells] = 1/(1 p) = n/(1 p).
j=1
(d) [5 points] Consider the following generalization of the ith element: the weighted
ith element is the element xk satisfying
X i X ni
wj < and wj
xj <xk
n x >x
n
j k
Modify your algorithms from parts (b) and (c) to compute the weighted ith
element in, respectively, (n lg n) and (n) worst-cased running time.
Answer:
Pk1 We can modify the algorithm in part (b) by simply comparing our sum
i=1 w i to i/n instead of 1/2. The rest of the analysis holds as before. The
running time is still dominated by the sort which takes (n lg n). The actual
selection of the weighted ith element from the sorted array takes time (n) since
it consists of one pass through the sorted array, summing the weights.
In part (c), we can simply call W-SELECT(A, 1, n, i/n).
(a) [10 points] Consider the following idea: one of the students is designated as the
counter. His job will be to toggle the switch OFF anytime he enters the room
and sees that the switch is set to ON. Everyone else will toggle the switch ON if
its set to OFF, but only once.
Who can make the claim and when? Formally analyze the expected running time
of this algorithm. Your answer should be asymptotic in terms of n.
Answer: E[time] = (n2 ). The counter can make the claim that everyone has
been in the room once he has toggled the switch to OFF n 1 times. Since each
student besides the counter sets the switch to ON exactly once, we know that
n 1 distinct students besides the counter have been in the room. So, the counter
Problem Set #2 Solutions 11
knows that all n students have been in the room exactly once.
We can analyze the expected running time of this algorithm by breaking the
sequence of students in the room into periods. We will define two types of periods
Xi - the number of students who enter the room until (and including) the one
who sets the switch to ON for the ith time.
Yi - the number of students who enter the room after the ith student has turned
the switch ON until (and including) the counter who turns the switch to OFF.
For example, if student 1 is the counter and there are 4 students, then the following
sequence would be broken up into the given periods:
4123431224131
X1 = {4}, Y1 = {1}, X2 = {2}, Y2 = {3, 4, 3, 1}, X3 = {2, 2, 4, 1, 3}, Y3 = {1}
Notice that the periods run X1 , Y1 , X2 , Y2 , . . . , Xn1 , Yn1. After period Yn1 we
are finished because the counter has turned off the switch n 1 times.
Pn1
The expected length of time for the algorithm is E[time] = E[ i=1 (Xi + Yi )].
By linearity of
Pn1 expectation, we can pull the sum outside of the expectation.
E[time] = i=1 (E[Xi ] + E[Yi ]).
Xi is the number of students who enter the room until one can set the switch to
ON. After i 1 students have set the switch to ON, there are (n 1) (i 1)
students who can set it to ON. Since we choose students uniformly at random,
the probability at any point that one of these is selected is (n i)/n. Thus,
P r{Xi = k} = ( ni
n
)j1( ni ). This is the geomtric series, and its expectation is
n
given by E[Xi ] = ni .
Similarily, we can find the expected value of Yi , the number of students who
enter the room after the ith student turns on the switch until the counter enters.
The probability that the counter enters is 1/n. So, P r{Yi = k} = ( n1 n
)j1( n1 ).
Again, this is a geometric distribution with expected value E[Yi ] = n. Using these
formulas, we can find the expected length of time.
n1
X n
E[time] = +n
i=1
n i
n1
X 1
= n(n 1) + n
i=1
ni
n1
X 1
= n(n 1) + n
j=1
j
= n(n 1) + n(ln(n 1))
= (n2 )
(b) [10 points] Now consider the following modification to this idea: instead of
deciding on the counter ahead of time, the counter will be the first student to
Problem Set #2 Solutions 12
enter the room twice. Now depending on what day that occurs, the counter will
know how many distinct people have been in the room already. Notice that this
means the students need different rules for the first n days of the imprisonment -
after that, they can continue using the algorithm from part (a).
How must the students behave during the first n days to make this work? Pay
particular attention to what happens on day n. Analyze the running time of
the modified algorithm - we do not expect a formal analysis this time, but your
explanation should be complete and mathematical.
Answer: For the first n days everyone should act as follows:
i. The switch remains OFF until someone has been in the room twice. That
person turns the switch ON and becomes the counter, setting the count to
j 2, where j is the day when he enters the room the second time.
ii. If someone other than the counter enters the room and finds the switch OFF,
the counter has not been determined yet. Thus, that person will be counted
in the j 2 and does not touch the switch after day n.
iii. If someone other than the counter enters the room and finds the switch
ON, the counter has been determined. Therefore, that person has not been
counted in j 2 and must participate in the algorithm after day n. Same
goes for anyone who has not entered the room at all during days 1 through
n.
iv. Finally, the person who is in the room on day n turns the switch OFF prior
to beginning the second phase. If the person entering the room on day n finds
the switch OFF, then nobody has been in the room twice and that person
can make the claim.
After the first n days, everyone should act as in part (a) with the counter
determined, except that the people who entered the room when the switch was
OFF (befor the counter was determined) no longer participate.
The expected running time of this algorithm depends on how many people we
counted during the first n days, call that number j. We will first analyze the
expected time after the first n days based on j. As in part (a), we alternate time
periods Xi , Yi , but now since j people have already been counted, we start with
period Xj+1. Therefore, the expected time is given below with the sum starting
from j + 1
n1
X n
E[time|j] = +n
i=j+1
n i
n1
X 1
= n(n j 1) + n
i=j+1
ni
= n(n j 1) + n(ln(n j 1))
Now we will bound the probability of j, the number of people counted during the
Problem Set #2 Solutions 13
first n days. This is the birthday paradox problem, where the days are the people
in the birthday
paradox, and the people are the birthdays. We know that when
j = ( n), we have P r{someone enters twice in j days} 1/2. So, we can
now bound our expected time.
n1
X
E[time] = n + P r{j}E[time|j]
j=0
c n n1
X X
= n+ P r{j}E[time|j] + P r{j}E[time|j]
j=0 j=c n+1
c n
X
n+ P r{j}E[time|c n] + 0
j=0
c n
X
= n + n(n c n 1) + n(ln(n c n 1)) P r{j}
j=0
n + n(n c n 1) + n(ln n) (1/2)
n + (1/2)n2 (c/2)n3/2 n + (n ln n)
= (n2 )
This expected time is also O(n2 ), as its bounded above by n+E[time|0] = (n2 ).
Thus, our asymptotic expected time remains (n2 ).
(c) [8 points] It is possible to do asymptotically better than your answers in parts
(a) and (b). However, there are well-defined lower bounds on the performance of
any correct algorithm for this problem. For instance, since all students have to
have been in the room at the time the claim is made, clearly no correct algorithm
can have an expected running time asymptotically less than (n). Can you come
up with a tighter lower bound? Argue that your answer is correct; you may use
results proven in class or in the textbook.
Answer: No correct algorithm can have a better expected running time than
(n lg n). As we proved in class with the balls and bins question, the expected
amount of time until each bin has at least one ball is (n lg n). In this question,
the days are balls and the students are bins. We want the expected number time
until each students has been in the room for at least one day. So, the expected
number of days is (n lg n). Any algorithm the students come up with cant
have an expected running time faster than the expected time for the condition to
actually hold, otherwise it would not be correct. Therefore, the expected running
time of any algorithm which will only tell the professor they have all been in the
room once they have actually all been there must take at least (n lg n) days.
Hence, E[time] = (n lg n).
CS161: Design and Analysis of Algorithms Summer 2004
D-PARENT(i)
1 return d(i 1)/de
D-CHILD(i, j)
2 return d(i 1) + j + 1
D-HEAPIFY(A, i, d)
1 largest i
2 for j 1 to d
3 j D-CHILD(i, j)
4 if j heapsize[A] and A[j] > A[largest]
5 then largest j
6 if largest 6= i
7 then exchange A[i] A[largest]
8 D-HEAPIFY(A, largest, d)
D-BUILD-HEAP(A, d)
1 heapsize[A] length[A]
2 for i d(length[A] 1)/de downto 1
3 do D-HEAPIFY(A, i, d)
We can bound the running time of this algorithm by modifying the analysis in
section 6.3 of CLRS. Notice that there are
lgd (n(d1))
X n(d 1)
T (n) dh
dh+1
h=0
X h
< n(d 1)
h=0
dh
1/d
n(d 1) A.8
(1 1/d)2
d
= n
d 1
1
= n 1+
d1
D-EXTRACT-MAX(A, d)
1 if heapsize[A] < 1
2 then error
3 max A[1]
4 A[1] A[heapsize]
5 heapsize heapsize 1
6 D-HEAPIFY(A, 1, d)
7 return max
All operations besides D-HEAPIFY take O(1) time. D-HEAPIFY takes time
(d logd n) so D-EXTRACT-MAX takes time (d logd n).
(f) [2 points] Do problem 6-2(e) on page 143 of CLRS.
Answer: If k < A[i], then D-INCREASE-KEY does not change the heap.
Otherwise, it will set A[i] to k and move the element upwards toward the root
until it is smaller than its parent.
D-INCREASE-KEY(A, i, k, d)
1 if k > A[i]
2 then A[i] k
3 while i > 1 and A[D-PARENT(A, i, d)] < A[i]
4 do exchange A[i] A[D-PARENT(A, i, d)]
5 i A[D-PARENT(A, i, d)]
Problem Set #3 Solutions 4
The running time of the algorithm is proportional to the height of the heap, and
each level takes a constant amount of work, so the overall time is (logd n).
(g) [1 point] Do problem 6-2(d) on page 143 of CLRS.
Answer: The procedure D-INSERT is the same as for the binary heap. We
add the element to the end of the array and set its key to . Then, we call
D-INCREASE-KEY to increase to increase the key to key.
D-INCREASE-KEY(A, key, d)
1 heapsize[A] heapsize[A] + 1
2 A[heapsize[A]]
3 D-INCREASE-KEY(A, heapsize[A], key, d)
The first two operations take constant time, and the call to D-INCREASE-KEY
takes time (logd n). So, the running time for D-INCREASE-KEY is (logd n).
with i0 and k i0 leaves respectively, since otherwise we could replace them with
better trees and get a lower value of D(T ).
Finally, the optimal tree T represents the best possible split of the k leaves into
(optimal) subtrees with i0 and k i0 leaves. Since each subtree must have at least
one leaf, we have d(k) = min1ik1 {d(i) + d(k i) + k}.
(d) [4 points] Do problem 8-1(d) on page 178 of CLRS.
Answer: We can find the minimum by setting the derivative with respect to i
equal to zero.
d
(i lg i + (k i) lg(k i)) = (i)(1/i)(lg e) + (1)(lg i) +
di
(k i)(1/(k i))( lg e) + (1)(lg(k i))
= lg e + lg i lg e lg(k i)
= lg i lg(k i)
contribute nothing to the sum), this is precisely D(TA )/n!. Thus, E[T (n)] =
(n! lg(n!))/n! = (lg n!) = (n lg n).
(f) [8 points] Do problem 8-1(f) on page 178 of CLRS.
Answer: Consider the decision tree TB for algorithm B. We will work from the
bottom to the top, replacing each randomization node with the branch that has
the smallest external path length. We call the resulting tree TA and we show that
TA represents a valid deterministic sorting algorithm and E[TA (n)] E[TB (n)].
First, consider the issue of correctness. Since the decision tree for A represents a
possible execution of B (with appropriate random choices made at each step), and
B is correct, the resulting decision tree must represent a valid sorting algorithm.
Since all randomization nodes have been removed, this algorithm is deterministic.
Now notice that at each randomizationPnode N, the expected execution time
r th
after that node is reached is equal to i=1 (1/r)E[T (Si )], where Si is the i
subtree of N. Clearly we have E[T (N)] minri=1 E[T (Si )], since all terms in the
average must be at least the minimum. Thus, each transformation that replaces
a randomization node with its best child does not increase the expected running
time.
We formalize these notions slightly. Consider a sequence of decision trees
T1 , . . . , Tm where T1 = TB and Tm = TA , and each Ti was obtained from Ti1
by choosing a randomization node all of whose descendants are deterministic
and replacing it with the subtree with the smallest external path length. Notice
that since no descendants of N are randomization nodes, the notion of expected
running time and external path length (given random input) is the same for its
children.
The correctness argument above applies, as does the running time argument:
E[T (Ti )] E[T (Ti1 )]. Finally, when all the randomization nodes are removed,
we have a deterministic sorting algorithm with the decision tree Tm = TA and
expected running time no worse than the expected running time of B.
e
k0 lg < 3 lg n
k0
k (lg e lg k0 ) < 3 lg n
0
c lg n c lg n
lg e lg < 3 lg n
lg lg n lg lg n
c
(lg e lg c lg lg n + lg lg lg n) < 3
lg lg n
c
( lg e + lg c + lg lg n lg lg lg n) > 3
lg lg n
For c > e, the lg c term is larger than the lg e term, so we need to make sure the
rest of the left side is greater than the right side, i.e.
Problem Set #3 Solutions 8
c
(lg lg n lg lg lg n) > 3
lg lg n
3 lg lg n
c >
lg lg n lg lg lg n
3
c > lg lg lg n
1 lg lg n
As n gets large, the second term in the denominator will go to zero; in fact for
n 16 we have lglglglglgnn 1/2 so the right hand side is 6.
Thus, we can choose an appropriate constant c to guarantee that (e/k0 )k0 < 1/n3
and therefore Qk < 1/n3 for k k0 . Thus, Pk nQk < 1/n2 for k k0 .
(e) [4 points] Do problem 11-2(e) on page 250 of CLRS.
Answer:
n
X
E[M] = i P r{M = i}
i=1
Xk0 n
X
= i P r{M = i} + i P r{M = i}
i=1 i=k0 +1
k0
X n
X
k0 P r{M = i} + n P r{M = i}
i=0 i=k0 +1
= k0 P r{M k0 } + nP r{M > k0 }
c lg n c lg n c lg n
= n Pr M > + Pr M
lg lg n lg lg n lg lg n
c lg n
E[M] (n)(n k0 )(1/n2 ) + 1
lg lg n
c lg n
(n)(n)(1/n2 ) +
lg lg n
On problem 1(f), many people assumed that if the key[y] < key[x] then y was in the
left subtree of x or that if priority[y] < priority[x] then y was a child of x. These
stipulations are not true, since in either case you can also have y not be a descendant
of x and the condition may still hold. Also, many people did not prove both the if and
the only if parts of the statement.
For augmented red-black trees in general, if the augmentation does not satisfy Theorem
14.1, you should show that it can be maintained efficiently through insertion, deletion,
and rotations.
On problem 5(b), a common mistake was to sort by the x indices, but to not take into
account the order for equal x index. For equal xi s, you need to sort by decreasing
index yj so that you can match at most one yj to each xi .
(these will go in the left subtree) and the elements larger than the root key (right
subtree).
(b) [2 points] Do problem 13-4(b) on page 298 of CLRS.
Answer: Recall from lecture and section 12.4 of CLRS that the expected height
of a randomly built binary search tree is O(lg n). We show that constructing the
unique treap is equivalent to inserting the elements into a binary search tree in
random order.
Suppose we insert the elements into a BST in order of increasing priority. Normal
BST insertion will maintain the binary search tree property, so we only need to
show that the min-heap property is maintained on the priorities. Suppose v is a
child of u. The insertion procedure from section 12.3 of CLRS always inserts each
element at the bottom of the tree, therefore v must have been inserted after u.
Thus, the priority of v is greater than the priority of u, and we have built a treap.
The priorites are assigned randomly, which means any permutation of the
priorities is equally likely. When considering this as BST insertion, it translates
into any ordering being equally likely, and therefore the expected height of a treap
is equal to the expected height of a randomly built BST, which is O(lg n).
(c) [6 points] Do problem 13-4(c) on page 298 of CLRS.
Answer: Treap-Insert works by first inserting the node according to its key and
the normal BST insertion procedure. This is guaranteed to maintain the first two
conditions of the treap, since those correspond to BST properties. However, we
may have violated the third condition, the min-heap ordering on the priorities.
Note that (initially) this violation is localized to the node were inserting and its
parent, since we always insert at the leaves. Rotations preserve BST properties,
so we will use rotations to move our node x up as long as the min-heap ordering
is violated, i.e. as long as priority[x] < priority[parent[x]]. If x is a left child we
will right-rotate it, and if its a right child we will left-rotate it. Notice that this
preserves the heap property elsewhere in the treap, assuming that the children
of x had higher priority than parent[x] prior to the rotation (to be completely
formal we would have to prove this using a loop invariant).
The pseudocode is as follows:
TREAP-INSERT(T, x, priority)
1 TREE-INSERT(T, x)
2 while parent[x] 6= NIL priority[x] < priority[parent[x]]
3 if lef t[parent[x]] == x
4 Right-Rotate(T, parent[x])
5 else
6 Left-Rotate(T, parent[x])
(d) [2 points] Do problem 13-4(d) on page 298 of CLRS.
Answer: TREAP-INSERT first performs a BST insert procedure which runs in
time proportional to the height of the treap. Then, it rotates the node up one
level until the min-heap property is satisfied. Thus, the number of rotations we
perform is bounded by the height of the treap. Each rotation takes constant
Problem Set #4 Solutions 3
time, so the total running time is proportional to the height of the treap, which
we showed in (b) to be expected (lg n).
(e) [4 points] Do problem 13-4(e) on page 298 of CLRS.
Answer: We will prove this using a loop invariant: after having performed k
rotations during the insertion of x, Ck + Dk = k.
Initialization: After we insert x using BST-INSERT but before performing any
rotations, x is a leaf and its subtrees are empty, so C0 = D0 = 0.
Maintenance: Assume that we have performed k rotations on x and that Ck +Dk =
k. Now, if we perform a right-rotation on x, the left child of x remains the same,
so Ck+1 = Ck . The right child of x changes from to y with subtrees (left)
and (right) using the notation from page 278. The left spine of the right child
used to be the left spine of and now it is y plus the left spine of . Therefore,
Dk+1 = Dk + 1. Thus, Ck+1 + Dk+1 = Ck + Dk + 1 = k + 1, which is precisely the
number of rotations performed. The same holds for left-rotations, where we can
show that Dk+1 = Dk and Ck+1 = Ck + 1.
Termination: After TREAP-INSERT is finished, the number of rotations we
performed is equal to C + D, precisely the condition that needed to be shown.
(f) [5 points] Do problem 13-4(f) on page 298 of CLRS.
Answer: First, assume Xi,k = 1. We will prove priority[y] > priority[x],
key[y] < key[x], and z such that key[y] < key[z] < key[x], we have priority[y] <
priority[z]. The first property follows from the min-heap property on priorities,
since y is a descendant of x. The second follows from the BST property,
since y is in the left subtree of x. Finally, consider any node z satisfying
key[y] < key[z] < key[x], and imagine an inorder tree walk on the treap. After
y is printed, we will print the right subtree of y, at which point the entire left
subtree of x will have been printed since y is in the right spine of that subtree.
Thus, the only nodes printed after y but before x are in the right subtree of
y; therefore, z must be in the right subtree of y and by the min-heap property
priority[y] < priority[z].
Now, we will assume the three properties and prove that Xi,k = 1. First consider
the possibility that y is in the left subtree of x but not in the right spine of
that subtree. Then there exists some node z in the spine such that going left
from z will lead to y. Note that this z satisfies key[y] < key[z] < key[x], but
priority[z] < priority[y] which violates the third property. Clearly y cannot be
in the right subtree of x without violating the second property, and x cannot be
a descendant of y without violating the first property. Suppose that x and y are
not descendants of each other, and let z be their common ancestor. Again we
have key[y] < key[z] < key[x] but priority[z] < priority[y], violating the third
property. The only remaining possibility is that y is in the right spine of the left
subtree of x, i.e. Xi,k = 1.
(g) [4 points] Do problem 13-4(g) on page 300 of CLRS.
Answer: Assume that k > i. P r{Xi,k = 1} is the probability that all the
conditions in part (f) hold. Consider all the elements with keys {i, i + 1, . . . , k}.
Problem Set #4 Solutions 4
There are k i + 1 such elements. The values of their priorities can be in any
order, so there are (k i + 1)! possible (and equally likely) permutations. In order
to satisfy our conditions, we need priority[z] > priority[y] > priority[x] for all
z {i + 1, i + 2, . . . , k 1}. This fixes the priorities of x and y to be the lowest
two priorities, allowing (k i1)! permutations of the remaining priorities among
the elements with keys in {i + 1, . . . , k 1}. The probability of Xi,k = 1 is the
(ki1)!
ratio of these two, which gives P r{Xi,k = 1} = (ki+1)! . Most of the terms in
the factorials will cancel, except for the first two terms in the denominator. This
1
leaves us with P r{Xi,k = 1} = (ki+1)(ki) .
(h) [3 points] Do problem 13-4(h) on page 300 of CLRS.
Answer: The expected value for C, which is the number of elements in the right
spine of the left subtree of x is simply the expectation of the sum of Xi,k over all
i < k. This is
Xk1
E[C] = E[ Xi,k ]
i=1
k1
X
= E[Xi,k ]
i=1
k1
X
= P r[Xi,k = 1]
i=1
k1
X 1
=
i=1
(k i + 1)(k i)
1 1 1
= + ++
(k)(k 1) (k 1)(k 2) (2)(1)
k1
X 1
=
(j + 1)(j)
j=1
k1
X 1 1
=
j=1
j j+1
1 1
=
1 k
1
= 1
k
After processing all endpoints, we will have a count of the number of intersections
inside the circle. The total running time is the time to sort plus the time to insert
and delete from the OS-tree, both of which are (n lg n). Therefore, the total
running time is (n lg n).
(b) [4 points] Do problem 14.2-1 on page 310 of CLRS.
Answer: We can find the SUCCESSOR and PREDECESSOR of any node x in
time (1) by storing pointers to the successor and predecessor of x inside the
node structure. Note that these values do not change during rotations, so we only
need to show that we can maintain them during insertions and deletions.
As soon as we insert a node x, we can call the regular TREE-SUCCESSOR(x)
and TREE-PREDECESSOR(x) functions (that run in O(lg n)) to set its fields.
Once the successor and predecessor are determined, we can set the appropriate
fields in x, and then update the SUCC field of the predecessor of x and the PRED
field of the successor of x to point to x. When we delete a node, we update its
successor and predecessor to point to each other rather than to x. To simplify the
handling of border cases (e.g. calling SUCCESSOR on the largest element in the
tree), it may be useful to keep a dummy node with key and a dummy node
with key +.
Finally, MINIMUM and MAXIMUM (which are global properties of the tree)
may be maintained directly. When we insert a node, we check if its key is
smaller than the current minimum or larger than the current maximum, and
update the MINIMUM and/or MAXIMUM pointer if necessary. When we delete
a node, we may need to set MINIMUM and/or MAXIMUM to its successor and/or
predecessor, respectively, if we are deleting the minimum or maximum element in
the tree.
Thus, these operations run in (1) and may be maintained without changing the
asymptotic running time of other tree operations.
(c) [5 points] Do problem 14.2-5 on page 311 of CLRS.
Answer: Using our answer from the previous part where we showed how to
maintain SUCCESSOR and PREDECESSOR pointers with O(1) running time,
we can easily perform RB-ENUMERATE.
RB-ENUMERATE(x, a, b)
1 start TREE-SEARCH(x, a)
2 if start < a
3 then start SUCC[start]
4 while start b
5 print start
6 start SUCC[start]
(e) [8 points] Do problem 14-1(b) on page 318 of CLRS. You may assume the result
of 14-1(a) without proof.
Answer: We know from part (a) that the point of maximum overlap is the
endpoint of one of the intervals. So, we wish to find the endpoint which is the
point of maximum overlap. To do this, we will follow the hint and keep a red-
black tree of the endpoints of the intervals. With each node x, we will associate
a field v[x] which equals +1 if x is a left endpoint and 1 if x is a left endpoint.
We will also augment each node with some additional information to allow us to
find the point of maximum overlap efficiently.
First, we will provide some intuition. If x1 , x2 , . . . , xn are the sorted sequence of
endpoints of the intervals, then if we sum from i = 1 to j of v[xi ], we find precisely
the number of intervals which overlap endpoint j (this assumes our intervals are
half-open of the form [a, b)). So, the point
Pj of maximum overlap will be the value
of j which has the maximum value of i=1 v[xi ].
We will augment our red-black tree with enough information to calculate the point
of maximum overlap for the subtrees rooted at each node. The point of maximum
overlap depends on the cumulative sum of the values of v[x]. So, in addition to
each v[x], we will keep a variable SUM[x] at each node which is the sum of the
v[i] values of all the nodes in xs subtree. Also, we will keep a variable MAX[x]
which is the maximum possible cumulative sum of v[i]s in the subtree rooted
at x. Finally, we will keep P OM[x] which is the endpoint xi which maximizes
the MAX expression above. Clearly, P OM[root] will be the point of maximum
overlap on the entire set of intervals.
We will demonstrate how to compute these fields using only information at
each node and its children. Therefore, by theorem 14.1, we can maintain this
information efficiently through insertions, deletions, and rotations. The sum of
the v[i] of the subtree rooted at node x will simply be
SUM[x] = SUM[lef t[x]] + v[x] + SUM[right[x]].
The maximum cumulative sum can either be in the left subtree, x itself, or in the
right subtree. If it is in the left subtree, it is simply MAX[lef t[x]]. If it is x, the
cumulative sum till x is SUM[lef t[x]] + v[x]. If it is in the right subtree, we have
to add the cumulative sum up till the right subtree to the max value of the right
subtree, SUM[lef t[x]] + v[x] + MAX[right[x]].
MAX[x] = max(MAX[lef t[x]], SUM[lef t[x]] + v[x], SUM[lef t[x]] + v[x] +
MAX[right[x]])
The point of maximum overlap is the value of the node which maximized the above
expression. (l[x] and r[x] refer to the left and right children of x respectively)
P MO[l[x]] if MAX[x] = MAX[l[x]]
P MO[x] = x if MAX[x] = SUM[l[x]] + x
P MO[r[x]] if MAX[x] = SUM[l[x]] + x + MAX[r[x]]
So, since each just depends on itself and its children, Interval-Insert and Interval-
Delete will run in time (lg n) since we either insert or delete 2 nodes from the
tree. At any time, P MO(root) will hold the point of maximum overlap and
Problem Set #4 Solutions 9
MAX(root) will return how many intervals overlap it. Thus, the operation Find-
PMO can be performed in (1) time.
(a) [7 points] You are planning a cross-country trip along a straight highway with
n + 1 gas stations. You start at gas station 0, and the distance to gas station i
is di miles (0 = d0 < d1 < . . . < dn ). Your cars gas tank holds G > 0 gallons,
and your car travels m > 0 miles on each gallon of gas. Assume that G, m, and
all di are integers. You start with an empty tank of gas (at gas station 0), and
your destination is gas station n. You may not run out of gas, although you may
arrive at a gas station with an empty tank. The price of gas at gas station i is pi
dollars per gallon (not necessarily an integer). You cannot sell gas.
Give an algorithm to calculate the cost of the cheapest possible trip. State
the running time and space usage of your algorithm in terms of the relevant
parameters.
Answer: We will solve this problem using dynamic programming by noting that
if we knew the optimal way to leave gas station i 1 with any amount of gas in
the tank, then we could easily calculate the optimal way to leave gas station i
with any amount of gas in the tank. We could do this by taking the minimum
over the cost of leaving gas station i 1 plus the cost of however much gas wed
need to put in the tank at gas station i. Notice that the amount of gas in the
tank times m must be an integer in the range from 0 to mG since the distances
we travel are given by gas/m and are all integers. From now on, we will call these
values of gas m units of gas. So, we can build a table T which is mG by n.
We initialize the first row of the table by noticing that the only way to leave gas
station 0 with k units of gas is to fill up k units of gas at gas station 0. This has
a cost of (p0 k)/m. So, our base case of the table T is
We can then define each additional row in terms of a minimum over the previous
row. For example, lets say we want to get to gas station i with no units of gas
in the tank. There is only one way to do that since we cant sell gas. We must
leave gas station i 1 with exactly enough gas to get to gas station i. This
distance is di di1 , and the amount of gas necessary is (di di1 ) m, so our
units of gas are exactly the distance, di di1 . So, the value of T [i, 0] is equal to
T [i 1, di di1 ]. Lets now consider the case where we want to get to gas station
i with 1 unit of gas in the tank. There are two ways we can acheive this. Either
we can leave gas station i 1 with one more unit of gas in the tank or we can
leave with exactly enough gas to get to i and buy one unit of gas at gas station
i. T [i, 1] = min(T [i 1, di di1 + 1], T [i 1, di di1 ] + (pi 1)/m). In general,
we take the minimum over many cells in the previous row. First, define i as the
distance between gas station i and i 1 so i = di di1 . We can calculate
Problem Set #4 Solutions 10
Some times we will look in the table for values that dont exists, for example when
k equals mG. Assume that T [i 1, k 0 > mG] will return infinity.
After filling the table, we will search in the row T [n, k] for the lowest value and
return that as the cheapest possible trip. Notice that this cheapest cell will always
be at T [n, 0] since any extra gas we have will have cost us something, and we may
as well not have bought it. Notice also that we can return the actual trip by
keeping pointers for each cell which point to the cell which caused the minimum.
For each cell in the table, we have to take a minimum over at most mG items.
Since there are n mG cells in the table, the running time for this algorithm
is (n(mG)2 ). The space requirement is the size of the table which is (nmG).
Since calculating each row in the table only depends on the previous row, we
can delete rows as we are finished processing them. So, we can reduce the space
requirement to (mG). However, if we wish to calculate the actual trip, we will
need to maintain the (nmG) table of pointers, which we arent able to delete,
since we dont know what our optimal trip is until after calculating the whole
table. So, in that case the space requirement remains at (nmG).
If we are clever in the way we update cells, we can actually reduce the running
time of the algorithm to (nmG) as well. Lets say we break the update to the
cells into two phases, before adding gas at station i and after adding gas at station
i. For the pre-fillup phase, we only have one way to arrive at a gas station with
a given amount of gas k, so we simply set each
T [i, k] = T [i 1, i + k]
where again we assume that T [i 1, k 0 > mG] = . For the post-fillup stage,
we can update each T [i, k] by looking at the cost of adding one unit of gas to
T [i, k 1], assuming T [i, k 1] already holds the minimum value to get there.
T [i, k] = T [i, k 1] + (pi 1)/m if that value is less than the current value in the
cell.
Since each cell now only looks back at a constant number of cells instead of O(mG)
cells, the running time is reduced to (nmG).
(b) [8 points] Do problem 15-7 on page 369 of CLRS.
Answer: Since there are so many variables in this problem, the first thing we
need to figure out is what our optimal subproblems are. We do this by considering
an optimal schedule over jobs a1 , . . . , aj that runs until time t. Assume that we
know which subset of jobs we perform to get the highest profit. What order do
we need to perform those jobs in? Notice that the job with the latest deadline
should be performed last. If we dont perform that job last, then we will waste
the time between the second last deadline and the last deadline. The same thing
Problem Set #4 Solutions 11
then applies for the second last deadline job being performed second last. We can
argue that the subset of jobs that we perform will be done in increasing order of
deadline.
This gives us our optimal subproblems. Namely, we order the jobs by increasing
deadline. When we consider job ai finishing at any time t (we assume these are
now in sorted order so it has the ith deadline) we can simply look back at the
optimal way to schedule the i 1 jobs and whether or not we add ai to the
schedule. We will also make the additional assumption that we leave no time
gaps between the jobs. It is easy to argue that if we have a schedule with time
gaps between jobs, we can also do the jobs in the same order with no time gaps
and receive the same profit and possibly more.
The actual algorithm is as follows. We keep a grid of the n jobs versus the time,
which can run up till dn (since the jobs are now sorted by deadline). Notice that
since the processing times are integers between 1 and n, the maximum time taken
to complete all the jobs is at most n2 . So, if we have any deadline which exceeds
n2 , we can simply replace it by n2 . Thus, our table is at most n n2 . Each cell in
the table i, j will represent the maximum profit possible for scheduling the first i
jobs in time exactly j, assuming that we have no gaps between our jobs.
We initialize the first row in the table by figuring out our possible profit based
on completing the first job at exactly time t. Since we assume no gaps in our
schedule, we must start the first job at time 0. Therefore, we will have a profit
of p1 in the cell at t1 if t1 d1 and a profit of zero otherwise. Well initialize all
other cells to zero. So, if we call our table T , we have
0 if t 6= t1
T [1, t] = p1 if t = t1 d1
0 if t = t1 > d1
Then, we can set the remaining cells in our table based simply on the previous
row. At each cell T [i, t], we have the choice of whether or not to perform job i. If
we decide not to perform job i, then our profit is simply T [i 1, t]. If we decide
to perform job i, then we know it takes ti units of time to complete it. So, we
must finish the previous jobs exactly at time t ti . We will get profit for the job
if t < di . We will pick the maximum profit from these two choices.
T [i 1, t]
T [i, t] = max T [i 1, t ti ] + pi if t di
T [i 1, t ti ] if t > di
At each cell, we will also keep a pointer to the cell which maximized the above
expression. After filling in the whole table, we can search through the T [n, t] row
for the highest profit. Then, we can follow the pointers back to find the schedule.
If in row i we take the pointer above us, we add i to the end of the schedule.
Otherwise, if we take the pointer that is diagonal, we say we complete i at that
time.
The running time of this algorithm is the number of cells in the table since each
cell takes a constant amount of time to evaluate. The search through the last row
Problem Set #4 Solutions 12
and following of the pointers both take time (n). Therefore, the running time
is (n3 ) if dn n2 and (ndn ) if dn < n2 .
The space requirement for this algorithm is the size of the table, which is the
same as the running time. Notice that since we want the actual schedule, we
cant compress the size requirement.
(a) [7 points] Consider a chess knight on an n n chessboard. The knight may move
from any square (i, j) to (i0 , j 0 ) if and only if (|i0 i| , |j 0 j|) {(1, 2), (2, 1)} and
that square is on the board; in other words, the knight may move two squares in
one direction and one square in a perpendicular direction (see diagram).
Give an algorithm to calculate the number of ways in which the knight can travel
from square (is , js ) to (it , jt ) in exactly k 0 moves. State the running time and
space usage of your algorithm in terms of the relevant parameters.
Answer: We can calculate the solution to this problem by noticing that if we
knew the number of ways to get to every square on the board in k 1 moves from
the starting location, we could easily calculate the number of ways to get to a
square in k moves by simply summing over the atmost 8 squares that the knight
could move from. Each way to get to the predecessor in k 1 moves contributes
one way to get to the square in k moves.
Our DP matrix will be n n k. We initialize for all k = 0 the number of ways
to get to that square in 0 moves. Namely,
W ays(a, b, 0) = 1 if a = is AND b = js and zero otherwise. We can build
up our DP matrix for each 0 < i k from these values. For each value of
i = 1 . . . k, and P for each square (a, b) on the n n board, we set the value of
W ays(a, b, i) = (u,v)neighbors(a,b) W ays(u, v, i 1). The neighbors of a cell are
simply those that follow the condition given above for the legal knights moves.
At the end, we look at W ays(it, jt , k) to find the number of ways to get to (it , jt )
from (is , js ) in exactly k moves.
Notice here a trend common to DP problems. In order to solve how many ways
there are to get from a certain cell to another cell in k moves, we actually solve
Problem Set #4 Solutions 13
more than asked for. We figure out how many ways there are to get from the
start cell to every cell in k moves. While it seems like we do more computation,
this actually makes solving the next layer more efficient.
The running time of this algorithm will be proportional to the number of cells in
the matrix, since each cell takes a constant amount of time to calculate. Thus,
the running time is (n2 k). The amount of space is the same at (n2 k). Notice,
however, that to calculate the matrix for a given value of i we only use the values
at i 1. So, at any time, we dont need to store the entire n2 k matrix, we only
need two levels of it. If we delete levels (in k) as we finish using them, we only
need space (n2 ).
(b) [8 points] The cat and mouse game is played as follows: there is an n n board
with a cat in (initial) location (ic , jc ), a mouse in (initial) location (im , jm ), and a
piece of swiss cheese in location (is , js ).
Each cell (i, j) on the board has up to four neighbors: north (i, j + 1), south
(i, j 1), east (i + 1, j), and west (i 1, j). All four cells adjacent in this manner
are considered neighbors, except for the edges of the board and where there are
walls blocking some of the directions. Therefore, assume that for any given
cell (i, j) you have a list of zero to four neighboring cells NBR(i, j) specified as
you like (linked list, for example). You may assume the neighbor relationship is
symmetric, i.e. if (i0 , j 0 ) NBR(i, j), then (i, j) NBR(i0 , j 0 ) and vice versa.
The game alternates between the moves of the mouse, who moves first, and the
cat. The cheese does not move. If the mouse reaches the cheese before the cat
catches it (i.e. without ever entering the same cell as the cat), then the mouse
wins (the cheese endows the mouse with super powers). If the cat catches the
mouse before the mouse can reach the cheese, the cat wins. If both conditions are
satisfied simultaneously, i.e. the mouse enters a cell with the cat and the cheese
in it, the game is considered a draw. You may assume that if none of this occurs
within 2n2 moves, the game ends in a draw. Also, assume the cat and the mouse
must move every round.
Assume that the cat and mouse are infinitely intelligent, can see the entire board
and location of each other, and always make optimal decisions.
Give a dynamic programming algorithm to predict who will win the game. State
its running time (a good upper bound is sufficient) and space usage in terms of
n.
Answer: We can solve this problem by noticing that if the mouse knew the
outcome for each of his at most four possible moves, he would know which way
to go. If there was a move for which the mouse wins, hell choose that move and
win. If not, but theres a move for which the game is a draw, hell choose that
and draw. Otherwise, the cat wins for every move of the mouse, so we know the
cat wins. Similar reasoning holds for the cats strategy.
From this, we can see that our subproblems are who wins for each possible board
configuration, whose move it is, and which move were on. Since after 2n2 moves
the game ends in a draw, we know the base case of whether the cat wins, mouse
Problem Set #4 Solutions 14
wins, or a draw for the 2n2 move. We can define a set of boards for 2n2 + 1 which
have the cat winning if the cat and the mouse are on the same square not on the
cheese, the mouse winning if the mouse is on the cheese and the cat is elsewhere,
and a draw otherwise.
We need to keep track of all possible boards for each move k. A board position
consists of a cat position (n2 ) and a mouse position (n2 ), as well as whose turn
it is. Since the mouse goes first, we know that for odd k it is the mouses turn
and for even k it is the cats turn. We keep 2n2 possible sets of boards, each
corresponding to which move number we are on. We initialize the k = 2n2 board
as above, and work backwards.
For each 0 k < 2n2 and each of the n4 possible board configurations, we wish
to calculate who wins or if it is a draw. Our base cases are if the cat and the
mouse are on the same square not on the cheese, then the cat wins. If the cat
and the mouse are both on the same square as the cheese, then its a draw. And
if the mouse is on the cheese and the cat is elsewhere, then the mouse wins.
Otherwise, we look at the at most four possible board configurations in the k + 1
level corresponding to the possible moves. If k is odd (mouses turn) and there is
a possible board where the mouse wins, then we label this board as mouse, else
if there is a possible board where there is a draw, we label this board draw, else
we label this board cat. The symmetric case holds for k even (cats turn).
After filling out our (2n2 ) (n4 ) table, we look at the board in the k = 0 level
corresponding to the starting locations of the cat and mouse and give the label of
that board (cat, mouse, draw) as who wins the game.
Each cell looks at a constant number of other cells, so the running time of this
algorithm is (n6 ). The memory requirement is also (n6 ). Notice however that
we only need the k+1 boards to calculate the k boards. So, we need only store two
sets of board configurations at a time, leading to a space requirement of (n4 ).
We can actually achieve a running time of (n4 ) if we do our updates in an
intelligent manner. Rather than doing update move by move, we will initailiaze
our board (n2 n2 2 states) with the known terminal states (cat win, mouse
win, draw), and the rest will start out unknown. As some state is updated, it may
trigger up to four updates for its neighboring states. If we keep the states that
need to be updated in a data structure such as a queue, we can process them one
by one, doing a constant amount of work per update. We begin with neighbors
of the terminal states in the queue, and continue adding neighbors to the queue
for each update. The total number of updates done is at most the number of
states on the board, which yields a running time of O(n4 ). Any state that fails to
be updated when the queue is empty is then labeled as a draw state due to the
assumption that any game that lasts at least 2n2 moves will end in a draw.
Pt
is i=1 wi . Describe a (n lg n) algorithm for finding the heaviest increasing
subsequence.
Answer: This problem is similar to the longest increasing subsequence problem,
where in that problem all elements had weight equal to 1. In that problem, we
were able to store the lowest value of the last element of a subsequence of length i
in an array L[i]. We showed in class that the L[i] array remained sorted. For each
element j in our sequence, we were able to perform a binary search in the L array
for the largest value smaller than j and update our array accordingly. This led to
a total running time of (n lg n) since each binary search took time (lg n).
In this case, we cant keep an array since the weights may not be integral values.
However, we can keep a red-black tree which will allow us to find elements in
time (lg n). Also notice that adding a single element may cause many previous
subsequences to never be used again. For example, if we have the sequence of
(key, weight) of (3, 3), (2, 2), (1, 1), (0, 5), we would find for the first three elements
a subsequence of weight 1 ending in key 1, one of weight 2 ending in key 2 and
one of weight 3 ending in key 3. Notice that all three of these subsequences may
still be part of our heaviest subsequence. But, when we add the fourth element
which gives a subsequence with weight 5 ending in key 0, none of the previous 3
subsequences can be part of our heaviest subsequence, since any time we could
append elements to those, we could append them to the subsequence ending in 0
and achieve a higher weight. Thus, in order to maintain only the useless sequences
we may now have to delete more than one element at a time.
Notice that the total number of elements deleted over the course of the algorithm is
n, which yields running time of O(n lg n). However, each particular update may
now take (n), so without this realization (which is a mild form of amortized
analysis) we do not have the desired bound with this approach.
The following is an alternative solution that uses augmented red-black trees. If
youre comfortable with counting the deletion time separate from insertion time
for the solution above, feel free to skip it. If you fear amortization like the plague,
read on.
So, for each key, we will wish to compute the heaviest subsequence
ending in that key. We will keep an augmented red-black tree of these
(key, weight of subsequence) pairs keyed on the key. We will refer to the weight
of the subsequence as ws , not to be confused with the weight of the element.
We will also keep pointers for each element of which element is its predecessor
in the heaviest subsequence ending in that element. At the end, we will search
through the tree for the highest weight and follow the pointers to find our heaviest
subsequence.
For each element i, we will need to find in the tree the node x with the highest
weight such that key[x] < key[i]. x is the node that we should append i onto to
get the heaviest increasing subsequence ending in i. To do this, we will augment
each node with W [x], the maximum weight of subsequences ending in elements
less than or equal to x in the subtree rooted at x. We can efficiently calculate W [x]
as max(W [lef t[x]], ws [x]). Since this property only depends on the values stored
Problem Set #4 Solutions 16
at node x and its children, we can maintain it during insertion, deletion, and
rotation. We will also keep at each node a pointer P [x] which points to the node
which attains that maximum weight. P [x] = P [lef t[x]] if W [lef t[x]] > weight[x]
and P [x] = x if weight[x] W [lef t[x]].
When we look at a new element i , we will insert it into the red-black tree. We
initialize totalweight = and pointer = NIL. Well search in the tree for
where i should go. Everytime we follow a left child, we do nothing. Everytime we
follow a right child of some parent x, we set totalweight = max(totalweight, W [x])
and pointer = P [x]if W [x] > totalweight. So, we essentially keep track of the
maximum weight and the pointer to the node that had it for all keys that are less
than i. When we find where to insert x, we set the weight of the subsequence
ending in x to be ws [x] = totalweight + weight[x]. We also set W [x] = ws [x] and
P [x] = x. In addition, in our original array, we add a pointer from x to pointer,
telling us how to backtrack to find the heaviest increasing subsequence.
We do this for all elements in the sequence (starting with the dummy element
with weight 0). Each insert into the tree takes time lg n and we have to
perform n of them. At the end, we search through the tree for the highest weight
((n)) and follow the pointers back from that element to find the actual heaviest
increasing sequence (O(n)). So, the total running time is (n lg n).
(b) [8 points] Let X = x1 , . . . , xm and Y = y1 , . . . , yn be two sequences. Assume
that the number of pairs (xi , yj ) that match (i.e. xi = yj ) is small, i.e. (k) for
some k(m, n). Assume the set of matching pairs is given to you as part of the
input. Give an O(k lg k) algorithm for finding the longest common subsequence
of X and Y under these conditions.
Hint: reduce this to another sparse DP problem we have seen.
Answer: We will try to reduce our problem to the longest increasing subsequence
problem. Notice that if we try to read through the entire input arrays, we will
take time (n + m) which could be bigger than k, so we are restricted to simply
looking through the pairs of elements given. The longest common subsequence of
X and Y must have increasing indexes in both X and Y . Thus, if we sort the
pairs according to their index in X and feed the corresponding Y indexes into the
longest increasing subsequence algorithm, we will obtain a common subsequence
that is nondecreasing in the X index and increasing in the Y index. To avoid
the problem of potentially having several pairs (xi , yj ) and (xi , yj 0 ) chosen by the
LIS algorithm, we place the pairs with equal values of xi in decreasing order of
yj , so that two pairs with the same X index can never be returned as part of
a subsequence with increasing Y index. Sorting can be done using mergesort in
(k lg k) time, and the sparse version of LIS runs in (k lg k) time, so the overall
running time is (k lg k).
CS161: Design and Analysis of Algorithms Summer 2004
(a) [2 points] Describe a greedy strategy for making change using as few coins
as possible. This strategy should work for the US coin system which uses
denominations {1, 5, 10, 25}. You do not need to prove the correctness of this
strategy for this set of denominations.
Answer: The greedy strategy consists of always taking the largest denomination
that we can at the time. We repeat this until we have the correct amount of
change.
MAKE-CHANGE(M)
1 for i = k downto 1
2 count[i] bM/di c
3 M M count[i] di
Since for each denomination we calculate the number of coins we can take until
we would make more than the amount of change asked for, this operation takes
time O(k).
We can also write this in a slightly less efficient manner that will be useful when
analyzing the correctness of the greedy strategy. In this case, we simply take one
coin at a time of the largest denomination possible.
MAKE-CHANGE-SLOW(M)
1 for i = k downto 1
2 while M di
3 count[i] count[i] + 1
4 M M di
This runs in time O(C + k) where C is the number of coins in the greedy solution.
Problem Set #5 Solutions 2
(b) [5 points] Suppose that di = ci1 for some integer c > 1, i.e. the denominations
are 1, c, c2 , . . . , ck1 . Prove that the greedy strategy from part (a) is optimal.
Answer: Well represent a change solution by a series Pof counts, one for each coin
denomination. The counts must obey the property ki=1 count[i] di = M. The
P
number of coins in this solution is ki=1 count[i]. Note that the counts for each
coin denomination di < dk must be strictly less than c in an optimal solution.
Otherwise, if we have count[i] c, we can replace c of the di = ci1 coins which
have value c ci1 = ci with one di+1 = ci coin and get a smaller count.
We will prove by contradiction that the greedy solution is optimal. Let the optimal
count values be represented by optimal[i]. Let j be the first index (highest)
for which count[j] 6= optimal[j]. We know that count[j] > optimal[j] because
the greedy solution always takes the maximum number of any coin and this is
the first value on which they disagree. If the optimal solution Pj1chose more of
coin dj , then it would have made too much change. Let B = i=1 count[i] di ,
the amount of change that the greedy solution makes with the coins 1 . . . j 1.
Since the optimal solution used fewer coins of denomination dj , we know that
the amount of change that the optimal solution makes with coins 1 . . . j 1 is
D = B + (count[j] optimal[j]) dj = B + (count[j] optimal[j]) cj1 . Since
this value is greater than cj1 , we will show that the optimal solution must take
at least c coins for some coin d1 . . . dj1.
Assume that the optimal solution has optimal[i] < c for i = 1 . . . j 1. Then, the
amount of change that the optimal solution makes with the first j 1 coins is
X
j1
D = optimal[i] di
i=1
X
j1
(c 1) ci1
i=1
X
j1
= (c 1) ci1
i=1
X
j2
= (c 1) ci
i=0
j1
c 1
= (c 1)
c1
= cj1 1
However, this contradicts the fact that D was greater than cj1 . Thus, the optimal
solution must take at least c coins for some denomination less than dj . But, as
we showed above, this solution cant be optimal. This is a contradiction and
therefore the greedy solution must take the same number of coins for all coins as
the optimal solution, and thus must itself be optimal.
(c) [2 points] Give a set of coin denominations for which the greedy algorithm will
Problem Set #5 Solutions 3
not always yield the optimal solution. Your set should include d1 = 1 so that
every positive integer amount can be formed.
Answer: A simple example is the set of US denominations without the nickel. If
you simply have the values {1, 10, 25}, the greedy solution will fail. For example,
if you are trying to make change for 30 cents, the greedy solution will first take a
quarter, followed by 5 pennies, a total of six coins. However, the optimal solution
actually consists of three dimes.
(d) [5 points] Suppose you wish to make change for the amount M using an arbitrary
set of denominations (although we still make the assumption that di are distinct
integers and d1 = 1). Give an O(Mk) time algorithm to calculate the minimum
number of coins needed. State the (asymptotic) space usage of your algorithm in
terms of the relevant parameters.
Answer: We can solve this problem using dynamic programming. Notice that
for any value M, we can calculate the optimal solution by looking back at the
number of coins needed to make M di coins for all i and choosing the smallest
number. We fill an array T which ranges from 1 . . . M. At each point in the array,
we store the optimal number of coins needed to make change. We initialize the
cell T [0] with the value 0. Then, for every other cell up to T [M] we calculate
T [i] = 1 + min T [i dj ]
1jk
where we assume that for x < 0, T [x] = . We fill in the table up till T [M] and
then simply look in that cell for the optimal number of coins. Since we have to fill
in M values in the table and each value looks back at k different cells and takes
the minimum, the running time of this algorithm is O(Mk). At any time, we can
look back until the i dk cell, so the space usage is O(min(dk , M)).
(e) [5 points] Suppose Eugene owes Nina an amount M. Give an O(Mkdk ) time
algorithm to determine the minimum number of coins that need to exchange hands
in order to settle the debt (Nina can give change). Assume that neither Eugene
nor Nina will run out of any denomination. State the (asymptotic) space usage
of your algorithm in terms of the relevant parameters.
Answer: We can define an upper bound on the amount of money that Eugene
gives Nina that can be part of an optimal solution. Lets say that Eugene gives
Nina an amount more than Mdk and Nina gives change. Eugene gives at least M
coins, and Nina still gives back coins. But, we know that Eugene could simply
have given Nina M coins of denomination d1 = 1 to make a total value of M
without Nina having to give any change. So, we know that Eugene giving an
amount more than Mdk cant be an optimal solution.
We can therefore use our solution from part (d) to find the optimal ways of giving
change for all values 1 . . . Mdk . Then, for each value i that Eugene can give in
the range M . . . Mdk , Nina will need to give i M change. We simply sum the
number of coins that Eugene gives plus the number of coins that Nina gives:
T [i] + T [i M]. The minimum over all i is the optimal way to settle the debt.
Problem Set #5 Solutions 4
To find the optimal ways to make change for all values up till Mdk takes time
O(Mkdk ) using the algorithm in part (d). Notice that in the process of computing
the optimum for Mdk the algorithm finds the optimal ways to make change for
all the values 1 . . . Mdk so we only need to run it once. Summing and taking the
minimum will take time O(Mdk ), so the running time is bounded by the dynamic
programming part. The algorithm will need to store the previous dk solutions for
the dynamic programming and the previous M solutions for the summation of
T [i] + T [i M]. So, the space usage of the algorithm is O(dk + M).
(f) [6 points] Consider again the greedy algorithm stated in part (a) of the problem.
We want to decide, in a given currency system d1 < . . . < dk , whether the greedy
algorithm is optimal. Give a dynamic programming algorithm to determine the
smallest amount M for which the greedy algorithm fails to produce the optimal
solution. If the greedy approach is always optimal your algorithm should detect
and report this. State the (asymptotic) running time and space usage of your
algorithm in terms of the relevant parameters. You may assume that k 2 so
that the greedy algorithm is not trivial.
Hint: Can you find an upper bound B such that if greedy is optimal for all
M B, then greedy must be optimal for all M?
Answer: We claim that if a counterexample to greedys optimality exists, it must
exist for some M < dk +dk1 . The main idea is that for such M, the greedy choice
is to choose dk , but we will show that doing so either yields an optimal solution
or a smaller counterexample to greedys optimality.
Thus, suppose (for a contradiction) that M dk + dk1 is the smallest amount
such that greedy is not optimal for this amount. This means that OP T [M] >
OP T [M dk ] + 1. However, there must be some denomination dj 6= dk such
that OP T [M] = OP T [M dj ] + 1 (recall how we computed OP T [i] in part
(d)). By assumption, greedy must be optimal for this amount, otherwise we
have a smaller counterexample. However, since dj dk1 (since j 6= k), this
means the optimal greedy choice for M dj dk is in fact dk ; therefore,
OP T [M dj ] = OP T [M dj dk ] + 1. Thus,
OP T [M] = OP T [M dj dk ] + 2
OP T [M] > OP T [M dk dj ] + 2
The space requirement O(dk ) and the running time of O(kdk ) are obtained by
letting M = O(dk ) in the results from part (d).
where (x) = |size[lef t[x]] size[right[x]]|, then the potential for any BST is
non-negative. This is easy to see as every term in the summation is the absolute
value of the difference in the subtree sizes and therefore is non-negative. Also, we
assume that the constant c is positive, thus giving a positive potential (T ).
For a 1/2-balanced BST, we know that size[lef t[x]] (1/2)size[x] and
size[right[x]] (1/2)size[x] for all nodes x in the tree. We will prove by
contradiction that (x) < 2 for all x in the tree. Assume that (x) 2. Then,
|size[lef t[x]] size[right[x]]| 2. Without loss of generality, assume that the
left subtree is larger, so we can drop the absolute value signs. We also know that
the sum of the sizes of the left subtree and the right subtree must be size[x] 1.
Substituting this in for size[right[x]], we find
size[lef t[x]] size[right[x]] 2
size[lef t[x]] (size[x] size[lef t[x]] 1) 2
2 size[lef t[x]] 1 + size[x]
size[lef t[x]] 1/2 + (1/2)size[x]
Problem Set #5 Solutions 6
But, we know from the -balanced condition that size[lef t[x]] (1/2)size[x],
which is a contradiction. Therefore, for all nodes x, (x) < 2. The summation
in the potential equation is only over nodes with (x) 2. Since no such nodes
exist, the summation is 0, and thus (T ) = 0 for a 1/2-balanced tree.
(d) [4 points] Do problem 17-3(d) on page 427 of CLRS.
Answer: We need to pick the constant c such that at any time that we need to
rebuild a subtree of size m, we have that the potential (T ) is at least m. This
is because the amortized cost of rebuilding the subtree is equal to the actual cost
plus the difference in potential. If we want a constant amortized cost, we need
crebuild = crebuild + i i1
O(1) = m + i i1
i1 m
since we know that the end potential i is always greater than zero.
We need to figure out the minimum possible potential in the tree that would
cause us to rebuild a subtree of size m rooted at x. x must not be -balanced, or
we wouldnt need to rebuild the subtree. Say the left subtree is larger. Then, to
violate the -balanced criteria, we must have size[lef t[x]] > m. Since the size of
the subtrees must equal m1, we know that size[right[x]] = m1size[lef t[x]] <
m 1 m = (1 )m 1.
We have
(x) = size[lef t[x]] size[right[x]]
> m ((1 )m 1)
= (2 1)m + 1
We can then bound the total tree potential (T ) > c((2 1)m + 1). We need
the potential larger than m to pay the cost of rebuilding the subtree, so
m c((2 1)m + 1)
m
c
(2 1)m + 1
1
=
2 1 + 1/m
1
2
Therefore, if we pick c larger than this constant based on , we can rebuild the
subtree of size m in amortized cost O(1).
(e) [4 points] Do problem 17-3(e) on page 427 of CLRS.
Answer: The amortized cost of the insert or delete operation in an n-node -
balanced tree is the actual cost plus the difference in potential between the two
states. From part (b), we showed that search took time O(lg n) in an -balanced
tree, so the actual time to insert or delete will be O(lg n). When we insert or
Problem Set #5 Solutions 7
delete a node x, we can only change the (i) for nodes i that are on the path
from the node x to the root. All other (i) will remain the same since we dont
change their subtree sizes. At worst, we will increase each of the (i) for i in the
path by 1 since we may add the node x to the larger subtree in every case. Again,
as we showed in part (b), therePare O(lg n) such nodes. The potential (T ) can
therefore increase by at most c ipath 1 = O(c lg n) = O(lg n). So, the amortized
cost for insertion and deletion is O(lg n) + O(lg n) = O(lg n).
an articulation point. If r has one child, then all other nodes in G are reachable
through the child. Therefore, removing r from G will not disconnect G and r is
not an articulation point.
For the reverse direction, we need to show that if the root has at least 2 children
in G then the root is an articulation point. We will prove this by contradiction.
Suppose the root has the two children C1 and C2 and that C1 was explored first in
the DFS. If the root is not an articulation point, then there exists a path between
a node in C1 and one in C2 that does not include the root r. But, while exploring
C1 , we should have explored this path and thus the nodes of C2 should be children
of some node of C1 . But, since they are in a separate subtree of the root, we know
that no path can exist between them and thus r is an articulation point.
(b) [4 points] Do problem 22-2(b) on page 558 of CLRS.
Answer: First we will prove the forward direction. Assume that v is an
articulation point of G. Let Cr be the connected component of G v containing
the root of the tree G . Let s be a neighbor of v that is not in Cr (this neighbor
must exist since removing v must create at least two connected components) and
Cs be the connected component containing s. In G , all the proper ancestors of
v are in Cr , and all the descendants of s are in Cs . Thus, there can be no edges
between descendants of s and proper ancestors of v.
To prove the backward direction, if such a vertex s exists and there is no back
edge from s or any of its descendants to a proper ancestor of v, then, we know
that the only path from the root of the tree to s goes through v. Therefore, if we
remove v from the graph, we have no path from the root of the tree G to s, and
we have disconnected the graph. Thus, v is an articulation point.
(c) [4 points] Do problem 22-2(c) on page 559 of CLRS.
Answer: We can compute low[v] for all vertices v by starting at the leaves of the
tree G . We compute low[v] as follows:
For leaves v, there are no descendants u of v, so this returns either d[v] or d[w] is
there is a back edge (v, w). For vertices v in the tree, if low[v] = d[w], then either
there is a back edge (v, w), or there is a back edge (u, w) for some descendant u.
The last term in the min expression handles the case where (v, w) is a back edge.
If u is a descendant of v in G , we know that d[u] > d[v] since u is visited after v
in the depth first search. Therefore, if d[w] < d[v], we also have d[w] < d[u], so
we will have set low[u] = d[w]. The middle term in the min expression therefore
handles the case where (u, w) is a back edge for some descendant u. Since we start
at the leaves of the tree and work our way up, we will have computed everything
we need when computing low[v].
For each node v, we look at d[v] and something related to all the edges leading
from v, either tree edges leading to the children or back edges. So, the total
running time is linear in the number of edges in G, O(E).
Problem Set #5 Solutions 10
in the subtree of v can only have back edges to other nodes in the subtree of
v. Therefore, we will have low[v] = d[v] since v is the first node visited in the
subtree rooted at v. Thus, we can look over all the edges of G and see whether
low[v] = d[v]. If so, then we will output that (parent[v]G , v) is a bridge, i.e. that
v and its parent in G form a bridge. Computing low[v] for all vertices v takes
time O(E) as we showed in part (c). Looping over all the edges takes time O(V )
since there are |V | 1 edges in G . Thus the total time to calculate the bridges
in G is O(E).
therefore depends on how many edges you have relative to the number of vertices.
If E = o(V 1.376 ) then you should convert to the adjacency list representation and
otherwise you should convert to the matrix. Notice that the number of edges
will keep changing for each Gi so you may need to convert back and forth when
calculating Gk .
(c) [4 points] Do problem 22.3-11 on page 549 of CLRS. How does your algorithm
compare (in the asymptotic running time sense) with the algorithm given in class
and in section 21.1 of CLRS for determining the connected components of G using
disjoint-set data structures?
Answer: We will first modify DFS to label the connected components.
DFS(G)
1 for each vertex u V [G]
2 do color[u] WHITE
3 [u] NIL
4 time 0
5 k0
6 for each vertex u V [G]
7 do if color[u] = WHITE
8 k k+1
9 DFS-VISIT(u, k)
Lines 5, 8, and 9 are the ones which were added or changed. In DFS-VISIT, we
will always call DFS-VISIT with the same value of k. In addition, after setting
the color of u to BLACK, we will set the connected component, cc[u] to k.
Since the graph G is undirected, two nodes u and v will only get the same
connected component label if there is a path between them in the graph G.
The running time of this algorithm is the same as for DFS which is O(V + E).
The algorithm given in section 21.1 of CLRS runs in time O((V + E)(V )), which
is asymptotically slower. However, (V ) is very small ( 4) for any reasonable
size V , so the running times are comparable.
(d) [5 points] Do problem 22.4-2 on page 552 of CLRS.
Answer: We first run topological sort on the graph G. This takes time O(V +E).
We know that any path that runs between s and t must use only the vertices
located between s and t in the topological sort. If there was a vertex a < s in the
topological sort, then there cant be a path from s a in G. Likewise there can
be no vertex b > t on a path from s to t. So, we can ignore all vertices < s or
> t in the topological sort. Then, we can use dynamic programming to calculate
the number of paths from s to t. We will label each node from s to t with the
number of paths from s to that node. We start by labelling the node s with a 1
since there is one path from s to s and by labelling all other nodes with 0. Then,
for each node i starting from s in the sort, we calculate the number of paths from
s as
Problem Set #5 Solutions 13
X
paths[i] = paths[j]
(j,i)E
u v and adds the edge (u, v). Our algorithm produces the same spanning tree
that Kruskals algorithm produces, and it is therefore a minimum spanning tree.
(b) [6 points] Do problem 23.2-7 on page 574 of CLRS.
Answer: Lets assume that we add the new vertex and incident edges and
initialize all the edge weights to . Then, we can take any of the edges and
add it to the original spanning tree to give a minimum spanning tree. Using the
answer from part (a), we know that we can reduce the weight of this edge and
the other edges one at a time, add the edge to the MST, and remove the edge
with the highest weight in the newly created cycle. This will run in time O(V 2 )
since there may be at most V edges from the new vertex. However, we can do
better by noticing that the only possible edges in the new MST are the ones in
the old MST or the new edges we just added. There are a total of |V | 1 edges
in the old MST and a total of at most |V | edges added. So, if we simply run
either Kruskals or Prims algorithm on the graph with all the vertices but only
these |E| = 2|V | 1 = O(V ) possible edges, we will create the new MST in time
O(V lg V ), which is better than the O(V 2 ) time.
(c) [6 points] Do problem 23.2-8 on page 574 of CLRS.
Answer: This algorithm does not compute the minimum spanning tree correctly.
Suppose we have a graph with three nodes, A, B, C. Suppose also that the graph
has three edges with the following weights: w(A, B) = 1, w(B, C) = 2, w(C, A) =
3. Lets say we partition the graph into the two sets V1 = {A, C} and V2 = {B}.
This partition satisfies the condition that |V1 | and |V2 | differ by at most 1. The
edges sets will be E1 = {(A, C)i} and E2 = . So, when we recursively solve the
subproblems, we will add the edge (A, C). Then, when we select the minimum
edge that crosses the partition, we will select the edge (A, B) with weight 1. The
total weight of our MST is 1 + 3 = 4. However, the actual minimum spanning
tree has edges (A, B) and (B, C) and weight 1 + 2 = 3. Therefore, this algorithm
fails to produce the correct minimum spanning tree.
CS161: Design and Analysis of Algorithms Summer 2004
Throughout this entire problem set, your proofs should be as formal as possible. However,
when asked to state an algorithm, you do not need to give pseudocode or prove correctness
at the level of loop invariants. Your running times should always be given in terms of |V |
and/or |E|. If representation of the graph is not specified, you may assume whichever is
convenient.
this O(E) scan of the lists is averaged over the O(V ) calls to EXTRACT-MIN,
for an amoritized cost of O(E/V ) each.
Our total running time therefore includes V calls to INSERT (O(V )), E calls
to DECREASE-KEY (O(E)) and V calls to EXTRACT-MIN (O(V + E)) for a
total running time of O(V + E). Since we know |E| > |V | 1, the O(E) term
dominates for a running time of O(E).
(b) [1 point] Do problem 24-4(b) on page 616 of CLRS.
Answer: Using part (a), we can show how to compute 1 (s, v) for all v V in
O(E) time. We know that 1 (s, v) is computed using the edge weight function
w1 , which only uses the first significant bit of the actual edge weights. Therefore,
the weights w1 are always either 0 or 1. The maximum number of edges on a
shortest path is |V | 1 since it can have no cycles. Since the maximum edge
weight w1 is 1, the maximum weight of a shortest path is |V | 1. Therefore, we
have the condition that for all vertices v V , we have 1 (s, v) |V | 1 |E| by
assumption. From part (a), we know that we can compute 1 (s, v) for all v V
in O(E) time.
(c) [3 points] Do problem 24-4(c) on page 616 of CLRS.
Answer: We know that wi is defined as bw(u, v)/2kic. We will show that either
wi (u, v) = 2wi1 (u, v) or wi (u, v) = 2wi1 (u, v) + 1. Weight wi is the i most
significant bits of w. We can get wi from wi1 by shifting wi1 to the left by
one space and adding the ith significant bit. Shifting to the left is equivalent to
multiplying by 2 and the ith significant bit can be either 0 or 1. So, we have that
wi (u, v) = 2wi1 (u, v) if the ith significant bit is a zero or wi (u, v) = 2wi1 (u, v)+1
if the ith significant bit is a one.
We now want to prove that 2i1 (s, v) i (s, v) 2i1 (s, v) + |V | 1. The
shortest path i (s, v) has weight
X
i (s, v) = min wi (e)
eP ath(s,v)
X
min 2wi1 (e)
eP ath(s,v)
X
= 2 min wi1 (e)
eP ath(s,v)
= 2i1 (s, v)
with the inequality holding because the wi (e) is greater than or equal to 2wi1 (e)
since it equals either that or that plus one. The last equality holds because the
minumum weight of any path from s to v using the weight function wi1 is equal
to the shortest path distance from s to v using wi1 .
Similarly,
Problem Set #6 3
X
i (s, v) = min wi (e)
eP ath(s,v)
X
min (2wi1 (e) + 1)
eP ath(s,v)
X X
= min 2 wi1 (e) + 1
eP ath(s,v) eP ath(s,v)
X
min 2 wi1 (e) + |V | 1
eP ath(s,v)
= 2i1 (s, v) + |V | 1
The first inequality holds because wi (e) is less than or equal to 2wi1 (e) + 1 by
similar reasoning as above. We then set the minimum weight of any paths from
s to v using the weight function wi1 equal to the shortest path as above. The
second inequality holds because the minimum path length is bounded by |V | 1
because it can have no cycles.
(d) [3 points] Do problem 24-4(d) on page 616 of CLRS.
Answer: We define for i = 2, 3, . . . k and all (u, v) E,
Thus, wi is never negative. It is an integer since all the edge weights are always
integers and thus all the shortest path distances are always integers.
(e) [4 points] Do problem 24-4(e) on page 617 of CLRS.
Answer: We define i (s, v) as the shortest path weight from s to v using wi . We
want to prove for i = 2, 3, . . . k and all v V that
i (s, v) = min
X
wi(e)
eP ath(s,v)
We expand i (s, v) in terms of the weights of edges along the path. We see that
if we also expand the wi terms, many things cancel. The term i1 (s, s) = 0
since the shortest path from a node to itself is zero regardless of the edge weight
function. Also, we know that the minimum path length from s to v using the wi
function is i (s, v).
From this, we can easily show that i |E| using the results from part (c).
(a) [8 points] Do problem 22.2-7 on page 539 of CLRS. Assume the edges of T
are undirected and weighted, and give your running time in terms of |V | (since
|E| = |V | 1).
Answer: First notice that the shortest path distance between two vertices is
given by the length of the path between the two vertices, since there is only one
path between any pair of vertices in a tree. So, we are basically asking for the
longest simple path between any two leaves in a tree.
We will keep two items of information at each edge (u, v) T . We will keep
d[u v], which represents the longest simple path distance in the tree from u to
v to any leaf. Also, we keep d[v u], defined symmetrically.
First we arbitrarily root the tree at some vertex r by pointing all the edges outward
from r. We will compute the downward distances bottom-up, and then the upward
distance top-down.
Problem Set #6 6
COMPUTE-DOWN(x)
1 if IsLeaf(x)
2 return
3 for y children(x)
4 COMPUTE-DOWN(y)
5 d[x y] w(x, y) + maxzchildren(y) d[y z]
After calling COMPUTE-DOWN on the root of the tree r, every edge will have
its downward field set. Notice that because of the recursion, the information is
actually first computed at the leaves and then propagated upward.
The top-down pass is somewhat tricker, because now we must integrate
information from different branches as we descend down the tree. There are
two differences: first, the information is now computed at the top first, and at
the bottom at the end; second, updating d[y x] where x is the parent of y now
depends not only on d[x p(x)] but also on d[x z], where z are other children
of x. However, these values were computed on the downward pass; therefore, as
long as we compute d[x p(x)] before d[y x], we have all the information we
need.
COMPUTE-UP(x)
1 p parent(x)
2 for y children(x)
3 d[y x] w(x, y) + maxzchildren(x){p},z6=y d[x z]
4 COMPUTE-UP(y)
Thus, to calculate the diameter, we root the tree arbitrarily at a vertex r, call
COMPUTE-DOWN(r) and then COMPUTE-UP(r). The largest value d[u v]
computed during these procedures is indeed the diameter of the tree.
The first procedure clearly runs in O(V ), since every edge u v is examined at
most twice - once when we d[u v], and once when we set d[p(u) u].
The second procedure as it is written runs in worst-case time O(V 2 ), since if we
have a node with (V ) chilren, we will be looking through all the downward
distance values V times for each maximization step in line 3. However, we notice
that the only useful values from that step are the two largest downward x z
values, since at most one such z can be the y we are currently looking at. Thus,
we precompute at each node x the two largest x z values, and then use the
largest unless z = y. Thus, each edge is considered at most a constant number of
times, and the running time is O(V ) as well.
(b) [8 points] Do problem 22-3(b) on page 559 of CLRS. You may assume the result
of 22-3(a) without proof.
Answer: From part (a), we know that if there is an Euler tour in the graph G,
then the in-degree equals the out-degree for every vertex v. Because of this fact,
we know that if we pick a path with unique edges, any time we reach a vertex
(use one of the in-degree edges), there must still be a way to leave the vertex (use
one of the out-degree edges) except for the first vertex in the path.
Our algorithm therefore is to pick a random starting vertex v. We pick an outgoing
Problem Set #6 7
edge from v, (v, u), at random. We move to vertex u and delete edge (v, u) from
the graph and repeat. If we get to a vertex with no outgoing edges and it is not
equal to v, we report that no Euler tour exists. If the vertex with no outgoing
edges is v, we have a cycle, which we represent as v u v.
The problem is that we may not have visited all the edges in the graph yet. This
may only occur if there are vertices in our cycle which have outgoing edges that
we didnt traverse. Otherwise, our graph would not be connected. If we can
efficiently pick one of these vertices u and start a cycle from that point, we know
that the new cycle from u will end at u by the same reasoning as above. Therefore,
we can connect the two cycles {. . . a u b . . . } and {u c d u} by
making one big cycle {. . . a u c d u b . . . }. This is a valid cycle
since we removed all the edges in the first cycle before computing the second cycle,
so we still have the property that each edge is used at most once. We assume
that when we start the new cycle from u, we keep a pointer to the position of u
in the old cycle so that we know where to break the old cycle in constant time.
If we repeat this process until there are no more edges in the graph, we will
have an Euler tour. The only problem is how to pick the vertex u that still has
outgoing edges from the current cycle. Our original vertex was v. For the first
cycle created, we simply walk along the cycle from v and let u equal the first
vertex which still has outgoing edges. Then, after adding the next cycle, we start
walking from u along the newly merged cycles and so on. Once we have traversed
the path from v ; u, we know that none of the vertices in that section can ever
have outgoing edges, so we dont need to consider them the second time. We will
need to walk over the entire Euler tour at some point in the algorithm, so the
total running time of finding the vertex u for all the cycles is O(E). Also, the
cost of actually finding the cycles traverses each edge exactly once, so it is also
O(E). Therefore, the total running time of the algorithm is O(E).
(c) [3 points] Suppose we have a single-source shortest path algorithm A that runs
in time f (|V |, |E|) when all edge weights are nonnegative. Give an algorithm to
solve problem 24.3-4 on page 600 of CLRS in time f (|V |, |E|) + O(E) by using A
as a subroutine.
Answer: The reliability of a path from u to v is the product of the reliabilities
of each edge in the path. This is because the probability that the channel does
not fail is the probability that all individual edges do not fail. Since the failures
or successes of the individual edges are independent, we simply multiply to get
the total probability of success.
But, our shortest path algorithm finds the shortest path by summing the edge
weights. We need some transformation of our reliabilities so that we can use the
shortest path algorithm given. One function we have seen that converts a product
of terms to a sum of terms is the log function.
If we define a new weight function w(u, v) = lg(r(u, v)), then all the edge
weights are positive since the log of a positive number less than 1 is negative. As
the reliability decreases, the value of the weight of the edge will increase. Since
we are trying to find the most reliable path, this will correspond to the shortest
Problem Set #6 8
Y
RP AT H(s, t) = r(e)
eP ath(s,t)
X
log RP AT H(s, t) = log r(e)
eP ath(s,t)
X
log RP AT H(s, t) = log r(e)
eP ath(s,t)
Since the log function is monotonic as long as the base is greater than 1, if we
find the minimum path with edge weights w, we will also find the most reliable
path.
The time for the conversion to the weight function w is constant for each edge,
so the total time is O(E). We then call the single-source shortest path algorithm
which runs in time f (|V |, |E|) and returns our most reliable path. Therefore, the
total running time is f (|V |, |E|) + O(E).
(d) [5 points] Do problem 25.2-9 on page 635 of CLRS.
Answer: If we have an algorithm to compute the transitive closure of a directed
acyclic graph, we can use it to calculate the transitive closure of any directed
graph. We first decompose the directed graph G into its strongly connected
component graph GSCC . This runs in time O(V + E) and results in a graph with
at most V vertices and E edges. We keep track of which set of vertices in G
map to each vertex in GSCC . Call this g(v) which returns a stronly connected
component in GSCC . We then call the transitive closure algorithm on the graph
GSCC . This runs in time O(f (|V |, |E|)). All that is left is to convert back from the
strongly connected component graphs transitive closure to Gs transitive closure.
There is an edge (u, v) in the transitive closure of G if and only if there is an edge
(g(u), g(v)) in the graph GSCC . The forward direction is easy to see. If there
is a path from u to v in G, then there must be a path between the component
that u belongs to and the one that v belongs to in GSCC . The reverse direction
is equally easy. If there is a path between us strongly connected component and
vs strongly connected component in GSCC , then there is a path between some
vertex a in g(u) and some vertex b in g(v) in G. But, since u and a are in the
same SCC, there is a path from u ; a and since v and b are in the same SCC,
there is a path from v ; b. Therefore, there is a path u ; a ; b ; v in G.
To actually calculate the transitive closure of G this way would require time V 2
since wed need to look at all pairs. Instead, we will look at the transitive closure
of GSCC . For each edge (a, b) in the transitive closure of GSCC , we assume that
we know the set of vertices in G that map to a and b, call these sets g 1(a) and
g 1 (b) respectively. We will add the edge (x, y) for all vertices x g 1(a) and
Problem Set #6 9
y g 1 (b). This will take time equal to the number of edges in the transitive
closure of G, O(E ).
Therefore the total running time for the algorithm is the time to build the strongly
connected components (O(V +E)), the time to run the transitive closure algorithm
on GSCC (O(f (|V |, |E|)) and the time to convert back to the transitive closure of
G (O(E )). Since we know that E < E , the total time is O(f (|V |, |E|)+V +E ).