BSc CSIT (TU) Science Design and Analysis of Algorithms (BSc CSIT, CSC314) Question Paper 2081 Nepal
This is the official BSc CSIT (TU) (Science stream) Design and Analysis of Algorithms (BSc CSIT, CSC314) question paper for 2081, 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 2081 paper is a great way to practise under real exam conditions.
Section A: Long Answer Questions
Attempt any TWO questions.
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)
The set of decision problems that can be solved by a deterministic algorithm in polynomial time . These are considered tractable.
Examples: sorting, searching, shortest path (Dijkstra), minimum spanning tree.
Class NP (Nondeterministic Polynomial time)
The set of decision problems for which a given certificate (proposed solution) can be verified in polynomial time by a deterministic algorithm. Equivalently, solvable in polynomial time on a nondeterministic machine.
Examples: SAT, Hamiltonian cycle, subset-sum, graph colouring. Note .
NP-Hard
A problem is NP-Hard if every problem in NP reduces to it in polynomial time. It is "at least as hard as the hardest problems in NP" but need not be in NP (need not even be a decision problem).
Examples: Halting problem, Travelling Salesman optimization problem.
NP-Complete
A problem is NP-Complete if it is (1) in NP and (2) NP-Hard. These are the hardest problems in NP; if any one is solved in polynomial time then .
Examples: SAT (Cook-Levin theorem), 3-SAT, Hamiltonian cycle, vertex cover, clique.
Proving a Problem is NP-Complete
To prove a problem is NP-Complete:
- Show : give a polynomial-time verifier — i.e., show that a candidate solution can be checked in polynomial time.
- Show is NP-Hard: pick a known NP-Complete problem and construct a polynomial-time reduction :
- Describe a polynomial-time transformation that maps any instance of into an instance of .
- Prove the equivalence: is a YES-instance of iff is a YES-instance of .
- Since is NP-Complete and , is NP-Hard. Combined with step 1, is NP-Complete.
Reduction works because polynomial-time reductions are transitive: every problem in NP reduces to , and reduces to , so every NP problem reduces to .
Example: 3-SAT Clique establishes that Clique is NP-Complete.
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
Problem: Given items with weights and values , and capacity , select a subset (each item 0 or 1 time) maximizing total value subject to total weight .
Recurrence
Let = max value using the first items with capacity .
Algorithm
Knapsack01(w[1..n], v[1..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: (pseudo-polynomial), space .
Solving the instance
Items: weights , values , .
| i\j | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 0 (—) | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 (w2,v3) | 0 | 0 | 3 | 3 | 3 | 3 |
| 2 (w3,v4) | 0 | 0 | 3 | 4 | 4 | 7 |
| 3 (w4,v5) | 0 | 0 | 3 | 4 | 5 | 7 |
| 4 (w5,v6) | 0 | 0 | 3 | 4 | 5 | 7 |
Optimal value .
Backtracking the items
(items 4 and 3 not used). , so item 2 (w=3, v=4) is taken; remaining capacity . , so item 1 (w=2, v=3) is taken.
Selected items: {item 1 (w2,v3), item 2 (w3,v4)}, total weight = 5, total value = 7.
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
A design paradigm with three steps:
- Divide the problem into smaller subproblems of the same type.
- Conquer each subproblem by solving recursively (base case solved directly).
- Combine the subsolutions into the solution of the original problem.
Examples: merge sort, quicksort, binary search, Strassen's matrix multiplication.
Merge Sort Algorithm
MergeSort(A, l, r):
if l < r:
m = (l + r) / 2
MergeSort(A, l, m)
MergeSort(A, m+1, r)
Merge(A, l, m, r)
Merge(A, l, m, r): // merge two sorted halves
copy A[l..m] -> L, A[m+1..r] -> R
i = j = 0; k = l
while i < |L| and j < |R|:
if L[i] <= R[j]: A[k++] = L[i++]
else: A[k++] = R[j++]
copy remaining L and R into A
Trace on [38, 27, 43, 3, 9, 82, 10]
Divide:
- [38,27,43,3] | [9,82,10]
- [38,27]|[43,3] and [9,82]|[10]
- [38]|[27], [43]|[3], [9]|[82], [10]
Conquer/Merge (bottom-up):
- [27,38], [3,43], [9,82], [10]
- [3,27,38,43], [9,10,82]
- Final: [3, 9, 10, 27, 38, 43, 82]
Time Complexity
The recurrence is:
where is the merge cost. By the Master Theorem with : , so case 2 gives
This holds for best, average and worst cases. Space complexity is (auxiliary), and merge sort is stable.
Section B: Short Answer Questions
Attempt any EIGHT questions.
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 jobs, each with a deadline and profit , where each job takes one unit of time and only one job runs at a time. Schedule a subset of jobs to maximize total profit, where a job earns its profit only if completed by its deadline.
Greedy Approach
- Sort jobs in decreasing order of profit.
- Find the maximum deadline ; create time slots, all free.
- For each job (highest profit first): place it in the latest free slot its deadline. If found, schedule the job; otherwise reject it.
JobSequencing(jobs):
sort jobs by profit (desc)
slot[1..dmax] = free
for each job j:
for t = min(dmax, j.deadline) down to 1:
if slot[t] free: slot[t] = j; break
Example
Jobs: J1(d=2,p=100), J2(d=1,p=19), J3(d=2,p=27), J4(d=1,p=25), J5(d=3,p=15).
Sort by profit: J1(100), J3(27), J4(25), J2(19), J5(15). Max deadline = 3 → slots [1,2,3].
- J1 (d=2): slot 2 free → place at 2.
- J3 (d=2): slot 2 taken → slot 1 free → place at 1.
- J4 (d=1): slot 1 taken → reject.
- J2 (d=1): slot 1 taken → reject.
- J5 (d=3): slot 3 free → place at 3.
Schedule: slot1=J3, slot2=J1, slot3=J5. Sequence J3 → J1 → J5, total profit = 27 + 100 + 15 = 142.
Time complexity: (or using a disjoint-set for slot allocation).
Explain the naive string matching algorithm and the Rabin-Karp algorithm with their time complexities.
Naive String Matching
Slide the pattern (length ) over the text (length ) one position at a time and, at each shift, compare characters one by one.
NaiveMatch(T, P):
for s = 0 to n - m:
if P[1..m] == T[s+1 .. s+m]:
report match at shift s
- Worst case: (e.g. , ).
- Best case: when first character rarely matches.
- No preprocessing, extra space.
Rabin-Karp Algorithm
Uses hashing to avoid full comparisons. Compute a hash of and of each length- window of ; only when hashes match do we verify character by character.
A rolling hash updates the window hash in :
where is the alphabet size and a prime.
RabinKarp(T, P, d, q):
compute hp = hash(P), ht = hash(T[1..m])
for s = 0 to n - m:
if ht == hp and T[s+1..s+m] == P: report match
update ht to next window (rolling hash)
Time complexity:
- Average / best case: when there are few spurious hash collisions.
- Worst case: when many hash collisions force verification (e.g. all hashes equal).
- Preprocessing , extra space .
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)
Works on a sorted array. Compare the target with the middle element; recurse into the left or right half, halving the search space each step.
BinarySearch(A, low, high, x):
if low > high: return -1 // not found
mid = (low + high) / 2
if A[mid] == x: return mid
else if x < A[mid]: return BinarySearch(A, low, mid-1, x)
else: return BinarySearch(A, mid+1, high, x)
Time Complexity Analysis
The recurrence (one half is discarded, comparison is ):
By the Master Theorem (, , case 2): .
- Best case: — target is the middle element on the first comparison.
- Worst case: — target absent or found only at the last level.
- Average case: — expected number of comparisons is about .
Space: for recursion stack, or iteratively.
Explain the randomized quicksort algorithm and analyze its expected time complexity.
Randomized Quicksort
Quicksort partitions an array around a pivot, recursively sorting the two parts. In randomized quicksort the pivot is chosen uniformly at random from the subarray, which makes the expected running time independent of input order and avoids the deterministic worst case on already-sorted data.
RandomizedQuicksort(A, p, r):
if p < r:
i = Random(p, r) // random pivot index
swap A[i], A[r]
q = Partition(A, p, r) // standard Lomuto/Hoare partition
RandomizedQuicksort(A, p, q-1)
RandomizedQuicksort(A, q+1, r)
Expected Time Complexity
Let be the total number of comparisons. Element ranks are compared at most once, with probability
Summing the expectations:
Using harmonic-number bounds, .
- Expected (average) time: .
- Worst case: (extremely unlikely with random pivots).
- Space: expected recursion depth.
Thus randomization makes the worst case occur only with vanishing probability, giving robust expected performance.
Write a greedy algorithm for the fractional knapsack problem and analyze its time complexity.
Fractional Knapsack — Greedy Algorithm
Problem: Given items with weight and value and capacity , choose fractions of each item to maximize subject to .
Greedy choice: take items in decreasing order of value-to-weight ratio ; take whole items while they fit, then take a fraction of the next item to fill the remaining capacity.
FractionalKnapsack(w[1..n], v[1..n], W):
compute ratio[i] = v[i]/w[i]
sort items by ratio (descending)
totalValue = 0
for each item i in sorted order:
if W >= w[i]:
take whole item; W -= w[i]; totalValue += v[i]
else:
take fraction W/w[i]; totalValue += v[i] * (W / w[i]); W = 0; break
return totalValue
Time Complexity
- Computing ratios: .
- Sorting by ratio: — the dominant cost.
- Greedy scan: .
Total: .
Unlike 0/1 knapsack, the greedy strategy is optimal here because fractions are allowed (proved via an exchange argument).
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 transform one string into another.
Recurrence
Let = ARTIFICIAL (m=10), = NATURAL (n=7). = distance between first chars of and first chars of .
DP 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
Time complexity , space .
Write Dijkstra's algorithm to find single-source shortest paths and explain it with an example.
Dijkstra's Algorithm (Single-Source Shortest Path)
Finds shortest paths from a source to all vertices in a graph with non-negative edge weights. It is a greedy algorithm that repeatedly extracts the closest unfinalized vertex and relaxes its outgoing edges.
Dijkstra(G, s):
for each vertex v: dist[v] = INF; prev[v] = NIL
dist[s] = 0
Q = min-priority queue of all vertices keyed by dist
while Q not empty:
u = Extract-Min(Q)
for each edge (u, v) with weight w:
if dist[u] + w < dist[v]: // relaxation
dist[v] = dist[u] + w
prev[v] = u
Decrease-Key(Q, v)
Example
Graph: A→B(4), A→C(1), C→B(2), C→D(5), B→D(1). Source = A.
- Init: dist A=0, others ∞. Extract A → relax: C=1, B=4.
- Extract C (1) → relax: B = min(4, 1+2)=3, D = 1+5=6.
- Extract B (3) → relax: D = min(6, 3+1)=4.
- Extract D (4) → done.
Shortest distances from A: A=0, C=1, B=3, D=4. Path to D: A→C→B→D.
Time Complexity
- With binary heap: .
- With Fibonacci heap: .
- With simple 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 , find the parenthesization that minimizes the number of scalar multiplications. Matrix multiplication is associative, so the result is the same but the cost differs greatly. Multiplying a by matrix costs scalar multiplications.
Dynamic Programming Formulation
Let = minimum cost to compute the product .
The index marks the split point of the optimal parenthesization; store it in to reconstruct the parenthesization.
MatrixChainOrder(p[0..n]):
for i = 1 to n: m[i][i] = 0
for len = 2 to n:
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
return m[1][n]
Complexity
Three nested loops over ranges → time , space . This exhibits optimal substructure and overlapping subproblems, the two hallmarks of dynamic programming.
Explain heap sort algorithm with an example and analyze its time complexity.
Heap Sort
A comparison sort that uses a binary max-heap (a complete binary tree stored in an array where each parent its children). For node at index : left child , right child , parent .
Steps
- Build a max-heap from the input array — .
- Repeatedly: swap the root (maximum) with the last element, reduce heap size by 1, and
Max-Heapifythe root to restore the heap property — times.
HeapSort(A):
BuildMaxHeap(A)
for i = n down to 2:
swap A[1], A[i]
heapSize--
MaxHeapify(A, 1)
MaxHeapify(A, i): // sift down, O(log n)
l=2i; r=2i+1; largest=i
if l<=heapSize and A[l]>A[largest]: largest=l
if r<=heapSize and A[r]>A[largest]: largest=r
if largest != i: swap A[i],A[largest]; MaxHeapify(A, largest)
Example: sort [4, 10, 3, 5, 1]
- Build max-heap → [10, 5, 3, 4, 1].
- Swap 10↔1, heapify → [5,4,3,1 | 10].
- Swap 5↔1, heapify → [4,1,3 | 5,10].
- Swap 4↔3, heapify → [3,1 | 4,5,10].
- Swap 3↔1 → [1 | 3,4,5,10].
- Sorted: [1, 3, 4, 5, 10].
Time Complexity
- BuildMaxHeap: .
- Each of heapify calls: .
- Total: in best, average and worst cases.
- In-place, space ; not stable.
Frequently asked questions
- Where can I find the BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) question paper 2081?
- The full BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2081 (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) 2081 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) 2081 paper?
- The BSc CSIT (TU) Design and Analysis of Algorithms (BSc CSIT, CSC314) 2081 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.