CSC325 · TU past paper
Design and Analysis of Algorithms 2076 question paper
The complete TU 2076 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.
- 110 marksAsymptotic NotationsHideAnswer
What do you mean by the complexity of an algorithm? Explain the asymptotic notations used to describe the time/space complexity of any algorithm with their geometrical interpretation and example.[10]
Algorithm complexity is a measure of the amount of time and/or space (memory) required by an algorithm as a function of the input size n. - Time Complexity -- the number of basic operations performed as a function of input size n (using ...
- 210 marksSorting AlgorithmsHideAnswer
Explain the divide and conquer paradigm from algorithm design with a suitable example. Write the Quick sort algorithm using a randomized approach and explain its time complexity.[10]
--- Divide and Conquer is a fundamental algorithm design paradigm that solves a problem by breaking it into smaller subproblems, solving each subproblem recursively, and then combining the results to produce the final solution. Step Desc...
- 310 marksBacktracking AlgorithmsHideAnswer
Explain in brief the Backtracking approach for algorithm design. How it differs with recursion? Explain the N-Queen problem and algorithm using backtracking and analyze its time complexity.[10]
--- Backtracking is an algorithmic design technique used to solve problems by building a solution incrementally, one step at a time, and abandoning (backtracking) a partial solution as soon as it is determined that it cannot lead to a va...
- 45 marksNumericalSolving RecurrencesHideAnswer
Solve the following recurrence relation using the master method.
a. $T(n) = 7 T(n/2) + n^2$
b. $T(n) = 4 T(n/4) + kn$
[5]
- Part a: $T(n) = 7T(n/2) + n^2$ - Part b: $T(n) = 4T(n/4) + kn$ (where $k$ is a positive constant) For a recurrence $T(n) = aT(n/b) + f(n)$ with $a \geq 1$, $b 1$, compare $f(n)$ with $n^{\logb a}$: Case Condition Result ---------------...
- 55 marksGreedy AlgorithmsHideAnswer
Explain the greedy algorithm for the fractional knapsack problem with its time complexity. [5]
A thief has a knapsack that can carry a maximum weight W. There are n items, where the i-th item has: - Weight: w[i] - Value: v[i] Any fraction of an item can be taken (unlike 0/1 knapsack). The objective is to maximize total profit by s...
- 65 marksGreedy Algorithms vs Dynamic Programming, HideAnswer
What do you mean by Dynamic programming strategy? Explain the element of DP. [5]
Dynamic Programming (DP) is an algorithm design strategy used to solve optimization problems by breaking them down into smaller overlapping subproblems, solving each subproblem only once, and storing the results to avoid redundant comput...
- 75 marksApproximation AlgorithmsHideAnswer
Explain the approximation for solving vertex cover with a suitable example. [5]
Approximation Algorithm for Vertex Cover
Definition
A vertex cover of an undirected graph G = (V, E) is a subset V' ⊆ V such that for every edge (u, v) ∈ E, at least one of u or v belongs to V'.
The optimization problem is to find the vertex cover with the fewest vertices. The approximation problem is to find a vertex cover with few vertices (not necessarily minimum, but within a guaranteed bound).
Approximation Algorithm (APPROX-VERTEX-COVER)
APPROX-VERTEX-COVER(G): 1. C = {} (empty set - will hold the vertex cover) 2. E' = E[G] (copy of all edges) 3. while E' is not empty: a. Pick any edge (u, v) from E' b. Add both u and v to C C = C ∪ {u, v} c. Remove from E' every edge incident to u or v 4. return C
Key Properties
- Time Complexity: O(V + E)
- Approximation Ratio: This algorithm is a 2-approximation, meaning:
|C| ≤ 2 × |C*|
where C* is the optimal (minimum) vertex cover.
Reason: Each edge picked in step 3(a) belongs to an optimal cover (at least one endpoint must be in C*). Since both endpoints are added, we add at most twice the optimal number of vertices.
Example
Consider the following graph:
Vertices: {1, 2, 3, 4, 5, 6, 7} Edges: {(1,2), (1,3), (2,4), (3,4), (4,5), (4,6), (5,7)}Step-by-step execution:
Step Edge Picked Vertices Added to C Edges Removed 1 (1, 2) {1, 2} (1,2), (1,3), (2,4) 2 (3, 4) {1, 2, 3, 4} (3,4), (4,5), (4,6) 3 (5, 7) {1, 2, 3, 4, 5, 7} (5,7) Result: C = {1, 2, 3, 4, 5, 7} → size = 6
Optimal cover: C* = {1, 4, 5} or {2, 3, 4, 7} → size = 3 or 4
Here |C| = 6 ≤ 2 × 3 = 6, which satisfies the 2-approximation guarantee.
Conclusion
The approximation algorithm for vertex cover guarantees a solution no worse than twice the optimal. It is efficient and practical because finding the exact minimum vertex cover is NP-hard, while this approximation runs in polynomial time O(V + E).
- 85 marksGreedy AlgorithmsHideAnswer
Explain Prim’s algorithm for MST problem and analyze its time complexity. [5]
A Minimum Spanning Tree (MST) of a weighted, connected, undirected graph is a spanning tree whose total edge weight is minimum among all possible spanning trees. Prim's Algorithm is a greedy algorithm that builds the MST by starting from...
- 95 marksConcept of Backtracking, Recursion vs BackHideAnswer
Write short notes on: a. Backtracking strategy b. Tractable and Intractable Problem [5]
--- Definition: Backtracking is a general algorithmic technique that considers searching every possible combination in order to solve a computational problem. It is used for finding solutions to computational problems where we have a set...
- 105 marksSorting AlgorithmsHideAnswer
Write the algorithm for selection sort and explain its time and space complexity. [5]
Selection Sort: Algorithm and Complexity Analysis
Definition
Selection Sort is a simple comparison-based sorting algorithm that works by repeatedly selecting the minimum element from the unsorted portion of the array and placing it at the correct position.
Algorithm (Steps)
- Start.
- Let array A have n elements (index 0 to n-1).
- For each position i from 0 to n-2:
- Assume the current position i holds the minimum element (set
min_index = i). - Scan the remaining unsorted portion (j from i+1 to n-1).
- If
A[j] < A[min_index], updatemin_index = j. - After scanning, swap
A[i]withA[min_index].
- Assume the current position i holds the minimum element (set
- Repeat step 3 until the array is fully sorted.
- Stop.
Pseudocode
SelectionSort(A, n) for i = 0 to n-2 min_index = i for j = i+1 to n-1 if A[j] < A[min_index] min_index = j swap(A[i], A[min_index])
Example Trace
Sort:
A = {64, 25, 12, 22, 11}Pass Array State Action 1 11 25 12 22 64 Min=11, swap with index 0 2 11 12 25 22 64 Min=12, swap with index 1 3 11 12 22 25 64 Min=22, swap with index 2 4 11 12 22 25 64 Min=25, already in place Done 11 12 22 25 64 Sorted
Time Complexity Analysis
Using the RAM model (each comparison and memory reference counts as 1 step):
- The outer loop runs (n - 1) times.
- For each pass i, the inner loop runs (n - 1 - i) times.
- Total comparisons:
$$T(n) = (n-1) + (n-2) + (n-3) + \ldots + 1 = \frac{n(n-1)}{2}$$
Case Complexity Reason Best Case O(n²) Inner loop always runs fully; no early exit Average Case O(n²) Same number of comparisons regardless Worst Case O(n²) Same number of comparisons regardless Unlike Bubble Sort (which has O(n) best case), Selection Sort always performs O(n²) comparisons because it must scan the entire unsorted portion to find the minimum, even if the array is already sorted.
Space Complexity Analysis
- Selection Sort sorts the array in-place.
- Only a constant number of extra variables are used:
i,j,min_index, and a temporary swap variable. - The array A itself takes n memory references, but no additional array is needed.
$$\text{Space Complexity} = O(1)$$
This is consistent with the approach used in Bubble Sort (O(1) extra space) as noted in the course notes, since both are in-place sorting algorithms.
Summary Table
Property Value Best Time Complexity O(n²) Average Time Complexity O(n²) Worst Time Complexity O(n²) Space Complexity O(1) Sorting Type In-place, Not stable - 115 marksNumericalSorting AlgorithmsHideAnswer
Trace heap sort algorithm for the following data: (2, 9, 3, 12, 15, 8, 11) [5]
- Array (0-indexed): $A = [2, 9, 3, 12, 15, 8, 11]$, size $n = 7$ - Sort in ascending order using Max-Heap. Index relations: left child $= 2i+1$, right child $= 2i+2$. --- Last non-leaf node $= \lfloor n/2 \rfloor - 1 = 2$. Heapify from ...
- 125 marksComplexity ClassesHideAnswer
Explain in brief about the classes P, NP, and NP complete with examples. [5]
--- Definition: P is the class of decision problems that can be solved by a deterministic algorithm in polynomial time, i.e., in O(n^k) for some constant k. - These are considered tractable (efficiently solvable) problems. Examples: - So...