Browse papers
A

Section A: Long Answer Questions

Attempt any TWO questions.

3 questions·10 marks each
1long10 marks

Define minimum spanning tree. Write Kruskal's algorithm to find the MST of a connected weighted graph, illustrate it with an example and analyze its complexity.

Minimum Spanning Tree (MST)

Given a connected, undirected, weighted graph G=(V,E)G=(V,E), a spanning tree is a subgraph that is a tree connecting all V|V| vertices using exactly V1|V|-1 edges. A Minimum Spanning Tree is a spanning tree whose total edge weight eTw(e)\sum_{e\in T} w(e) is minimum among all spanning trees.

Kruskal's Algorithm

Kruskal's algorithm is a greedy algorithm. It repeatedly picks the smallest-weight edge that does not form a cycle, using a disjoint-set (Union-Find) structure to detect cycles.

KRUSKAL(G):
    A = {}                      // edges of MST
    for each vertex v in V: MAKE-SET(v)
    sort edges of E in non-decreasing order of weight
    for each edge (u,v) in sorted order:
        if FIND-SET(u) != FIND-SET(v):   // adding edge does not form a cycle
            A = A union {(u,v)}
            UNION(u, v)
    return A

Example

Consider a graph with vertices {A,B,C,D,E}\{A,B,C,D,E\} and edges: AB=1, BC=2, AC=3, CD=4, BD=5, DE=6, CE=7AB=1,\ BC=2,\ AC=3,\ CD=4,\ BD=5,\ DE=6,\ CE=7.

Process edges in increasing order:

EdgeWeightAction
AB1Add (no cycle)
BC2Add (no cycle)
AC3Reject (A,C already connected)
CD4Add (no cycle)
BD5Reject (B,D connected)
DE6Add (no cycle)

MST edges = {AB,BC,CD,DE}\{AB, BC, CD, DE\}, total weight =1+2+4+6=13=1+2+4+6=13, with V1=4|V|-1=4 edges.

Complexity Analysis

  • Sorting the EE edges: O(ElogE)O(E\log E).
  • VV MAKE-SET and at most 2E2E FIND/UNION operations with union by rank + path compression: O(Eα(V))O(E\,\alpha(V)), nearly linear.
  • Total: O(ElogE)=O(ElogV)O(E\log E)=O(E\log V) (since EV2E\le V^2, logE=O(logV)\log E=O(\log V)).

Correctness follows from the greedy cut property: the lightest edge crossing any cut is safe to add to the MST.

graphmst
2long10 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

Let f(n),g(n)f(n),g(n) be non-negative functions.

  • Big-O (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 growth)
  • Big-Omega (lower bound): f(n)=Ω(g(n))f(n)=\Omega(g(n)) if c>0,n0>0\exists c>0,n_0>0 with 0cg(n)f(n)0\le c\,g(n)\le f(n) for all nn0n\ge n_0. (best-case / lower growth)
  • Big-Theta (tight bound): f(n)=Θ(g(n))f(n)=\Theta(g(n)) if f(n)=O(g(n))f(n)=O(g(n)) and f(n)=Ω(g(n))f(n)=\Omega(g(n)), i.e. c1,c2,n0\exists c_1,c_2,n_0 with c1g(n)f(n)c2g(n)c_1 g(n)\le f(n)\le c_2 g(n).

Master Theorem

For a recurrence T(n)=aT(n/b)+f(n)T(n)=a\,T(n/b)+f(n) with a1, b>1a\ge 1,\ b>1, let c=logbac=\log_b a. Compare f(n)f(n) with ncn^{c}:

  1. If f(n)=O(ncε)f(n)=O(n^{c-\varepsilon}) for some ε>0\varepsilon>0: T(n)=Θ(nc)T(n)=\Theta(n^{c}).
  2. If f(n)=Θ(nc)f(n)=\Theta(n^{c}): T(n)=Θ(nclogn)T(n)=\Theta(n^{c}\log n).
  3. If f(n)=Ω(nc+ε)f(n)=\Omega(n^{c+\varepsilon}) and the regularity condition af(n/b)kf(n)a f(n/b)\le k f(n) holds for k<1k<1: T(n)=Θ(f(n))T(n)=\Theta(f(n)).

(a) 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. So c=log231.585c=\log_2 3\approx 1.585.

Since n=O(ncε)n = O(n^{c-\varepsilon}) (because 1<1.5851<1.585), Case 1 applies:

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

(b) 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}. So c=log42=12c=\log_4 2=\tfrac12.

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

T(n)=Θ ⁣(n1/2logn)=Θ(nlogn).T(n)=\Theta\!\left(n^{1/2}\log n\right)=\Theta(\sqrt{n}\,\log n).
asymptoticrecurrence
3long10 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

  • P (Polynomial time): Decision problems solvable by a deterministic algorithm in polynomial time O(nk)O(n^k). Examples: sorting, shortest path (Dijkstra), checking primality.
  • NP (Nondeterministic Polynomial): Decision problems whose yes-answers can be verified in polynomial time given a certificate. Equivalently, solvable by a nondeterministic machine in polynomial time. Examples: SAT, Hamiltonian cycle, subset-sum. Note PNPP\subseteq NP.
  • 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 be in NP (and need not be decision problems). Example: the optimization Travelling Salesman Problem, the Halting problem.
  • NP-Complete: A problem that is both in NP and NP-Hard. These are the hardest problems in NP. Examples: SAT (Cook–Levin theorem), 3-SAT, vertex cover, clique, Hamiltonian cycle.

Relationship: PNPP\subseteq NP, NPC=NPNP-HardNPC = NP \cap NP\text{-Hard}. Whether P=NPP=NP is open.

Proving NP-Completeness via Polynomial-Time Reduction

To prove a problem XX is NP-Complete:

  1. Show XNPX\in NP: give a polynomial-time verifier that checks a proposed certificate (solution).
  2. Show XX is NP-Hard: pick a known NP-Complete problem YY and construct a polynomial-time reduction YpXY\le_p X — a transformation that maps any instance yy of YY to an instance f(y)f(y) of XX in polynomial time such that
y is a YES-instance of Y    f(y) is a YES-instance of X.y \text{ is a YES-instance of } Y \iff f(y)\text{ is a YES-instance of } X.
  1. Since YY is NP-Complete, every NP problem reduces to YY and hence (by transitivity of reductions) to XX. Therefore XX is NP-Hard, and combined with step 1, XX is NP-Complete.

Example: 3-SAT p\le_p Clique shows Clique is NP-Hard; together with Clique NP\in NP this proves Clique is NP-Complete.

np-completenesscomplexity-classes
B

Section B: Short Answer Questions

Attempt any EIGHT questions.

9 questions·5 marks each
4short5 marks

What are approximation algorithms? Explain the approximation algorithm for the vertex cover problem.

Approximation Algorithms

For NP-Hard optimization problems exact optimal solutions cannot (assuming PNPP\ne NP) be found in polynomial time. An approximation algorithm runs in polynomial time and returns a solution provably close to optimal. For a minimization problem it has approximation ratio ρ\rho if for every input CCρ\frac{C}{C^*}\le \rho, where CC is the cost returned and CC^* the optimal cost.

Approximation Algorithm for Vertex Cover

A vertex cover is a set of vertices covering every edge. Finding a minimum vertex cover is NP-Hard. A simple 2-approximation:

APPROX-VERTEX-COVER(G):
    C = {}
    E' = edges of G
    while E' is not empty:
        pick any edge (u, v) from E'
        C = C union {u, v}
        remove from E' all edges incident to u or v
    return C

Why ratio 2: The edges picked in the loop form a matching MM (no two share a vertex). Any vertex cover must include at least one endpoint of each matched edge, so CM|C^*|\ge |M|. The algorithm adds both endpoints, so C=2M2C|C|=2|M|\le 2|C^*|. Hence it is a 2-approximation, running in O(V+E)O(V+E) time.

approximation
5short5 marks

Explain the branch and bound technique. How is it used to solve the travelling salesman problem?

Branch and Bound

Branch and Bound (B&B) is a systematic technique for solving optimization problems by exploring a state-space tree. It has two key operations:

  • Branching: split a problem into smaller subproblems (children nodes).
  • Bounding: compute a bound (lower bound for minimization) on the best solution obtainable in each subtree. If a node's bound is worse than the best solution found so far (the incumbent), the whole subtree is pruned, avoiding unnecessary exploration.

Unlike pure backtracking, B&B uses bounds to prune, and can use best-first / least-cost selection of the next node to expand.

Solving the Travelling Salesman Problem (TSP)

TSP asks for a minimum-cost tour visiting every city exactly once and returning to start.

  1. State-space tree: each node represents a partial path; branching extends the path to an unvisited city.
  2. Cost / lower bound: for each node compute a lower bound, commonly using the reduced cost matrix — reduce each row and column by its minimum; the sum of reductions plus the parent bound gives the node's lower bound on the completed tour cost.
  3. Search: expand the live node with the smallest lower bound (least-cost B&B). Maintain the best complete tour found (incumbent / upper bound).
  4. Pruning: discard any node whose lower bound \ge current best tour cost.
  5. Continue until the least-cost live node is a complete tour; that tour is optimal.

B&B does not improve worst-case complexity (O(n!)O(n!) in the worst case) but in practice prunes large portions of the search space, making moderate instances tractable.

branch-and-bound
6short5 marks

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

BFS vs DFS

Both traverse a graph G=(V,E)G=(V,E) visiting every reachable vertex once.

  • BFS explores level by level from a source, using a queue (FIFO). It first visits all neighbours, then their neighbours, finding shortest paths in unweighted graphs.
  • DFS explores as deep as possible along a branch before backtracking, using a stack (LIFO) or recursion. Used for topological sort, cycle detection, connected components.
FeatureBFSDFS
Data structureQueue (FIFO)Stack / recursion (LIFO)
StrategyLevel by levelDeep then backtrack
Shortest path (unweighted)YesNo
SpaceO(V)O(V) (queue holds a level)O(V)O(V) (recursion stack)
ApplicationsShortest path, level orderTopological sort, cycle detection, SCC

Example

Graph: AABB, AACC, BBDD, CCDD. Starting at AA:

  • BFS order: A,B,C,DA, B, C, D (visit AA's neighbours B,CB,C first, then DD).
  • DFS order: A,B,D,CA, B, D, C (go deep ABDA\to B\to D, backtrack, then CC).

Time Complexity

Using an adjacency list, both run in O(V+E)O(V+E). With an adjacency matrix both are O(V2)O(V^2).

graphbfs-dfs
7short5 marks

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

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

This is the merge-sort recurrence. We show T(n)=Θ(nlogn)T(n)=\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 some c>0c>0.

Assume it holds for n/2n/2: T(n/2)cn2logn2T(n/2)\le c\,\tfrac{n}{2}\log\tfrac{n}{2}. Then

T(n)2(cn2logn2)+n=cn(logn1)+n=cnlogncn+n.T(n)\le 2\Big(c\tfrac{n}{2}\log\tfrac{n}{2}\Big)+n = c\,n(\log n-1)+n = c\,n\log n - cn + n.

This is cnlogn\le c\,n\log n provided cn+n0-cn+n\le 0, i.e. c1c\ge 1. So the guess holds and T(n)=O(nlogn)T(n)=O(n\log n). A symmetric argument gives Ω(nlogn)\Omega(n\log n), hence T(n)=Θ(nlogn)T(n)=\Theta(n\log n).

Recursion Tree Method

At the root the cost is nn. It splits into 2 subproblems of size n/2n/2, each costing n/2n/2, so level-1 cost =2n2=n=2\cdot\tfrac{n}{2}=n. In general at level ii there are 2i2^i nodes each of size n/2in/2^i, contributing 2in2i=n2^i\cdot\tfrac{n}{2^i}=n.

  • Cost per level: nn (constant across levels).
  • Number of levels: the size shrinks to 1 after log2n\log_2 n splits, so there are log2n+1\log_2 n + 1 levels.

Total cost =n×(log2n+1)=Θ(nlogn)=n\times(\log_2 n+1)=\Theta(n\log n).

Result: T(n)=Θ(nlogn)T(n)=\Theta(n\log n) (confirmed by both methods; also matches Master Theorem Case 2 with a=b=2a=b=2).

recurrencesubstitution
8short5 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

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 at a time. A job earns its profit only if completed by its deadline. Goal: schedule a subset of jobs to maximize total profit.

Greedy Approach

  1. Sort jobs in decreasing order of profit.
  2. Find the maximum deadline DD; create DD time slots, all free.
  3. For each job (highest profit first), place it in the latest free slot \le its deadline. If no free slot exists, skip the job.
  4. The filled slots give the schedule.
JOB-SEQUENCING(jobs):
    sort jobs by profit descending
    D = max deadline; slot[1..D] = free
    for each job j:
        for t = min(D, j.deadline) down to 1:
            if slot[t] is free:
                slot[t] = j; break
    return filled slots

Example

JobABCDE
Profit20151051
Deadline22133

Sorted by profit: A,B,C,D,E. Max deadline =3=3, slots [_,_,_][\_,\_,\_].

  • A (d=2) → slot 2: [_,A,_][\_,A,\_]
  • B (d=2) → slot 1 (2 taken): [B,A,_][B,A,\_]
  • C (d=1) → slot 1 taken, skip.
  • D (d=3) → slot 3: [B,A,D][B,A,D]
  • E (d=3) → all 3\le 3 taken, skip.

Schedule: B → A → D, maximum profit =15+20+5=40=15+20+5=40.

Complexity: O(nlogn)O(n\log n) for sorting plus O(nD)O(n\cdot D) for slot assignment (improvable to near O(nlogn)O(n\log n) with a disjoint-set structure).

greedyjob-scheduling
9short5 marks

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

Naive String Matching

To find a pattern PP of length mm in text TT of length nn, the naive algorithm slides PP over every position s=0,,nms=0,\dots,n-m and checks character by character.

NAIVE-MATCH(T, P):
    n = length(T); m = length(P)
    for s = 0 to n - m:
        if T[s+1 .. s+m] == P[1 .. m]:
            report match at shift s
  • Time: O((nm+1)m)=O(nm)O((n-m+1)\,m)=O(nm) in the worst case (e.g. T=aaaaT=aaaa\ldots, P=aaabP=aaab). No preprocessing.

Rabin–Karp Algorithm

Rabin–Karp uses hashing. It computes a hash of the pattern and of each length-mm window of the text; only when hashes match does it compare characters (to rule out spurious hits).

  1. Compute hash h(P)h(P) of the pattern.
  2. Compute hash of T[0..m1]T[0..m-1].
  3. Slide the window: update the hash in O(1)O(1) using a rolling hash
hs+1=(d(hsT[s]dm1)+T[s+m])modq,h_{s+1} = \big(d\,(h_s - T[s]\,d^{\,m-1}) + T[s+m]\big) \bmod q,

where dd is the alphabet size and qq a large prime. 4. When hs=h(P)h_s = h(P), verify the actual characters to confirm.

  • Time: Preprocessing O(m)O(m); matching O(n+m)O(n+m) on average (with a good hash and few collisions), but O(nm)O(nm) in the worst case when many spurious hash collisions force full comparisons.

Summary: both report all occurrences; Rabin–Karp is typically faster in practice and naturally extends to multi-pattern and 2-D matching.

string-matching
10short5 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)

Binary search finds a key xx in a sorted array A[low..high]A[low..high] by repeatedly comparing with the middle element and discarding half the array.

BINARY-SEARCH(A, low, high, x):
    if low > high: return -1          // not found
    mid = low + (high - low) / 2
    if A[mid] == x: return mid
    else if x < A[mid]:
        return BINARY-SEARCH(A, low, mid - 1, x)   // search left half
    else:
        return BINARY-SEARCH(A, mid + 1, high, x)  // search right half

The recurrence is T(n)=T(n/2)+O(1)T(n)=T(n/2)+O(1), which by the Master Theorem (Case 2 with a=1,b=2a=1,b=2) gives T(n)=Θ(logn)T(n)=\Theta(\log n).

Complexity Analysis

  • Best case: key is the middle element on the first comparison → O(1)O(1).
  • Worst case: key absent or at the deepest level; the array halves each step, so log2n+1\lfloor\log_2 n\rfloor + 1 comparisons → O(logn)O(\log n).
  • Average case: also O(logn)O(\log n) (the search tree has depth logn\log n, so expected comparisons log2n1\approx \log_2 n - 1).
  • Space: O(logn)O(\log n) for the recursion stack, or O(1)O(1) if written iteratively.
binary-search
11short5 marks

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

Randomized Quicksort

Quicksort sorts by choosing a pivot, partitioning the array so that elements smaller than the pivot go left and larger go right, then recursively sorting the two parts. Standard quicksort degrades to O(n2)O(n^2) when the pivot is consistently the smallest/largest (e.g. already-sorted input).

Randomized quicksort avoids this by choosing the pivot uniformly at random from the subarray, so no particular input triggers worst-case behaviour.

RANDOMIZED-QUICKSORT(A, p, r):
    if p < r:
        i = RANDOM(p, r)          // random index
        swap A[i] with A[r]       // random pivot
        q = PARTITION(A, p, r)    // O(n) partition around pivot
        RANDOMIZED-QUICKSORT(A, p, q - 1)
        RANDOMIZED-QUICKSORT(A, q + 1, r)

Expected Time Complexity

Let XX be the total number of comparisons. Define indicator Xij=1X_{ij}=1 if elements of ranks ii and jj are ever compared. Two elements are compared iff one of them is chosen as pivot before any element ranked between them, which happens with probability 2ji+1\frac{2}{j-i+1}. Thus

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).

Therefore the expected running time is Θ(nlogn)\Theta(n\log n), independent of the input ordering. The worst case is still O(n2)O(n^2) but occurs only with negligibly small probability.

quicksort
12short5 marks

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

Fractional Knapsack Problem

Given nn items each with weight wiw_i and value viv_i, and a knapsack capacity WW, choose fractions xi[0,1]x_i\in[0,1] of each item to maximize xivi\sum x_i v_i subject to xiwiW\sum x_i w_i \le W. Because items can be divided, a greedy strategy by value-to-weight ratio gives the optimal solution.

Greedy Algorithm

  1. Compute the ratio vi/wiv_i/w_i for each item.
  2. Sort items in decreasing order of vi/wiv_i/w_i.
  3. Take items fully in that order until the next item does not fit; take a fraction of that item to fill the remaining capacity exactly.
FRACTIONAL-KNAPSACK(items, W):
    sort items by (v_i / w_i) descending
    total = 0; cap = W
    for each item i in sorted order:
        if w_i <= cap:
            take whole item; total += v_i; cap -= w_i
        else:
            total += v_i * (cap / w_i)   // take fraction
            break
    return total

Optimality: the exchange argument shows replacing any part of a lower-ratio item with a higher-ratio one never decreases value, so the greedy choice is optimal.

Time Complexity

  • Sorting by ratio: O(nlogn)O(n\log n).
  • Single greedy pass: O(n)O(n).
  • Total: O(nlogn)O(n\log n), dominated by sorting.
greedyknapsack

Frequently asked questions

Where can I find the BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) question paper 2079?
The full BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2079 (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) 2079 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) 2079 paper?
The BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2079 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.