Level 4 — ApplicationAlgorithm Paradigms

Algorithm Paradigms

60 minutes60 marksprintable — key stays hidden on paper

Level 4 — Application (novel problems, no hints) Time limit: 60 minutes Total marks: 60


Instructions: Answer all questions. Show reasoning, recurrences, and complexity analysis clearly. Pseudocode or Python-like code is acceptable. Justify optimality claims where asked.


Question 1 — Weighted Interval Scheduling vs Greedy (12 marks)

You are given nn jobs. Job ii has start time sis_i, finish time fif_i, and weight wiw_i. You may run at most one job at a time (no overlaps) and want to maximize total weight.

Consider these 4 jobs:

Job start finish weight
A 0 3 5
B 1 4 6
C 3 6 5
D 4 7 6

(a) The classic "earliest-finish-time greedy" solves unweighted activity selection optimally. Give the schedule this greedy produces for the jobs above (using earliest-finish tie-break) and its total weight. (3)

(b) State the optimal-weight schedule and its total weight. Hence give a one-sentence explanation of why the unweighted greedy fails here. (3)

(c) Give the DP recurrence for weighted interval scheduling. Define p(i)p(i) = largest index j<ij<i (jobs sorted by finish time) with fjsif_j \le s_i. Write OPT(i)\text{OPT}(i) and explain the two choices. (4)

(d) State the running time of your DP when the sort dominates and p(i)p(i) is found by binary search. (2)


Question 2 — A Novel DP: "Painting Fence" Counting (12 marks)

A fence has nn posts in a row. You have kk colours. You must paint every post such that no more than two adjacent posts share the same colour (i.e., three consecutive posts may not all be the same colour).

(a) Identify the state and formulate a recurrence. Let same(i)\text{same}(i) = number of ways to paint the first ii posts where posts i1,ii-1,i are the same colour, and diff(i)\text{diff}(i) = number where posts i1,ii-1,i differ. Give recurrences for both and the base case at i=2i=2. (6)

(b) Compute the total number of ways for n=4n=4, k=3k=3. (4)

(c) State the time and space complexity, and describe how to reduce space to O(1)O(1). (2)


Question 3 — Bitmask DP / Bit Tricks (12 marks)

(a) For the Travelling Salesman Problem on nn cities using the Held–Karp bitmask DP, state the DP state, the recurrence, and the overall time and space complexity. (5)

(b) Given a 32-bit unsigned integer xx, write a bit-manipulation expression that: (i) isolates the lowest set bit, (ii) clears the lowest set bit, (iii) tests whether xx is a power of two (return boolean). (3)

(c) An array of 2n+12n+1 integers contains each value twice except one value which appears once. Give an O(n)O(n)-time, O(1)O(1)-space algorithm to find the single value and prove why it works in one sentence. (2)

(d) Evaluate: for the array [4, 1, 2, 1, 2], trace the running XOR and give the answer. (2)


Question 4 — Greedy Exchange Argument Proof (12 marks)

Consider the problem: schedule nn jobs on a single machine, each job ii has processing time tit_i; minimize the sum of completion times iCi\sum_i C_i (where CiC_i is the time job ii finishes).

(a) State the greedy rule you would use. (2)

(b) Prove your greedy is optimal using an exchange argument: assume an optimal schedule violates the rule and show swapping two adjacent out-of-order jobs does not increase the objective. Give the algebra for the swap. (8)

(c) For processing times {3,1,2}\{3, 1, 2\}, give the optimal order and the resulting Ci\sum C_i. (2)


Question 5 — Edit Distance Application + Randomized Reasoning (12 marks)

(a) Compute the Levenshtein edit distance between "AXBY" and "ABCY" (insert/delete/substitute cost 1 each). Show the DP table and state the distance. (6)

(b) Classify each of the following as Las Vegas or Monte Carlo, with a one-line justification each: (i) Randomized QuickSort (always correct, random runtime). (ii) Freivalds' algorithm for verifying AB=CAB=C (may err, fixed runtime). (2)

(c) Freivalds' algorithm errs with probability 12\le \tfrac12 per trial when ABCAB \ne C. If you run tt independent trials and accept only if all pass, give the worst-case error probability, and find the smallest tt so this is 0.001\le 0.001. (4)

Answer keyMark scheme & solutions

Question 1 (12)

(a) Sort by finish: A(3), B(4), C(6), D(7). Earliest-finish greedy: pick A (finish 3). Next compatible must have start 3\ge 3: C (start 3) — pick. Next start 6\ge 6: D starts 4 (no). None. Greedy schedule = {A, C}, weight =5+5=10= 5+5 = 10. (3) (correct schedule 2, weight 1)

(b) Optimal: {B, D}, weight =6+6=12= 6+6 = 12. B(1–4), D(4–7) compatible. Unweighted greedy ignores weights — it optimizes count/earliest finish, so it can pick low-weight jobs. (3) (schedule+weight 2, reason 1)

(c) Sort jobs by finish time. Define p(i)p(i) as given. OPT(i)=max(wi+OPT(p(i))include i, OPT(i1)exclude i),OPT(0)=0.\text{OPT}(i) = \max\big(\underbrace{w_i + \text{OPT}(p(i))}_{\text{include } i},\ \underbrace{\text{OPT}(i-1)}_{\text{exclude } i}\big),\quad \text{OPT}(0)=0. Include: take wiw_i plus best from jobs finishing before sis_i. Exclude: best without job ii. (4) (recurrence 2, two-choice explanation 2)

(d) Sorting O(nlogn)O(n\log n) dominates; each p(i)p(i) by binary search O(logn)O(\log n); DP fill O(n)O(n). Total O(nlogn)O(n\log n). (2)


Question 2 (12)

(a)

  • same(i)\text{same}(i): posts i1,ii-1,i equal ⇒ previous pair i2,i1i-2,i-1 must differ (else 3 same). So same(i)=diff(i1)\text{same}(i) = \text{diff}(i-1).
  • diff(i)\text{diff}(i): post ii differs from i1i-1, and it can be any of the other k1k-1 colours regardless of the earlier pair: diff(i)=(k1)(same(i1)+diff(i1))\text{diff}(i) = (k-1)\cdot(\text{same}(i-1) + \text{diff}(i-1)).
  • Base (i=2i=2): same(2)=k\text{same}(2)=k, diff(2)=k(k1)\text{diff}(2)=k(k-1).

(6) (same recurrence 2, diff recurrence 2, base 2)

(b) k=3k=3:

  • i=2i=2: same=3, diff=6, total=9.
  • i=3i=3: same = diff(2)=6; diff = 2·(3+6)=18; total=24.
  • i=4i=4: same = diff(3)=18; diff = 2·(6+18)=48; total=66.

Answer: 66 ways. (4)

(c) Time O(n)O(n), space O(n)O(n) (or O(1)O(1) by keeping only previous same, diff and rolling them each step). (2)


Question 3 (12)

(a) State: dp[S][j]\text{dp}[S][j] = min cost path starting at city 0, visiting exactly the set SS of cities, ending at city jSj\in S. Recurrence: dp[S][j]=miniS{j}dp[S{j}][i]+dist(i,j).\text{dp}[S][j] = \min_{i \in S\setminus\{j\}} \text{dp}[S\setminus\{j\}][i] + \text{dist}(i,j). Base dp[{0}][0]=0\text{dp}[\{0\}][0]=0. Answer =minjdp[full][j]+dist(j,0)= \min_j \text{dp}[\text{full}][j]+\text{dist}(j,0). Time O(2nn2)O(2^n \cdot n^2), space O(2nn)O(2^n \cdot n). (5)

(b) (i) lowest set bit: x & (-x). (ii) clear lowest set bit: x & (x-1). (iii) power of two: x != 0 and (x & (x-1)) == 0. (3) (1 each)

(c) XOR all elements: ans = a[0] ^ a[1] ^ ... . Since xx=0x\oplus x=0 and XOR is associative/commutative, all paired values cancel, leaving the unique one. O(n)O(n) time, O(1)O(1) space. (2)

(d) 41=54\oplus1=5; 52=75\oplus2=7; 71=67\oplus1=6; 62=46\oplus2=4. Answer 4. (2)


Question 4 (12)

(a) Greedy rule: Shortest Processing Time first (SPT) — sort jobs by tit_i ascending. (2)

(b) Exchange argument. Consider any optimal schedule; suppose two adjacent jobs aa (in position pp) then bb (position p+1p+1) are out of order, i.e., ta>tbt_a > t_b. Let TT = total processing time of all jobs before position pp. Completion times of aa and bb:

  • Original: Ca=T+taC_a = T + t_a, Cb=T+ta+tbC_b = T + t_a + t_b. Their sum =2T+2ta+tb= 2T + 2t_a + t_b.
  • Swapped (bb then aa): Cb=T+tbC_b' = T + t_b, Ca=T+tb+taC_a' = T + t_b + t_a. Sum =2T+2tb+ta= 2T + 2t_b + t_a.

All other jobs' completion times unchanged (jobs before position pp unaffected; jobs after position p+1p+1 see the same total ta+tbt_a+t_b preceding them). Difference in objective: Δ=(2T+2tb+ta)(2T+2ta+tb)=tbta<0since ta>tb.\Delta = (2T+2t_b+t_a) - (2T+2t_a+t_b) = t_b - t_a < 0 \quad\text{since } t_a>t_b. So swapping strictly decreases the objective — contradicting optimality unless no such inversion exists. Repeatedly removing adjacent inversions transforms any optimal schedule into SPT order without increasing cost; hence SPT is optimal. (8) (setup/assumption 2, completion-time algebra 3, Δ computation 2, conclusion 1)

(c) SPT order of {3,1,2}\{3,1,2\}: 1,2,31,2,3. Completion times: 1,3,61, 3, 6. Ci=1+3+6=10\sum C_i = 1+3+6 = \mathbf{10}. (2)


Question 5 (12)

(a) Table (rows = AXBY prefixes, cols = ABCY prefixes), D[i][j]D[i][j]:

"" A B C Y
"" 0 1 2 3 4
A 1 0 1 2 3
X 2 1 1 2 3
B 3 2 1 2 3
Y 4 3 2 2 2

Distance = 2 (substitute X→B... actually: align A,B,Y; edit AXBYABCY: substitute X→B then B→C? Two operations). Distance =2=2. (6) (recurrence/table 4, correct value 2)

(b) (i) Las Vegas — always produces correct sorted output; only runtime is random. (ii) Monte Carlo — fixed runtime but may give a wrong answer with bounded probability. (2)

(c) Trials independent, each errs 1/2\le 1/2 when ABCAB\ne C; accept only if all pass, so error (1/2)t\le (1/2)^t. Need (1/2)t0.0012t1000tlog210009.97t=10(1/2)^t \le 0.001 \Rightarrow 2^t \ge 1000 \Rightarrow t \ge \log_2 1000 \approx 9.97 \Rightarrow t = \mathbf{10} (since 210=102410002^{10}=1024\ge1000, 29=512<10002^9=512<1000). (4)


[
  {"claim":"Painting fence n=4,k=3 total is 66","code":"k=3\nsame={}; diff={}\nsame[2]=k; diff[2]=k*(k-1)\nfor i in range(3,5):\n    same[i]=diff[i-1]\n    diff[i]=(k-1)*(same[i-1]+diff[i-1])\ntotal=same[4]+diff[4]\nresult = (total==66)"},
  {"claim":"XOR of [4,1,2,1,2] equals 4","code":"from functools import reduce\nvals=[4,1,2,1,2]\nx=reduce(lambda a,b:a^b, vals)\nresult = (x==4)"},
  {"claim":"SPT order of {3,1,2} gives sum of completion times 10","code":"ts=sorted([3,1,2])\nt=0; s=0\nfor x in ts:\n    t+=x; s+=t\nresult = (s==10)"},
  {"claim":"Smallest t with (1/2)**t <= 0.001 is 10","code":"import math\nt=0\nwhile (Rational(1,2))**t > Rational(1,1000):\n    t+=1\nresult = (t==10)"},
  {"claim":"Edit distance AXBY vs ABCY is 2","code":"a='AXBY'; b='ABCY'\nm=len(a); n=len(b)\nD=[[0]*(n+1) for _ in range(m+1)]\nfor i in range(m+1): D[i][0]=i\nfor j in range(n+1): D[0][j]=j\nfor i in range(1,m+1):\n    for j in range(1,n+1):\n        c=0 if a[i-1]==b[j-1] else 1\n        D[i][j]=min(D[i-1][j]+1, D[i][j-1]+1, D[i-1][j-1]+c)\nresult = (D[m][n]==2)"}
]