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

Analysis of Algorithm M.I.T Final Past Paper – UOS Main Campus

ExamFinal Term
CampusUOS Main
AnswersSolved ✓

University of Sargodha main campus (Department of Computer Science) Analysis of Algorithm final paper for M.I.T (60 marks, 2 hours). Retyped from the original paper (the figures have been redrawn), with solved answers below.

Solved answers

Q1 – Short answers (5×4)

1. If the graph has a negative-weight cycle reachable from the source, you can go around it again and again and keep reducing the path cost, so no shortest path exists (it tends to −∞). Bellman–Ford detects this. (Cycles with positive weight are not a problem.)

2. The code is no longer a prefix code, so decoding becomes ambiguous. Example: a = 0, b = 01 — the bits 01 could be “a” followed by something or “b”. Huffman codes avoid this because characters are only at the leaves of the tree.

3. Huffman gives little or no compression when all characters have (nearly) equal frequencies — the tree becomes balanced and every code has about log₂n bits, the same as a fixed-length code. Example: 8 characters with equal frequency all get 3-bit codes.

4. Components of dynamic programming: optimal substructure, overlapping subproblems, a recursive formula (recurrence) for the optimal value, bottom-up computation in a table (or memoization), and reconstruction of the optimal solution from the stored choices.

Q2 – Out-degree from an adjacency list (10)

OUT-DEGREE(G)
  for each vertex u in G.V
      out[u] = 0
      for each v in G.Adj[u]
          out[u] = out[u] + 1
  return out

Each list is scanned once, so the running time is Θ(V + E).

Q3 – Floyd–Warshall with predecessor matrix (10)

FLOYD-WARSHALL(W, n)
  D = W
  for i = 1 to n
      for j = 1 to n
          if i ≠ j and W[i][j] < ∞ then P[i][j] = i else P[i][j] = NIL
  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] then
                  D[i][j] = D[i][k] + D[k][j]
                  P[i][j] = P[k][j]
  return D, P

P[i][j] stores the predecessor of j on the shortest path from i; the path is printed recursively from P. Time Θ(n³).

Q4 – Assembly-line scheduling (10)

f₁[1] = e₁ + a₁,₁ = 2 + 7 = 9; f₂[1] = e₂ + a₂,₁ = 4 + 8 = 12. For j ≥ 2: f₁[j] = min(f₁[j−1] + a₁,ⱼ, f₂[j−1] + t₂,ⱼ₋₁ + a₁,ⱼ) and similarly for f₂.

j12345
f₁[j]918202432
l₁[j]–1211
f₂[j]1216222530
l₂[j]–1212

f* = min(f₁[5] + x₁, f₂[5] + x₂) = min(32 + 3, 30 + 6) = 35, exiting from line 1.

Stations: S₁,₁ → S₂,₂ → S₁,₃ → S₁,₄ → S₁,₅ — line 1 for stations 1, 3, 4, 5 and line 2 for station 2. Check: 2 + 7 + 2 + 5 + 1 + 3 + 4 + 8 + 3 = 35 ✓.

Q5 – Kruskal’s MST (10)

Edges sorted: a–b 1, d–f 1, b–f 2, c–d 2, b–c 3, d–e 3, b–d 4, c–e 4, e–f 5, a–c 6, c–f 7.

  • a–b (1) ✓, d–f (1) ✓, b–f (2) ✓ (joins {a,b} and {d,f}), c–d (2) ✓, b–c (3) ✗ (cycle), d–e (3) ✓ — 5 edges for 6 vertices, done.

MST = {a–b, d–f, b–f, c–d, d–e}, total weight 9. Number of optimal trees: 1 (the MST is unique — checked by enumerating all spanning trees).

MST-KRUSKAL(G, w)
  A = ∅
  for each vertex v in G.V: MAKE-SET(v)
  sort the edges of G.E into nondecreasing order by weight w
  for each edge (u, v) in sorted order
      if FIND-SET(u) ≠ FIND-SET(v)
          A = A ∪ {(u, v)}
          UNION(u, v)
  return A
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.