


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



ECS 122A: Algorithm Design and Analysis Handout 1 UC Davis — Charles Martel May 5, 2008
Consider the interval scheduling problem where we are given a series of activities and associated start and finish times, and we want to schedule the maximum number of activities. Suppose all the start times and finish times are integers. We now allow two activities to be scheduled if they overlap by one time unit. However, we do not allow an activity to be scheduled if it overlaps at both the start and finish (e.g. if an activity is from 5 to 9 it can be scheduled along with either an activity from 3 to 6 or one from 8 to 10, but not along with both such activities or along with an activity that overlaps for two units, such as one from 7 to 10). As before we assume we have sorted the activities so f 1 ≤ f 2 ≤... fn.
a) Briefly argue that activity one (with the minimum finish time) is always part of some optimal solution. Solution: Let O∗^ = {o 1 , o 2 ,... , ok} be any optimal solution ordered by finishing time. Since two activities are compatible if they overlap by one time unit or less, f 1 ≤ fo 1 ≤ so 2 +1. Thus, we can simply “exchange” the first activity in O∗^ with activity one. The resulting solution still contains the same number of activities as O∗^ and thus is still optimal.
b) Suppose we schedule activity one, then we remove all activities that overlap by more than one (i.e. their si < f 1 − 1). Let j be a remaining activity with minimum finish time. Can we always add j to our solution and still be optimal? Consider two cases:
i) each finish time is distinct ii) two (or more) activities have the same finish time.
In each case either argue that adding activity j is always optimal or give a counter example to show it may not be optimal.
Solution i: By part a, let O∗^ = { 1 , o 2 ,... , ok} be an optimal solution ordered by finishing time. Since the finishing times are distinct, and since j is the activity with minimum finishing time, fj < fo 2 ≤ so 3 + 1. Thus, selecting activity j is always optimal.
Solution ii: Let {(1, 3), (2, 4), (3, 4), (3, 6)} be a set of start and finish times. We can schedule (1, 3), (3, 4) and (3, 6) since activity 3 only overlaps with one other activity. However, we cannot schedule (1, 3), (2, 4) and (3, 6), since activity 2 overlaps on both ends. Thus, if the finishing times are not distinct, the algorithm may make a mistake when it tries to break a tie.
1 2 3 4
1 2 3 4 5 6
Suppose you are given two strings x = x 1 x 2 · · · xn and y = y 1 y 2 · · · ym. You wish to determine the minimum “distance” between x and y (the cheapest sequence of operations to convert x to y under the following conditions:
For example, converting AABB into AXB has cost d+e if we delete the first character and modifying the third “B” into an “X”.
Structure your answer in the following way: 1) Specify your data structure (array or matrix), and describe in words what kind of data is stored in a particular cell. 2) What is the precise size of your data structure, and what cells, if any, are filled at initialization? 3) Show and describe your formula for computing the value of a cell in terms of prior cell values. 4) At the end of your algo- rithm, how do you find the minimum edit-distance? 5) How do you reconstruct the exact sequence of operations (insert, delete, etc.) in order to change x into y? 6) What is the runtime of this algorithm, and what is the runtime to reconstruct the solution?
Solution:
M [i, j] = min
M [i − 1 , j] + d, M [i, j − 1] + c, M [i − 1 , j − 1] + e ∗ change(i, j)
a. In binary search, we are given a sorted array and a target value X to search for. If the array is empty, we return “not found”, otherwise we compare X to the mid-point of our current array.
i) If they are “=”, we have “found” X and stop. ii) If X is smaller, we continue recursively with the elements smaller than the midpoint. iii) If X is larger, we continue recursively with the elements larger than the midpoint.
Write a recurrence relation for B(n), the worst case number of compares used by binary search. For convenience you may assume the array will always divide evenly (so you don’t have to worry about floors/ceilings). Solve your recurrence to big Theta accuracy. Solution: B(n) = B(n/2) + 1 where B(2) = 1. We will prove by induction that B(n) = Θ(log(n)). Since log(2) = 1, this assumption holds in the base case. Now, we assume that B(2k) = log(2k), and we will prove that B(2k+1) = log(2k+1). Thus,
B(2k+1) = B(2k+1/2) + 1 = B(2k) + 1 = log(2k) + 1 = log(2k) + log(2) = log(2k+1)
An alternative proof is via the Master’s Theorem. Notice that a = 1, b = 2 and i = 0. Thus, a = bi^ or 1 = 2^0 , which implies that B(n) = Θ(ni^ log(n)) = Θ(n^0 log(n)) = Θ(log(n)).
b. Consider the following recurrence relation:
T (n) = 2T (3n/4) + n T (2) = 1
Solve for T (n) to within big Theta accuracy. Solution: Our proof is via the Master’s Theorem. Notice that a = 2, b = 4/3 and i = 1. Thus, a > bi^ or 2 = 4/ 31 , which implies that T (n) = Θ(nlogb(a)) = Θ(nlog^4 /^3 (2)) = Θ(n^2.^4094 ), via the change of bases formula:
log 4 / 3 (2) = log 2 (2) log 2 (4/3)