LP 3 DAA Lab Manual
LP 3 DAA Lab Manual
AY 2024-25
Class:-BE 1 & 2
Subject Teacher:-Dr.(Mrs.)S.P.khedkar
Modern Education Society’s
Wadia College of Engineering, Pune
AIM: Write a non-recursive and recursive program to calculate Fibonacci numbers and analyze
their time and space complexity.
OBJECTIVES:
1. To find Fibonacci Number
2. Analyse time and space complexity
PER-REQUISITES:
1. Knowledge of programming language.
2. Knowledge of Time and space complexity
THEORY:
The Fibonacci numbers or Fibonacci series are the numbers in the following integer sequence:
0,1,1,2,3,5,8,13,21,.... .
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent
number is the sum of the previous two. In mathematical terms, the sequence F n of Fibonacci
numbers is defined by the recurrence relation Fn= Fn-1 + Fn-2
ALGORITHM:-
1) Read n, the number of terms to be generated
2) Term 1 = 0, term2 = 1.
3) Display term1, term2.
4) Vary the counter in a loop from 0 to (n – 1) incremented by 1.
5) Term = term1 + term2
6) Display term.
7) Term 1 =term2.
8) Term 2 = term →
9) End loop
OBJECTIVES:
1) Analyze performance of an algorithm.
2) Learn how to implement algorithms that follow greedy strategy.
PER-REQUISITES:
1. Knowledge of programming language.
2. Knowledge of Time and space complexity
THEORY:
Huffman coding is a lossless data compression algorithm. The idea is to assign variable-length
codes to input characters, lengths of the assigned codes are based on the frequencies of
corresponding characters. The most frequent character gets the smallest code and the least
frequent character gets the largest code.
ALGORITHM:-
Steps to build Huffman Tree
Input is an array of unique characters along with their frequency of occurrences and
output is Huffman Tree.
1. Create a leaf node for each unique character and build a min heap of all leaf nodes (Min
Heap is used as a priority queue. The value of frequency field is used to compare two nodes
in min heap. Initially, the least frequent character is at root).
2. Extract two nodes with the minimum frequency from the min heap.
3. Create a new internal node with a frequency equal to the sum of the two nodes frequencies.
Make the first extracted node as its left child and the other extracted node as its right child.
Add this node to the min heap.
4. Repeat step 2 and 3 until the heap contains only one node. The remaining node is the root
node and the tree is complete.
CONCLUSION:-Implemented Huffman Encoding using a greedy strategy successfully.
AIM: Write a program to solve a fractional Knapsack problem using a greedy method.
OBJECTIVES:
PER-REQUISITES:
1. Knowledge of Fractional Knapsack problem using a greedy method.
THEORY:
Given the weights and values of N items, put these items in a knapsack of capacity W to get the
maximum total value in the knapsack. In Fractional Knapsack, we can break items for maximizing
the total value of the knapsack
Note: In the 0-1 Knapsack problem, we are not allowed to break items. We either take the whole
item or don’t take it.
ALGORITHM:-
1) Write realistic applications of this experiment in brief (at least two applications).?
2) Explain Kanpsack with example using greedy approach?
OBJECTIVES:
To study dynamic programming.
To implement 0/1 knapsack problem.
Calculate time complexity of algorithm.
THEORY:
In a DP[][] table let’s consider all the possible weights from ‘1’ to ‘W’ as the columns and weights
that can be kept as the rows. The state DP[i][j] will denote maximum value of ‘j-weight’
considering all values from ‘1 to ith’. So if we consider ‘wi’ (weight in ‘ith’ row) we can fill it in all
columns which have ‘weight values > wi’. Now two possibilities can take place:
Fill ‘wi’ in the given column.
Do not fill ‘wi’ in the given column.
Now we have to take a maximum of these two possibilities, formally if we do not fill ‘ith’ weight in
‘jth’ column then DP[i][j] state will be same as DP[i-1][j] but if we fill the weight, DP[i][j] will be
equal to the value of ‘wi’+ value of the column weighing ‘j-wi’ in the previous row. So we take the
maximum of these two possibilities to fill the current state.
1.Write time & space complexity of 0/1 knapsack algorithm using dynamic programming.
2.Write realistic applications of this experiment in brief (at least two applications).
3. The weight limit for this knapsack is 10 find solution using dynamic programming?
Modern Education Society’s
Wadi College of Engineering, Pune
AIM: Design n-Queens matrix having first Queen placed. Use backtracking to place remaining
Queens to generate the final n-queen‘s matrix
OBJECTIVES:
To understand the concept of Backtracking.
To understand the concept of State Space Tree.
To compare the space & time complexity of Recursive & Non-Recursive techniques of
Backtracking.
THEORY:
What is Backtracking?
Backtracking formulation is used to solve problems which deal with searching for a set of
solutions or which ask for an optimal solution satisfying some constraints.
Constraint satisfaction problems:-
1. These are problems with complete solution, where the order of elements does not matter.
2. The problem consists of set of variables each of which must be assigned a value, subject to
particular constraints of problem.
3. Backtracking attempts to try all the combinations in order to obtain a solution.
4. Its strength is that many implementations avoid trying many partial combinations,
thus speeding up the running time.
State space trees:-Backtracking algorithms determined problems solution by systematically searching the
solution space for the given problem instance.This search is facilitated by using a tree organization for the
solution space. For a given problem space many tree organizations are possible.
Consider a 4 * 4 chessboard. We have to place 4 queens such that no two queens are on the same row, column
or diagonal. For the solution to this problem ,we place each of the 4 queens on a separate row. Now we have
to place each queen on a unique column too such that no 2 queens are on same diagonals. Various
permutations of queens position are possible but only the permutations that satisfy the constraints are valid.
The Various permutations of queens positions can be depicted by a tree organization .Let the level of the tree
denote the row and edge denote the column. Nodes denote the states reached. Definition of state space tree:-
The tree organization of the solution space is referred to as state space tree. If the tree organizations are
independent of the problem instance being solved, they are called static trees. If the tree organization is
dependent of the problem instance being solved, they are called dynamic trees.
Step 3: Now we have to place queen 3 in row 3.We can’t choose column 2, 1 or 3. So we backtrack
and shift the queen to column 4. Then we place queen 3 in row 3, column 2 as shown.
Step 4: now we have to place queen 4 in row 4. We cannot place queen 4 in column 1, 2, 3 or 4
since no queen should be in same column or diagonal.
Step 5: Thus we backtrack. We cannot shift queen 3 and still satisfy implicit constraints. We cannot
move queen 2 and still satisfy constraints. Thus we shift queen 1 to column 2, row 1.
The tree generated during the backtracking is as follows. The edges denote the column. The level(1)
denotes the row of the queen(1).
ALGORITHM:-
else
k:=k-1; //backtrack to previous set.
}
}
Recursive backtracking method.
Algorithm backtrack(k)
//this schema describes the backtracking process using recursion.
//On entering the first k-1 values x[1],x[2],x[3],......x[k-1] of the solution the vector x[1:n] have
been assigned x[] and n is global.
{For (each x[k]belongs t(x[1],x[k-1]))
do{if Bk(x [1], x[2],....x[k]!=0)then{
IfBk (x[1],x[2],……x[k] is a path to answer node)
then wrie (x[1:k]);
if(k<n) then backtrack(k+1);
}
}
}
CONCLUSION: Implemented 8 queen successfully.