Browse papers
A

Section A: Long Answer Questions

Attempt any TWO questions.

3 questions·10 marks each
1long10 marks

What is dynamic programming? Explain Floyd-Warshall algorithm to compute all-pairs shortest paths in a weighted graph with a suitable example and analyze its time complexity.

Dynamic Programming

Dynamic Programming (DP) is an algorithm-design technique for solving problems that exhibit:

  1. Optimal substructure — an optimal solution is built from optimal solutions of subproblems.
  2. Overlapping subproblems — the same subproblems recur many times.

DP solves each subproblem once, stores its result in a table (memoization / tabulation), and reuses it, avoiding the exponential blow-up of plain recursion.

Floyd-Warshall Algorithm (All-Pairs Shortest Paths)

Given a weighted graph with nn vertices, Floyd-Warshall finds the shortest distance between every pair of vertices. It allows negative edges (but no negative cycles).

Idea: Let dij(k)d_{ij}^{(k)} = shortest distance from ii to jj using only intermediate vertices from {1,2,,k}\{1,2,\dots,k\}. The recurrence is:

dij(k)=min(dij(k1),  dik(k1)+dkj(k1))d_{ij}^{(k)} = \min\big(d_{ij}^{(k-1)},\; d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\big)

with base case dij(0)=w(i,j)d_{ij}^{(0)} = w(i,j) (edge weight, \infty if no edge, 00 if i=ji=j).

FLOYD-WARSHALL(W, n):
    D = W                       // copy weight matrix
    for k = 1 to n:
        for i = 1 to n:
            for j = 1 to n:
                if D[i][k] + D[k][j] < D[i][j]:
                    D[i][j] = D[i][k] + D[k][j]
    return D

Example

Consider 3 vertices with weight matrix (row = from, col = to, \infty = no edge):

123
108\infty
2\infty01
34\infty0
  • k = 1: no improvement through vertex 1 yet.
  • k = 2: d13=min(,d12+d23)=min(,8+1)=9d_{13} = \min(\infty, d_{12}+d_{23}) = \min(\infty, 8+1) = 9.
  • k = 3: d21=min(,d23+d31)=min(,1+4)=5d_{21} = \min(\infty, d_{23}+d_{31}) = \min(\infty, 1+4) = 5; d11d_{11} stays 0.

Final all-pairs shortest distances:

123
1089
2501
34120

Time and Space Complexity

Three nested loops over nn vertices give time complexity O(n3)O(n^3) and space complexity O(n2)O(n^2) for the distance matrix.

dynamic-programmingshortest-path
2long10 marks

Explain the greedy method of algorithm design. Generate the prefix code for the string "CYBER CRIME" using Huffman algorithm and find the total number of bits required to encode it.

Greedy Method

The greedy method builds a solution piece by piece, at every step choosing the option that looks best right now (the locally optimal / greedy choice), never reconsidering it. It yields a globally optimal solution when the problem has the greedy-choice property and optimal substructure (e.g. Huffman coding, fractional knapsack, Dijkstra, MST). It is simple and fast but does not work for every optimization problem.

Huffman Coding of "CYBER CRIME"

Step 1 — Frequencies (string length = 11, 8 distinct symbols):

SymbolCYBER(space)IM
Freq21122111

Step 2 — Build the Huffman tree. Repeatedly remove the two lowest-frequency nodes, merge them into a node whose weight is their sum, and reinsert it, until one node (root, weight 11) remains. Merging the five weight-1 symbols and three weight-2 symbols produces a nearly balanced tree of depth 3.

Step 3 — Assign codes (left = 0, right = 1). One valid prefix-free code set:

SymbolFreqCodeBits = Freq × len
C21016
E21106
R21116
Y10003
B10013
space10103
I10113
M11003

(Exact bit-strings may differ between valid Huffman trees, but the optimal lengths and total are the same.)

Step 4 — Total bits required:

Total=3(3)2+531  =  (2+2+2)3+(1+1+1+1+1)3=18+15=33 bits\text{Total} = 3(3) \cdot 2 + 5 \cdot 3 \cdot 1 \;=\; (2+2+2)\cdot3 + (1+1+1+1+1)\cdot3 = 18 + 15 = \boxed{33 \text{ bits}}

Each symbol turns out to use 3 bits because the frequencies are nearly uniform, so Huffman gives the same 33 bits as a fixed 3-bit code here. The codes are prefix-free (no code is a prefix of another), allowing unambiguous decoding.

greedyhuffman
3long10 marks

What is backtracking and how does it differ from recursion? Solve the 4-Queens problem using backtracking and analyze its time complexity.

Backtracking

Backtracking is a systematic, depth-first search of the solution space that builds candidates incrementally and abandons a partial candidate ("backtracks") as soon as it determines the candidate cannot be extended to a valid complete solution (a bounding / feasibility check). It is used for constraint-satisfaction problems such as N-Queens, graph colouring, and subset-sum.

Backtracking vs. Recursion

RecursionBacktracking
A technique where a function calls itself to solve subproblems.A problem-solving strategy that uses recursion plus a bounding/pruning step.
Explores all paths fully.Prunes infeasible paths early using constraints.
No notion of "undoing" a choice.Explicitly undoes a choice and tries the next option.

In short, backtracking is recursion plus pruning and state-undoing.

4-Queens Problem

Place 4 queens on a 4×4 board so that no two share a row, column, or diagonal. We place one queen per row and try columns left-to-right, pruning placements that conflict with already-placed queens.

Search (showing one successful line):

  • Row 1: place Q at col 1.
  • Row 2: cols 1,2 conflict (col/diagonal); col 3 is safe → place at col 3.
  • Row 3: cols 1,2,3,4 all conflict → backtrack to row 2.
  • Row 2: try col 4 → safe.
  • Row 3: col 2 is safe → place at col 2.
  • Row 4: col 4 attacks diagonally; col... only col... none with start col1 → backtrack further.
  • Eventually starting Row 1 at col 2 succeeds:

Solution 1: (row, col) = (1,2), (2,4), (3,1), (4,3)

. Q . .
. . . Q
Q . . .
. . Q .

Solution 2 (mirror): (1,3), (2,1), (3,4), (4,2)

. . Q .
Q . . .
. . . Q
. Q . .

The 4-Queens problem has exactly 2 solutions.

Time Complexity

Without pruning, placing one queen per row over nn columns gives O(nn)O(n^n); with the column-permutation structure it is O(n!)O(n!). For n=4n=4 the worst case is bounded by 4!=244! = 24 leaf possibilities, drastically reduced by the diagonal/column pruning. In general N-Queens backtracking runs in O(n!)O(n!) worst-case time, far better than brute force (n2n)\binom{n^2}{n}.

backtrackingn-queen
B

Section B: Short Answer Questions

Attempt any EIGHT questions.

9 questions·5 marks each
4short5 marks

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

Fractional Knapsack — Greedy Algorithm

Problem: Given nn items with values viv_i and weights wiw_i, and a knapsack of capacity WW, maximize total value where items may be taken fractionally (a fraction xi[0,1]x_i \in [0,1] of each).

Greedy idea: Take items in decreasing order of value-to-weight ratio vi/wiv_i/w_i, filling the knapsack until it is full (taking a fraction of the last item if needed). This is optimal for the fractional version.

FRACTIONAL-KNAPSACK(v[], w[], n, W):
    for i = 1 to n:
        ratio[i] = v[i] / w[i]
    sort items by ratio in decreasing order
    totalValue = 0;  remaining = W
    for i = 1 to n:
        if w[i] <= remaining:
            take whole item i
            totalValue += v[i];  remaining -= w[i]
        else:
            take fraction (remaining / w[i]) of item i
            totalValue += v[i] * (remaining / w[i])
            break        // knapsack full
    return totalValue

Example: W=20W=20; items (v,w)(v,w) = (60,10), (100,20), (120,30) with ratios 6, 5, 4. Take all of item 1 (value 60, used 10), then 10/2010/20 of item 2 (value 50) → total 110.

Time Complexity

  • Computing ratios: O(n)O(n).
  • Sorting by ratio: O(nlogn)O(n \log n) (dominant).
  • Single pass to fill: O(n)O(n).

Overall time = O(nlogn)O(n \log n), space O(1)O(1) extra.

greedyknapsack
5short5 marks

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

Edit Distance: "ARTIFICIAL" → "NATURAL"

Edit (Levenshtein) distance = minimum number of insertions, deletions, and substitutions (each cost 1) to transform one string into another. With AA="ARTIFICIAL" (m=10m=10) and BB="NATURAL" (n=7n=7):

dp[i][j]={dp[i1][j1]if Ai=Bj1+min(dp[i1][j],  dp[i][j1],  dp[i1][j1])otherwisedp[i][j] = \begin{cases} dp[i-1][j-1] & \text{if } A_i = B_j \\ 1 + \min(dp[i-1][j],\; dp[i][j-1],\; dp[i-1][j-1]) & \text{otherwise} \end{cases}

with dp[i][0]=idp[i][0]=i, dp[0][j]=jdp[0][j]=j.

DP table (rows = ARTIFICIAL, cols = NATURAL):

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

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

The value at the bottom-right corner is the answer. (Time complexity O(mn)O(mn), space O(mn)O(mn).)

dynamic-programmingedit-distance
6short5 marks

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

Dijkstra's Algorithm (Single-Source Shortest Paths)

Finds the shortest path from a source ss to every other vertex in a graph with non-negative edge weights. It is greedy: it repeatedly picks the unvisited vertex with the smallest tentative distance and relaxes its outgoing edges.

DIJKSTRA(G, w, s):
    for each vertex v: dist[v] = INFINITY
    dist[s] = 0
    Q = all vertices (min-priority queue keyed by dist)
    while Q not empty:
        u = EXTRACT-MIN(Q)          // closest unvisited vertex
        for each edge (u, v):
            if dist[u] + w(u,v) < dist[v]:   // relaxation
                dist[v] = dist[u] + w(u,v)
                parent[v] = u
    return dist, parent

Example

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

  1. dist: A=0, others ∞. Pick A. Relax: B=4, C=1.
  2. Pick C (1). Relax: B = min(4, 1+2)=3, D = 1+5 = 6.
  3. Pick B (3). Relax: D = min(6, 3+1) = 4.
  4. Pick D (4). Done.

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

Time Complexity

  • Array implementation: O(V2)O(V^2).
  • Binary-heap (priority queue): O((V+E)logV)O((V+E)\log V).

Dijkstra fails with negative edge weights (use Bellman-Ford instead).

shortest-pathdijkstra
7short5 marks

Explain the matrix chain multiplication problem and write its dynamic programming formulation.

Matrix Chain Multiplication

Problem: Given a chain of matrices A1,A2,,AnA_1, A_2, \dots, A_n where AiA_i has dimension pi1×pip_{i-1} \times p_i, find the order of multiplication (parenthesization) that minimizes the total number of scalar multiplications. Matrix multiplication is associative, so the result is the same, but the cost varies hugely with the order. Multiplying a p×qp \times q by a q×rq \times r matrix costs pqrp \cdot q \cdot r scalar multiplications.

Example of why order matters: A1(10×100)A2(100×5)A3(5×50)A_1(10\times100)\,A_2(100\times5)\,A_3(5\times50).

  • ((A1A2)A3)((A_1A_2)A_3): 101005+10550=5000+2500=750010\cdot100\cdot5 + 10\cdot5\cdot50 = 5000+2500 = 7500.
  • (A1(A2A3))(A_1(A_2A_3)): 100550+1010050=25000+50000=75000100\cdot5\cdot50 + 10\cdot100\cdot50 = 25000+50000 = 75000.

The first order is 10× cheaper.

Dynamic Programming Formulation

Let m[i][j]m[i][j] = minimum cost to multiply AiAjA_i \cdots A_j. Then:

m[i][j]={0if i=jminik<j(m[i][k]+m[k+1][j]+pi1pkpj)if i<jm[i][j] = \begin{cases} 0 & \text{if } i = j \\ \displaystyle\min_{i \le k < j}\big(m[i][k] + m[k+1][j] + p_{i-1}\,p_k\,p_j\big) & \text{if } i < j \end{cases}

Here kk is the split point; s[i][j]=ks[i][j]=k records the optimal split for reconstructing the parenthesization.

MATRIX-CHAIN-ORDER(p, n):
    for i = 1 to n: m[i][i] = 0
    for L = 2 to n:                 // chain length
        for i = 1 to n-L+1:
            j = i+L-1; m[i][j] = INFINITY
            for k = i to j-1:
                q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]
                if q < m[i][j]: m[i][j] = q; s[i][j] = k
    return m[1][n]

Time complexity O(n3)O(n^3), space O(n2)O(n^2). The answer is m[1][n]m[1][n].

dynamic-programmingmatrix-chain
8short5 marks

Explain heap sort algorithm with an example and analyze its time complexity.

Heap Sort

Heap sort sorts an array using a binary max-heap (a complete binary tree where every parent ≥ its children, stored in an array; for index ii: left child 2i+12i+1, right child 2i+22i+2).

Steps:

  1. Build a max-heap from the input array (BUILD-MAX-HEAP, O(n)O(n)).
  2. Repeatedly extract the max: swap the root (largest) with the last element, shrink the heap by 1, and MAX-HEAPIFY the root to restore the heap property.
HEAP-SORT(A, n):
    BUILD-MAX-HEAP(A, n)
    for i = n downto 2:
        swap A[1] and A[i]      // largest goes to the end
        heapSize = i-1
        MAX-HEAPIFY(A, 1, heapSize)

Example: sort [4, 10, 3, 5, 1]

  • Build max-heap: [10, 5, 3, 4, 1].
  • Swap 10↔1 → [1,5,3,4,10], heapify → [5,4,3,1,10].
  • Swap 5↔1 → [1,4,3,5,10], heapify → [4,1,3,5,10].
  • Swap 4↔3 → [3,1,4,5,10], heapify → [3,1,4,5,10].
  • Swap 3↔1 → [1,3,4,5,10].
  • Sorted: [1, 3, 4, 5, 10].

Time Complexity

  • BUILD-MAX-HEAP: O(n)O(n).
  • Each of nn extractions does a MAX-HEAPIFY costing O(logn)O(\log n).

Overall: O(nlogn)O(n \log n) in best, average, and worst cases. Space is O(1)O(1) (in-place); heap sort is not stable.

sortingheap
9short5 marks

Explain how the subset-sum problem is solved using backtracking with an example.

Subset-Sum by Backtracking

Problem: Given a set of positive integers S={w1,w2,,wn}S = \{w_1, w_2, \dots, w_n\} and a target TT, find a subset whose elements sum exactly to TT.

Backtracking approach: Traverse a binary state-space tree — at each element decide include (left) or exclude (right). Maintain the running sum and prune a branch (bound) when:

  • current sum exceeds TT (overshoot), or
  • current sum + sum of all remaining elements < TT (can never reach target).
SUBSET-SUM(w, n, T):
    sort w ascending
    backtrack(index=0, currentSum=0, chosen=[])

backtrack(i, sum, chosen):
    if sum == T: report chosen; return
    if i == n or sum > T: return            // prune
    if sum + remainingSum(i) < T: return    // prune
    backtrack(i+1, sum + w[i], chosen + [w[i]])   // include w[i]
    backtrack(i+1, sum, chosen)                   // exclude w[i]

Example

S={3,5,6,7}S = \{3, 5, 6, 7\}, T=15T = 15.

  • Include 3 (sum 3) → include 5 (8) → include 6 (14) → exclude 7 (14 ≠ 15) → include 7 (21 > 15, prune). Backtrack.
  • Include 3 (3) → include 5 (8) → exclude 6 → include 7 (15) → found {3, 5, 7}.
  • Continuing finds {3, 6, ...} none; also no others.

Solution subset: {3, 5, 7} = 15.

Worst-case time is O(2n)O(2^n) (the full state-space tree), but pruning typically explores far fewer nodes.

backtrackingsubset-sum
10short5 marks

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

Approximation Algorithms

Many important problems are NP-hard, so no known polynomial-time algorithm finds the exact optimum. An approximation algorithm runs in polynomial time and returns a solution provably close to optimal. For a minimization problem, an algorithm has approximation ratio ρ\rho if for every input:

CCρ\frac{C}{C^*} \le \rho

where CC is the cost returned and CC^* is the optimal cost. A ratio-2 algorithm is also called a 2-approximation.

Vertex Cover — 2-Approximation

A vertex cover is a set of vertices covering every edge (each edge has at least one endpoint in the set); finding the minimum is NP-hard.

APPROX-VERTEX-COVER(G):
    C = empty set
    E' = all edges of G
    while E' is not empty:
        pick any edge (u, v) from E'
        add both u and v to C
        remove from E' every edge incident on u or v
    return C

Why it works: Each picked edge (u,v)(u,v) must have at least one endpoint in any optimal cover, so the optimum contains at least one of the picked edges' endpoints. The algorithm picks a matching MM (the chosen edges are vertex-disjoint), so C=2M|C| = 2|M| while CMC^* \ge |M|. Hence:

C=2M2C|C| = 2|M| \le 2\,C^*

This is a 2-approximation running in O(V+E)O(V+E) time.

Example: Edges {(1,2),(2,3),(3,4)}. Pick (1,2) → C={1,2}, removes (1,2),(2,3). Pick (3,4) → C={1,2,3,4}. (Optimal here is {2,3}; the approximation gives ≤ twice the optimum size.)

approximation
11short5 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 technique for solving optimization problems exactly by systematically exploring a state-space tree, but pruning subtrees that cannot contain a better solution than one already found. Two operations:

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

Nodes are explored using FIFO, LIFO, or (most commonly) least-cost (best-first) order. B&B always finds the optimum, but is exponential in the worst case.

B&B for the Travelling Salesman Problem (TSP)

TSP: find the minimum-cost tour visiting every city exactly once and returning to the start.

Applying B&B:

  1. State-space tree: each node represents a partial tour (a sequence of cities visited so far).
  2. Cost / lower bound at a node: a common bound = cost of the path already fixed plus a lower estimate for the rest, e.g. for each unvisited city add half the sum of its two minimum-cost incident edges (reduced-cost-matrix bound). This gives a guaranteed lower bound on any completion of the partial tour.
  3. Branch: from a node, create a child for each unvisited next city.
  4. Bound & prune: maintain the best complete tour found so far (upper bound). If a node's lower bound \ge best tour cost, discard that node — its subtree cannot beat the incumbent.
  5. Expand the most promising (lowest-bound) live node first until the live list is empty; the best complete tour found is optimal.

Result: B&B avoids enumerating all (n1)!(n-1)! tours by pruning, finding the exact optimal tour much faster on average (worst case still exponential).

branch-and-bound
12short5 marks

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

BFS vs. DFS

Both traverse all vertices of a graph; they differ in order and data structure.

  • BFS explores level by level (all neighbours of a vertex before going deeper) using a queue (FIFO).
  • DFS explores as deep as possible along one branch before backtracking, using a stack (LIFO) or recursion.

Comparison Table

FeatureBFSDFS
Data structureQueue (FIFO)Stack / recursion (LIFO)
OrderLevel by level (breadth-wise)Branch by branch (depth-wise)
Finds shortest path (unweighted)YesNot guaranteed
MemoryO(V)O(V) — can be high (stores whole level)O(V)O(V) — proportional to depth
Typical usesShortest path, level order, connectivityTopological sort, cycle detection, connected components

Example

Graph: A–B, A–C, B–D, C–D, starting at A.

  • BFS order: A, B, C, D (visit A's neighbours B,C, then D).
  • DFS order: A, B, D, C (go deep A→B→D, backtrack, then C).

Time Complexity

Using an adjacency list, both visit every vertex and edge once:

O(V+E)O(V + E)

Using an adjacency matrix, both are O(V2)O(V^2). Space for both is O(V)O(V) for the visited array plus the queue/stack.

graphbfs-dfs

Frequently asked questions

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