Browse papers
A

Section A: Long Answer Questions

Attempt any TWO questions.

3 questions·10 marks each
1long10 marks

Define asymptotic notations (Big-O, Big-Omega, Big-Theta). State the master theorem and use it to solve the recurrences T(n)=3T(n/2)+n and T(n)=2T(n/4)+sqrt(n).

Asymptotic Notations

Asymptotic notations describe the growth rate of an algorithm's running time as input size nn \to \infty.

  • Big-O (OO)upper bound. f(n)=O(g(n))f(n) = O(g(n)) if there exist constants c>0,n0>0c>0, n_0>0 such that 0f(n)cg(n)0 \le f(n) \le c\,g(n) for all nn0n \ge n_0. (worst-case guarantee).
  • Big-Omega (Ω\Omega)lower bound. f(n)=Ω(g(n))f(n) = \Omega(g(n)) if 0cg(n)f(n)0 \le c\,g(n) \le f(n) for all nn0n \ge n_0.
  • Big-Theta (Θ\Theta)tight bound. f(n)=Θ(g(n))f(n) = \Theta(g(n)) if 0c1g(n)f(n)c2g(n)0 \le c_1 g(n) \le f(n) \le c_2 g(n) for all nn0n \ge n_0; i.e. ff is both O(g)O(g) and Ω(g)\Omega(g).

Master Theorem

For a recurrence of the form T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n) with a1,b>1a \ge 1, b > 1, compare f(n)f(n) with nlogban^{\log_b a}:

  1. If f(n)=O(nlogbaϵ)f(n) = O(n^{\log_b a - \epsilon}) for some ϵ>0\epsilon > 0, then T(n)=Θ(nlogba)T(n) = \Theta(n^{\log_b a}).
  2. If f(n)=Θ(nlogba)f(n) = \Theta(n^{\log_b a}), then T(n)=Θ(nlogbalogn)T(n) = \Theta(n^{\log_b a}\log n).
  3. If f(n)=Ω(nlogba+ϵ)f(n) = \Omega(n^{\log_b a + \epsilon}) and the regularity condition af(n/b)cf(n)a\,f(n/b) \le c\,f(n) holds for c<1c<1, then T(n)=Θ(f(n))T(n) = \Theta(f(n)).

Solving the Recurrences

(i) T(n)=3T(n/2)+nT(n) = 3T(n/2) + n

Here a=3,b=2,f(n)=na = 3, b = 2, f(n) = n. Compute nlogba=nlog23n1.585n^{\log_b a} = n^{\log_2 3} \approx n^{1.585}.

Since f(n)=n=O(nlog23ϵ)f(n) = n = O(n^{\log_2 3 - \epsilon}) (because 1<1.5851 < 1.585), Case 1 applies:

T(n)=Θ(nlog23)Θ(n1.585).T(n) = \Theta(n^{\log_2 3}) \approx \Theta(n^{1.585}).

(ii) T(n)=2T(n/4)+nT(n) = 2T(n/4) + \sqrt{n}

Here a=2,b=4,f(n)=n=n1/2a = 2, b = 4, f(n) = \sqrt{n} = n^{1/2}. Compute nlogba=nlog42=n1/2n^{\log_b a} = n^{\log_4 2} = n^{1/2}.

Since f(n)=Θ(n1/2)=Θ(nlog42)f(n) = \Theta(n^{1/2}) = \Theta(n^{\log_4 2}), Case 2 applies:

T(n)=Θ(n1/2logn)=Θ(nlogn).T(n) = \Theta(n^{1/2}\log n) = \Theta(\sqrt{n}\,\log n).
asymptoticrecurrence
2long10 marks

Explain the complexity classes P, NP, NP-Hard and NP-Complete with examples. Discuss how a problem is proved to be NP-Complete using polynomial-time reduction.

Complexity Classes

Class P (Polynomial time) — Set of decision problems solvable by a deterministic algorithm in polynomial time O(nk)O(n^k). These are the tractable problems. Examples: sorting, shortest path (Dijkstra), matrix multiplication, primality testing.

Class NP (Nondeterministic Polynomial time) — Set of decision problems whose yes-answer (a certificate/witness) can be verified in polynomial time by a deterministic algorithm. Equivalently, solvable in polynomial time by a nondeterministic machine. Examples: SAT, Hamiltonian cycle, subset-sum, vertex cover. Note PNPP \subseteq NP; whether P=NPP = NP is open.

NP-Hard — A problem HH is NP-Hard if every problem in NP reduces to it in polynomial time. NP-Hard problems are "at least as hard as the hardest problems in NP" but need not themselves be in NP (may not even be decision problems). Example: the optimization version of the Travelling Salesman Problem, Halting problem.

NP-Complete — A problem is NP-Complete if it is (i) in NP and (ii) NP-Hard. These are the hardest problems in NP. If any one NP-Complete problem is solved in polynomial time, then P=NPP = NP. Examples: CNF-SAT (Cook–Levin), 3-SAT, clique, vertex cover, Hamiltonian cycle.

![Relationship](Diagram in words: P is a subset of NP; NP-Complete is the intersection of NP and NP-Hard; NP-Hard extends beyond NP. Assuming P != NP, P and NP-Complete are disjoint regions inside NP.)

Proving a Problem NP-Complete by Polynomial-Time Reduction

To prove that a problem XX is NP-Complete:

  1. Show XNPX \in NP — give a polynomial-time verifier: a certificate that can be checked in poly time.
  2. Show XX is NP-Hard — pick a known NP-Complete problem YY and give a polynomial-time reduction YpXY \le_p X, i.e. a poly-time function ff that maps every instance yy of YY to an instance f(y)f(y) of XX such that
yY    f(y)X.y \in Y \iff f(y) \in X.

Because YY is NP-Complete, every problem in NP reduces to YY, hence (by transitivity of reduction) to XX, so XX is NP-Hard. Together with step 1, XX is NP-Complete.

Example: 3-SAT p\le_p Clique is used to prove Clique is NP-Complete.

np-completenesscomplexity-classes
3long10 marks

Write a dynamic programming algorithm for the 0/1 knapsack problem and solve it for items with weights {2, 3, 4, 5} and values {3, 4, 5, 6} with knapsack capacity W = 5.

0/1 Knapsack — Dynamic Programming

Given nn items with weights wiw_i and values viv_i, and capacity WW, choose a subset (each item taken fully or not at all) maximizing total value without exceeding WW.

Let K[i][j]K[i][j] = maximum value using first ii items with capacity jj.

K[i][j]={0i=0 or j=0K[i1][j]wi>jmax(K[i1][j],  vi+K[i1][jwi])wijK[i][j] = \begin{cases} 0 & i=0 \text{ or } j=0 \\ K[i-1][j] & w_i > j \\ \max\big(K[i-1][j],\; v_i + K[i-1][j-w_i]\big) & w_i \le j \end{cases}
Knapsack01(w, v, n, W):
  for j = 0 to W: K[0][j] = 0
  for i = 1 to n:
      for j = 0 to W:
          if w[i] > j: K[i][j] = K[i-1][j]
          else: K[i][j] = max(K[i-1][j], v[i] + K[i-1][j - w[i]])
  return K[n][W]

Time complexity: O(nW)O(nW) (pseudo-polynomial).

Solving for weights {2,3,4,5}, values {3,4,5,6}, W = 5

Items (1-indexed): item1 (w=2,v=3), item2 (w=3,v=4), item3 (w=4,v=5), item4 (w=5,v=6).

i\j012345
0000000
1 (2,3)003333
2 (3,4)003447
3 (4,5)003457
4 (5,6)003457

Key cell K[2][5]=max(K[1][5],4+K[1][2])=max(3,4+3)=7K[2][5] = \max(K[1][5],\, 4 + K[1][2]) = \max(3,\, 4+3) = 7.

Maximum value = K[4][5]=7K[4][5] = 7, achieved by selecting item 1 (w=2, v=3) and item 2 (w=3, v=4), total weight 2+3=5W2+3 = 5 \le W.

dynamic-programmingknapsack
B

Section B: Short Answer Questions

Attempt any EIGHT questions.

9 questions·5 marks each
4short5 marks

Differentiate between Breadth First Search (BFS) and Depth First Search (DFS) with examples and their time complexities.

BFS vs DFS

AspectBFSDFS
Traversal orderLevel by level (nearest first)As deep as possible before backtracking
Data structureQueue (FIFO)Stack (or recursion)
FindsShortest path (in edges) on unweighted graphsPath existence; topological sort, cycle detection, SCC
SpaceO(V)O(V) (can hold a whole level)O(V)O(V) (recursion/stack depth)
CompletenessVisits closest vertices firstExplores one branch fully first

Time complexity (both): O(V+E)O(V + E) with adjacency list; O(V2)O(V^2) with adjacency matrix.

Example — graph with edges A–B, A–C, B–D, C–D, starting at A:

  • BFS order: A, B, C, D (expands A's neighbours first, then theirs).
  • DFS order: A, B, D, C (goes A→B→D, backtracks, then C).

Applications: BFS — shortest path in unweighted graphs, peer-to-peer broadcasting; DFS — topological sorting, detecting cycles, finding connected/strongly-connected components.

graphbfs-dfs
5short5 marks

Solve the recurrence relation T(n) = 2T(n/2) + n using the substitution method and the recursion tree method.

Solving T(n)=2T(n/2)+nT(n) = 2T(n/2) + n

Recursion Tree Method

At each level the work splits into 2 subproblems of half the size; the non-recursive cost at the root is nn.

  • Level 0: cost nn.
  • Level 1: 2(n/2)=n2 \cdot (n/2) = n.
  • Level 2: 4(n/4)=n4 \cdot (n/4) = n.
  • Level ii: 2i(n/2i)=n2^i \cdot (n/2^i) = n.

The tree has height log2n\log_2 n (since size halves until it reaches 1), so there are log2n+1\log_2 n + 1 levels, each contributing nn work:

T(n)=n(log2n+1)=Θ(nlogn).T(n) = n\,(\log_2 n + 1) = \Theta(n \log n).

Substitution Method

Guess: T(n)=O(nlogn)T(n) = O(n \log n), i.e. T(n)cnlognT(n) \le c\,n \log n for suitable cc and large nn.

Inductive step: assume it holds for n/2n/2:

T(n)=2T(n/2)+n2(cn2logn2)+nT(n) = 2T(n/2) + n \le 2\left(c \cdot \tfrac{n}{2}\log\tfrac{n}{2}\right) + n =cn(logn1)+n=cnlogncn+n.= c\,n(\log n - 1) + n = c\,n\log n - c\,n + n.

This is cnlogn\le c\,n\log n provided cn+n0-cn + n \le 0, i.e. c1c \ge 1. Hence the guess holds, and

T(n)=Θ(nlogn).\boxed{T(n) = \Theta(n\log n)}.

(This is exactly the merge-sort recurrence.)

recurrencesubstitution
6short5 marks

Explain the job sequencing with deadlines problem and solve it using the greedy approach for a given set of jobs.

Job Sequencing with Deadlines

Problem: Given nn jobs, each with a deadline did_i and profit pip_i, where each job takes one unit of time and only one job runs per unit, schedule a subset of jobs to maximize total profit such that each scheduled job finishes within its deadline.

Greedy Strategy

  1. Sort jobs in decreasing order of profit.
  2. Maintain a time-slot array of size = max deadline, initially all free.
  3. For each job (highest profit first), place it in the latest free slot \le its deadline. If no such slot is free, discard the job.
JobSequencing(jobs):
  sort jobs by profit descending
  maxDeadline = max(d_i); slot[1..maxDeadline] = free
  for each job j:
      for t = min(maxDeadline, d_j) downto 1:
          if slot[t] is free: assign j to t; break

Time complexity: O(n2)O(n^2) (or O(nlogn)O(n \log n) with a disjoint-set / priority-queue optimization).

Example

JobABCDE
Deadline21213
Profit60100204020

Sort by profit: B(100), A(60), D(40), C(20), E(20).

  • B (d=1): slot 1 → B.
  • A (d=2): slot 2 free → A.
  • D (d=1): slot 1 taken → discard.
  • C (d=2): slots 1,2 taken → discard.
  • E (d=3): slot 3 free → E.

Schedule: slot1=B, slot2=A, slot3=E → jobs {B, A, E}, maximum profit = 100 + 60 + 20 = 180.

greedyjob-scheduling
7short5 marks

Explain the naive string matching algorithm and the Rabin-Karp algorithm with their time complexities.

Naive String Matching

Slide the pattern PP of length mm over text TT of length nn; at each of the nm+1n-m+1 positions, compare characters one by one.

NaiveMatch(T, P):
  n = len(T); m = len(P)
  for s = 0 to n - m:
      if T[s+1 .. s+m] == P[1 .. m]:
          report match at shift s
  • Time complexity: worst case O((nm+1)m)=O(nm)O((n-m+1)\,m) = O(nm) (e.g. T=aaaa...T = aaaa..., P=aaabP = aaab); best case O(n)O(n).
  • No preprocessing, O(1)O(1) extra space.

Rabin–Karp Algorithm

Uses a rolling hash to compare the pattern's hash with the hash of each text window in O(1)O(1) per shift; only on a hash match are characters compared to confirm (handle spurious hits / collisions).

Hash of window using base dd and prime modulus qq:

hs+1=(d(hsT[s+1]dm1)+T[s+m+1])modq.h_{s+1} = \big(d\,(h_s - T[s+1]\,d^{m-1}) + T[s+m+1]\big) \bmod q.
RabinKarp(T, P, d, q):
  compute hp = hash(P), ht = hash(T[1..m])
  for s = 0 to n - m:
      if hp == ht and T[s+1..s+m] == P: report match
      update ht to next window using rolling hash
  • Time complexity: average / best case O(n+m)O(n + m); worst case O(nm)O(nm) when many spurious hash collisions occur.
  • Preprocessing O(m)O(m); useful for multiple-pattern and 2-D matching.

Summary: Rabin–Karp matches naive's worst case but is typically much faster on average due to constant-time hash comparison.

string-matching
8short5 marks

Write an algorithm for binary search using divide and conquer. Analyze its best-case, worst-case and average-case time complexity.

Binary Search (Divide and Conquer)

Precondition: array AA is sorted. Repeatedly compare the key with the middle element and discard half the array.

BinarySearch(A, low, high, key):
  if low > high: return -1          // not found
  mid = (low + high) / 2
  if A[mid] == key: return mid       // found
  else if key < A[mid]:
      return BinarySearch(A, low, mid-1, key)
  else:
      return BinarySearch(A, mid+1, high, key)

Recurrence: T(n)=T(n/2)+O(1)T(n) = T(n/2) + O(1), which by the master theorem (Case 2, a=1,b=2,f(n)=Θ(1)=Θ(nlog21)a=1, b=2, f(n)=\Theta(1)=\Theta(n^{\log_2 1})) gives T(n)=Θ(logn)T(n) = \Theta(\log n).

Complexity Analysis

  • Best case: O(1)O(1) — key is the middle element on the first comparison.
  • Worst case: O(log2n)O(\log_2 n) — key absent or at the deepest level; array is halved each step.
  • Average case: O(log2n)O(\log_2 n) — expected number of comparisons is log2n1\approx \log_2 n - 1.

Space: O(logn)O(\log n) recursive (call stack); O(1)O(1) iterative.

binary-search
9short5 marks

Explain the randomized quicksort algorithm and analyze its expected time complexity.

Randomized Quicksort

Quicksort partitions the array around a pivot so that elements << pivot lie left and elements >> pivot lie right, then recurses on each side. In randomized quicksort the pivot is chosen uniformly at random (or the array is randomly shuffled), which removes worst-case dependence on the input order.

RandomizedQuicksort(A, p, r):
  if p < r:
      i = Random(p, r)            // random pivot index
      swap A[i] with A[r]
      q = Partition(A, p, r)      // Lomuto/Hoare partition
      RandomizedQuicksort(A, p, q-1)
      RandomizedQuicksort(A, q+1, r)

Expected Time Complexity

Let XX be the total number of comparisons. For sorted elements zi,zjz_i, z_j, let Xij=1X_{ij}=1 if they are ever compared. They are compared iff one of them is the first pivot chosen from the set {zi,,zj}\{z_i, \dots, z_j\}, so

Pr[Xij=1]=2ji+1.\Pr[X_{ij}=1] = \frac{2}{j-i+1}.

Then the expected number of comparisons is

E[X]=i<j2ji+1=i=1n1k=1ni2k+12nk=1n1k=2nHn=O(nlogn).E[X] = \sum_{i<j} \frac{2}{j-i+1} = \sum_{i=1}^{n-1}\sum_{k=1}^{n-i}\frac{2}{k+1} \le 2n\sum_{k=1}^{n}\frac{1}{k} = 2n\,H_n = O(n\log n).

Expected running time =O(nlogn)= O(n\log n), independent of input order. The worst case is still O(n2)O(n^2) (all random pivots extreme), but it occurs with vanishingly small probability. Average extra space O(logn)O(\log n) for the recursion stack.

quicksort
10short5 marks

Write a greedy algorithm for the fractional knapsack problem and analyze its time complexity.

Fractional Knapsack — Greedy Algorithm

Unlike 0/1 knapsack, here a fraction of an item may be taken. The greedy choice of taking items in decreasing value-to-weight ratio vi/wiv_i / w_i is optimal.

FractionalKnapsack(items, W):
  for each item: ratio_i = v_i / w_i
  sort items by ratio descending
  totalValue = 0; cap = W
  for each item i (in sorted order):
      if w_i <= cap:
          take whole item; totalValue += v_i; cap -= w_i
      else:
          take fraction cap/w_i; totalValue += v_i * (cap / w_i); break
  return totalValue

Time Complexity

  • Computing ratios: O(n)O(n).
  • Sorting by ratio: O(nlogn)O(n \log n) — dominant term.
  • Greedy scan: O(n)O(n).

Total: Θ(nlogn)\Theta(n \log n).

Why greedy is optimal: taking the highest density first always fills capacity with the most value per unit weight; since items are divisible, the knapsack is filled exactly to capacity WW and an exchange argument shows no other selection can do better.

greedyknapsack
11short5 marks

Find the edit distance between the strings "ARTIFICIAL" and "NATURAL" using dynamic programming.

Edit Distance: "ARTIFICIAL" → "NATURAL"

Edit (Levenshtein) distance = minimum insertions, deletions, substitutions to convert one string to another. With XX of length mm, YY of length nn:

D[i][j]={ij=0ji=0D[i1][j1]Xi=Yj1+min(D[i1][j],D[i][j1],D[i1][j1])XiYjD[i][j] = \begin{cases} i & j=0 \\ j & i=0 \\ D[i-1][j-1] & X_i = Y_j \\ 1 + \min(D[i-1][j],\, D[i][j-1],\, D[i-1][j-1]) & X_i \ne Y_j \end{cases}

Let X=X = ARTIFICIAL (m=10m=10), Y=Y = NATURAL (n=7n=7).

Filling the 11×811 \times 8 DP table (rows = ARTIFICIAL, cols = NATURAL):

""NATURAL
""01234567
A11123456
R22223345
T33323445
I44433455
F55544456
I66655556
C77766666
I88877777
A99888878
L1010999987

Edit distance = D[10][7]=7D[10][7] = 7.

The two strings share the subsequence A, T, R, A, L; aligning these and editing the rest costs 7 operations. Time complexity: O(mn)O(mn).

dynamic-programmingedit-distance
12short5 marks

Write Dijkstra's algorithm to find single-source shortest paths and explain it with an example.

Dijkstra's Single-Source Shortest Path Algorithm

Finds shortest paths from a source ss to all vertices in a graph with non-negative edge weights, using a greedy approach with a min-priority queue.

Dijkstra(G, w, s):
  for each vertex v: dist[v] = INF; prev[v] = NIL
  dist[s] = 0
  Q = all vertices keyed by dist        // min-priority queue
  while Q not empty:
      u = ExtractMin(Q)                  // closest unsettled vertex
      for each edge (u, v) with weight w(u,v):
          if dist[u] + w(u,v) < dist[v]: // relaxation
              dist[v] = dist[u] + w(u,v)
              prev[v] = u
              DecreaseKey(Q, v, dist[v])
  return dist, prev

Time complexity: O((V+E)logV)O((V+E)\log V) with a binary-heap priority queue; O(V2)O(V^2) with an array.

Example

Graph: A→B (4), A→C (1), C→B (2), B→D (1), C→D (5). Source = A.

  1. Start: dist[A]=0, others \infty. Extract A; relax: dist[C]=1, dist[B]=4.
  2. Extract C (1); relax C→B: 1+2=3<41+2=3 < 4 ⇒ dist[B]=3; C→D: dist[D]=6.
  3. Extract B (3); relax B→D: 3+1=4<63+1=4 < 6 ⇒ dist[D]=4.
  4. Extract D (4); done.

Final shortest distances from A: A=0, C=1, B=3, D=4. Shortest path to D: A→C→B→D.

Note: Dijkstra fails with negative edge weights; use Bellman–Ford in that case.

shortest-pathdijkstra

Frequently asked questions

Where can I find the BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) question paper 2080?
The full BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2080 (regular) question paper is available free on Kekkei. You can read every question online and attempt the paper under timed exam conditions.
Does the Design and Analysis of Algorithms (BSc CSIT, CSC314) 2080 paper come with solutions?
Yes. Every question on this Design and Analysis of Algorithms (BSc CSIT, CSC314) past paper includes a step-by-step solution, plus instant AI feedback when you attempt it on Kekkei.
How many marks is the BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2080 paper?
The BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2080 paper carries 60 full marks and is meant to be completed in 180 minutes, across 12 questions.
Is practising this Design and Analysis of Algorithms (BSc CSIT, CSC314) past paper free?
Yes — reading and attempting this Design and Analysis of Algorithms (BSc CSIT, CSC314) past paper on Kekkei is completely free.