Algorithm Paradigms
Level: 2 (Recall — definitions, standard textbook problems, short derivations) Time limit: 30 minutes Total marks: 40
Q1. Define optimal substructure and overlapping subproblems. Which one distinguishes dynamic programming from plain divide-and-conquer? (4 marks)
Q2. State the general divide-and-conquer recurrence for an algorithm that splits a problem of size into subproblems of size with combine cost . Then apply the Master Theorem to solve . (4 marks)
Q3. For the Fibonacci numbers, write the memoized (top-down) recurrence and state the time and space complexity of the memoized solution compared to the naive recursion. (4 marks)
Q4. Give a concrete counter-example showing that the greedy "highest value/weight ratio first" strategy fails for the 0/1 knapsack problem. Use a capacity of 10 and exactly three items, and state the greedy vs optimal values. (5 marks)
Q5. Compute the length of the Longest Common Subsequence (LCS) of the strings AGCAT and GAC. Show the final answer and name one such subsequence. (4 marks)
Q6. For the coin change (minimum coins) problem with coins and target amount , give the minimum number of coins and the tabulation recurrence used. (4 marks)
Q7. State the edit distance (Levenshtein) recurrence for transforming string into , and compute the edit distance between kitten and sitting. (5 marks)
Q8. Bit manipulation: (4 marks)
(a) Given an array where every element appears twice except one, name the trick to find the single element.
(b) Compute the number of set bits in the integer .
(c) Write the expression to isolate the lowest set bit (LSB) of an integer x.
Q9. Briefly distinguish a Las Vegas algorithm from a Monte Carlo algorithm (one line each). (3 marks)
Q10. State the exchange argument proof technique in one or two sentences and name one greedy problem whose correctness it proves. (3 marks)
End of paper.
Answer keyMark scheme & solutions
Q1. (4 marks)
- Optimal substructure: an optimal solution to the problem contains within it optimal solutions to its subproblems. (1.5)
- Overlapping subproblems: the recursive solution revisits the same subproblems many times (a small number of distinct subproblems solved repeatedly). (1.5)
- Overlapping subproblems distinguishes DP from plain divide-and-conquer (D&C subproblems are typically disjoint, so caching gives no benefit). (1)
Q2. (4 marks)
- General recurrence: . (1.5)
- For : , so . Since , we are in Master Theorem Case 2. (1.5)
- Result: . (1)
Q3. (4 marks)
- Recurrence: , base ; store computed values in a memo. (2)
- Memoized time: ; space: . (1)
- Naive recursion is (exponential) — memoization removes repeated work. (1)
Q4. (5 marks) Capacity . Items (value, weight):
- Item A: value 60, weight 10 → ratio 6
- Item B: value 100, weight 20... (must fit in 10; use valid example below)
Valid counter-example — capacity 10:
- A: v=6, w=1 (ratio 6)
- B: v=10, w=5 (ratio 2)
- C: v=10, w=5 (ratio 2)
Greedy takes A (ratio 6, w=1), then B or C (w=5), remaining capacity 4 unused for the other → value . (2) Optimal takes B+C (w=10) → value . (2) Since , greedy by ratio fails. (1) (Any correct counter-example with greedy < optimal earns full marks.)
Q5. (4 marks)
LCS of AGCAT (len 5) and GAC (len 3).
- Build DP: matches give subsequence
GAorAC. (2) - Length = 2. (1)
- One LCS:
GA(orAC). (1)
Q6. (4 marks) Coins , amount 6. Recurrence: , . (2) Compute: . (coins 3+3). (1) Minimum coins = 2. (1)
Q7. (5 marks)
Recurrence ( = prefix lengths):
with . (2)
kitten → sitting: substitute k→s, e→i, insert g at end = 3 operations. (3)
Q8. (4 marks)
(a) XOR all elements; pairs cancel, leaving the single element. (1.5)
(b) → 3 set bits. (1.5)
(c) x & (-x) (or x & (~x + 1)). (1)
Q9. (3 marks)
- Las Vegas: always returns a correct answer; running time is random. (1.5)
- Monte Carlo: runs in bounded time; answer may be wrong with some (bounded) probability. (1.5)
Q10. (3 marks)
- Exchange argument: assume an optimal solution differs from the greedy one; show you can swap an element of the optimal for a greedy choice without worsening the objective, transforming optimal into greedy step by step — hence greedy is also optimal. (2)
- Example: activity selection (or fractional knapsack / Huffman). (1)
[
{"claim":"Master theorem: T(n)=2T(n/2)+n is Theta(n log n) — check f(n)=n^(log_b a)","code":"a,b=2,2; import sympy; expo=sympy.log(a,b); result = (sympy.simplify(expo)==1)"},
{"claim":"Min coins for amount 6 with coins {1,3,4} is 2","code":"coins=[1,3,4]; N=6; INF=float('inf'); dp=[0]+[INF]*N\nfor x in range(1,N+1):\n for c in coins:\n if c<=x: dp[x]=min(dp[x], dp[x-c]+1)\nresult = (dp[6]==2)"},
{"claim":"LCS length of AGCAT and GAC is 2","code":"A='AGCAT'; B='GAC'; m,n=len(A),len(B)\nd=[[0]*(n+1) for _ in range(m+1)]\nfor i in range(1,m+1):\n for j in range(1,n+1):\n d[i][j]=d[i-1][j-1]+1 if A[i-1]==B[j-1] else max(d[i-1][j],d[i][j-1])\nresult = (d[m][n]==2)"},
{"claim":"Edit distance kitten -> sitting is 3","code":"A='kitten'; B='sitting'; m,n=len(A),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 D[i][j]=D[i-1][j-1] if A[i-1]==B[j-1] else 1+min(D[i-1][j],D[i][j-1],D[i-1][j-1])\nresult = (D[m][n]==3)"},
{"claim":"Number of set bits in 13 is 3","code":"result = (bin(13).count('1')==3)"}
]