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

Analysis of Algorithms Past Paper 2015 – MSc IT 4th Term UOS (CS-610)

ProgramMSc IT
Term4th
Year2015
Course codeCS-610
AnswersSolved ✓

University of Sargodha M.Sc. IT 4th term Analysis of Algorithms (CS-610) paper, 2015 exam (80 marks, 2:30 hours). The paper has been retyped, and solved answers are given below for all 16 short questions and all five long questions, including quick sort, sparse matrices, the size-16 hash table with linear probing, dynamic programming (knapsack) and the KMP string-matching algorithm.

Solved answers

Q1 – Short answers (16 × 2)

A. Constant time: An operation whose running time does not depend on input size – O(1), e.g. accessing an array element by index.

B. Average-case efficiency: The expected running time over all inputs of size n, assuming a probability distribution of inputs (e.g. sequential search needs about n/2 comparisons on average).

C. Recursion: A technique in which a function calls itself on a smaller sub-problem until a base case is reached.

D. Time complexity: The number of basic operations an algorithm performs as a function of input size n, expressed asymptotically.

E. Cluster graph: A graph that is a disjoint union of complete graphs (cliques) – every connected component is a clique; used in clustering problems (cluster editing).

F. How a non-deterministic computer solves a problem: It “guesses” a candidate solution in one step (choosing among many possibilities in parallel) and then verifies it in polynomial time; problems solvable this way are in class NP.

G. Worst-case efficiency: The maximum running time over all inputs of size n – an upper guarantee, e.g. O(n²) for quick sort.

H. Binary search: Search in a sorted array by repeatedly comparing the key with the middle element and discarding half the array – O(log n).

I. Big-O notation: f(n) = O(g(n)) if there are constants c > 0 and n0 such that f(n) ≤ c·g(n) for all n ≥ n0; an asymptotic upper bound.

J. Main advantage of data structures: They organise data so it can be accessed and modified efficiently, which makes algorithms faster and memory use better (e.g. a hash table gives O(1) average search).

K. Collision in hashing: When two different keys produce the same hash address, e.g. h(15) = h(31) = 15 with mod 16; it must be resolved by probing or chaining.

L. Asymptotic complexity: The growth rate of an algorithm’s running time or space as n → ∞, ignoring constants and lower-order terms.

M. Optimal solution: The best of all feasible solutions with respect to the objective function (minimum cost, maximum profit, shortest path).

N. A loop that iterates N times: O(N), assuming its body takes constant time.

O. Why we use algorithms: To solve problems in a clear, correct and efficient way, to analyse cost before implementation and to reuse solutions.

P. Chaining: A collision-resolution technique in which each hash-table slot holds a linked list of all keys that hash to that slot.

Q2 – Quick sort with an example

Quick sort is a divide-and-conquer algorithm: choose a pivot, partition the array so that smaller elements are on the left and larger on the right, then recursively sort the two parts.

QUICKSORT(A, p, r)
  if p < r
     q = PARTITION(A, p, r)
     QUICKSORT(A, p, q - 1)
     QUICKSORT(A, q + 1, r)

PARTITION(A, p, r)          // Lomuto, last element as pivot
  x = A[r];  i = p - 1
  for j = p to r - 1
     if A[j] <= x
        i = i + 1;  swap A[i], A[j]
  swap A[i + 1], A[r]
  return i + 1

Example: A = [10, 80, 30, 90, 40, 50, 70], pivot 70.

StepArrayNote
j = 10[10, 80, 30, 90, 40, 50, 70]10 ≤ 70, i = 0 (stays)
j = 30[10, 30, 80, 90, 40, 50, 70]swap 80 and 30
j = 40[10, 30, 40, 90, 80, 50, 70]swap 80 and 40
j = 50[10, 30, 40, 50, 80, 90, 70]swap 90 and 50
place pivot[10, 30, 40, 50, 70, 90, 80]swap 80 and 70; pivot at index 4

Recursively sort [10, 30, 40, 50] and [90, 80] → final [10, 30, 40, 50, 70, 80, 90]. Time: best/average O(n log n), worst O(n²) (already-sorted input with bad pivot); in-place, not stable. (Trace checked in Python.)

Q3 – Sparse vs dense matrix

A sparse matrix has most of its elements equal to zero (e.g. a 1000 × 1000 matrix with 2,000 non-zero values); a dense matrix has most elements non-zero.

Suitable data structures: a dense matrix is stored in an ordinary 2-D array (row-major or column-major), giving O(1) access. A sparse matrix should store only the non-zero values to save memory and time:

  • Triplet (coordinate) representation: an array of (row, column, value) entries.
  • Linked list representation: each node holds row, column, value and a next pointer (or orthogonal lists linking rows and columns).
  • Compressed Sparse Row (CSR): arrays values[], col_index[] and row_ptr[].
Matrix            Triplet form (row, col, value)
0 0 3 0           (0, 2, 3)
0 0 0 0           (2, 1, 5)
0 5 0 0           (3, 3, 7)
0 0 0 7

For an m × n matrix with k non-zeros, the triplet form needs 3k values instead of m·n – a saving whenever k < mn/3.

Q4 – Hash table of size 16 with linear probing

Hash function h(k) = k mod 16; on a collision, try (h(k) + 1) mod 16, (h(k) + 2) mod 16, …

Keyh(k) = k mod 16Slot used
222
555
777
2599
151515
101010
2044
301414
1711
131313
Index0123456789101112131415
Key–172–205–7–2510––133015

With this hash function every key lands in a different slot, so no collision occurs and linear probing is never needed (checked in Python). If, for example, 31 were inserted next, h(31) = 15 is occupied, so it would probe 0 and be stored at index 0.

Q5 – Dynamic programming with an example

Dynamic programming (DP) solves a problem by combining solutions of overlapping sub-problems, solving each sub-problem only once and storing its answer (memoisation – top down; or tabulation – bottom up). It needs optimal substructure and overlapping sub-problems.

Example – 0/1 knapsack, capacity W = 5, items (weight, value): (2, 3), (3, 4), (4, 5), (5, 6). K[i][w] = max(K[i−1][w], vi + K[i−1][w − wi]).

Items \ w012345
0 items000000
item 1 (2,3)003333
item 2 (3,4)003447
item 3 (4,5)003457
item 4 (5,6)003457

Maximum value = 7 by taking items 1 and 2 (weights 2 + 3 = 5). DP takes O(nW) time instead of trying all 2n subsets. Other DP examples: Fibonacci, LCS, matrix-chain multiplication, Floyd-Warshall. (Table computed in Python.)

Q6 – String matching: the KMP algorithm

String matching finds all occurrences of a pattern P (length m) in a text T (length n). The naive method tries every shift: O(nm). The Knuth-Morris-Pratt (KMP) algorithm runs in O(n + m) by never re-examining text characters: it pre-computes a prefix (failure) function π that tells how far the pattern can shift after a mismatch.

Example: pattern P = “ABABC”. Prefix function (length of the longest proper prefix that is also a suffix):

i12345
P[i]ABABC
π[i]00120

Text T = “ABABDABABC”. KMP matches A, B, A, B, then mismatches D with C at text position 5; instead of restarting, it sets q = π[4] = 2 (the “AB” already matched), compares D with P[3] = A (mismatch), q = π[2] = 0, and continues. Later it matches A B A B C at text positions 6–10, so the pattern occurs at shift 5 (0-based). Other string-matching algorithms: naive, Rabin-Karp (hashing), Boyer-Moore, finite-automaton matcher.

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.