Daa Qnpaper
Daa Qnpaper
NOTION OF AN ALGORITHM
An algorithm is a sequence of non-ambiguous instructions for
solving a problem in a finite amount of time.
* An input to an algorithm specifies an instance of the
problem.
* An algorithm can be specified in a natural language or a
pseudo code.
* Algorithm can be implemented as computer programs.
Characteristics of an Algorithm
The important and prime characteristics of an algorithm are
1. Input
Zero (or) more quantities externally supplied.
2. Output
At least one quantity is produced.
3. Definiteness
Each instruction is clear and ambiguous.
4. Finiteness
The algorithm terminates after a finite number of steps.
5. Efficiency
Every instruction must be very basic.
6. Unambiguity
An algorithm must be expressed in a fashion that is
completely free of ambiguity.
Example 1
1. An algorithm to calculate simple interest
Step 1: Read the input values for principles (P),
No. of years (N), Rate (R)
Step 2: Compute SI = P*N* R
Step 3: Print SI
Step 4: Stop the process.
b.) Discuss the steps involved in mathematical analysis of
recursive algorithm. Do the same for finding factorial of N
numbers and binary search.
Recursive Algorithm
The algorithm executed recursively until it met a condition.
General Plan for Analyzing Efficiency of Recursive Algorithm
1. Decide on parameter indicating an input's size
2. Identify the algorithm's basic operation
3. Check whether the number of times the basic operation is
executed can vary on different inputs of the same size
4. Set up a recurrence relation, with an appropriate initial
condition for the number of times the basic operation is
executed.
5. Solve the recurrence equation.
Example 1
Find the factorial of a given n0
Algorithm F(n)
// Computes n! recursively.
// Input - A non negative integer n.
// Output -the value of n!
If (n = 0).
return 1
Inti
else
return F(n - 1)* n.
Formula
F(n) is computed as
F(n) = F(n - 1). n for n > 0
The number of multiplication M(n) is computed as
M(n) = M(n - 1) + i for n > 0
Recurrence Relation
• The recurrence relations play an important role in analysis
of an algorithm.
M(n) = M(n - 1) + 1 for n > 0
M(0) = 0
Solving Recurrence equation by backward substitutions
M(n) = M(n - 1) + 1
Substitute M(n - 1) = M(n - 2) + 1
Now M(n) becomes
M(n) = [M(n - 2) + 1] + 1
= M(n - 2) + 2
SNow M(n) becomes
M(n) = [M(n - 3) + 1] + 2
= M(n - 3) + 3
From the above, the general formula is obtained.
The general formula is
M(n) = M(n -i) + i
M(n) = M(n - 1) + 1
Substitute n = 0, i = n
M(n) = M(n - n) = n
= M(0) + n
M(n) = n