Blog
Advanced Algorithm Analysis Past Paper 2014 – MS CS 1st Term UOS (CS-5143)
University of Sargodha Advanced Algorithm Analysis (CS-5143) paper for MS Computer Science 1st term, 2014 exam, session 2014-16 Spring (80 marks, 3 hours). The paper has been retyped, including the shortest-path graph, and solved answers for every question are given below.
Solved answers
Q1 – Short answers (2 × 8)
i. Bipartite graph with example: A graph whose vertices can be split into two sets U and V so that every edge joins a vertex of U to a vertex of V (no edge inside a set). Example: U = {1, 2}, V = {a, b, c} with edges 1–a, 1–b, 2–b, 2–c. A graph is bipartite exactly when it has no odd-length cycle.
ii. What is dynamic programming? A technique that solves a problem by breaking it into overlapping sub-problems, solving each sub-problem once and storing the answer in a table (memoization or bottom-up). It needs optimal substructure and overlapping sub-problems – e.g. matrix-chain multiplication, LCS, 0/1 knapsack.
iii. What is asymptotic complexity? The growth rate of an algorithm’s running time or space as the input size n → ∞, ignoring constants and lower-order terms. It is written with O (upper bound), Ω (lower bound) and Θ (tight bound), e.g. merge sort is Θ(n log n).
iv. Define algorithm. A finite sequence of well-defined, unambiguous steps that takes input, produces output and always terminates, solving a given problem (properties: input, output, definiteness, finiteness, effectiveness).
v. What are multithreaded algorithms? Algorithms designed to run on several processors sharing memory, using constructs such as spawn, sync and parallel for. Their cost is measured by work T1, span T∞ and parallelism T1/T∞ (e.g. parallel merge sort).
vi. What are Fibonacci heaps? A mergeable-heap data structure made of a collection of heap-ordered trees. Insert, union, find-min and decrease-key take O(1) amortized time and extract-min O(log n) amortized, which speeds up Dijkstra’s and Prim’s algorithms to O(E + V log V).
vii. What are maximum flow networks? A directed graph G = (V, E) with a source s, a sink t and a capacity c(u, v) ≥ 0 on each edge. A flow respects capacity and flow conservation; the maximum-flow problem finds the largest total flow from s to t (Ford–Fulkerson / Edmonds–Karp; max-flow = min-cut).
viii. Define parallel algorithms. Algorithms that divide a problem into parts executed simultaneously on multiple processors and then combine the results, to reduce running time (e.g. parallel prefix sum, parallel merge sort on PRAM or multicore).
Q2 – Dynamic programming (16)
a) Matrix-chain order for A (10×30), B (30×5), C (5×60). Dimensions p = ⟨10, 30, 5, 60⟩.
- (AB)C: AB costs 10·30·5 = 1,500, then (10×5)(5×60) costs 10·5·60 = 3,000 → 4,500 multiplications.
- A(BC): BC costs 30·5·60 = 9,000, then (10×30)(30×60) costs 10·30·60 = 18,000 → 27,000 multiplications.
DP table: m[1,2] = 1,500, m[2,3] = 9,000, m[1,3] = min(m[1,1] + m[2,3] + 10·30·60, m[1,2] + m[3,3] + 10·5·60) = min(27,000, 4,500) = 4,500 with split k = 2. Optimal parenthesization: ((A·B)·C), cost 4,500.
b) LCS of X = XMJYAUZ and Y = MZJAWXU. Fill the table c[i, j] = c[i−1, j−1] + 1 if xi = yj, else max(c[i−1, j], c[i, j−1]). The last cell gives length 4; tracing back the arrows gives LCS = MJAU (M, J, A, U appear in this order in both strings).
Q3 – Greedy algorithms (16)
a) Huffman coding (A 24, B 12, C 10, D 8, E 8; total 62).
- Merge the two smallest: D(8) + E(8) = 16.
- Next two smallest: C(10) + B(12) = 22.
- Merge 16 + 22 = 38.
- Merge A(24) + 38 = 62 (root).
Taking left = 0, right = 1: A = 0, D = 100, E = 101, C = 110, B = 111. Total bits = 24·1 + 12·3 + 10·3 + 8·3 + 8·3 = 138 bits, against 62 × 3 = 186 bits with a fixed 3-bit code.
b) Coins for Rs 21 with {1, 2, 7, 8}. The greedy choice (largest coin first) gives 8 + 8 + 2 + 2 + 1 = 5 coins, which is not optimal. Dynamic programming (min coins[s] = 1 + min over coins c ≤ s of coins[s − c]) gives 3 coins: 7 + 7 + 7. This shows the greedy method fails for this coin system.
Q4 – Shortest paths from s (16)
Edges: s→b 7, s→a 2, a→b 3, b→a 2, b→c 1, a→c 8, a→d 5, c→d 4, d→c 5.
Dijkstra: start d(s)=0, others ∞.
- Extract s: a = 2, b = 7.
- Extract a (2): b = min(7, 2+3) = 5, c = 2+8 = 10, d = 2+5 = 7.
- Extract b (5): c = min(10, 5+1) = 6.
- Extract c (6): d = min(7, 6+4) = 7.
- Extract d (7): no change.
Bellman-Ford: relax all 9 edges |V|−1 = 4 times. Pass 1 already gives a = 2, b = 5, c = 6, d = 7 (in the order listed); passes 2–4 change nothing, and a 5th check finds no negative cycle.
Result: d(a) = 2 (s→a), d(b) = 5 (s→a→b), d(c) = 6 (s→a→b→c), d(d) = 7 (s→a→d).
Q5 – Amortized analysis of a stack with Multipop (16)
Operations: PUSH O(1), POP O(1), MULTIPOP(S, k) pops min(s, k) items. A single MULTIPOP can cost O(n), so a naive bound for n operations is O(n²).
1. Aggregate method: each object can be popped at most once for each time it is pushed. In n operations there are at most n pushes, so total pops (including inside MULTIPOP) ≤ n. Total cost ≤ 2n = O(n); amortized cost per operation = O(1).
2. Accounting method: charge PUSH 2, POP 0, MULTIPOP 0. When pushing, 1 pays for the push and 1 is stored as credit on the object. Every pop is paid by that stored credit, so credit never becomes negative. Total charge ≤ 2n = O(n).
(3. Potential method: Φ = number of objects in the stack, Φ ≥ 0. PUSH: 1 + 1 = 2; POP: 1 − 1 = 0; MULTIPOP of k′ items: k′ − k′ = 0. Every amortized cost is O(1).)
Q6 – Notes (6 + 5 + 5)
a) Vertex cover: a set C ⊆ V such that every edge has at least one endpoint in C; finding a minimum vertex cover is NP-complete. APPROX-VERTEX-COVER repeatedly picks any remaining edge (u, v), adds both u and v to C and deletes all edges touching them; it runs in O(V + E) and is a 2-approximation.
b) Travelling salesman: find a minimum-cost tour visiting every city exactly once and returning to the start; NP-hard. With the triangle inequality, build a minimum spanning tree, walk it in preorder and shortcut repeated vertices – a 2-approximation (Christofides gives 1.5).
c) Set covering: given a universe X and a family F of subsets whose union is X, choose the fewest subsets that still cover X; NP-hard. Greedy-Set-Cover repeatedly picks the set covering the most uncovered elements; it is an H(max |S|) ≈ ln n + 1 approximation.
Q7 – Naïve and Rabin-Karp string matching (16)
Naïve: for every shift s = 0 … n−m compare pattern P[1..m] with T[s+1..s+m] character by character. Worst case O((n−m+1)m). Example T = AABAACAADAABAABA, P = AABA: matches found at shifts 0, 9 and 12.
Rabin-Karp: treat strings as numbers in base d and compare hash values modulo a prime q.
- Compute p = hash(P) and t0 = hash(T[1..m]) using Horner’s rule.
- For each shift, if ts = p, verify characters (to rule out a spurious hit).
- Roll the hash: ts+1 = (d(ts − T[s+1]·h) + T[s+m+1]) mod q, where h = dm−1 mod q.
Example with digits, q = 13, P = 31415: p = 31415 mod 13 = 7. Sliding over T = 2359023141526739921, windows with value ≡ 7 are checked; 31415 is a valid match and 67399 is a spurious hit. Expected time O(n + m), worst case O((n−m+1)m).
