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

Analysis of Algorithm Past Paper 2015 – MSc IT 4th Term UOS (CS-610, Old Course)

ProgramMSc IT
Term4th
Year2015
Course codeCS-610
AnswersSolved ✓

University of Sargodha M.Sc (IT) 4th term Analysis of Algorithm (CS-610, old course) final 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 six long questions, including quick sort, a circular queue program, sorting complexities, searching the tree for Section 1.2.2, insertion sort and Dijkstra’s algorithm.

Solved answers

Q1 – Short answers (16 × 2)

1. Algorithm: A finite sequence of well-defined, unambiguous steps that takes input and produces the required output in a finite time.

2. Cycle in a graph: A path that starts and ends at the same vertex with no other repeated vertices, e.g. A → B → C → A.

3. Divide and conquer: Divide the problem into smaller sub-problems of the same type, solve them recursively (conquer), and combine their solutions – e.g. merge sort, quick sort, binary search.

4. Complexity of an algorithm: The amount of resources (time and memory) an algorithm needs as a function of input size n, usually expressed asymptotically (e.g. O(n log n)).

5. Efficiency of an algorithm: How well an algorithm uses time and space; an efficient algorithm solves the problem with fewer steps and less memory, i.e. lower time and space complexity.

6. Notations for complexity: Big-O (upper bound), Big-Omega Ω (lower bound), Big-Theta Θ (tight bound), and little-o, little-omega.

7. Binary search: Searching a sorted array by comparing the key with the middle element and continuing in the left or right half; O(log n).

8. Optimal solution: The best feasible solution according to the objective – e.g. minimum cost or maximum profit – among all possible solutions.

9. Dynamic programming: A technique for problems with overlapping sub-problems and optimal substructure: solve each sub-problem once, store the result in a table and reuse it (e.g. Fibonacci, LCS, knapsack).

10. Complete binary search tree: A binary search tree in which every level is completely filled except possibly the last, whose nodes are as far left as possible; its height is ⌊log2 n⌋.

11. Two N-times nested loops: O(N²) – the inner loop runs N times for each of the N iterations of the outer loop.

12. When not to use sequential search: When the list is large and sorted (binary search is faster), or when many searches are needed (use hashing or a search tree); sequential search is O(n).

13. Methods to represent a graph: Adjacency matrix, adjacency list, incidence matrix, and edge list.

14. Recursion: A function that calls itself with a smaller input, with a base case that stops the calls, e.g. factorial(n) = n × factorial(n − 1).

15. Why use algorithms? To solve problems systematically and correctly, to analyse and compare efficiency before coding, and to reuse proven solutions.

16. A* search algorithm: A best-first graph search that finds the shortest path using f(n) = g(n) + h(n), where g is the cost so far and h a heuristic estimate to the goal; with an admissible heuristic it is optimal (used in games and maps).

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 – Insert and delete in a circular queue

#include <iostream>
using namespace std;
#define SIZE 5
int q[SIZE], front = -1, rear = -1;

void insert(int x) {
    if ((rear + 1) % SIZE == front) { cout << "Queue is full\n"; return; }
    if (front == -1) front = 0;
    rear = (rear + 1) % SIZE;
    q[rear] = x;
}
int remove() {
    if (front == -1) { cout << "Queue is empty\n"; return -1; }
    int x = q[front];
    if (front == rear) front = rear = -1;      // queue becomes empty
    else front = (front + 1) % SIZE;
    return x;
}
void display() {
    if (front == -1) { cout << "Empty\n"; return; }
    for (int i = front; ; i = (i + 1) % SIZE) { cout << q[i] << " "; if (i == rear) break; }
    cout << "\n";
}
int main() {
    insert(10); insert(20); insert(30); insert(40); insert(50);
    insert(60);                      // Queue is full
    cout << remove() << " deleted\n"; // 10 deleted
    insert(60);                      // wraps around to index 0
    display();                       // 20 30 40 50 60
}

The indices wrap with (index + 1) % SIZE, so empty slots at the front are reused. (Compiled with g++ and run – output as shown in the comments.)

Q4 – Time complexity and two sorting algorithms

Time complexity is the number of basic operations an algorithm performs as a function of input size n, expressed asymptotically with O, Ω or Θ. It lets us compare algorithms independently of the machine.

AlgorithmBestAverageWorstSpaceRemarks
Insertion sortO(n) (already sorted)O(n²)O(n²) (reverse order)O(1)Stable, good for small or nearly sorted data
Merge sortO(n log n)O(n log n)O(n log n)O(n)Stable; T(n) = 2T(n/2) + n
Quick sortO(n log n)O(n log n)O(n²)O(log n)In-place; fastest in practice

Insertion sort: in the worst case element i is compared with all i − 1 earlier elements: 1 + 2 + … + (n − 1) = n(n − 1)/2 = O(n²). Merge sort: the recurrence T(n) = 2T(n/2) + cn has log2n levels each costing cn, so T(n) = O(n log n).

Q5 – Binary search on the tree for “Section 1.2.2”

The tree is ordered like a book index: in each node the left child holds the lower-numbered part and the right child the higher-numbered part, so it can be searched like a binary search tree by comparing the section numbers.

PassCurrent nodeComparisonMove
1BookTarget is in chapter 1 (1.2.2 starts with 1)go left to Chapter1
2Chapter11.2.2 > 1.1, it belongs under 1.2go right to Section 1.2
3Section 1.21.2.2 > 1.2.1go right to Section 1.2.2
4Section 1.2.2equalfound

The item is found in 4 comparisons (tree height 3), instead of up to 11 node visits in a sequential search; each pass discards one subtree, which is why the search is O(log n) on a balanced tree.

Q6 – Insertion sort on A = {12, 11, 13, 5, 6}

PassKeyArray after pass
Start–12, 11, 13, 5, 6
11111, 12, 13, 5, 6
21311, 12, 13, 5, 6 (no shift)
355, 11, 12, 13, 6
465, 6, 11, 12, 13

Each key is inserted into its place in the sorted left part by shifting larger elements one position right. Sorted array: 5, 6, 11, 12, 13 (checked in Python).

Q7 – Dijkstra’s algorithm and its uses

Dijkstra’s algorithm finds the shortest paths from a source vertex s to all other vertices in a weighted graph with non-negative edge weights. It is greedy: it repeatedly selects the unvisited vertex with the smallest tentative distance and relaxes its outgoing edges.

DIJKSTRA(G, w, s)
  for each v in V: d[v] = ∞, π[v] = NIL
  d[s] = 0;  S = ∅;  Q = V          // min-priority queue on d
  while Q ≠ ∅
      u = EXTRACT-MIN(Q);  S = S ∪ {u}
      for each v adjacent to u
          if d[v] > d[u] + w(u, v)
              d[v] = d[u] + w(u, v);  π[v] = u

Running time: O(V²) with an array, O((V + E) log V) with a binary heap, O(E + V log V) with a Fibonacci heap.

Example: edges A–B 4, A–C 1, C–B 2, B–D 1, C–D 5. From A: C = 1, then B = min(4, 1 + 2) = 3, then D = min(1 + 5, 3 + 1) = 4.

Uses: GPS and map navigation (shortest route), network routing protocols such as OSPF (link-state routing), flight and transport planning, robot path planning, telephone/network design and as part of other algorithms (e.g. Johnson’s). It does not work with negative edge weights – Bellman-Ford is used then.

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.