Tribhuwan University

Institute of Science and Technology

2080

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 is recurrence relation? How it can be solved? Show that time complexity of the recurrence relation T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1 is O(n)O(n) using substitution method.[10]

Recurrence Relation and Solving using Substitution Method

A recurrence relation is a mathematical equation that defines a function in terms of its value on smaller inputs, typically used to express the running time of recursive algorithms.


What is a Recurrence Relation?

A recurrence relation describes the time complexity of a recursive algorithm by relating T(n)T(n) to TT of smaller values. It consists of:

  • Recursive case — expresses T(n)T(n) in terms of smaller subproblems (e.g., T(n/2)T(n/2), $T(n-1)$)
  • Base case — gives the value for the smallest input (e.g., $T(1) = 1$)

Example: T(n)=2T(n/2)+nT(n) = 2T(n/2) + n represents the recurrence for Merge Sort.


Methods to Solve Recurrence Relations

There are three main methods to solve recurrence relations:

  • Substitution Method — Guess the solution, then prove it correct using mathematical induction.
  • Recursion Tree Method — Expand the recurrence into a tree, sum up the costs at each level.
  • Master Method — Directly apply the Master Theorem formula for recurrences of the form T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n).

Proof: T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1 is O(n)O(n) using Substitution Method

Given: T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1

Step A: Guess the solution

We guess that T(n)=O(n)T(n) = O(n), i.e., T(n)cnT(n) \leq cn for some constant c>0c > 0.

Step B: Assume it holds for smaller inputs (Inductive Hypothesis)

Assume T(n/2)c(n/2)T(n/2) \leq c(n/2) for all values less than nn.

Step C: Substitute into the recurrence

T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1

Substituting the hypothesis:

T(n)2c(n/2)+1T(n) \leq 2 \cdot c(n/2) + 1

T(n)cn+1T(n) \leq cn + 1

Step D: We need to show T(n)cnT(n) \leq cn

From above, T(n)cn+1T(n) \leq cn + 1

This does not directly satisfy T(n)cnT(n) \leq cn. So we strengthen our guess.

Step E: Strengthen the guess

Let us guess T(n)cndT(n) \leq cn - d for constants c>0c > 0 and d>0d > 0.

Assume T(n/2)c(n/2)dT(n/2) \leq c(n/2) - d

Substituting:

T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1

T(n)2[c(n/2)d]+1T(n) \leq 2[c(n/2) - d] + 1

T(n)cn2d+1T(n) \leq cn - 2d + 1

T(n)cnd(if 2d+1d, i.e., d1)T(n) \leq cn - d \quad \text{(if } -2d + 1 \leq -d \text{, i.e., } d \geq 1\text{)}

Step F: Verify the condition

Choose d=1d = 1, then:

T(n)cn2(1)+1=cn1cndT(n) \leq cn - 2(1) + 1 = cn - 1 \leq cn - d

This satisfies our guess T(n)cndT(n) \leq cn - d.

Step G: Verify the base case

For T(1)=1T(1) = 1 (base case), we need:

T(1)c(1)dT(1) \leq c(1) - d

1c11 \leq c - 1

c2c \geq 2

Choose c=2c = 2 and d=1d = 1, both conditions are satisfied.


Conclusion

Since we have proven by induction that T(n)cndcnT(n) \leq cn - d \leq cn for c=2c = 2 and d=1d = 1, we conclude:

T(n)=O(n)\boxed{T(n) = O(n)}

The substitution method works by guessing the form of the solution and then proving it correct using mathematical induction. The key trick is to strengthen the hypothesis when the direct guess fails.

2.
Write down the advantages of dynamic programming over greedy strategy. Find optimal bracketing to multiply 4 matrices of order 2,3,4,2,52,3,4,2,5.[10]
3.
Discuss heapify operation with example. Write down its algorithm and analyze its time and space complexity.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
Define RAM model. Write down iterative algorithm for finding factorial and provide its detailed analysis. [5]
5.
Write down nimmax algorithm and analyze its complexity. [5]
6.
When greedy strategy provides optimal solution? Write down job sequencing with deadlines algorithm and analyze its complexity. [5]
7.
Suppose that a message contains alphabet frequencies as given below and find Huffman codes for each alphabet.
SymbolFrequencya30b20c25d15e35\begin{array}{|c|c|}\hline \text{Symbol} & \text{Frequency} \\ \hline a & 30 \\ b & 20 \\ c & 25 \\ d & 15 \\ e & 35 \\ \hline \end{array}
[5]
8.
Does backtracking give multiple solution? Trace subset sum algorithm for the set (3,5,2,4,1)(3,5,2,4,1) and sum=8. [5]
9.
Why extended euclidean algorithm is used? Write down its algorithm and analyze its complexity. [5]
10.
Write short notes on a) Aggregate Analysis b) Selection problems [5]
11.
Write down algorithm of insertion sort and analyze its time and space complexity. [5]
12.
Define NP-complete problems with examples. Give brief proof of the statement "SAT is NP-complete". [5]