Algorithms Exam Help
Algorithms Exam Help
Solve parts (a), (b), and (c) in two ways: drawing a recursion tree and
applying Master Theorem. Solve part (d) only by substitution.
(a)
(b)
(c)
(d)
1 def satisfying_booking(R):
2 ’’’
3 Input: R | Tuple of |R| talk request tuples (s, t)
4 Output: B | Tuple of room booking triples (k, s, t)
that is the booking schedule that satisfies R
2 ’’’
3 B = []
4 ##################
5 # YOUR CODE HERE #
6 ##################
7 return tuple(B)
Solution-1
(a)
Solution: T (n) = Θ(n2) by case 1 of the Master Theorem, since
logb a = 2 and f(n) = O(n2−ε) for any positive ε ≤ 1. Note that this is
true no matter the choice of f(n) ∈ O(n).
Drawing a tree, there are 4i vertices at depth i each doing at most c2i
work, so the n n total work at depth i is at most 4ic2i = 2icn.
Summing over the entire tree, the total work is at most
Since Θ(1) work is done at each leaf, and there are n2 leaves, the
total work is at least Ω(n2) leading to Θ(n2) running time.
(b)
(b) Solution: For this problem, reads and writes take constant time,
but comparisons are expensive, O(log n). Selection and
insertion sorts both perform O(n2) comparisons in the worst
case, while merge sort only performs O(n log n) comparisons,
so we choose merge sort.
Solution -3.
Solution: We can generalize the idea of binary search to search
quickly from both ends. The idea will be to alternately search
inward from either end of the island exponentially until Πcard just
passes Datum, and then use normal binary search to pinpoint
Datum’s precise location. Specifically, tell Πcard to alternately
teleport to 2i and n − 2i for increasing i starting at 0 until either
Datum is found, or until Datum has been passed, i.e., Datum is
observed north of 2j−1 but south of 2j for some j, or south of n −
2j−1 but north of n − 2j for some j. Reaching this state will take
O(j) time, since at most 2j locations will be visited, and then binary
searching within the remaining 2j−1 kilometer stretch of the island
will also take at most O(j) time. But since either 2j−1 < k < 2j or n−
2j < n− k < n− 2j−1, then j − 1 < lg k < j and j = O(log k), as
desired. This algorithm is correct because it identifies a bounded
range where Datum is known to exists, and reduces to binary search
which will correctly return Datum’s location.
Rubric:
• 6 points for description of a correct algorithm
• 2 points for a correct argument of correctness
• 2 points for a correct analysis of running time
• Partial credit may be awarded
Solution -4.
(b) Solution: Evenly divide R into two Θ(|R|) sized sets R1 and R2 and
recursively compute booking schedules B1 and B2 that satisfy R1 and R2
respectively. Then compute a satisfying booking B for R using part (a) in
O(n) time. As a base case, when |R| =1 and R = {(s, t)}, the satisfying
booking is ((1, s, t), ), so return it in T heta(1) time. This algorithm follows
the recurrence T (n)=2T (n/2) + O(n), so by Master Theorem case 2, this
algorithm runs in O(n log n) time.
Rubric:
• 3 points for description of a correct algorithm
• 1 point for a correct argument of correctness
• 1 point for a correct analysis of running time
• Partial credit may be awarded
(c) Solution:
1 def merge_bookings(B1, B2):
2 n1, n2, i1, i2 = len(B1), len(B2), 0, 0
3 x = 0 # invariant: t < min(t1, t2)
4 B = [] # invariant: B is satisfying booking up to time x
5 while i1 + i2 < n1 + n2:
6 if i1 < n1: k1, s1, t1 = B1[i1]
7 if i2 < n2: k2, s2, t2 = B2[i2]
8 if i2 == n2: # only bookings in B1 remain
9 k, s, x = k1, max(x, s1), t1
10 i1 += 1
11 elif i1 == n1: # only bookings in B2 remain
12 k, s, x = k2, max(x, s2), t2
13 i2 += 1
14 else: # bookings remain in B1 and B2
15if x < min(s1, s2): # shift x to start of first booking
16 x = min(s1, s2)
17 if t1 <= s2: # overlaps only B1 up to t1
18 k, s, x = k1, x, t1
19 i1 += 1
20 elif t2 <= s1: # overlaps only B2 up to t2
21 k, s, x = k2, x, t2
22 i2 += 1
23 elif x < s2: # overlaps only B1 up to s2
24 k, s, x = k1, x, s2
25 elif x < s1: # overlaps only B2 up to s1
26 k, s, x = k2, x, s1
27 else: # overlaps B1 and B2 up to t1 or t2
28 k, s, x = k1 + k2, x, min(t1, t2)
29 if t1 == x: i1 += 1
30 if t2 == x: i2 += 1
31 B.append((k, s, x))
32 B_ = [B[0]] # remove adjacent with same rooms
33 for k, s, t in B[1:]:
34 k_, s_, t_ = B_[-1]
35 if (k == k_) and (t_ == s):
36 B_.pop()
37 s = s_
38 B_.append((k, s, t))
39 return B_
40
41 def satisfying_booking(R):
42 if len(R) == 1: # base case
43 s, t = R[0]
44 return ((1, s, t),)
45 m = len(R) // 2
46 R1, R2 = R[:m], R[m:] # divide
47 B1 = satisfying_booking(R1) # conquer
48 B2 = satisfying_booking(R2) # conquer
49 B = merge_bookings(B1, B2) # combine
50 return tuple(B)