Blog
Design and Analysis of Algorithms Past Paper 2015 – BSCS 5th Term UOS (CS-310)
University of Sargodha BS Computer Science 5th term Design & Analysis of Algorithms (CS-310) paper, 2015 exam (80 marks, 2:30 hours). The paper is retyped below without the handwritten ticks, followed by solved answers for all 16 short questions and all five subjective questions: merge sort steps, greedy algorithms, a hash table with linear probing, dynamic programming with a knapsack table, and P, NP and NP-complete. The worked answers were checked with Python.
Solved answers
Q1 – Short answers (16 × 2)
i. Algorithm. A finite sequence of well-defined, unambiguous steps that takes input and produces the required output in finite time – e.g. binary search. Properties: input, output, definiteness, finiteness, effectiveness.
ii. Asymptotic complexity. The growth rate of an algorithm’s running time or space as input size n → ∞, ignoring constants and lower-order terms, expressed with O, Ω and Θ (e.g. 3n² + 5n + 2 = Θ(n²)).
iii. Data structure. A way of organising and storing data so it can be accessed and modified efficiently – arrays, linked lists, stacks, queues, trees, heaps, hash tables, graphs.
iv. Fast machine or fast algorithm? Fast algorithms matter more. A faster computer only gives a constant-factor gain, while a better algorithm changes the growth rate: sorting 107 numbers with insertion sort (n²) on a 100× faster machine is still far slower than merge sort (n lg n) on the slow one.
v. Constant time. Running time that does not depend on input size, O(1) – e.g. accessing an array element by index or pushing onto a stack.
vi. Big O notation. f(n) = O(g(n)) if there exist constants c > 0 and n0 such that 0 ≤ f(n) ≤ c·g(n) for all n ≥ n0 – an asymptotic upper bound. Example: 2n + 3 = O(n).
vii. Worst-case efficiency. The maximum running time over all inputs of size n – e.g. linear search makes n comparisons when the key is last or absent. It guarantees an upper limit.
viii. Average-case efficiency. The expected running time over all inputs of size n under an assumed probability distribution – e.g. successful linear search takes about (n + 1)/2 comparisons.
ix. NP problems. Decision problems whose solutions can be verified in polynomial time by a deterministic machine (or solved in polynomial time by a non-deterministic one) – e.g. SAT, Hamiltonian cycle, subset sum.
x. Recurrence relations. Equations that define a function in terms of its values on smaller inputs, used for recursive algorithms – e.g. merge sort T(n) = 2T(n/2) + n, T(1) = 1, solved by substitution, recursion tree or master method.
xi. B-trees. Balanced multi-way search trees designed for disks: each node has many keys (between t − 1 and 2t − 1) and children; all leaves are at the same depth, so search, insert and delete take O(log n) disk accesses. Used in databases and file systems.
xii. Hashing. Storing and finding keys in a table using a hash function h(k) that maps a key to an index, giving O(1) average search/insert/delete; collisions are handled by chaining or open addressing.
xiii. Heaps. Nearly complete binary trees stored in arrays with the heap property – parent ≥ children (max-heap) or parent ≤ children (min-heap). Used for heapsort and priority queues.
xiv. Good solution. A solution that is correct, terminates, is efficient in time and space (good asymptotic complexity), and is simple to understand, implement and maintain; for optimisation problems it is optimal or close to optimal.
xv. Heuristic. A rule of thumb or shortcut that finds a good solution quickly but without a guarantee of optimality or correctness – used for hard (NP-hard) problems, e.g. nearest-neighbour for TSP, A* search heuristics.
xvi. Linear search. Checking each element of a list one by one until the key is found or the list ends; O(n) worst case, works on unsorted data.
Q2 – Merge sort steps
Data: 10, 5, 90, 65, 50, 35, 100, 30, 85, 15, 70, 75, 55, 40, 20, 80 (16 elements).
Divide repeatedly into halves until each sub-list has one element:
[10 5 90 65 50 35 100 30 | 85 15 70 75 55 40 20 80]
[10 5 90 65 | 50 35 100 30] [85 15 70 75 | 55 40 20 80]
[10 5|90 65] [50 35|100 30] [85 15|70 75] [55 40|20 80]
[10][5][90][65][50][35][100][30][85][15][70][75][55][40][20][80]Merge pairs of sorted lists back together:
Pass 1: [5 10] [65 90] [35 50] [30 100] [15 85] [70 75] [40 55] [20 80]
Pass 2: [5 10 65 90] [30 35 50 100] [15 70 75 85] [20 40 55 80]
Pass 3: [5 10 30 35 50 65 90 100] [15 20 40 55 70 75 80 85]
Pass 4: [5 10 15 20 30 35 40 50 55 65 70 75 80 85 90 100]Example merge (pass 4): compare fronts 5 vs 15 → 5; 10 vs 15 → 10; 30 vs 15 → 15; 30 vs 20 → 20; 30 vs 40 → 30; … until one list is empty, then copy the rest.
Complexity: lg 16 = 4 levels, each merging level costs Θ(n), so T(n) = 2T(n/2) + Θ(n) = Θ(n lg n) in all cases; extra space Θ(n). (Steps produced by a Python program.)
Q3 – Greedy algorithms
A greedy algorithm builds a solution step by step, always choosing the option that looks best at the moment (locally optimal), never reconsidering earlier choices. It gives an optimal solution when the problem has:
- Greedy-choice property – a global optimum can be reached by making locally optimal choices.
- Optimal substructure – an optimal solution contains optimal solutions to subproblems.
General structure: candidate set, selection function (pick best), feasibility check, objective function, solution check.
Example 1 – Activity selection. Activities (start, finish): a1(1,4), a2(3,5), a3(0,6), a4(5,7), a5(3,9), a6(5,9), a7(6,10), a8(8,11), a9(8,12), a10(2,14), a11(12,16). Sort by finish time and always pick the activity that finishes first and is compatible: a1 (ends 4) → a4 (starts 5, ends 7) → a8 (starts 8, ends 11) → a11 (starts 12). Maximum set = {a1, a4, a8, a11}, 4 activities. Time O(n lg n) for sorting.
Example 2 – Coin change with coins 50, 20, 10, 5, 1 for Rs. 87: take 50 → 37 left, 20 → 17, 10 → 7, 5 → 2, 1, 1 → 6 coins. (For some coin systems, e.g. {1, 3, 4} for 6, greedy gives 4+1+1 = 3 coins but 3+3 = 2 coins is optimal – greedy is not always optimal.)
Other greedy algorithms: Kruskal’s and Prim’s MST, Dijkstra’s shortest paths, Huffman coding, fractional knapsack. Advantages: simple and fast. Disadvantage: must be proved correct for each problem.
Q4 – Hash table of size 16 with linear probing
Hash function h(k) = k mod 16; on collision try (h(k) + 1) mod 16, (h(k) + 2) mod 16, …
| Key | k mod 16 | Probes | Stored at |
|---|---|---|---|
| 10 | 10 | — | 10 |
| 5 | 5 | — | 5 |
| 90 | 10 | 10 full | 11 |
| 65 | 1 | — | 1 |
| 50 | 2 | — | 2 |
| 35 | 3 | — | 3 |
| 100 | 4 | — | 4 |
| 30 | 14 | — | 14 |
| 85 | 5 | 5 full | 6 |
| 15 | 15 | — | 15 |
| 70 | 6 | 6 full | 7 |
| 75 | 11 | 11 full | 12 |
| 55 | 7 | 7 full | 8 |
| 40 | 8 | 8 full | 9 |
| 20 | 4 | 4,5,6,7,8,9,10,11,12 full | 13 |
| 80 | 0 | — | 0 |
Final table
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Key | 80 | 65 | 50 | 35 | 100 | 5 | 85 | 70 | 55 | 40 | 10 | 90 | 75 | 20 | 30 | 15 |
The table is now completely full (load factor 1). Key 20 needed 9 extra probes because of primary clustering, the main weakness of linear probing. (Computed with a Python program.)
Q5 – Dynamic programming
Dynamic programming (DP) solves a problem by breaking it into overlapping subproblems, solving each subproblem only once and storing the answers in a table, then combining them. It applies when the problem has optimal substructure and overlapping subproblems. Approaches: top-down with memoization, or bottom-up tabulation.
Steps: (1) characterise the structure of an optimal solution, (2) define the value recursively, (3) compute values bottom-up, (4) construct the optimal solution.
Example – 0/1 knapsack. Capacity W = 5; items (weight, value): 1 (2, 12), 2 (1, 10), 3 (3, 20), 4 (2, 15). V[i][w] = max(V[i−1][w], vi + V[i−1][w − wi]) if wi ≤ w.
| i \ w | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 (2,12) | 0 | 0 | 12 | 12 | 12 | 12 |
| 2 (1,10) | 0 | 10 | 12 | 22 | 22 | 22 |
| 3 (3,20) | 0 | 10 | 12 | 22 | 30 | 32 |
| 4 (2,15) | 0 | 10 | 15 | 25 | 30 | 37 |
Maximum value = 37 with items 1, 2 and 4 (weights 2 + 1 + 2 = 5, values 12 + 10 + 15). Time Θ(nW). Without DP, trying all subsets takes Θ(2n).
Other DP examples: Fibonacci with memoization (O(n) instead of O(2n)), longest common subsequence, matrix-chain multiplication, Floyd-Warshall, Bellman-Ford.
Q6 – P, NP and NP-complete
| Class | Definition | Examples |
|---|---|---|
| P | Decision problems solvable by a deterministic algorithm in polynomial time O(nk) | Sorting, shortest path, MST, 2-SAT, primality testing |
| NP | Decision problems whose “yes” answers can be verified in polynomial time given a certificate (or solved in polynomial time by a non-deterministic machine). P ⊆ NP | SAT, Hamiltonian cycle, subset sum, graph colouring, all of P |
| NP-complete (NPC) | Problems that are in NP and NP-hard: every NP problem reduces to them in polynomial time | SAT (Cook-Levin), 3-SAT, vertex cover, clique, Hamiltonian cycle, TSP (decision), 0/1 knapsack (decision) |
| NP-hard | At least as hard as every NP problem; need not be in NP | Halting problem, TSP optimisation |
Relationship: P ⊆ NP and NPC ⊆ NP. If any NP-complete problem is solved in polynomial time, then P = NP. Whether P = NP is the most famous open problem in computer science; most believe P ≠ NP.
Proving a problem X is NP-complete: (1) show X ∈ NP (a certificate is checkable in polynomial time); (2) reduce a known NP-complete problem Y to X in polynomial time (Y ≤p X). For NP-complete problems we use approximation algorithms, heuristics, or exponential exact algorithms for small inputs.
