Tribhuwan University

Institute of Science and Technology

2076

Bachelor Level / Third Year / Fifth Semester / Science

B.Sc in Computer Science and Information Technology (CSC325)

(Design and Analysis of Algorithms)

Full Marks: 60

Pass Marks: 24

Time: 3 Hours

Candidates are required to give their answers in their own words as for as practicable.

The figures in the margin indicate full marks.

Section A

Long Answers Questions

Attempt any TWO questions.
[2*10=20]
1.
What do you mean by the complexity of an algorithm? Explain the asymptotic notations used to describe the time/space complexity of any algorithm with their geometrical interpretation and example.[10]

Complexity of an Algorithm and Asymptotic Notations

Complexity of an algorithm is a measure of the amount of time and/or space (memory) required by an algorithm as a function of the input size nn.


What is Algorithm Complexity?

Algorithm complexity describes how the resource usage (time or space) of an algorithm grows as the input size increases. It helps us compare algorithms and choose the most efficient one.

  • Time Complexity — the number of basic operations performed as a function of input size nn
  • Space Complexity — the amount of memory used as a function of input size nn

We express complexity using asymptotic notations, which describe the behavior of a function for large values of nn (i.e., as $n \to \infty$).


Asymptotic Notations

There are three main asymptotic notations:


A. Big-O Notation (O) — Upper Bound

Big-O gives the worst-case (upper bound) of an algorithm's growth rate.

Definition: A function f(n)=O(g(n))f(n) = O(g(n)) if there exist positive constants cc and n0n_0 such that:

f(n)cg(n)for all nn0f(n) \leq c \cdot g(n) \quad \text{for all } n \geq n_0

Geometrical Interpretation: The graph of f(n)f(n) lies on or below the graph of cg(n)c \cdot g(n) for all values beyond n0n_0. It acts as a ceiling on the growth.

Example: If f(n)=3n+2f(n) = 3n + 2, then f(n)=O(n)f(n) = O(n) because for c=4c = 4 and n0=2n_0 = 2: $3n + 2 \leq 4n$ for all n2n \geq 2


B. Big-Omega Notation (Ω) — Lower Bound

Big-Omega gives the best-case (lower bound) of an algorithm's growth rate.

Definition: A function f(n)=Ω(g(n))f(n) = \Omega(g(n)) if there exist positive constants cc and n0n_0 such that:

f(n)cg(n)for all nn0f(n) \geq c \cdot g(n) \quad \text{for all } n \geq n_0

Geometrical Interpretation: The graph of f(n)f(n) lies on or above the graph of cg(n)c \cdot g(n) for all values beyond n0n_0. It acts as a floor on the growth.

Example: If f(n)=3n+2f(n) = 3n + 2, then f(n)=Ω(n)f(n) = \Omega(n) because for c=3c = 3 and n0=1n_0 = 1: $3n + 2 \geq 3n$ for all n1n \geq 1


C. Big-Theta Notation (Θ) — Tight Bound

Big-Theta gives the exact/tight bound — the function grows at the same rate as g(n)g(n).

Definition: A function f(n)=Θ(g(n))f(n) = \Theta(g(n)) if there exist positive constants c1c_1, c2c_2, and n0n_0 such that:

c1g(n)f(n)c2g(n)for all nn0c_1 \cdot g(n) \leq f(n) \leq c_2 \cdot g(n) \quad \text{for all } n \geq n_0

Geometrical Interpretation: The graph of f(n)f(n) is sandwiched between c1g(n)c_1 \cdot g(n) and c2g(n)c_2 \cdot g(n) for all values beyond n0n_0. The function is bounded both above and below.

Example: If f(n)=3n+2f(n) = 3n + 2, then f(n)=Θ(n)f(n) = \Theta(n) because for c1=3c_1 = 3, c2=4c_2 = 4, and n0=2n_0 = 2: $3n \leq 3n + 2 \leq 4n$ for all n2n \geq 2


Summary Table

Notation Bound Type Meaning Symbol
O(g(n))O(g(n)) Upper Bound f(n)cg(n)f(n) \leq c \cdot g(n) Ceiling
Ω(g(n))\Omega(g(n)) Lower Bound f(n)cg(n)f(n) \geq c \cdot g(n) Floor
Θ(g(n))\Theta(g(n)) Tight Bound c1g(n)f(n)c2g(n)c_1 \cdot g(n) \leq f(n) \leq c_2 \cdot g(n) Sandwich

Geometrical Diagram

Imagine a graph with nn on the x-axis and f(n)f(n) on the y-axis:

image


Conclusion

Asymptotic notations provide a mathematical framework to classify algorithms based on their efficiency. Big-O is most commonly used in practice to describe worst-case performance, while Big-Theta gives the most precise characterization of an algorithm's complexity.

2.
Explain the divide and conquer paradigm from algorithm design with a suitable example. Write the Quick sort algorithm using a randomized approach and explain its time complexity.[10]
3.
Explain in brief the Backtracking approach for algorithm design. How it differs with recursion? Explain the N-Queen problem and algorithm using backtracking and analyze its time complexity.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
Solve the following recurrence relation using the master method. a. T(n)=7T(n/2)+n2T(n) = 7 T(n/2) + n^2 b. T(n)=4T(n/4)+knT(n) = 4 T(n/4) + kn [5]
5.
Explain the greedy algorithm for the fractional knapsack problem with its time complexity. [5]
6.
What do you mean by Dynamic programming strategy? Explain the element of DP. [5]
7.
Explain the approximation for solving vertex cover with a suitable example. [5]
8.
Explain Prim’s algorithm for MST problem and analyze its time complexity. [5]
9.
Write short notes on: a. Backtracking strategy b. Tractable and Intractable Problem [5]
10.
Write the algorithm for selection sort and explain its time and space complexity. [5]
11.
Trace heap sort algorithm for the following data: (2, 9, 3, 12, 15, 8, 11) [5]
12.
Explain in brief about the classes P, NP, and NP complete with examples. [5]