Lab 2 - DSA - 23f
Lab 2 - DSA - 23f
Lab#02
SHOW PERFORMANCE ANALYSIS
(TIME COMPLEXITY AND SPACE COMPLEXITY)
LAB OBJECTIVE:
Show Performance Analysis (Time Complexity and Space Complexity)
Make a program of recursive algorithms of Fibonacci series and Factorial numbers.
LAB DESCRIPTION:
Performance Analysis:
Designing of the efficient algorithm is one of the major tasks. Time and space are the two main variables
on which the efficiency of an algorithm is depended. Time-Space tradeoff is done in different conditions,
it depends that by increasing the time, the space may be reduced and vice versa.
Time Complexity: of a program is the amount of computer time it needs to run to completion.
Fibonacci sequence:
A Fibonacci sequence is the sequence of integer in which each element in the sequence is the sum of the
two previous elements.
Fn = Fn-1 + Fn-2
Recursive Algorithm
A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which
obtains the result for the current input by applying simple operations to the returned value for the smaller
(or simpler) input.
Example:
Fibonacci: Iterative Algorithm
First we try to draft iterative algorithm for Fibonacci series.
Procedure Fibonacci(n)
declare f0, f1, fib, loop
set f0 to 0
set f1 to 1
display f0, f1
for loop ← 1 to n
fib ← f0 + f1
f0 ← f1
f1 ← fib
display fib
end for
end procedure
display f0, f1
for loop ← 1 to n
fib ← f0 + f1
f0 ← f1
f1 ← fib
display fib
end for
END
OR
1. START
2. Input the non-negative integer ‘n’
3. If (n==o || n==1)
return n;
else
return fib(n-1)+fib(n-2);
4. Print, nth Fibonacci number
5. END
LAB TASK:
1. Make a program that prints the Fibonacci series.
2. Compile an algorithm to calculate the factorial and make a program.