Space ADA
Space ADA
B.E. Semester 5
(Information Technology)
Certificate
this Institute (GTU Code: 021) has satisfactorily completed the Practical work
for the subject Analysis and Design of Algorithms(3150703) for the academic
year 2023.
Preface
Main motto of any laboratory/practical/field work is for enhancing required skills as well as
creating ability amongst students to solve real time problem by developing relevant competencies
in psychomotor domain. By keeping in view, GTU has designed competency focused outcome-
based curriculum for engineering degree programs where sufficient weightage is given to
practical work. It shows importance of enhancement of skills amongst the students and it pays
attention to utilize every second of time allotted for practical amongst students, instructors and
faculty members to achieve relevant outcomes by performing the experiments rather than having
merely study type experiments. It is must for effective implementation of competency focused
outcome-based curriculum that every practical is keenly designed to serve as a tool to develop
and enhance relevant competency required by the various industry among every student. These
psychomotor skills are very difficult to develop through traditional chalk and board content
delivery method in the classroom. Accordingly, this lab manual is designed to focus on the
industry defined relevant outcomes, rather than old practice of conducting practical to prove
concept and theory.
By using this lab manual students can go through the relevant theory and procedure in advance
before the actual performance which creates an interest and students can have basic idea prior to
performance. This in turn enhances pre-determined outcomes amongst students. Each experiment
in this manual begins with competency, industry relevant skills, course outcomes as well as
practical outcomes (objectives). The students will also achieve safety and necessary precautions
to be taken while performing practical.
This manual also provides guidelines to faculty members to facilitate student centric lab activities
through each experiment by arranging and managing necessary resources in order that the
students follow the procedures with required safety and necessary precautions to achieve the
outcomes. It also gives an idea that how students will be assessed by providing rubrics.
Algorithms are an integral part of computer science and play a vital role in solving complex
problems efficiently. The goal of this subject is to equip students with the knowledge and skills
required to design and analyze algorithms for various applications. Designing of algorithms is
important before implementation of any program or solving any problem. Analysis and Design
of Algorithms is essential for efficient problem-solving, optimizing resource utilization,
developing new technologies, and gaining a competitive advantage. This lab manual is designed
to help you learn algorithms by doing. Each experiment is structured to provide you with step-
by-step instructions on how to analyze and design a particular algorithm for specific problem.
You will learn how to analyze various algorithms and decide efficient algorithm in terms of time
complexity. By the end of this lab, you will have a solid understanding of algorithm design and
analysis.
Utmost care has been taken while preparing this lab manual however always there is chances of
improvement. Therefore, we welcome constructive suggestions for improvement and removal of
errors if any.
Analysis and Design of Algorithms (3150703)
The following industry relevant competencies are expected to be developed in the student by
undertaking the practical work of this laboratory.
1. Expertise in algorithm analysis
2. Judge best algorithm among various algorithms
3. Ability to solve complex problems
4. Ability to design efficient algorithm for some problems
Index
(Progressive Assessment Sheet)
Experiment No: 1
Implement a function for each of following problems and count the number of steps executed/time
taken by each function on various inputs (100 to 500) and write equation for the growth rate of each
function. Also draw a comparative chart of number of input versus steps executed/time taken. In
each of the following function N will be passed by user.
1. To calculate sum of 1 to N numbers using loop.
2. To calculate sum of 1 to N numbers using equation.
3. To calculate sum of 1 to N numbers using recursion.
Date:
Theory:
1
Analysis and Design of Algorithms (3150703) 230213116014
Algorithm :
Observations:
2
Analysis and Design of Algorithms (3150703) 230213116014
Result: Complete the below table based on your implementation of functions and steps executed
by each function.
200
300
400
500
Equation
Chart:
Conclusion:
Quiz:
1. What is the meaning of constant growth rate of an algorithm?
Answer:
3
Analysis and Design of Algorithms (3150703) 230213116014
2. If one algorithm has a growth rate of n2 and second algorithm has a growth rate of n then which
algorithm execute faster? Why?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty :
4
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 2
Write user defined functions for the following sorting methods and compare their performance by
steps executed/time taken for execution on various inputs (1000 to 5000) of random nature,
ascending order and descending order sorted data. Also, draw a comparative chart of number of
inputs versus steps executed/time taken for each cases (random, ascending, and descending).
1.Selection Sort
2.Bubble Sort
3.Insertion Sort
4.Merge Sort
5.Quick Sort
Date:
Theory:
}
}
5
Analysis and Design of Algorithms (3150703) 230213116014
6
Analysis and Design of Algorithms (3150703) 230213116014
{
if(a[k]<=a[j])
{ temp[i] = a[k] ;
k++;
}
else
{ temp[i] = a[j] ;
j++;
}
i++;
}
if(k<=mid)
for(;k<=mid;k++)
temp[i++] = a[k];
else
for(;j<=high;j++)
temp[i++]=a[j];
for(i=low;i<=high;i++)a[i]=temp[i];
}
Void merge_sort(int *a, long intlow,longint high ) //call this function from main
{
if(low!=high)
{
int mid = (low+high)/2;
merge_sort(a,low,mid);
merge_sort(a,mid+1,high);
merge(a,low,mid,high);
}}
while(flag==1)
{
i++;
while(q[i]<key)
{
i++;
}
j--;
while(q[j]>key)
j--;
if(i<j)
{
7
Analysis and Design of Algorithms (3150703) 230213116014
t1=q[i];
q[i]=q[j];
q[j]=t1;
}
else{
flag=0;
}
}
t2=q[lb];
q[lb]=q[j];
q[j]=t2;
quicksort(q,lb,j-1);
quicksort(q,j+1,ub);
}
return;
}
Algorithms:
1. Selection Sort
2. Bubble Sort
8
Analysis and Design of Algorithms (3150703) 230213116014
3. Insertion Sort
9
Analysis and Design of Algorithms (3150703) 230213116014
4. Merge Sort
5. Quick Sort
10
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result: Complete the below table based on your implementation of functions and steps executed
by each function. Also, prepare similar tables for ascending order sorted data and descending
order sorted data.
11
Analysis and Design of Algorithms (3150703) 230213116014
Chart:
250000
200000
150000
100000
50000
0
100 200 300 400 500
Conclusion:
Quiz:
1. Which sorting function execute faster (has small steps count) in case of ascending order sorted
data?
Answer:
2. Which sorting function execute faster (has small steps count) in case of descending order sorted
data?
Answer:
3. Which sorting function execute faster (has small steps count) in case of random data?
Answer:
12
Analysis and Design of Algorithms (3150703) 230213116014
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
13
Analysis and Design of Algorithms (3150703) 230213116014
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty :
14
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 3
Implement a function of sequential search and count the steps executed by function on various
inputs (1000 to 5000) for best case, average case and worst case. Also, write time complexity in
each case and draw a comparative chart of number of input versus steps executed by sequential
search for each case.
Date:
Objectives: (a) Identify Best, Worst and Average cases of given problem.
(b) Derive time complexity from steps count on various inputs.
Theory:
The algorithm works by sequentially iterating through the elements of the array and comparing each
element to the target value. If a match is found, the algorithm exits the loop.
Algorithm
15
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result: Complete the below table based on your implementation of sequential search algorithm
and steps executed by the function.
Chart:
Conclusion:
Quiz:
1. Which is the best case of an algorithm?
Answer:
16
Analysis and Design of Algorithms (3150703) 230213116014
2. Which is the worst case of an algorithm?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Signature of Faculty:
17
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 4
Compare the performances of linear search and binary search for Best case, Average case and Worst
case inputs.
Date:
Objectives: (a) Identify Best, Worst and Average cases of given problem.
(b) Derive time complexity from steps count for different inputs.
Theory:
Alorithm
18
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result: Complete the below table based on your implementation of sequential search algorithm
and steps executed by the function.
Prepare similar tables for Average case and Worst case of both algorithms.
Chart:
19
Analysis and Design of Algorithms (3150703) 230213116014
Conclusion:
Quiz:
1. Which element should be searched for the best case of binary search algorithm?
Answer:
2. Which element should be searched for the worst case of binary search algorithm?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
20
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 5
Implement functions to print nth Fibonacci number using iteration and recursive method. Compare
the performance of two methods by counting number of steps executed on various inputs. Also draw
a comparative chart. (Fibonacci series 1, 1, 2, 3, 5, 8….. Here 8 is the 6th Fibonacci number).
Date:
Objectives: (a) Compare the performances of two different versions of same problem.
(b) Find the time complexity of algorithms.
(C) Understand the polynomial and non-polynomial problems
Theory:
The Fibonacci series is the sequence of numbers (also called Fibonacci numbers), where every
number is the sum of the preceding two numbers, such that the first two terms are '0' and '1'. In
some older versions of the series, the term '0' might be omitted. A Fibonacci series can thus be given
as, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . It can thus be observed that every term can be calculated by
adding the two terms before it. We are ignoring initial zero in the series.
To represent any (n+1)th term in this series, we can give the expression as, Fn = Fn-1 + Fn-2. We can
thus represent a Fibonacci series as shown in the image below,
21
Analysis and Design of Algorithms (3150703) 230213116014
th
Recursive version to print n Fibonacci number is as below:
Algorithm:
Observations:
Result: Complete the below table based on your implementation of sequential search algorithm
and steps executed by the function.
22
Analysis and Design of Algorithms (3150703) 230213116014
Chart:
Conclusion:
Quiz:
1. What is the time complexity of iterative version of Fibonacci function?
Answer:
3. Can you execute recursive version of Fibonacci function for more inputs?
Answer:
4. What do you mean by polynomial time algorithms and exponential time algorithms?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
23
Analysis and Design of Algorithms (3150703) 230213116014
Signature of Faculty:
24
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 6
Implement a program for randomized version of quick sort and compare its performance with the
normal version of quick sort using steps count on various inputs (1000 to 5000) of random nature,
ascending order and descending order sorted data. Also, draw a comparative chart of number of
input versus steps executed/time taken for each cases (random, ascending, and descending).
Date:
Theory:
25
Analysis and Design of Algorithms (3150703) 230213116014
Algorithm:
Observations:
Result: Complete the below table based on your implementation of sequential search algorithm
and steps executed by the function.
26
Analysis and Design of Algorithms (3150703) 230213116014
Chart:
Conclusion:
Quiz:
2 What is the time complexity of basic version of Quick Sort on sorted data? Give reason of your
answer.
Answer:
3 Can we always ensure O(n*lg n) time complexity for Randomized Quick Sort?
Answer:
27
Analysis and Design of Algorithms (3150703) 230213116014
4 Which algorithm executes faster on ascending order sorted data?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
28
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 7
Implement program to solve problem of making a change using dynamic programming.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Theory:
Making Change problem is to find change for a given amount using a minimum number of coins
from a set of denominations. If we are given a set of denominations D = {d0, d1, d2, …,dn} and if
we want to change for some amount N, many combinations are possible. Suppose {d1, d2, d5, d8},
{d0, d2, d4}, {d0, d5, d7} all are feasible solutions but the solution which selects the minimum
number of coins is considered to be an optimal solution. The aim of making a change is to find a
solution with a minimum number of coins / denominations. Clearly, this is an optimization problem.
General assumption is that infinite coins are available for each denomination. We can select any
denomination any number of times.
Sort all the denominations and start scanning from smallest to largest denomination. In every
iteration i, if current denomination di is acceptable, then 1 coin is added in solution and total amount
is reduced by amount di. Hence,
C[i, j] = 1 + (c [i, j – di])
C[i,j] is the minimum number of coins to make change for the amount j. Below figure shows the
content of matrix C.
using coins if current denomination is larger than current problem size, then we have to skip the
denomination and stick with previously calculated solution. Hence,
C[i, j] = C[i – 1, j]
29
Analysis and Design of Algorithms (3150703) 230213116014
If above cases are not applicable then we have to stick with choice which returns minimum number
of coin. Mathematically, we formulate the problem as,
C[i, j] = min {C[i – 1, j] , 1 + C[i, j – di]}
Algorithm MAKE_A_CHANGE(d,N)
// d[1…n] = [d1,d2,…,dn] is array of n denominations
// C[1…n, 0…N] is n x N array to hold the solution of sub problems
// N is the problem size, i.e. amount for which change is required
fori ← 1 to n do
C[i, 0] ← 0
end
fori ← 1 to n do
for j ← 1 to N do
ifi = = 1 ans j < d [i] then
C[i, j] ← ∞
else if i == 1 then
C[i, j] ← 1 + C[1, j – d[1])
else if j < d [i] then
C[i, j] ← C[I – 1, j]
else
C[i, j] ← min (C[i – 1, j] ,1 + C[i, j – d[i])
end
end
end
return C[n, N]
Algorithm :
30
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result
Conclusion:
Quiz:
1. What is the time complexity of above algorithm?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
3. https://codecrucks.com/making-change-problem-using-dynamic-programming/
31
Analysis and Design of Algorithms (3150703) 230213116014
References used by the students:
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
32
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 8
Implement program of chain matrix multiplication using dynamic programming.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Theory:
Given a sequence of matrices A1, A2,...,An and dimensions p0, p1,...,pn where Ai is of dimension pi−1
× pi, determine the order of multiplication (represented, say, as a binary tree) that minimizes the
number of operations.
This algorithm does not perform the multiplications; it just determines the best order in which to
perform the multiplications
Two matrices are called compatible only if the number of columns in the first matrix and the number
of rows in the second matrix are the same. Matrix multiplication is possible only if they are
compatible. Let A and B be two compatible matrices of dimensions p x q and q x r
Suppose dimension of three matrices are :
A1 = 5 x 4
A2 = 4 x 6
A3 = 6 x 2
Matrix multiplication is associative. So
(A1 A2 ) A3 = {(5 x 4 ) x (4 x 6) } x (6 x 2)
= (5 x 4 x 6) + (5 x 6 x 2)
= 180
= 88
The answer of both multiplication sequences would be the same, but the numbers of multiplications
are different. This leads to the question, what order should be selected for a chain of matrices to
minimize the number of multiplications?
Let us denote the number of alternative parenthesizations of a sequence of n matrices by p(n). When
n = 1, there is only one matrix and therefore only one way to parenthesize the matrix. When n ≥ 2,
33
Analysis and Design of Algorithms (3150703) 230213116014
a fully parenthesized matrix product is the product of two fully parenthesized matrix sub-products,
and the split between the two subproducts may occur between the k and (k + 1)st matrices for any k
= 1, 2, 3…, n – 1. Thus we obtain the recurrence.
The solution to the recurrence is the sequence of Catalan numbers, which grows as Ω(4n / n3/2),
roughly equal to Ω(2n). Thus, the numbers of solutions are exponential in n. A brute force attempt
is infeasible to find the solution.
Any parenthesizations of the product Ai Ai + 1 … Aj must split the product between Ak and Ak+1 for
some integer k in the range i ≤ k < j. That is for some value of k, we first compute the matrices
Ai….k and Ak + 1…j and then multiply them together to produce the final product Ai…j The cost of
computing these parenthesizations is the cost of computing Ai….k , plus the cost of computing Ak +
1…j plus the cost of multiplying them together.
We can define m[i, j] recursively as follows. If i == j, the problem is trivial; the chain consists of
only one matrix Ai…i = A. No scalar multiplications are required. Thus m[i, i] = 0 for i = 1, 2 …n.
To compute m[i, j] when i< j, we take advantage of the structure of an optimal solution of the first
step. Let us assume that the optimal parenthesizations split the product Ai Ai + 1…Aj between Ak and
Ak + 1, where i ≤ k < j. Then m[i, j] is equal to the minimum cost for computing the subproducts
Ai…k and Ak + 1…j plus the cost of multiplying these two matrices together.
34
Analysis and Design of Algorithms (3150703) 230213116014
end
end
end
return m and s
Implementation:
Observations:
Result
35
Analysis and Design of Algorithms (3150703) 230213116014
Conclusion:
Quiz:
1. What is the time complexity of above algorithm?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
36
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 9
Implement program to solve LCS (Longest Common Subsequence) problem using dynamic
programming.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Theory:
The Longest Common Subsequence (LCS) problem is a classic computer science problem that
involves finding the longest subsequence that is common to two given sequences.
A subsequence is a sequence that can be derived from another sequence by deleting some or no
elements without changing the order of the remaining elements. For example, given the sequence
"ABCDE", "ACE" is a subsequence of "ABCDE", but "AEC" is not a subsequence.
Given two sequences X and Y, the LCS problem involves finding the longest common subsequence
(LCS) of X and Y. The LCS need not be contiguous in the original sequences, but it must be in the
same order. For example, given the sequences "ABCDGH" and "AEDFHR", the LCS is "ADH"
with length 3.
Naïve Method:
Let X be a sequence of length m and Y a sequence of length n. Check for every subsequence
of X whether it is a subsequence of Y, and return the longest common subsequence found.There
are 2m subsequences of X. Testing sequences whether or not it is a subsequence of Y takes O(n)
time. Thus, the naïve algorithm would take O(n2m) time.
37
Analysis and Design of Algorithms (3150703) 230213116014
Step 3 − Once the table is filled, backtrack from the last value in the table. Backtracking here is
done by tracing the path where the counter incremented first.
Step 4 − The longest common subseqence obtained by noting the elements in the traced path.
Consider the example, we have two strings X=BDCB and Y=BACDB to find the longest common
subsequence. Following table shows the construction of LCS table.
Once the values are filled, the path is traced back from the last value in the table at T[5, 4].
Algorithm is as below:
for i = 1 to m do
for j = 1 to n do
if xi = yj
C[i, j] := C[i - 1, j - 1] + 1
B[i, j] := ‘D’
else
if C[i -1, j] ≥ C[i, j -1]
C[i, j] := C[i - 1, j] + 1
B[i, j] := ‘U’
else
38
Analysis and Design of Algorithms (3150703) 230213116014
C[i, j] := C[i, j - 1] + 1
B[i, j] := ‘L’
return C and B
39
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result
Conclusion:
Quiz:
1. What is the time complexity of above algorithm?
Answer:
40
Analysis and Design of Algorithms (3150703) 230213116014
3. Does Dynamic programming approach to find LCS perform well compare to naïve approach?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
41
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 10
Implement program to solve Knapsack problem using dynamic programming.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Theory:
The knapsack problem is useful in solving resource allocation problem. Let X = < x1, x2, x3, . . . . .
,xn> be the set of n items. Sets W = <w1, w2, w3, . . . ,wn> and V = < v1, v2, v3, . . . , vn> are weight
and value associated with each item in X. Knapsack capacity is M unit.
The knapsack problem is to find the set of items which maximizes the profit such that collective
weight of selected items does not cross the knapsack capacity. Select items from X and fill the
knapsack such that it would maximize the profit.
Knapsack problem has two variations. 0/1 knapsack, that does not allow breaking of items. Either
add an entire item or reject it. It is also known as a binary knapsack. Fractional knapsack allows
breaking of items. Profit will be earned proportionally.
Following are the steps to implement binary knapsack using dynamic programming.
The above algorithm will just tell us the maximum value we can earn with dynamic programming.
It does not speak anything about which items should be selected. We can find the items that give
optimum result using the following algorithm.
Algorithm TRACE_KNAPSACK(w, v, M)
// w is array of weight of n items
// v is array of value of n items
// M is the knapsack capacity
SW ← { }
SP ← { }
i←n
j←M
while( j> 0 ) do
if(V[i, j] == V[i – 1, j]) then
i←i–1
else
V[i, j] ← V[i, j] – vi
j ← j – w[i]
SW ← SW +w[i]
SP ← SP +v[i]
end
end
Implementation:
43
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result
Conclusion:
44
Analysis and Design of Algorithms (3150703) 230213116014
Quiz:
1. What is the time complexity of above binary knapsack algorithm?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
45
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 11
Implement program for solution of fractional Knapsack problem using greedy design technique.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Theory:
Knapsack problem is as stated below:
Given a set of items, each having different weight and value or profit associated with it. Find the
set of items such that the total weight is less than or equal to a capacity of the knapsack and the total
value earned is as large as possible.
Brute-force approach: The brute-force approach tries all the possible solutions with all the
different fractions but it is a time-consuming approach.
Greedy approach: In Greedy approach, we calculate the ratio of profit/weight, and accordingly,
we will select the item. The item with the highest ratio would be selected first.
Following are the steps to implement fractional knapsack using greedy design strategy.
Implementation:
46
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result
Conclusion:
Quiz:
1. What is the time complexity of above knapsack algorithm?
Answer:
3. What is the time complexity solving knapsack problem using brute-force method?
Answer:
47
Analysis and Design of Algorithms (3150703) 230213116014
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
48
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 12
Implement program for solution of Making Change problem using greedy design technique.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Theory:
Making Change problem is to find change for a given amount using a minimum number of coins
from a set of denominations. If we are given a set of denominations D = {d0, d1, d2, …,dn} and if
we want to change for some amount N, many combinations are possible. {d 1, d2, d5, d8}, {d0, d2,
d4}, {d0, d5, d7} can be considered as all feasible solutions if sum of their denomination is N. The
aim of making a change is to find a solution with a minimum number of coins / denominations.
Following are the steps to solve coin change problem using greedy design technique
Algorithm:
49
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result
Conclusion:
Quiz:
1. What is the time complexity of above knapsack algorithm?
Answer:
50
Analysis and Design of Algorithms (3150703) 230213116014
3. What are some variations of the Making Change problem?
Answer:
4. What is the difference between the unbounded coin change problem and the limited coin change
problem?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
51
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 13
Implement program for Kruskal's algorithm to find minimum spanning tree.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Objectives: (a) Understand how to use Kruskal's algorithm to find the minimum spanning tree.
(b) Solve the optimization based problem.
(c) Find the time complexity of the algorithm.
Theory:
In graph theory, a minimum spanning tree (MST) of an undirected, weighted graph is a tree that
connects all the vertices of the graph with the minimum possible total edge weight. In other words,
an MST is a subset of the edges of the graph that form a tree and have the smallest sum of weights.
52
Analysis and Design of Algorithms (3150703) 230213116014
Implementation:
53
Analysis and Design of Algorithms (3150703) 230213116014
Observations
Result
Conclusion:
Quiz:
1. What is the time complexity of krushkal’s algorithm?
Answer:
54
Analysis and Design of Algorithms (3150703) 230213116014
3. What data structure is typically used to keep track of the connected components in Kruskal's
algorithm?
Answer:
4. When does Kruskal's algorithm stop adding edges to the minimum spanning tree?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
55
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 14
Implement program for Prim's algorithm to find minimum spanning tree.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Objectives: (a)Understand how to use Prim's algorithm to find the minimum spanning tree.
(b) Solve the optimization based problem.
(c) Find the time complexity of the algorithm.
Theory:
In graph theory, a minimum spanning tree (MST) of an undirected, weighted graph is a tree that
connects all the vertices of the graph with the minimum possible total edge weight. In other words,
an MST is a subset of the edges of the graph that form a tree and have the smallest sum of weights.
56
Analysis and Design of Algorithms (3150703) 230213116014
Implementation:
57
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result
Conclusion:
Quiz:
1. What is the time complexity of Prim’s algorithm?
Answer:
58
Analysis and Design of Algorithms (3150703) 230213116014
3. When does Prim's algorithm stop adding edges to the minimum spanning tree?
Answer:
4. What data structure is typically used to keep track of the vertices in Prim's algorithm?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
59
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 15
Implement DFS and BFS graph traversal techniques and write its time complexities.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Theory:
Depth First Search is a graph traversal algorithm that explores as far as possible along each branch
before backtracking. It is used to search for a node or a path in a graph, and is implemented
recursively or iteratively.
The algorithm starts at a specified node and visits all the nodes in the graph by exploring each
branch as far as possible before backtracking to the previous node. When a node is visited, it is
marked as visited to prevent loops.
60
Analysis and Design of Algorithms (3150703) 230213116014
Implementation:
61
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result
Conclusion:
Quiz:
1. What data structure is typically used in the iterative implementation of DFS and BFS?
Answer:
2. What is the time complexity of DFS on a graph with V vertices and E edges?
Answer:
3. What is the time complexity of BFS on a graph with V vertices and E edges?
Answer:
62
Analysis and Design of Algorithms (3150703) 230213116014
4. In which order are nodes visited in a typical implementation of BFS?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
63
Analysis and Design of Algorithms (3150703) 230213116014
Experiment No: 16
Implement Rabin-Karp string matching algorithm.
Date:
Competency and Practical Skills: Algorithmic thinking, Programming Skills, Problem solving
Theory:
It is a string searching algorithm that is named after its authors Richard M. Carp and Michael O.
Rabin.This algorithm is used to find all the occurrences of a given pattern ‘P’’ in a given string ‘S’
in O(Ns + Np) time in average case, where ‘Ns’ and ‘Np’ are the lengths of ‘S’’ and ‘P’, respectively.
We can see that “xyz” is occurring in “cxyzghxyzvjkxyz” at three positions. So, we have to print
that pattern ‘P’ is occurring in string ‘S’ at indices 1, 6, and 12.
Naive Pattern Searching (brute force) algorithm slides the pattern over text one by one and checks
for a match. If a match is found, then slide by 1 again to check for subsequent matches. This
approach has a time complexity of O(P* (S-P)).
The Rabin-Karp algorithm starts by computing, at each index of the text, the hash value of the string
starting at that particular index with the same length as the pattern. If the hash value of that equals
to the hash value of the given pattern, then it does a full match at that particular index.
Rabin Karp algorithm first computes the hash value of pattern P and first Np characters from S. If
hash values are same, we check the equality of actual strings. If the pattern is found, then it is called
hit. Otherwise, it is called a spurious hit. If hash values are not same, no need to compare actual
strings.
1. Calculate the hash value of the pattern: The hash value of the pattern is calculated using a hash
function, which takes the pattern as input and produces a hash value as output.
64
Analysis and Design of Algorithms (3150703) 230213116014
2. Calculate the hash values of all the possible substrings of the same length in the text: The hash
values of all the possible substrings of the same length as the pattern are calculated using the
same hash function.
3. Compare the hash value of the pattern with the hash values of all the possible substrings: If a
match is found, the algorithm checks the characters of the pattern and the substring to verify
that they are indeed equal.
4. Move on to the next possible substring: If the characters do not match, the algorithm moves on
to the next possible substring and repeats the process until all possible substrings have been
compared.
Implementation:
65
Analysis and Design of Algorithms (3150703) 230213116014
Observations:
Result
Conclusion:
Quiz:
1. What is the Rabin-Karp algorithm used for?
Answer:
66
Analysis and Design of Algorithms (3150703) 230213116014
2. What is the time complexity of the Rabin-Karp algorithm in the average case?
Answer:
3. What is the main advantage of the Rabin-Karp algorithm over the brute force algorithm for
string matching?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
Book : horowitz-and-sahani-fundamentals-of-computer-algorithms
Notes from respected subject teacher and Khaleel khan sir from Physics Wallah
Signature of Faculty:
67