BSc CSIT (TU) Science Design and Analysis of Algorithms (BSc CSIT, CSC314) Question Paper 2075 Nepal
This is the official BSc CSIT (TU) (Science stream) Design and Analysis of Algorithms (BSc CSIT, CSC314) question paper for 2075, 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 2075 paper is a great way to practise under real exam conditions.
Section A: Long Answer Questions
Attempt any TWO questions.
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:
- Optimal substructure — an optimal solution is built from optimal solutions of subproblems.
- 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 vertices, Floyd-Warshall finds the shortest distance between every pair of vertices. It allows negative edges (but no negative cycles).
Idea: Let = shortest distance from to using only intermediate vertices from . The recurrence is:
with base case (edge weight, if no edge, if ).
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, = no edge):
| 1 | 2 | 3 | |
|---|---|---|---|
| 1 | 0 | 8 | |
| 2 | 0 | 1 | |
| 3 | 4 | 0 |
- k = 1: no improvement through vertex 1 yet.
- k = 2: .
- k = 3: ; stays 0.
Final all-pairs shortest distances:
| 1 | 2 | 3 | |
|---|---|---|---|
| 1 | 0 | 8 | 9 |
| 2 | 5 | 0 | 1 |
| 3 | 4 | 12 | 0 |
Time and Space Complexity
Three nested loops over vertices give time complexity and space complexity 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, 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):
| Symbol | C | Y | B | E | R | (space) | I | M |
|---|---|---|---|---|---|---|---|---|
| Freq | 2 | 1 | 1 | 2 | 2 | 1 | 1 | 1 |
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:
| Symbol | Freq | Code | Bits = Freq × len |
|---|---|---|---|
| C | 2 | 101 | 6 |
| E | 2 | 110 | 6 |
| R | 2 | 111 | 6 |
| Y | 1 | 000 | 3 |
| B | 1 | 001 | 3 |
| space | 1 | 010 | 3 |
| I | 1 | 011 | 3 |
| M | 1 | 100 | 3 |
(Exact bit-strings may differ between valid Huffman trees, but the optimal lengths and total are the same.)
Step 4 — Total bits required:
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.
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
| Recursion | Backtracking |
|---|---|
| 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 columns gives ; with the column-permutation structure it is . For the worst case is bounded by leaf possibilities, drastically reduced by the diagonal/column pruning. In general N-Queens backtracking runs in worst-case time, far better than brute force .
Section B: Short Answer Questions
Attempt any EIGHT questions.
Write a greedy algorithm for the fractional knapsack problem and analyze its time complexity.
Fractional Knapsack — Greedy Algorithm
Problem: Given items with values and weights , and a knapsack of capacity , maximize total value where items may be taken fractionally (a fraction of each).
Greedy idea: Take items in decreasing order of value-to-weight ratio , 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: ; items = (60,10), (100,20), (120,30) with ratios 6, 5, 4. Take all of item 1 (value 60, used 10), then of item 2 (value 50) → total 110.
Time Complexity
- Computing ratios: .
- Sorting by ratio: (dominant).
- Single pass to fill: .
Overall time = , space extra.
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 ="ARTIFICIAL" () and ="NATURAL" ():
with , .
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 |
Edit distance = .
The value at the bottom-right corner is the answer. (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 Paths)
Finds the shortest path from a source 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.
- dist: A=0, others ∞. Pick A. Relax: B=4, C=1.
- Pick C (1). Relax: B = min(4, 1+2)=3, D = 1+5 = 6.
- Pick B (3). Relax: D = min(6, 3+1) = 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: .
- Binary-heap (priority queue): .
Dijkstra fails with negative edge weights (use Bellman-Ford instead).
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 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 by a matrix costs scalar multiplications.
Example of why order matters: .
- : .
- : .
The first order is 10× cheaper.
Dynamic Programming Formulation
Let = minimum cost to multiply . Then:
Here is the split point; 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 , space . The answer is .
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 : left child , right child ).
Steps:
- Build a max-heap from the input array (
BUILD-MAX-HEAP, ). - Repeatedly extract the max: swap the root (largest) with the last element, shrink the heap by 1, and
MAX-HEAPIFYthe 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: .- Each of extractions does a
MAX-HEAPIFYcosting .
Overall: in best, average, and worst cases. Space is (in-place); heap sort is not stable.
Explain how the subset-sum problem is solved using backtracking with an example.
Subset-Sum by Backtracking
Problem: Given a set of positive integers and a target , find a subset whose elements sum exactly to .
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 (overshoot), or
- current sum + sum of all remaining elements < (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
, .
- 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 (the full state-space tree), but pruning typically explores far fewer nodes.
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 if for every input:
where is the cost returned and 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 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 (the chosen edges are vertex-disjoint), so while . Hence:
This is a 2-approximation running in 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.)
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:
- State-space tree: each node represents a partial tour (a sequence of cities visited so far).
- 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.
- Branch: from a node, create a child for each unvisited next city.
- Bound & prune: maintain the best complete tour found so far (upper bound). If a node's lower bound best tour cost, discard that node — its subtree cannot beat the incumbent.
- 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 tours by pruning, finding the exact optimal tour much faster on average (worst case still exponential).
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
| Feature | BFS | DFS |
|---|---|---|
| Data structure | Queue (FIFO) | Stack / recursion (LIFO) |
| Order | Level by level (breadth-wise) | Branch by branch (depth-wise) |
| Finds shortest path (unweighted) | Yes | Not guaranteed |
| Memory | — can be high (stores whole level) | — proportional to depth |
| Typical uses | Shortest path, level order, connectivity | Topological 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:
Using an adjacency matrix, both are . Space for both is for the visited array plus the queue/stack.
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.