Design and Analysis of Algorithm, Past Papers, UOS Main Campus

Analysis of Algorithms Final Past Paper (50 Marks) – UOS Main Campus

ExamFinal Term
CampusUOS Main
AnswersSolved ✓

University of Sargodha main campus Analysis of Algorithms final paper (50 marks, 120 minutes). Retyped from the original paper (the graph has been redrawn), with solved answers below.

The weight of edge c–d was handwritten on the original copy and is taken as 7.

Solved answers

Problem 1

i. Min heap vs max heap [2]: in a min heap every parent ≤ its children, so the minimum is at the root; in a max heap every parent ≥ its children, so the maximum is at the root. Min heaps are used in priority queues for Dijkstra/Prim; max heaps in heap sort.

ii. [1] True. Both are greedy, grow a tree from a source, and use a min-priority queue with a key update (Prim’s key = lightest edge to the tree; Dijkstra’s key = distance from the source).

iii. DP vs greedy [4]: DP solves all overlapping subproblems (bottom-up, stores results in a table) and then chooses, so it always gives an optimal answer when optimal substructure holds (e.g. 0/1 knapsack, LCS). Greedy makes the locally best choice first and never reconsiders; it is faster but correct only when the greedy-choice property holds (e.g. fractional knapsack, Huffman, Prim).

iv. Assembly line recursion [2]: f₁[1] = e₁ + a₁,₁; f₁[j] = min(f₁[j−1] + a₁,ⱼ, f₂[j−1] + t₂,ⱼ₋₁ + a₁,ⱼ) for j ≥ 2; similarly f₂[j]; f* = min(f₁[n] + x₁, f₂[n] + x₂).

v. [1] False. All-pairs shortest paths are computed with dynamic programming (Floyd–Warshall); a greedy method (running Dijkstra from every vertex) works only without negative edges.

Problem 2 (i) – Prim’s MST [5+2]

Starting from a: add a–b (4); then the lightest edges to the tree are all 8 (a–h, b–h, b–c) — choose b–c (8); then c–i (2); i–h (2); h–g (1); g–f (2); f–e (1); c–d (7).

MST weight = 4 + 8 + 2 + 2 + 1 + 2 + 1 + 7 = 27. Edges: a–b, b–c (or a–h or b–h), c–i, i–h, h–g, g–f, f–e, c–d.

Number of MSTs: 3 — the tree needs exactly one of the three weight-8 edges a–h, b–h, b–c to connect {a, b} to the rest, and each choice gives weight 27 (verified by enumerating all spanning trees).

Problem 2 (ii) – Optimal knapsack solution [3]

After filling the table K[i][w], trace back: start at i = n, w = W. If K[i][w] ≠ K[i−1][w], item i is in the solution — output it and set w = w − wᵢ. Decrease i and repeat until i = 0.

Problem 3

i. Knapsack recursion [1.5]: K[i][w] = 0 if i = 0 or w = 0; K[i][w] = K[i−1][w] if wᵢ > w; otherwise K[i][w] = max(K[i−1][w], vᵢ + K[i−1][w − wᵢ]).

ii. All-pairs shortest path recursion [1.5]: dᵢⱼ⁽⁰⁾ = wᵢⱼ; dᵢⱼ⁽ᵏ⁾ = min(dᵢⱼ⁽ᵏ⁻¹⁾, dᵢₖ⁽ᵏ⁻¹⁾ + dₖⱼ⁽ᵏ⁻¹⁾) for k ≥ 1 (Floyd–Warshall).

iii. MIN-HEAPIFY [3]:

MIN-HEAPIFY(A, i)
  l = LEFT(i); r = RIGHT(i)
  smallest = i
  if l ≤ A.heap-size and A[l] < A[smallest]: smallest = l
  if r ≤ A.heap-size and A[r] < A[smallest]: smallest = r
  if smallest ≠ i
      exchange A[i] with A[smallest]
      MIN-HEAPIFY(A, smallest)

iv. Faster naive matcher when all pattern characters are different [4]: if a mismatch happens after matching q characters (q ≥ 1), none of the matched text characters can be the start of a new match, because P[1] differs from P[2..q]. So instead of shifting by 1, shift by q (skip the matched characters). Each text character is examined at most twice, giving O(n) time instead of O((n − m + 1)m).

Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.