Design and Analysis of Algorithms 2076 (new)

Tribhuwan University
Institute of Science and Technology
2076 (new)
Bachelor Level / Fifth Semester / Science
Computer Science and Information Technology ( CSC314 )
( 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 far as practicable.
The figures in the margin indicate full marks.

                                                        Section A

Long Answer Questions.

Attempt any two questions.                                (2x10=20)

1.  What do you mean by complexity of an algorithm? Explain about the asymptotic notations used to describe the time/space complexity of any algorithm with their geometrical interpretation and example.    (1+9)

10 marks view

Complexity of an algorithm is a measure of the amount of time and/or space required by an algorithm for an input of a given size (n). The complexity of an algorithm can be divided into two types: The time complexity and the space complexity.

  • Time complexity of an algorithm is the measure of total time required to execute the algorithm.
  • The space complexity is defined as how much space is required to execute an algorithm.

Asymptotic notations are the general representation of time and space complexity of an algorithm. Majorly, we use three types of Asymptotic Notations and those are as follows:

1. Big-Oh notation:

A function f(n) is said to be “big-Oh of  g(n)” and we write, f(n)=O(g(n)) or simply f=O(g), if there are two positive constants c and n0 such that f(n)<=c*g(n) for all n>=n0.

E.g. The big oh notation of  f(n)=n+4 is O(n) since n+4<=2n for all n>=4.

The big oh notation of  f(n)=n2+3n+4 is O(n2) since n2+3n+4<=2n2  for all n>=4.


Big O notation specifically describes worst case scenario. It represents the upper bound running time complexity of an algorithm.


2. Big-Omega notation:

A function f(n) is said to be “big-Omega of  g(n)” and we write, f(n)=Ω(g(n)) or simply f=Ω(g), if there are two positive constants c and n0 such that f(n)>=c*g(n) for all n>=n0.


E.g. The big omega notation of f(n)=5n+6  is Ω(n) since 5n+6>=5n for all n>=1.

The big omega notation of f(n)=3n2+2n+4   is Ω(n2) since 3n2+2n+4 >=3n2 for all n>=1.

 

Big-Omega notation specifically describes best case scenario. It represents the lower bound running time complexity of an algorithm. 


3. Big Theta notation:

A function f(n) is said to be “big-Theta of g(n)” and we write, f(n)= Θ(g(n)) or simply f= Θ(g), if there exist positive constants c1, c2 and n0 such that c1*g(n)<=f(n)<= c2*g(n) for all n>=n0.

E.g. The big theta notation of  f(n)=5n+2  is Θ(n) since 6n>=5n+2>=5n for all n>=3.

The big theta notation of f(n)=3n2+2n+1 is Θ(n2) since 4n2 >=3n2+2n+1>=3n22 for all n>=3. 

This notation describes both upper bound and lower bound of an algorithm so we can say that it defines exact asymptotic behaviour.

2.  Explain about divide and conquer paradigm for algorithm design with suitable example. Write the Quick sort algorithm using randomized approach and explain its time complexity.    (4+6)

10 marks view

In divide and conquer paradigm, a problem is divided into smaller problems, then the smaller problems are solved independently, and finally the solutions of smaller problems are combined into a solution for the large problem.

Generally, divide-and-conquer algorithms have three parts:

  • Divide: Divide the given problem into sub-problems using recursion.
  • Conquer: Solve the smaller sub-problems recursively. If the subproblem is small enough, then solve it directly.
  • Combine: Combine the solutions of the sub-problems that are part of the recursive process to solve the actual problem.


Example: Some problems which are based on the Divide & Conquer approach are:

  • Binary Search
  • Sorting (merge sort, quick sort)

Quick sort algorithm using randomized approach

RandQuickSort(A, l, r)

{

    if(l<r)

    {

        m = RandPartition(A, l, r);

        RandQuickSort(A, l, m-1);

        RandQuickSort(A, m+1, r);

    }

}

RandPartition(A, l, r)

{

    k = random(l, r);     //generates random number between i and j including both.

    swap(A[l], A[k]);

    return Partition(A, l, r);

}

Partition(A,l,r)

{

    x =l; y =r ; p = A[l];

    while(x<y)

    {

        do {

                    x++;

            }while(A[x] <= p);

        do {

                    y--;

            } while(A[y] >=p);

        if(x<y)

            swap(A[x],A[y]);

    }

    A[l] = A[y];  A[y] = p;

    return y;         //return position of pivot

}

Time Complexity:

  •     Worst Case: T(n) = O(n2)
  •     Average Case: T(n) = O(nlogn)

3.  Explain in brief about the Backtracking approach for algorithm design. How it differs with recursion? Explain the N-Queen Problem and its algorithm using backtracking and analyze its time complexity.    (2+2+6)

10 marks view

                                                            Section B

Short Answer Questions.

Attempt any eight questions.                                (8x5=40)

4.  Write the algorithm for Selection Sort and explain its time and space complexity.    (5)

5 marks view

5.  Solve the following recurrence relations using master method.    (2.5+2.5)

        a. T(n)=7T(n/2)+n2

        b. T(n)=4T(n/4)+kn

5 marks view

6.  Explain the greedy algorithm for fractional knapsack problem with its time complexity.    (5)

5 marks view

7.  Trace the heap-sort algorithm for the following data: {12, 45, 62, 50, 85, 15, 28}.    (5)

5 marks view

8.  What do you mean by Dynamic Programming Strategy? Explain the elements of DP.    (2+3)

5 marks view

9.  Explain the approximation algorithm for solving vertex cover with suitable example.    (5)

5 marks view

10.  Explain the Prim's algorithm for MST problem and analyze its time complexity.    (5)

5 marks view

11.  Explain in brief about the classes P, NP, NP complete with example.    (5)

5 marks view

12.  Write short notes on:        (2x2.5)

        a. Backtracking Strategy

        b. Tractable and Intractable problems

5 marks view