CSC325 · TU past paper
Design and Analysis of Algorithms 2079 question paper
The complete TU 2079 exam paper for Design and Analysis of Algorithms (CSC325), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- 1Order StatisticsHideAnswer
Divide and Conquer Strategy and Worst-Case Linear Time Selection Algorithm
--- A divide and conquer algorithm recursively breaks down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined t...
- 2NumericalDP AlgorithmsHideAnswer
Matrix Chain Multiplication
Matrix Index Size --------------------- A 1 $5 \times 10$ B 2 $10 \times 15$ C 3 $15 \times 20$ D 4 $20 \times 30$ Number of matrices: $n = 4$ Dimension array: $$p = [p0, p1, p2, p3, p4] = [5, 10, 15, 20, 30]$$ (Note: the question text r...
- 310 marksNumericalBacktracking AlgorithmsHideAnswer
What do you mean by Backtracking? Explain the backtracking algorithm for solving knapsack problem and find the solution for the problem given below and capacity of knapsack is 10 kg.
$$\begin{bmatrix} \text{Items} & 1 & 2 & 3 & 4 \ \text{Weight (w}_i\text{)} & 2 & 3 & 4 & 5 \ \text{Profit (P}_i\text{)} & 3 & 5 & 6 & 10 \end{bmatrix}$$
[10]
Backtracking and 0/1 Knapsack Solution
What is Backtracking?
Backtracking is a systematic algorithmic technique that builds a solution incrementally, one component at a time, and abandons a partial candidate ("backtracks") as soon as it determines that this candidate cannot lead to a valid or optimal complete solution.
Key points:
- It explores a state-space tree of all possible states.
- At each node a decision is made (for knapsack: include or exclude an item).
- A bounding function prunes branches that cannot improve upon the best solution found so far.
- More efficient than brute force since unpromising subtrees are cut.
Backtracking Algorithm for 0/1 Knapsack
- Each item is either taken (1) or not (0), so the state-space tree is binary.
- Items are usually sorted by profit/weight ratio (descending) so the bound is tight.
- At each node compute an upper bound using the fractional knapsack relaxation on remaining items. If
bound <= bestProfit, prune.
Algorithm BKnap(i, cw, cp): // cw=current weight, cp=current profit if cw <= W and cp > maxProfit: maxProfit = cp if i > n: return if Bound(i, cw, cp) > maxProfit: // include item i BKnap(i+1, cw + w[i], cp + p[i]) // only if cw+w[i] <= W // exclude item i BKnap(i+1, cw, cp)
Numerical Solution
Given Data
Item Weight $w_i$ Profit $p_i$ Ratio $p_i/w_i$ 1 2 3 1.50 2 3 5 1.67 3 4 6 1.50 4 5 10 2.00 Capacity $W = 10$ kg.
Sort by ratio (descending):
Order Item $w$ $p$ Ratio A 4 5 10 2.00 B 2 3 5 1.67 C 1 2 3 1.50 D 3 4 6 1.50 Bound formula
$$\text{Bound}=cp+\text{(fractional knapsack value of remaining items in remaining capacity)}$$
State-Space Tree (order 4,2,1,3)
Node 1 (root): $cw=0,\ cp=0$ Bound $=10+5+3+\tfrac{0}{4}\cdot6 = 18$ (after 4,2,1 weight $=10$, capacity full). So overall root bound $=18$.
Include Item 4: $cw=5,\ cp=10$
-
Bound: add 2 ($p=5$) → cap 2 left, add 1 ($p=3$) → cap 0. Bound $=10+5+3=18$.
Include Item 2: $cw=8,\ cp=15$
-
Bound $=15+3=18$ (item 1 fits, cap 2).
Include Item 1: $cw=10,\ cp=18$ - feasible, cap $=0$. $$\boxed{maxProfit = 18,\quad {4,2,1}}$$
Exclude Item 1: $cw=8,\ cp=15$, cap 2. Bound $=15+\tfrac{2}{4}\cdot6=18$. Item 3 (w=4) does not fit; best stays 15 < 18. Pruned / no improvement.
Exclude Item 2: $cw=5,\ cp=10$, cap 5. Bound $=10+3+\tfrac{3}{4}\cdot6=17.5 < 18$. Pruned.
-
Exclude Item 4: $cw=0,\ cp=0$, cap 10. Bound $=5+3+6+\tfrac{2}{5}\cdot10$? Remaining items 2,1,3 total weight $=3+2+4=9\le10$, so all fit: profit $=5+3+6=14 < 18$. Pruned.
Verification (best combination)
Items 1, 2, 4: weight $=2+3+5=10 \le 10$, profit $=3+5+10=18$. ✔
Any other feasible set:
- ${2,3,1}$: w $=9$, p $=14$
- ${4,3}$: w $=9$, p $=16$
- ${4,2}$: w $=8$, p $=15$
Maximum is 18.
Final Answer
$$\textbf{Optimal profit} = 18,\qquad \textbf{Items selected} = {1,\ 2,\ 4}$$ with total weight $10$ kg exactly filling the knapsack.
- 45 marksNumericalHuffman CodingHideAnswer
Generate the prefix code for the string "CYBER CRIME" using Huffman algorithm and find the total number of bits required. [5]
String: CYBER CRIME (11 characters including one space) Character frequencies: Character Frequency ---------------------- C 2 Y 1 B 1 E 2 R 2 Space 1 I 1 M 1 Total = $2+1+1+2+2+1+1+1 = 11$ characters, 8 distinct symbols. --- Order the no...
- 55 marksNumericalDP AlgorithmsHideAnswer
Find the edit distance between the string "ARTIFICIAL" and "NATURAL" Using dynamic programming. [5]
- String A = "ARTIFICIAL", length $m = 10$: A-R-T-I-F-I-C-I-A-L - String B = "NATURAL", length $n = 7$: N-A-T-U-R-A-L - Operations allowed: insertion, deletion, substitution (each cost 1). $$dp[i][j] = \begin{cases} i & j = 0 \ j & i = ...
- 65 marksTime and Space ComplexityHideAnswer
Write short notes on: a) Best, Worst and average case complexity b) Greedy Strategy [5]
--- When analyzing an algorithm, the running time often depends not just on the size of the input but also on the particular instance of the input. To capture this, we define three cases: --- - It gives the lower bound on the running tim...
- 75 marksNumericalSolving RecurrencesHideAnswer
Solve the following recurrence relations using masters method.
a. $T(n) = 2T(n/4) + kn^2,\ n > 1;\ T(1) = 1$
b. $T(n) = 5T(n/4) + kn,\ n > 1;\ T(1) = 1$
[5]
For $T(n) = aT(n/b) + f(n)$ with $a \geq 1$, $b 1$, compare $f(n)$ with $n^{\logb a}$: - Case 1: $f(n) = O(n^{\logb a - \epsilon})$ $\Rightarrow$ $T(n) = \Theta(n^{\logb a})$ - Case 2: $f(n) = \Theta(n^{\logb a})$ $\Rightarrow$
- 85 marksNumericalSolving Modular Linear Equations, Chinese HideAnswer
Solve the following linear congruences using Chinese Remainder Theorem.x≡1(mod2),x≡3(mod5),x≡6(mod7)x \equiv 1 \pmod{2},\quad x \equiv 3 \pmod{5},\quad x \equiv 6 \pmod{7}x≡1(mod2),x≡3(mod5),x≡6(mod7) [5]
$$x \equiv 1 \pmod{2}, \quad x \equiv 3 \pmod{5}, \quad x \equiv 6 \pmod{7}$$ Residues: $a1 = 1,\ a2 = 3,\ a3 = 6$ Moduli: $m1 = 2,\ m2 = 5,\ m3 = 7$ The moduli are pairwise coprime, so a unique solution exists modulo their product. --- ...
- 95 marksNumericalGreedy AlgorithmsHideAnswer
Find the MST from following graph using Kruskal's algorithm. [5]
The question asks to find the MST from a given graph using Kruskal's algorithm. Missing data: No graph image, adjacency matrix, edge list, or vertex/weight information was provided with the question. The actual graph is unreadable/absent...
- 105 marksNumericalSorting AlgorithmsHideAnswer
Trace the quick sort algorithm for sorting the array $A[] = {15, 7, 6, 23, 18, 34, 25}$ and write its best and worst complexity. [5]
Quick Sort - Trace, Best and Worst Complexity
STEP 1 - Given Data
- Array to sort: $A[] = {15, 7, 6, 23, 18, 34, 25}$, indices $0$ to $6$
- $n = 7$ elements
- Required: trace Quick Sort, and best and worst case time complexity
- Pivot rule not specified. I use the first element as pivot (standard TU convention, Lomuto/Hoare style).
STEP 2 - Solve
I use the Hoare-style partition with pivot = first element.
Initial: pivot = 15, range [0..6]
15 7 6 23 18 34 25 0 1 2 3 4 5 6- $i$ from left moves right while $A[i] < 15$: $A[1]=7<15$, $A[2]=6<15$, $A[3]=23\ge15$ → stop at $i=3$.
- $j$ from right moves left while $A[j] > 15$: $A[6]=25>15$, $A[5]=34>15$, $A[4]=18>15$, $A[3]=23>15$, $A[2]=6\le15$ → stop at $j=2$.
- $i > j$, so swap pivot $A[0]$ with $A[j]=A[2]$:
$${6, 7, \mathbf{15}, 23, 18, 34, 25}$$
Pivot 15 now at index 2. Left part = ${6,7}$, right part = ${23,18,34,25}$.
Left sub-array {6, 7}, pivot = 6, range [0..1]
- $i$: $A[1]=7\ge6$ → stop at $i=1$.
- $j$: $A[1]=7>6$ → move to $j=0$.
- $i>j$, swap pivot $A[0]$ with $A[0]$ → no change.
$${\mathbf{6}, 7}$$
Pivot 6 at index 0; right part ${7}$ single element, sorted.
Right sub-array {23, 18, 34, 25}, pivot = 23, range [3..6]
23 18 34 25 3 4 5 6- $i$: $A[4]=18<23$, $A[5]=34\ge23$ → stop at $i=5$.
- $j$: $A[6]=25>23$, $A[5]=34>23$, $A[4]=18\le23$ → stop at $j=4$.
- $i>j$, swap pivot $A[3]$ with $A[4]$:
$${18, \mathbf{23}, 34, 25}$$
Pivot 23 at index 4. Left part ${18}$ sorted; right part ${34,25}$.
Sub-array {34, 25}, pivot = 34, range [5..6]
- $i$: $A[6]=25<34$ → $i$ goes past range, stop at $i=6$ (or beyond).
- $j$: $A[6]=25\le34$ → stop at $j=6$.
- $i \ge j$, swap pivot $A[5]$ with $A[6]$:
$${25, \mathbf{34}}$$
Final Sorted Array
Index 0 1 2 3 4 5 6 Value 6 7 15 18 23 25 34 $$A[] = {6, 7, 15, 18, 23, 25, 34}$$
Time Complexity
Best Case: $O(n \log n)$
- Pivot splits the array into two nearly equal halves each time.
- Recurrence: $T(n) = 2T(n/2) + O(n)$
- Solution: $T(n) = O(n \log n)$
Worst Case: $O(n^2)$
- Pivot is always the smallest or largest element (e.g., an already sorted array).
- One partition has $n-1$ elements, the other $0$.
- Recurrence: $T(n) = T(n-1) + O(n)$
- Solution: $T(n) = O(n^2)$
Case Complexity Best $O(n \log n)$ Average $O(n \log n)$ Worst $O(n^2)$ Space $O(\log n)$ avg - 115 marksBasic AlgorithmsHideAnswer
Explain the iterative algorithm to find the GCD of given two numbers and analyze its complexity. [5]
The GCD (Greatest Common Divisor) of two numbers is found using the iterative Euclidean algorithm based on the property that GCD(m, n) = GCD(n, m mod n). Steps: Pseudocode: --- Find GCD(48, 18): Step m n r = m mod n ---------------------...
- 125 marksTractable and Intractable Problems, ConcepHideAnswer
Define tractable and intractable problem. Illustrate vertex cover problem with an example. [5]
--- A problem is said to be tractable if it can be solved in polynomial time, i.e., its time complexity is O(n^k) for some constant k. These problems are considered efficiently solvable in practice. Examples: Sorting (O(n log n)), Binary...