HW3_solution

advertisement
1. [Grading Rule]
Algorithm costs 15 pts.
Running time costs 5 pts.
Correctness costs 5 pts.
Greedy Solution:
1. Sort the set so that x1 <= x2 <= … <= xn.
2. Let xmin be the smallest number in the set.
3. Place the closed interval at [x, x+1].
4. Remove from the sorted set all the numbers that do not fall in the above
interval.
5. Repeat steps (2)–(4) until the set is empty.
Running time:
Step(1) takes O(n lgn) time using any of the standard sorting techniques.
Step(2) just has to return the first number in the set. Therefore, Steps(2) & (3)
take O(1) time.
Step(4): Since the set is sorted, this step takes time proportional to the number of
elements that fall in the particular interval; this will take O(1) time per element in
the set, and thus O(n) time on the whole.
Therefore the running time of the algorithm is O(n lgn) + O(n) = O(n lgn).
PROOF of correctness
Greedy Choice property:
We claim that (one of) the optimal solution will have the first unit–interval
starting at the smallest number xmin in the set.
Proof:
The smallest number xmin should be covered by at least one of the unit–
intervals. Say the interval covering xmin starts before xmin, i.e. it is given by
some [y, y+1], where y < xmin. Since there are no numbers less that xmin, we
can move the interval from [y, y+1] to [xmin, xmin+1] without increasing the
number of intervals. Therefore ourclaim is correct.
Optimal Sub–structure property:
Once the first interval is chosen, we claim that the remaining problem has
an optimal sub–structure.
Proof:
Since the elements that are contained in the first interval are already
covered, the position for the next interval can be completely determined by
the rest of the elements in the set. The rest of the points form a new set with
fewer elements and the same problem of being covered with the fewest
number of unit–intervals. Therefore this has an optimal sub–structure
property.
2. [Grading Rule] (Step 1 costs 5 pts.) Step 2 & 3 each costs 10 pts.
Recall the definition of matroid:
A matroid is an ordered pair M = (R, I) satisfying the definitions:
1. R is a finite nonempty set.
2. I is hereditary: I is a nonempty family of subsets of R, called the independent
subsets of R, such that if B∈I and A⊆B, then A∈I, obviously, ∅∈I
3. M satisfies the exchange property: If A∈I, B∈I and |A|<|B|, then ∃x∈B-A such
that A∪{x}∈I
To prove that (S, l) is a matroid, consider the following steps:
Step 1. Since S1, S2, …, Sk are nonempty disjoint finite sets, S, union of S1, S2, …, Sk is
also a finite nonempty set.
Step 2. Suppose that X∈l and Y⊆X, →|X∩Si|≦1 for all i
Assume |Y∩Sj| > 1 for some Sj,
let Yj = Y∩Sj , since Yj is a subset of X
then |X∩Sj| > 1. Contradiction! →|Y∩Si|≦1 for all i
Step 3. Suppose that X∈l and Y∈l, |X| < |Y|
If |X∩Si| = 0 for all Si, it’s trivial to find a x that x∈Y-X s.t. X∪{x}∈I
If |X∩Si| = 1 for some of the Si, then |X|≧1.
Given |X|<|Y|, we have |Y|≧2.
If |Y∩Si| = 0 for all Si, any x, x∈Y-X satisfies {x}∪X∈I
If | Y∩Si | = 1 for some Si, let Y∩Si = {y}, then any z, z∈Y-{y}-X satisfies
exchange property
Thus, (S, l) is a matroid.
3. [Grading Rule] Each case costs 10 pts.
Suppose that i-th operation is TABLE_DELETE, consider the value of load factor α : αi =
(number of entries in table after iteration i) / (size of table after iteration i) = num i / sizei.
case 1.
If αi - 1 = 1/2, αi < 1/2
ci’ = ci + Φi - Φi-1 = 1 + ( sizei – 2numi ) - ( 2numi-1 - sizei-1 )
= 1 + ( sizei-1 – 2(numi-1 -1)) - ( 2numi-1 - sizei-1 ) = 3 + 2 sizei-1 - 4 numi-1
= 3 + 2 sizei-1 - 4αi-1 sizei-1≦-1+2 sizei-1-4/2 sizei-1 = 3
case 2.
If 1/3 ≦ αi - 1 < 1/2, αi ≦ 1/2 ,i-th operation would not cause a
contraction
ci’ = ci + Φi - Φi-1 = 1 + ( sizei – 2numi ) - (sizei-1 - 2numi-1)
= 1 + (sizei-1 – 2(numi-1-1)) -(sizei-1 - 2numi-1) = 3
case 3. If αi - 1 = 1/3, αi ≦ 1/2 thus i-th operation would cause a contraction
ci’ = ci + Φi - Φi-1 = numi +1 + ( sizei – 2numi ) - (sizei-1 - 2numi-1)
= numi-1 -1 + 1 +( 2/3 sizei-1 – 2(numi-1 -1) ) - (sizei-1 - 2numi-1)
= numi-1 – 1/3 sizei-1 + 2 = 2
Thus the amortized cost of TABLE-DELETE is bounded by 3.
4. [Grading Rule] (a) costs 10 pts. (b) costs 15 pts.
(a)
When both operations have to pop the entire stack, the running time of each op is
the size of the stack, so worst case MultiPopA runs in O(n) and MultiPopB runs in
O(m) time. Transfer involves popping elements off of A and pushing them onto B.
Since in the worst case we transfer the entire stack on A, we use n pops and n pushes
for a worst case running time of O(n).
(b)
Define Φ(n,m) = 3n + m.
Initially the potential is zero, and for non-empty stacks,
the potential is always positive.
The amortized costs are as follows:
PushA
ˆci = ci + (Di+1) − (Di)
= 1 + 3(n + 1) + m − (3n + m)
=4
PushB
ˆci = 1 + 3n + (m + 1) − (3n + m)
=2
MultiPopA
ˆci = k + 3(n − k) + m − (3n + m)
= −2k
MultiPopB
ˆci = k + 3n + (m − k) − (3n + m)
=0
Transfer
ˆci = 2k + 3(n − k) + (m + k) − (3n + m)
=0
The amortized cost of each function is bounded above by a constant, so the overall
run time for all operations is O(1). It is ok to have a negative amortized cost in the
MultiPopA example. PushA pays for a possible transfer, but a MultiPopA makes a
transfer unnecessary even though PushA has paid for it.
Download