Design and Analysis of Algorithm, Past Papers

Design & Analysis of Algorithms Past Paper 2024 – BS 4th Term (UOS)

Design and Analysis of Algorithms Past Paper 2024 UOS (CSCC-202)

University: University of Sargodha (UOS)
Program: BS 4th Term
Subject: Design & Analysis of Algorithms (CSCC-202)
Year: 2024
Time Allowed: 2 Hours 30 Minutes
Maximum Marks: 60


Why Use This Algorithms Past Paper?

This past paper helps students understand exam format, important topics and commonly repeated questions. Practicing past papers improves problem solving speed and confidence.

  • Understand algorithm concepts
  • Practice short definitions
  • Prepare complex algorithm problems
  • Improve exam writing technique

Design & Analysis of Algorithms Past Paper 2024 (Complete)

Note: Objective part is compulsory. Attempt any three questions from subjective part.

Objective Part (Compulsory)

Q.1 Write short answers of the following in 2–3 lines each. (2×12)

  1. What is a loop invariant?
  2. Define average case complexity.
  3. Other than speed, what measures of efficiency are useful?
  4. What is an in-place algorithm?
  5. Give a recurrence describing worst-case binary search.
  6. Is 2n+1 = O(2n)? Explain.
  7. Does the array 20,15,18,7,9,5,12,3,6,2 form a max-heap?
  8. Can heapsort be used as auxiliary sorting in radix sort?
  9. What is a pseudograph?
  10. How many vertices are in Cn graph?
  11. Advantages of adjacency list vs adjacency matrix?
  12. Insertion cost for inserting into max-heap?

Subjective Part (Attempt any Three)

Q.2 Algorithm to locate last occurrence of largest number.

Q.3 Sort array of −1, 0, 1 in O(n).

Q.4 Algorithm to check path existence in directed graph.

Q.5 Detect positive cycle in directed acyclic graph.

Q.6 Single-source longest path in DAG.


Download

Download Algorithms Past Paper 2024 (PDF)


Related Past Papers

More papers coming soon – stay connected!


Short Question Solutions

Click to view answers:

1. What is a loop invariant? A loop invariant is a logical condition that remains true before and after every iteration of a loop. It is used to prove correctness of algorithms.
Reference: CLRS – Introduction to Algorithms
2. Define average case complexity. Average-case complexity measures expected running time of an algorithm over all possible valid inputs of given size.
Reference: GeeksforGeeks – Algorithm Analysis
3. Other measures of efficiency? Memory usage, scalability, stability, reliability, and implementation cost.
4. What is an in-place algorithm? An algorithm that uses only a constant amount of extra memory besides input storage.
5. Recurrence for binary search (worst case) T(n) = T(n/2) + 1
6. Is 2n+1 = O(2n)? Yes. 2n+1 = 2 × 2n = O(2n).
7. Does array form max heap? Yes — it satisfies heap property: every parent ≥ children.
8. Can heapsort be auxiliary for radix sort? No. Radix sort requires stable sorting; heapsort is not stable.
9. What is pseudograph? A graph that may contain self-loops and multiple edges.
10. Vertices in Cn? Cn contains n vertices.
11. Advantages of adjacency list? Uses less memory and is efficient for sparse graphs.
12. Max-heap insertion cost? O(log n)

Solved answers – DAA 2024

Short questions

  1. A loop invariant is a condition that is true before the loop starts and after every iteration. Proving initialization, maintenance and termination shows the algorithm is correct (e.g. in insertion sort, A[1..j−1] is always sorted).
  2. Average-case complexity is the expected running time over all inputs of size n, assuming a probability distribution of inputs (often all inputs equally likely), e.g. Θ(n log n) for quicksort.
  3. Memory/space used, number of disk or I/O accesses, network communication, energy/power consumption, cache behaviour, and simplicity (cost to implement and maintain).
  4. An in-place algorithm transforms the input using only a constant (or very small, e.g. O(log n)) amount of extra memory – e.g. insertion sort, heapsort, quicksort.
  5. T(n) = T(n/2) + c, T(1) = c, which solves to T(n) = Θ(log n).
  6. Yes. 2n+1 = 2 · 2n ≤ c · 2n for c = 2 and all n ≥ 1, which is the definition of O(2n).
  7. Yes. Every parent ≥ its children: 20 ≥ 15, 18; 15 ≥ 7, 9; 18 ≥ 5, 12; 7 ≥ 3, 6; 9 ≥ 2.
  8. No. Radix sort needs a stable auxiliary sort so that the order from earlier digits is kept; heapsort is not stable, so being in place does not help.
  9. A pseudograph is a graph that may contain loops (an edge from a vertex to itself) and multiple edges between the same pair of vertices.
  10. The cycle graph Cn has n vertices (and n edges), for n ≥ 3.
  11. Adjacency list needs only O(V + E) space instead of O(V·E) for an incidence matrix; listing a vertex’s neighbours takes time proportional to its degree; and adding edges is easy – ideal for sparse graphs.
  12. O(log n): the new key is placed at the end and “bubbles up”, making at most ⌊log₂ n⌋ comparisons/swaps (the height of the heap).

Subjective questions

Q.2 Last occurrence of the largest number

LastLargest(A[1..n])
    max = A[1];  pos = 1
    for i = 2 to n
        if A[i] >= max          // ">=" keeps the LAST occurrence
            max = A[i]
            pos = i
    return pos

Using ≥ instead of > moves pos to every later copy of the maximum, so the last one is returned. Comparisons: exactly n − 1 element comparisons (plus about n loop-counter comparisons), so the algorithm is Θ(n). This is optimal, since every element must be examined.

Q.3 Sorting an array of −1, 0 and 1 in O(n)

Sort3(A[0..n-1])              // Dutch national flag, one pass
    low = 0; mid = 0; high = n - 1
    while mid <= high
        if A[mid] == -1
            swap(A[low], A[mid]); low++; mid++
        else if A[mid] == 0
            mid++
        else                         // A[mid] == 1
            swap(A[mid], A[high]); high--

Invariant: A[0..low−1] = −1, A[low..mid−1] = 0, A[high+1..n−1] = 1. Each step either increases mid or decreases high, so the loop runs at most n times: O(n) time, O(1) extra space. (Alternative: count the −1s, 0s and 1s in one pass and rewrite the array – also O(n).)

Q.4 Is there a path from u to v in a DAG?

PathExists(G, u, v)            // DFS from u
    for each vertex x: visited[x] = false
    return DFS(u)

DFS(x)
    if x == v: return true
    visited[x] = true
    for each edge (x, y) in G.adj[x]
        if not visited[y] and DFS(y): return true
    return false

Each vertex is visited at most once and each edge examined at most once, so the time is O(V + E) with adjacency lists and O(V) extra space. BFS works equally well. (Optimization: in topological order, if v comes before u there can be no path.)

Q.5 Positive-weight cycle in a DAG

By definition a directed acyclic graph has no cycles at all, so it cannot have a positive-weight cycle – the answer is always “no”. The only work needed is to confirm that the input really is acyclic: run a topological sort (Kahn’s algorithm, repeatedly removing vertices of in-degree 0). If all V vertices are removed the graph is a DAG and has no cycle; if some remain, a cycle exists. Complexity: O(V + E). (For a general directed graph, a positive cycle could be found by negating the weights and running Bellman–Ford in O(VE).)

Q.6 Single-source longest paths in a DAG

DAG-Longest-Paths(G, w, s)
    order = TopologicalSort(G)              // O(V + E)
    for each vertex v: d[v] = -infinity; parent[v] = NIL
    d[s] = 0
    for each u in order
        if d[u] != -infinity
            for each edge (u, v)
                if d[u] + w(u, v) > d[v]    // relax with max instead of min
                    d[v] = d[u] + w(u, v)
                    parent[v] = u
    return d, parent

Because vertices are processed in topological order, when u is processed all paths into u have already been considered, so d[u] is final. Topological sort is O(V + E), initialization O(V), and each edge is relaxed once, O(E). Total: Θ(V + E). (Equivalently: negate all weights and find shortest paths in the DAG.) Longest paths are hard in general graphs but easy in DAGs because there are no cycles.

Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.

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.