Docsity

Midterm Exam Solutions for Algorithm Design and Analysis | ECS 122A, Exams of Computer Science

Toggle sidebar
bg1
ECS 122A: Algorithm Design and Analysis Handout 1
UC Davis — Charles Martel May 5, 2008
Midterm Exam 122A
1 Interval Scheduling (30 points)
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 f1≤f2≤. . . fn.
a) Briefly argue that activity one (with the minimum finish time) is always part of some optimal
solution.
Solution: Let O∗={o1, o2, . . . , ok}be any optimal solution ordered by finishing time. Since
two activities are compatible if they overlap by one time unit or less, f1≤fo1≤so2+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< f1−1). Let jbe a remaining activity with minimum finish time. Can we
always add jto 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 jis always optimal or give a counter example
to show it may not be optimal.
Solution i: By part a, let O∗={1, o2, . . . , ok}be an optimal solution ordered by finishing time.
Since the finishing times are distinct, and since jis the activity with minimum finishing time,
fj< fo2≤so3+ 1. Thus, selecting activity jis 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 432 5 6
pf3
pf4

Partial preview of the text

Download Midterm Exam Solutions for Algorithm Design and Analysis | ECS 122A and more Exams Computer Science in PDF only on Docsity!

ECS 122A: Algorithm Design and Analysis Handout 1 UC Davis — Charles Martel May 5, 2008

Midterm Exam 122A

1 Interval Scheduling (30 points)

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

2 Weighted Edit Distance (25 points)

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:

  • insert(x, i, a) = x 1 x 2 · · · xiaxi+1 · · · xn and has cost c
  • delete(x, i) = x 1 x 2 · · · xi− 1 xi+1 · · · xn and has cost d
  • modify(x, i, a) = x 1 x 2 · · · xi− 1 axi+1 · · · xn and has cost e

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:

  1. A matrix, where cell M [i, j] contains the minimum cost of converting x 1 x 2 · · · xi into y 1 y 2 · · · yj.
  2. The matrix M is n + 1 × m + 1, and M [i, 0] = di, and M [0, j] = ci.
  3. As in class, let change(i, j) = 1 (if xi = yj ), and 0 otherwise. The recurrence relation is:

M [i, j] = min

M [i − 1 , j] + d, M [i, j − 1] + c, M [i − 1 , j − 1] + e ∗ change(i, j)

  1. The minimum edit-distance is stored in M [n, m].
  2. In order to find the exact sequence of operations, begin at cell M [n, m] and backtrack through the matrix by determining min{M [n − 1 , m] + d, M [n, m − 1] + c, M [n − 1 , m − 1] + e ∗ change(n, m)}. If the minimum was from cell M [n − 1 , m], then the last operation was a delete. If the minimum was from cell M [n, m − 1], then the last operation was an insert, and if the minimum was from cell M [n − 1 , m − 1] then there was either no operation (if xn 6 = ym) or a modify (if xn = ym).
  3. The runtime is O(nm), and O(n + m) to reconstruct the solution (in the worse case, we might do m inserts, and then n deletes).

4 Recurrence Relation (25 points)

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)