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

Design and Analysis of Algorithms Past Paper 2015 – BSCS 5th Term UOS (CS-310)

ProgramBSCS
Term5th
Year2015
Course codeCS-310
AnswersSolved ✓

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, …

Keyk mod 16ProbesStored at
1010—10
55—5
901010 full11
651—1
502—2
353—3
1004—4
3014—14
8555 full6
1515—15
7066 full7
751111 full12
5577 full8
4088 full9
2044,5,6,7,8,9,10,11,12 full13
800—0

Final table

Index0123456789101112131415
Key80655035100585705540109075203015

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 \ w012345
0000000
1 (2,12)0012121212
2 (1,10)01012222222
3 (3,20)01012223032
4 (2,15)01015253037

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

ClassDefinitionExamples
PDecision problems solvable by a deterministic algorithm in polynomial time O(nk)Sorting, shortest path, MST, 2-SAT, primality testing
NPDecision problems whose “yes” answers can be verified in polynomial time given a certificate (or solved in polynomial time by a non-deterministic machine). P ⊆ NPSAT, 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 timeSAT (Cook-Levin), 3-SAT, vertex cover, clique, Hamiltonian cycle, TSP (decision), 0/1 knapsack (decision)
NP-hardAt least as hard as every NP problem; need not be in NPHalting 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.

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.