LP III DAA Write Up
LP III DAA Write Up
LP III DAA Write Up
Aim : Write a program non-recursive and recursive program to calculate Fibonacci numbers and
analyze their time and space complexity.
Theory :
The Fibonacci sequence, also known as Fibonacci numbers, is defined as the sequence of numbers
in which each number in the sequence is equal to the sum of two numbers before it. The Fibonacci
Sequence is given as:
Fibonacci Sequence = 0, 1, 1, 2, 3, 5, 8, 13, 21, ….
Here, the third term “1” is obtained by adding the first and second term. (i.e., 0+1 = 1)
Similarly,
“2” is obtained by adding the second and third term (1+1 = 2)
“3” is obtained by adding the third and fourth term (1+2) and so on.
For example, the next term after 21 can be found by adding 13 and 21. Therefore, the next term in
the sequence is 34.
The Fibonacci sequence of numbers “Fn” is defined using the recursive relation with the seed values
F0=0 and F1=1: Fn = Fn-1+Fn-2
Here, the sequence is defined using two different parts, such as kick-off and recursive relation.
The kick-off part is F0=0 and F1=1.
The recursive relation part is Fn = Fn-1+Fn-2.
The sequence starts with 0 rather than 1. So, F5 should be the 6th term of the sequence.
Iterative Approach:
Time Complexity:
The time complexity of the iterative code is linear, as the loop runs from 2 to n, i.e. it runs in O(n)
time
Space Complexity :
For the iterative approach, the amount of space required is the same for fib(6) and fib(100), i.e. as N
changes the space/memory used remains the same. Hence it’s space complexity is O(1) or constant.
Recursive approach:
Time Complexity :
T(n) = T(n-1) + T(n-2) + c
= 2T(n-1) + c //from the approximation T(n-1) ~ T(n-2)
= 2*(2T(n-2) + c) + c
= 4T(n-2) + 3c
= 8T(n-3) + 7c
= 2^k * T(n - k) + (2^k - 1)*c
n-k=0k=n
T(n) = 2^n * T(0) + (2^n - 1)*c
= 2^n * (1 + c) – c
T(n) = 2^n
Space Complexity :
For Fibonacci recursive implementation or any recursive algorithm, the space required is
proportional to the maximum depth of the recursion tree, because, that is the maximum number of
elements that can be present in the implicit function call stack.
Input :
No. of terms
Output:
Fibonacci series upto nth terms
Program Implementation : Code i s written in C++ to implement program to implement
Fibonacci series using iterative and recursive approach.
Conclusion : Thus , Fibonacci series using iterative and recursive approach is implemented using
C++.
Assignment No.– 2
Theory:
Greedy Method:
In greedy method following activities are performed:
2) Optimal substructure:
A problem shows optimal substructure if an optimal solution to problem contains optimal solution
to subproblem.
Huffman tree:
In Huffman coding method, data is inputted as a sequence of characters. Then a table of frequency
of occurrence of each character in data is built.
- From table of frequencies Huffman tree is constructed.
- The Huffman’s tree is further used for encoding each character so that binary encoding
is obtained for the given data.
- In Huffman’s tree is further specific method of representing each symbol. This method
produces a code. In such a manner that no code coded in prefix of some other code
word such code word are called as prefix codes.
Algorithm :
Input :
No. of characters with frequencies.
Output:
Encoding of characters
Program Implementation : Code i s written in C++ to implement program to implement
Huffman coding using Greedy approach.
Conclusion : Thus , Huffman coding using Greedy approach is implemented using C++.
Assignment No. 3
Aim : Write a program to solve a fractional Knapsack problem using a greedy method.
Theory:
Greedy Method:
In greedy method following activities are performed:
2) Optimal substructure:
A problem shows optimal substructure if an optimal solution to problem contains optimal solution
to subproblem.
Fractional Knapsack
In this case, items can be broken into smaller pieces.
According to the problem statement,
There are n items in the store
Weight of ith item wi>0
Profit for ith item pi>0 and
Capacity of the Knapsack is W
In this version of Knapsack problem, items can be broken into smaller pieces. So, the thief may take
only a fraction xi of ith item.
0⩽xi⩽1
The ith item contributes the weight xi.wixi.wi to the total weight in the knapsack and
profit xi.pixi.pi to the total profit.
Hence, the objective of this algorithm is to
maximize∑n=1n(xi.pi)maximize∑n=1n(xi.pi)
subject to constraint,
∑n=1n(xi.wi)⩽W
Algorithm :
If the provided items are already sorted into a decreasing order of piwipiwi, then the whileloop
takes a time in O(n); Therefore, the total time including the sort is in O(n logn).
Input :
No. of Objects, Capacity of sack, weights and profits of all objects.
Output:
Maximum profit value
Program Implementation : Code i s written in C++ to implement program to implement
fractional knapsack problem using Greedy approach.
Conclusion : Thus , fractional knapsack problem using Greedy approach is implemented using
C++.
Assignment No. 4
Aim : Write a program to solve a 0-1 Knapsack problem using dynamic programming or branch
and bound strategy.
Theory :
Input :
No. of Objects, Capacity of sack, weights and profits of all objects.
Output:
Maximum profit value
Program Implementation : Code i s written in C++ to implement program to implement
fractional knapsack problem using dynamic programming.
Conclusion : Thus , fractional knapsack problem using dynamic programming is implemented
using C++.
Assignment -5
Aim: Design n-Queens matrix having first Queen placed. Use backtracking to place remaining
Queens to generate the final n-queen‘s matrix.
Theory :-
Backtracking
In backtracking method :-
1 Algorithm Queen(n)
2 //Input : n-queens
3 // output : Solution Vector
4 for column <-- 1 to n do
5{
6 if(place(row, column)) then
7 {
8 board [row]=column;
9 if(row=n) then
10 print_board(n);
11 else
12 Queen(row t1,n)
13 }
14 }
Input :
No. of queens.
Output:
Solution vector showing the column position of all the queens.
Program Implementation : Code i s written in C++ to implement program to find the
solution of placing n queens on chessboard using backtracking.
Conclusion : Thus , N-Queens algorithm using backtracking is implemented using C++.