Tribhuwan University

Institute of Science and Technology

2078

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 are the elementary properties of algorithm? Explain. Why do you need algorithm? Discuss about analysis of the RAM model for analysis of algorithm with suitable example.[10]

Elementary Properties of Algorithm, Need for Algorithm, and RAM Model Analysis

Elementary Properties of Algorithm

An algorithm is a finite set of well-defined instructions to solve a specific problem in a finite amount of time.

Every algorithm must satisfy the following elementary properties:

  • Finiteness — An algorithm must always terminate after a finite number of steps. It cannot run indefinitely.

  • Definiteness — Each step of the algorithm must be precisely and unambiguously defined. There should be no confusion about what action to perform.

  • Input — An algorithm has zero or more inputs taken from a specified set of objects.

  • Output — An algorithm has one or more outputs that have a specific relation to the inputs.

  • Effectiveness — Every instruction must be basic enough to be carried out, in principle, by a person using only pencil and paper. Each operation must be feasible.

  • Generality — The algorithm should work for all instances of the problem, not just a particular input.

  • Independence — An algorithm should be independent of any programming language; it is a logical step-by-step solution.


Why Do We Need Algorithms?

  • Problem Solving — Algorithms provide a systematic and logical approach to solve complex problems step by step.

  • Efficiency — They help us find the most efficient solution in terms of time and space among multiple possible solutions.

  • Correctness — A well-designed algorithm guarantees that the correct output is produced for every valid input.

  • Communication — Algorithms serve as a clear way to communicate the solution approach to other programmers or stakeholders.

  • Scalability — They help us predict how a solution will perform when input size grows large.

  • Reusability — Once designed, an algorithm can be implemented in any programming language and reused across projects.

  • Comparison — Algorithms allow us to compare different solutions to the same problem and pick the best one.


Analysis of the RAM Model for Analysis of Algorithm

The RAM (Random Access Machine) model is a theoretical model of computation where each simple operation (+, −, ×, ÷, comparison, assignment, memory access) takes exactly one unit of time, and each memory access also takes one unit of time.

Key Assumptions of the RAM Model:

  • Each simple operation (addition, subtraction, multiplication, division, comparison, assignment) takes 1 step.
  • Each memory read/write takes 1 step.
  • Instructions are executed sequentially (one after another).
  • Memory is unlimited and access to any memory location takes constant time (random access).
  • No concurrent/parallel operations are considered.

Why RAM Model is Used:

  • It provides a machine-independent framework to analyze algorithms.
  • It allows us to count the number of basic operations as a function of input size nn.
  • It simplifies analysis by ignoring hardware-specific details.

Example — Linear Search Analysis using RAM Model:

int linearSearch(int arr[], int n, int key) {
    for (int i = 0; i < n; i++) {    // comparison + increment each iteration
        if (arr[i] == key)            // memory access + comparison
            return i;                 // 1 step
    }
    return -1;                        // 1 step
}

Counting steps using RAM model:

Operation Cost Frequency (Worst Case)
i = 0 (assignment) 1 1
i < n (comparison) 1 n + 1
i++ (increment) 1 n
arr[i] == key (access + compare) 2 n
return -1 1 1

Total steps (Worst Case): 1+(n+1)+n+2n+1=4n+31 + (n+1) + n + 2n + 1 = 4n + 3

So, in the RAM model, the time complexity of linear search is T(n)=4n+3=O(n)T(n) = 4n + 3 = O(n).

  • Best Case: Element found at first position → O(1)O(1)
  • Worst Case: Element not found → O(n)O(n)
  • Average Case: Element found at middle → O(n/2)=O(n)O(n/2) = O(n)

Conclusion

The elementary properties ensure an algorithm is correct and practical. The need for algorithms arises from the demand for efficient, correct, and scalable solutions. The RAM model provides a simple yet powerful abstraction to analyze algorithm performance by counting basic operations, independent of actual hardware, making it the standard model for theoretical algorithm analysis.

2.
Explain about the divide and conquer paradigm for algorithm design with suitable example. Write the Quick sort algorithm using randomized approach and explain its time complexity.[10]
3.
Explain in brief about the Dynamic Programming Approach for algorithm design. How it differs with recursion? Explain the algorithm for solving the 0/1 Knapsack problem using the dynamic programming approach and explain its complexity.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
Explain the recursion tree method for solving the recurrence relation. Solve following recurrence relation using this method. T(n)=2T(n/2) +1 for n>1, T(n)=1 for n=1 [5]
5.
What do you mean by optimization problem? Explain the greedy strategy for algorithm design to solve optimization problems. [5]
6.
Explain the algorithm and its complexity for solving job sequencing with deadline problem using greedy strategy. [5]
7.
What do you mean by memorization strategy? Compare memorization with dynamic programming. [5]
8.
Explain the concept of backtracking. How it differ with recursion? [5]
9.
Write an algorithm to find the maximum element of an array and analyze its time complexity. [5]
10.
Write the algorithm for bubble sort and explain its time complexity. [5]
11.
Explain in brief about the complexity classes P, NP and NP Complete. [5]
12.
Write short notes on: a. NP Hard Problems and NP Completeness b. Problem Reduction [5]