Algorithm Paradigms
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 jobs. Job has start time , finish time , and weight . 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 = largest index (jobs sorted by finish time) with . Write and explain the two choices. (4)
(d) State the running time of your DP when the sort dominates and is found by binary search. (2)
Question 2 — A Novel DP: "Painting Fence" Counting (12 marks)
A fence has posts in a row. You have 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 = number of ways to paint the first posts where posts are the same colour, and = number where posts differ. Give recurrences for both and the base case at . (6)
(b) Compute the total number of ways for , . (4)
(c) State the time and space complexity, and describe how to reduce space to . (2)
Question 3 — Bitmask DP / Bit Tricks (12 marks)
(a) For the Travelling Salesman Problem on 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 , write a bit-manipulation expression that: (i) isolates the lowest set bit, (ii) clears the lowest set bit, (iii) tests whether is a power of two (return boolean). (3)
(c) An array of integers contains each value twice except one value which appears once. Give an -time, -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 jobs on a single machine, each job has processing time ; minimize the sum of completion times (where is the time job 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 , give the optimal order and the resulting . (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 (may err, fixed runtime). (2)
(c) Freivalds' algorithm errs with probability per trial when . If you run independent trials and accept only if all pass, give the worst-case error probability, and find the smallest so this is . (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 : C (start 3) — pick. Next start : D starts 4 (no). None. Greedy schedule = {A, C}, weight . (3) (correct schedule 2, weight 1)
(b) Optimal: {B, D}, weight . 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 as given. Include: take plus best from jobs finishing before . Exclude: best without job . (4) (recurrence 2, two-choice explanation 2)
(d) Sorting dominates; each by binary search ; DP fill . Total . (2)
Question 2 (12)
(a)
- : posts equal ⇒ previous pair must differ (else 3 same). So .
- : post differs from , and it can be any of the other colours regardless of the earlier pair: .
- Base (): , .
(6) (same recurrence 2, diff recurrence 2, base 2)
(b) :
- : same=3, diff=6, total=9.
- : same = diff(2)=6; diff = 2·(3+6)=18; total=24.
- : same = diff(3)=18; diff = 2·(6+18)=48; total=66.
Answer: 66 ways. (4)
(c) Time , space (or by keeping only previous same, diff and rolling them each step). (2)
Question 3 (12)
(a) State: = min cost path starting at city 0, visiting exactly the set of cities, ending at city . Recurrence: Base . Answer . Time , space . (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 and XOR is associative/commutative, all paired values cancel, leaving the unique one. time, space. (2)
(d) ; ; ; . Answer 4. (2)
Question 4 (12)
(a) Greedy rule: Shortest Processing Time first (SPT) — sort jobs by ascending. (2)
(b) Exchange argument. Consider any optimal schedule; suppose two adjacent jobs (in position ) then (position ) are out of order, i.e., . Let = total processing time of all jobs before position . Completion times of and :
- Original: , . Their sum .
- Swapped ( then ): , . Sum .
All other jobs' completion times unchanged (jobs before position unaffected; jobs after position see the same total preceding them). Difference in objective: 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 : . Completion times: . . (2)
Question 5 (12)
(a) Table (rows = AXBY prefixes, cols = ABCY prefixes), :
| "" | 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 AXBY→ABCY: substitute X→B then B→C? Two operations). Distance . (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 when ; accept only if all pass, so error . Need (since , ). (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)"}
]