Advanced Algorithm Analysis, Past Papers, UOS Main Campus

Advanced Analysis of Algorithms Past Paper 2015 – MS CS 1st Term UOS (CS-5143)

ProgramMS CS
Term1st
Year2015
Course codeCS-5143
AnswersSolved ✓

University of Sargodha MS Computer Science 1st term Advanced Analysis of Algorithms (CS-5143) paper, 2015 exam (80 marks, 3 hours). The paper has been retyped, and solved answers are given below for all short questions and all five long questions, including a Bellman-Ford dry run, proofs of optimal substructure and the cut property, counting sort and Huffman coding.

Solved answers

Q1 – Short answers (8 × 2)

1. Asymptotic analysis: Studying how the running time or space of an algorithm grows as the input size n tends to infinity, ignoring constant factors and lower-order terms. It is expressed with O, Ω and Θ notations.

2. Big-Oh notation: f(n) = O(g(n)) if there exist positive constants c and n0 such that 0 ≤ f(n) ≤ c·g(n) for all n ≥ n0. It gives an asymptotic upper bound, e.g. 3n² + 5n = O(n²).

3. Fast algorithm: An algorithm whose running time grows slowly with input size – usually polynomial and preferably near-linear (e.g. O(n log n) merge sort vs O(n²) bubble sort). In complexity terms, an algorithm is called efficient/fast if it runs in polynomial time.

4. Properties needed for dynamic programming: (i) Optimal substructure – an optimal solution contains optimal solutions to its subproblems; (ii) Overlapping subproblems – the same subproblems are solved again and again, so their results can be stored and reused.

5. Steps for designing a DP algorithm: 1) Characterise the structure of an optimal solution; 2) recursively define the value of an optimal solution; 3) compute the value bottom-up (or with memoisation) in a table; 4) construct an optimal solution from the computed information.

6. Main idea of dynamic programming: Solve each subproblem only once, store its answer in a table, and reuse it when needed instead of recomputing – trading memory for time (e.g. Fibonacci in O(n) instead of O(2n)).

7. Optimal substructure property: A problem has optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems, e.g. every subpath of a shortest path is itself a shortest path.

8. Longest common subsequence (LCS): Given two sequences X and Y, find a longest sequence Z that is a subsequence of both (characters in the same order, not necessarily contiguous). E.g. X = ABCBDAB, Y = BDCABA → LCS = BCBA (length 4). DP solves it in O(mn).

Q2 – Bellman-Ford algorithm and dry run

BELLMAN-FORD(G, w, s)
1  for each vertex v in G.V
2      v.d = ∞ ;  v.π = NIL
3  s.d = 0
4  for i = 1 to |V| − 1
5      for each edge (u, v) in G.E
6          RELAX(u, v, w):  if v.d > u.d + w(u,v)
7                               v.d = u.d + w(u,v) ;  v.π = u
8  for each edge (u, v) in G.E
9      if v.d > u.d + w(u,v)
10         return FALSE          // negative-weight cycle reachable
11 return TRUE

Running time is O(VE). The algorithm works with negative edge weights and detects negative cycles.

Dry run (source s; edges relaxed in the order (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)):

After passd(s)d(t)d(x)d(y)d(z)Changes
Initial0∞∞∞∞–
106∞7∞t = 6 (from s), y = 7 (from s)
206472x = 4 (via y: 7 − 3), z = 2 (via t: 6 − 4)
302472t = 2 (via x: 4 − 2)
40247−2z = −2 (via t: 2 − 4)

Final check: no edge can be relaxed further, so there is no negative cycle and the algorithm returns TRUE. Shortest distances: t = 2 (s→y→x→t), x = 4 (s→y→x), y = 7 (s→y), z = −2 (s→y→x→t→z). Predecessors: π(t) = x, π(x) = y, π(y) = s, π(z) = t. (Verified in Python.)

Q3 – Optimal substructure of shortest paths

Proof (cut-and-paste). Decompose p as v1 ⇝ vi ⇝ vj ⇝ vk, i.e. p = p1i · pij · pjk, so w(p) = w(p1i) + w(pij) + w(pjk).

Suppose, for contradiction, that pij is not a shortest path from vi to vj. Then there is another path p′ij from vi to vj with w(p′ij) < w(pij). Replace pij by p′ij to get the path p′ = p1i · p′ij · pjk from v1 to vk, whose weight is

w(p') = w(p1i) + w(p'ij) + w(pjk)  <  w(p1i) + w(pij) + w(pjk) = w(p)

So p′ is a path from v1 to vk lighter than p, contradicting the assumption that p is a shortest path. Hence pij must be a shortest path from vi to vj. ∎ This property is why greedy/DP algorithms (Dijkstra, Bellman-Ford, Floyd-Warshall) work.

Q4 – Correctness of the cut property

Cut property: Let G = (V, E) be a connected undirected weighted graph, S ⊂ V a cut (S, V − S), and e = (u, v) a minimum-weight edge crossing the cut (light edge). Then e belongs to some minimum spanning tree (if weights are distinct, to every MST). More generally (CLRS): if A is a subset of some MST and the cut respects A, a light edge crossing the cut is safe for A.

Proof (exchange argument). Let T be an MST that does not contain e. Adding e to T creates exactly one cycle. This cycle goes from u ∈ S to v ∈ V − S through e, so it must cross the cut again through some other edge e′ = (x, y) of T with x ∈ S, y ∈ V − S. Remove e′: T′ = T − {e′} ∪ {e} is still connected and has |V| − 1 edges, so it is a spanning tree. Since e is the lightest edge crossing the cut, w(e) ≤ w(e′), therefore

w(T') = w(T) − w(e') + w(e) ≤ w(T)

Because T is minimum, w(T′) = w(T), so T′ is also an MST and it contains e. (If all weights are distinct, w(e) < w(e′) would give w(T′) < w(T), a contradiction, so every MST contains e.) ∎ Kruskal’s and Prim’s algorithms are correct because every edge they add is a light edge across some cut.

Q5 – Counting sort on [4, 5, 3, 6, 3, 5, 4, 2]

Range of keys k = 6. Step 1 – count occurrences in C[0..6]:

Index0123456
C (counts)0012221
C (cumulative)0013578

Step 2 – cumulative counts tell the last position of each key. Step 3 – scan A from right to left, put A[j] at B[C[A[j]]] and decrement C[A[j]]:

jA[j]Placed at B[…]C after decrementB
821C[2] = 02 _ _ _ _ _ _ _
745C[4] = 42 _ _ _ 4 _ _ _
657C[5] = 62 _ _ _ 4 _ 5 _
533C[3] = 22 _ 3 _ 4 _ 5 _
468C[6] = 72 _ 3 _ 4 _ 5 6
332C[3] = 12 3 3 _ 4 _ 5 6
256C[5] = 52 3 3 _ 4 5 5 6
144C[4] = 32 3 3 4 4 5 5 6

Sorted output: 2, 3, 3, 4, 4, 5, 5, 6. Counting sort is stable and runs in Θ(n + k).

Q6 – Huffman coding

Repeatedly merge the two lowest-frequency nodes:

  1. A(10) + B(20) → 30
  2. C(30) + AB(30) → 60
  3. D(40) + E(50) → 90
  4. F(60) + CAB(60) → 120
  5. G(70) + DE(90) → 160
  6. 120 + 160 → root (280)
LetterFrequencyCodeBits
A1001104
B2001114
C300103
D401103
E501113
F60002
G70102

Total bits = 10·4 + 20·4 + 30·3 + 40·3 + 50·3 + 60·2 + 70·2 = 740 bits (average 2.64 bits/letter), compared with 280 × 3 = 840 bits for a fixed 3-bit code. Codes are prefix-free (left = 0, right = 1; other tie-breaking choices give different codes but the same total). Checked with Python’s heapq.

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.