BSc CSIT (TU) Science Design and Analysis of Algorithms (BSc CSIT, CSC314) Question Paper 2074 Nepal
This is the official BSc CSIT (TU) (Science stream) Design and Analysis of Algorithms (BSc CSIT, CSC314) question paper for 2074, as set in the regular annual examination. It carries 60 full marks and a time allowance of 180 minutes, across 12 questions. On Kekkei you can attempt this Design and Analysis of Algorithms (BSc CSIT, CSC314) past paper online with a timer, get instant AI feedback and step-by-step solutions, and track the topics where you lose marks — completely free. Whether you are revising for your BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) exam or solving previous years' question papers, this 2074 paper is a great way to practise under real exam conditions.
Section A: Long Answer Questions
Attempt any TWO questions.
Explain the divide and conquer strategy of algorithm design. Write the merge sort algorithm, trace it on the array [38, 27, 43, 3, 9, 82, 10] and analyze its time complexity using the recurrence relation.
Divide and Conquer Strategy
Divide and conquer solves a problem in three steps:
- Divide the problem into smaller subproblems of the same type.
- Conquer each subproblem by solving it recursively (base case is solved directly).
- Combine the solutions of the subproblems into a solution for the original problem.
Classic examples: merge sort, quicksort, binary search, Strassen's matrix multiplication.
Merge Sort Algorithm
MERGE-SORT(A, l, r):
if l < r:
m = (l + r) / 2 # divide
MERGE-SORT(A, l, m) # conquer left
MERGE-SORT(A, m+1, r) # conquer right
MERGE(A, l, m, r) # combine
MERGE(A, l, m, r):
L = A[l..m]; R = A[m+1..r] (append +inf sentinels)
i = j = 0; k = l
while elements remain in L and R:
if L[i] <= R[j]: A[k++] = L[i++]
else: A[k++] = R[j++]
copy remaining elements of L (or R) into A
Trace on [38, 27, 43, 3, 9, 82, 10]
Divide phase (split until single elements):
[38,27,43,3,9,82,10]
-> [38,27,43,3] | [9,82,10]
-> [38,27]|[43,3] [9,82]|[10]
-> [38][27] [43][3] [9][82] [10]
Merge phase (combine in sorted order):
[38][27] -> [27,38]
[43][3] -> [3,43]
[27,38][3,43] -> [3,27,38,43]
[9][82] -> [9,82]
[9,82][10] -> [9,10,82]
[3,27,38,43] + [9,10,82] -> [3,9,10,27,38,43,82]
Sorted result: [3, 9, 10, 27, 38, 43, 82]
Time Complexity (Recurrence)
Let be the running time. Dividing takes , two recursive calls on size , and merging takes :
By the Master Theorem with : since , this is Case 2, giving
Merge sort runs in in best, average and worst cases, using extra space.
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 a technique for solving problems that exhibit two properties:
- Optimal substructure — an optimal solution is built from optimal solutions of subproblems.
- Overlapping subproblems — the same subproblems recur many times.
DP solves each distinct subproblem once and stores its result in a table (memoization / bottom-up tabulation), avoiding recomputation.
Floyd-Warshall Algorithm (All-Pairs Shortest Paths)
Floyd-Warshall finds the shortest distance between every pair of vertices in a weighted graph (may have negative edges, but no negative cycles). It considers, for each intermediate vertex , whether routing improves the current distance.
Recurrence. Let be the shortest path from to using only intermediate vertices in :
FLOYD-WARSHALL(W): # W = weight matrix, n vertices
D = W # D[i][j] = w(i,j), 0 on diagonal, inf if no edge
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
Graph with vertices {1,2,3}, edges: 1→2 (8), 1→3 (5), 3→2 (2), 2→3 (1). Initial matrix ( where no edge):
| 1 | 2 | 3 | |
|---|---|---|---|
| 1 | 0 | 8 | 5 |
| 2 | ∞ | 0 | 1 |
| 3 | ∞ | 2 | 0 |
Using as intermediate: . Final distance matrix:
| 1 | 2 | 3 | |
|---|---|---|---|
| 1 | 0 | 7 | 5 |
| 2 | ∞ | 0 | 1 |
| 3 | ∞ | 2 | 0 |
Time Complexity
Three nested loops over vertices give
with space for the distance matrix.
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, always choosing the option that looks best (locally optimal) at the current step, without reconsidering earlier choices. It yields a globally optimal solution when the problem has the greedy-choice property and optimal substructure (e.g., Huffman coding, fractional knapsack, MST, Dijkstra).
Huffman Coding for "CYBER CRIME"
Step 1 — Frequencies (the string is CYBER CRIME, including the space):
| Char | C | Y | B | E | R | space | I | M |
|---|---|---|---|---|---|---|---|---|
| Freq | 2 | 1 | 1 | 2 | 2 | 1 | 1 | 1 |
Total characters = 11.
Step 2 — Build the Huffman tree by repeatedly merging the two lowest-frequency nodes:
Merge Y(1)+B(1) -> 2
Merge space(1)+I(1) -> 2
Merge M(1)+C(2) -> 3 (M is a leftover single, paired with C)
Merge (YB=2)+(space,I=2) -> 4
Merge E(2)+R(2) -> 4
Merge (MC=3)+(YB,space,I=4)-> 7
Merge (ER=4)+(...=7) -> 11 (root)
Step 3 — Assign prefix codes (left = 0, right = 1). One valid optimal code:
| Char | Freq | Code | Bits | Freq×Bits |
|---|---|---|---|---|
| E | 2 | 00 | 2 | 4 |
| R | 2 | 01 | 2 | 4 |
| M | 1 | 100 | 3 | 3 |
| C | 2 | 101 | 3 | 6 |
| Y | 1 | 1100 | 4 | 4 |
| B | 1 | 1101 | 4 | 4 |
| space | 1 | 1110 | 4 | 4 |
| I | 1 | 1111 | 4 | 4 |
Step 4 — Total bits to encode the string:
A fixed-length code for 8 symbols would need bits each bits here too; Huffman is optimal and never worse. (Exact codes may differ depending on tie-breaking, but the optimal total length is 33 bits.)
Section B: Short Answer Questions
Attempt any EIGHT questions.
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 in a sorted array by repeatedly halving the search interval.
BINARY-SEARCH(A, l, r, key): # A sorted ascending
if l > r: return -1 # not found
m = (l + r) / 2
if A[m] == key: return m
else if key < A[m]:
return BINARY-SEARCH(A, l, m-1, key) # search left half
else:
return BINARY-SEARCH(A, m+1, r, key) # search right half
Complexity Analysis
Each step discards half the elements, giving the recurrence
| Case | Condition | Time |
|---|---|---|
| Best | key found at first mid | |
| Worst | key absent or at a leaf | |
| Average | random target |
So binary search runs in in the worst and average cases and in the best case, using extra space (iterative) or stack space (recursive).
Explain the randomized quicksort algorithm and analyze its expected time complexity.
Randomized Quicksort
Randomized quicksort is quicksort that selects the pivot uniformly at random from the current subarray instead of always taking a fixed position. This makes the running time independent of the input order, so no specific input can force worst-case behaviour.
RANDOMIZED-PARTITION(A, p, r):
i = RANDOM(p, r) # random index
swap A[i] with A[r]
return PARTITION(A, p, r) # standard Lomuto/Hoare partition
RANDOMIZED-QUICKSORT(A, p, r):
if p < r:
q = RANDOMIZED-PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, q-1)
RANDOMIZED-QUICKSORT(A, q+1, r)
Expected Time Complexity
The total work is dominated by element comparisons. Let indicate that elements of ranks and are compared. Two elements are compared iff one of them is the first among ranks to be chosen as a pivot, so
The expected number of comparisons is
Hence the expected running time is . The worst case is still (extremely unlikely), and space is expected stack depth.
Write a greedy algorithm for the fractional knapsack problem and analyze its time complexity.
Fractional Knapsack — Greedy Algorithm
In the fractional knapsack problem, items may be taken in fractions. The greedy strategy picks items in decreasing order of value-to-weight ratio , taking as much of each as fits.
FRACTIONAL-KNAPSACK(v[], w[], n, W):
for each item i: ratio[i] = v[i] / w[i]
sort items by ratio in decreasing order
totalValue = 0; cap = W
for each item i in sorted order:
if w[i] <= cap:
take whole item: cap -= w[i]; totalValue += v[i]
else:
take fraction: totalValue += v[i] * (cap / w[i]); cap = 0
break
return totalValue
This greedy choice is optimal because swapping any quantity from a lower-ratio item to a higher-ratio one never decreases total value (greedy-choice property).
Time Complexity
- Computing ratios: .
- Sorting by ratio: — the dominant cost.
- Single pass to fill the knapsack: .
Find the edit distance between the strings "ARTIFICIAL" and "NATURAL" using dynamic programming.
Edit Distance: "ARTIFICIAL" → "NATURAL"
The edit (Levenshtein) distance is the minimum number of insertions, deletions and substitutions to convert one string into another. DP recurrence with = distance between first chars of and first chars of :
with and .
Here ARTIFICIAL (m = 10), NATURAL (n = 7). Filling the table (rows = ARTIFICIAL, cols = NATURAL):
| ε | N | A | T | U | R | A | L | |
|---|---|---|---|---|---|---|---|---|
| ε | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| A | 1 | 1 | 1 | 2 | 3 | 4 | 5 | 6 |
| R | 2 | 2 | 2 | 2 | 3 | 3 | 4 | 5 |
| T | 3 | 3 | 3 | 2 | 3 | 4 | 4 | 5 |
| I | 4 | 4 | 4 | 3 | 3 | 4 | 5 | 5 |
| F | 5 | 5 | 5 | 4 | 4 | 4 | 5 | 6 |
| I | 6 | 6 | 6 | 5 | 5 | 5 | 5 | 6 |
| C | 7 | 7 | 7 | 6 | 6 | 6 | 6 | 6 |
| I | 8 | 8 | 8 | 7 | 7 | 7 | 7 | 7 |
| A | 9 | 9 | 8 | 8 | 8 | 8 | 7 | 8 |
| L | 10 | 10 | 9 | 9 | 9 | 9 | 8 | 7 |
Result
It takes a minimum of 7 operations to transform ARTIFICIAL into NATURAL. The DP runs in time and space.
Write Dijkstra's algorithm to find single-source shortest paths and explain it with an example.
Dijkstra's Algorithm (Single-Source Shortest Paths)
Dijkstra finds shortest paths from a source to all vertices in a graph with non-negative edge weights. It greedily extracts the closest unfinalized vertex and relaxes its outgoing edges.
DIJKSTRA(G, w, s):
for each vertex v: dist[v] = inf; prev[v] = nil
dist[s] = 0
Q = all vertices (min-priority queue keyed by dist)
while Q not empty:
u = EXTRACT-MIN(Q) # closest unprocessed vertex
for each edge (u, v):
if dist[u] + w(u,v) < dist[v]: # relax
dist[v] = dist[u] + w(u,v)
prev[v] = u
return dist, prev
Example
Graph: edges A→B(4), A→C(1), C→B(2), C→D(5), B→D(1). Source = A.
| Step | Extract | dist[A] | dist[B] | dist[C] | dist[D] |
|---|---|---|---|---|---|
| init | — | 0 | ∞ | ∞ | ∞ |
| 1 | A | 0 | 4 | 1 | ∞ |
| 2 | C | 0 | 3 | 1 | 6 |
| 3 | B | 0 | 3 | 1 | 4 |
| 4 | D | 0 | 3 | 1 | 4 |
Shortest distances from A: A=0, C=1, B=3 (via A→C→B), D=4 (via A→C→B→D).
Complexity
With a binary-heap priority queue: ; with an array: .
Explain the matrix chain multiplication problem and write its dynamic programming formulation.
Matrix Chain Multiplication Problem
Given a chain of matrices where has dimension , matrix multiplication is associative, so the product can be parenthesized in many ways. Each way yields the same result but a different number of scalar multiplications. The goal is to find the parenthesization that minimizes the total scalar multiplications (multiplying a by matrix costs ).
Dynamic Programming Formulation
Let = minimum cost to multiply . The optimal split places the last multiplication at some ():
MATRIX-CHAIN-ORDER(p[0..n]):
for i = 1 to n: m[i][i] = 0
for len = 2 to n: # chain length
for i = 1 to n-len+1:
j = i + len - 1
m[i][j] = inf
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 # store split for parenthesization
return m, s
The table records the optimal split point so the parenthesization can be reconstructed.
Complexity
Three nested loops give time and space — far better than the exponential number of possible parenthesizations.
Explain heap sort algorithm with an example and analyze its time complexity.
Heap Sort
Heap sort uses a binary max-heap (a complete binary tree where each parent ≥ its children, stored in an array). It (1) builds a max-heap, then (2) repeatedly swaps the root (max) to the end and re-heapifies the reduced heap.
HEAPIFY(A, n, i): # sift down node i in a heap of size n
largest = i; l = 2i+1; r = 2i+2
if l < n and A[l] > A[largest]: largest = l
if r < n and A[r] > A[largest]: largest = r
if largest != i:
swap A[i], A[largest]; HEAPIFY(A, n, largest)
HEAP-SORT(A, n):
for i = n/2 - 1 downto 0: HEAPIFY(A, n, i) # build max-heap
for i = n-1 downto 1:
swap A[0], A[i] # move max to the end
HEAPIFY(A, i, 0) # restore heap on A[0..i-1]
Example: sort [4, 10, 3, 5, 1]
Build max-heap: → [10, 5, 3, 4, 1]
Extract maxima:
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 | ...]
swap 3,1 -> [1 | 3,4,5,10]
Sorted: [1, 3, 4, 5, 10].
Time Complexity
- BUILD-HEAP: .
- extractions, each with a heapify: .
with extra space (in-place).
Explain how the subset-sum problem is solved using backtracking with an example.
Subset-Sum via Backtracking
Problem. Given a set of positive integers and a target sum , find a subset whose elements add up to .
Backtracking idea. Build the subset incrementally. At each element decide to include or exclude it, extending the partial sum. Prune a branch when:
- the running sum exceeds (overshoot), or
- the running sum plus all remaining elements is still (cannot reach target).
SUBSET-SUM(w[], i, currSum, target, remaining):
if currSum == target: report solution; return
if i == n or currSum > target: return
if currSum + remaining < target: return # bound
# include w[i]
SUBSET-SUM(w, i+1, currSum + w[i], target, remaining - w[i])
# exclude w[i]
SUBSET-SUM(w, i+1, currSum, target, remaining - w[i])
Example
Set = {3, 4, 5, 6}, target .
{} (0)
|-- +3 (3)
| |-- +4 (7) -> +5=12 (prune>9), -5 then +6=13 (prune)
| |-- skip4 -> +5=8 -> +6=14(prune); skip5 -> +6=9 SOLUTION {3,6}
|-- skip3
|-- +4 (4) -> +5=9 SOLUTION {4,5}
Solutions found: {3, 6} and {4, 5}, each summing to 9.
The state-space tree has nodes in the worst case, so worst-case time is , but pruning prunes large portions in practice.
What are approximation algorithms? Explain the approximation algorithm for the vertex cover problem.
Approximation Algorithms
Many optimization 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 if for every input
Vertex Cover Approximation (2-approximation)
A vertex cover is a set of vertices that touches every edge. Finding the minimum vertex cover is NP-hard. The classic 2-approximation repeatedly picks an uncovered edge and adds both its endpoints:
APPROX-VERTEX-COVER(G):
C = {}
E' = edges of G
while E' != empty:
pick any edge (u, v) from E'
C = C union {u, v}
remove from E' every edge incident to u or v
return C
Why ratio 2
The edges picked in the loop form a matching (no two share a vertex). Any vertex cover must include at least one endpoint of every matched edge, so (number of matched edges) . Therefore
so this is a 2-approximation, running in time.
Frequently asked questions
- Where can I find the BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) question paper 2074?
- The full BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2074 (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) 2074 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) 2074 paper?
- The BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2074 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.