solution - UF CISE

Problem 1
The proposed approach—selecting the last activity to start that is compatible with
all previously selected activities—is really the greedy algorithm but starting from
the end rather than the beginning.
Another way to look at it is as follows. We are given a set S = {a1 , a2 , . . . , an }
of activities, where ai = [si , f i ), and we propose to Þnd an optimal solution by
selecting the last activity to start that is compatible with all previously selected
activities. Instead, let us create a set S = {a1 , a2 , . . . , an }, where ai = [ f i , si ).
That is, ai is ai in reverse. Clearly, a subset of {ai1 , ai2 , . . . , aik } ⊆ S is mutually
compatible if and only if the corresponding subset {ai1 , ai2 , . . . , aik } ⊆ S is also
mutually compatible. Thus, an optimal solution for S maps directly to an optimal
solution for S and vice versa.
The proposed approach of selecting the last activity to start that is compatible with
all previously selected activities, when run on S, gives the same answer as the
greedy algorithm from the text—selecting the Þrst activity to Þnish that is compatible with all previously selected activities—when run on S . The solution that
the proposed approach Þnds for S corresponds to the solution that the text’s greedy
algorithm Þnds for S , and so it is optimal.
Problem 2
(a) k ln k = Θ(n) means
∃c1 , c2 > 0, n0 > 0 : c1 n ≤ k ln k ≤ c2 n ∀n ≥ n0
(1)
or equivalently,
∃c1 , c2 > 0, n0 > 0 : c1
Therefore, we need to bound
ln k
ln n .
n
ln k
n
≤k
≤ c2
ln n
ln n
ln n
∀n ≥ n0
Taking log both sides of (1) gives
ln c1 + ln n ≤ ln k + ln (ln k) ≤ ln c2 + ln n
(2)
Since ln c1 + ln n ≤ ln k + ln (ln k) ≤ 2 ln k for big enough k, we have
1
ln c1
ln k
ln k
1
+
≤
which means
≥
2 2 ln n
ln n
ln n
2
(3)
Now, since ln k ≤ ln k + ln (ln k) ≤ ln c2 + ln n for big enough n, we have
ln c1
ln k
≤1+
≤2
ln n
ln n
(4)
From (2) and (3), we have
c1 n
1 ln k
≤ k
≤k
2 ln n
2 ln n
and from (2) and (4), we have
ln k
n
n
1
k≤k
≤ c2
which implies k ≤ 2c2
2
ln n
ln n
ln n
Thus, k = Θ(n/ ln n)
Problem 2
(b) It’s easy to show that lg∗ (lg n) = lg∗ (n) − 1. Since lg∗ (n) − 1 > lg(lg∗ (n)), it implies that lg∗ (lg n) is
asymptotically larger than lg(lg∗ (n))
(c) Changing variable: let m = ln n and the recurrence becomes
T (2m ) = 4T (2m/2 ) + ln m
Again let S(m) = T (2m ), we have
S(m) = 4S(m/2) + ln m
which in turn implies
S(m) = 4ln m S(1) + ln m
ln
m
X
i=0
or equivalently,
4i −
ln
m
X
i4i
i=0
2
S(m) = m2 S(1) +
(4m − 1) ln m (3 ln m − 1)4m2 + 4
−
= O(m2 )
3
9
Thus, T (n) = O(ln2 n)
Problem 3
(a) Greedy Algorithm: Give the shortest skier the shortest ski, give the second shortest skier the second
shortest ski, give the third shortest skier the third shortest ski and so on.
(b)Time complexity: O(n ln n) for sorting skiers and skies and O(n) for distributing skies to skiers. Overall
O(n ln n)
Proof of correctness:
This algorithm is CORRECT. The proof is by contradiction. Assume the people and skis
are numbered in increasing order by height. If the greedy algorithm is not optimal, then there
is some input p1 , ..., pn , s1 , ..., sn for which it does not produce an optimal solution. Let the
optimal solution be T = {(p1 , sj(1) ), ..., (pn , sj (n))}, and let the output of the greedy algorithm
be G = {(p1 , s1 ), ..., (pn , sn )}. Beginning with p1 , compare T and G. Let pi be the first person
who is assigned different skis in G than in T . Let sj be the pair of skis assigned to pi in T
. Create solution T 0 by switching the ski assignments of pi and pj . By the definition of the
greedy algorithm, si ≤ sj . The total cost of T 0 is given by
Cost(T 0 ) = Cost(T ) − 1/n(|pi − sj | + |pj − si | − |pi − si | − |pj − sj |).
There are six cases to be considered. For each case, one needs to show that
(|pi − sj | + |pj − si | − |pi − si | − |pj − sj |) ≥ 0.
Case 1: pi <= pj <= si <= sj .
|pi − sj | + |pj − si | − |pi − si | − |pj − sj | = (sj − pi ) + (si − pj ) − (si − pi ) − (sj − pj ) = 0.
Case 2: pi ≤ si ≤ pj ≤ sj .
|pi −sj |+|pj −si |−|pi −si |−|pj −sj | = (sj −pi )+(pj −si )−(si −pi )−(sj −pj ) = 2(pj −si ) >= 0.
Case 3: pi <= si <= sj <= pj .
|pi −sj |+|pj −si |−|pi −si |−|pj −sj | = (sj −pi )+(pj −si )−(si −pi )−(pj −sj ) = 2(sj −si ) >= 0
Case 4: si <= sj <= pi <= pj .
|pi − sj |+|pj − si |−|pi − si |−|pj − sj |= (pi − sj ) + (pj − si ) − (pi − si ) − (pj − sj ) = 0.
Case 5: si <= pi <= sj <= pj .
|pi − sj |+|pj − si |−|pi − si |−|pj − sj |= (sj − pi ) + (pj − si ) − (pi − si ) − (pj − sj ) = 2(sj − pi ) >= 0
Case 6: si <= pi <= pj <= sj .
|pi − sj |+|pj − si |−|pi − si |−|pj − sj |= (sj − pi ) + (pj − si ) − (pi − si ) − (sj − pj ) = 2(pj − pi ) >= 0
Problem 4
(a) Sequence: {1, 10, 2, 3}
(b)
First define T (i, j) to be the maximum amount of money that player 1 can guarantee given
that it is currently his turn and the game is currently being played on the subarray [ai , ai+1 , . . . , aj ].
We now note that
T (i, j) = max {ai + min {T (i + 1, j − 1), T (i + 2, j)} , aj + min {T (i + 1, j − 1), T (i, j − 2)}}
The intuition behind this is very simple. When it’s player 1’s turn he has two choices: namely
pick ai or pick aj . We are essentially maximizing over this choice. Now given that player
1 has chosen either ai or aj player 2 now makes a move. Once player 2 makes a move it
becomes player 1’s turn. We take the minimum of the amount of money we can make on our
next move because that’s the smallest we can guarantee since player 2 can use any strategy
he pleases.
The base cases are now just T (i, j) = 0 when i > j.
Given all of this we can now build a dynamic programming algorithm for computing the
maximum amount of money player 1 can guarantee he wins. This bottom up dynamic programming algorithm would simply fill in the n × n tables by starting at the largest diagonal
(filling in T [i, i] for all 1 ≤ i ≤ n) and then filling in the next diagonal. Once the table is
filled out we can then simply output the T [1, n] entry of the table as our final solution.
Problem 4
(c)
1,2,10,3
11
1
3
2,10,3
1,2,10
10
2
3
2
10
3
0
11
10
1
10,3
2,10
2,10
1,2
10
10
10
2
3
10
0
4
2
10
0
3
10
2
0
11
2
10
0
5
10
2
0
13
1
2
0
4
2
1
0
5
Color elements red and black (alternating). If you go first, you can always
decide in advance that you will take all of the reds (or all of the blacks), and force your
opponent to cooperate (this works since n is even). All you need to do is calculate in advance
which subset has a larger sum, and then take only items of the correponding color.
Bonus Problem
(1) It is clear that S is finite. S is nonempty since S = S1 ∪ ... ∪ Sk and each Si is nonempty. Thus, S is a
finite nonempty set.
(2) We show that I is hereditary . For any B ∈ I, we have |B∩Si | ≤ 1 and for A ⊆ B, |A∩Si | ≤ |B∩Si | ≤ 1.
Thus A ∈ I and I is hereditary.
(3) To show that (S, I) satisfies exchange property, take A, B ∈ I and |A| < |B|. Let TA = {Si :
|A ∩ Si | = 1} and TB = {Sj : |B ∩ Sj | = 1}. Since S1 , S2 , ..., Sk is a partition of S into disjoint subsets, we
Sk
Sk
have A = i=1 A ∩ Si and B = j=1 B ∩ Sj . This implies |TA | = |A| < |B| = |TB |. Thus, there would be
at least a set Sk ∈ TB \TA . Hence, moving xk ∈ B ∩ Sk to A will not violate the membership of A in I, i.e.,
A ∪ {x} ∈ I.