0% found this document useful (0 votes)
26 views75 pages

Space ADA

Uploaded by

YouKnow MyName
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views75 pages

Space ADA

Uploaded by

YouKnow MyName
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

A Laboratory Manual for

Analysis and Design of


Algorithms
(3150703)

B.E. Semester 5
(Information Technology)

Directorate of Technical Education, Gandhinagar,


Gujarat
Government Engineering College, Bhavnagar

Certificate

This is to certify that Mr./Ms. Zanzarukiya Krishna Rajendrabhai

Enrollment No. 230213116014 of B.E. Semester 5 Information Technology of

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.

Place: GEC-Bhavnagar Date: __________

Name and Sign of Faculty member

Head of the Department


Analysis and Design of Algorithms (3150703)

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)

Practical – Course Outcome matrix

Course Outcomes (Cos):

1. Analyze the asymptotic performance of algorithms.


2. Derive and solve recurrences describing the performance of divide-and-conquer algorithms.
3. Find optimal solution by applying various methods.
4. Apply pattern matching algorithms to find particular pattern.
5. Differentiate polynomial and non-polynomial problems.
6. Explain the major graph algorithms and their analyses. Employ graphs to model engineering
problems, when appropriate.
Sr. CO CO CO CO CO CO
Objective(s) of Experiment
No. 1 2 3 4 5 6
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 time
complexity of each function. Also draw a comparative
1. 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.
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 input versus steps executed/time
2. taken for each cases (random, ascending, and √ √
descending).
1. Selection Sort
2. Bubble Sort
3. Insertion Sort
4. Merge Sort
5. Quick Sort
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,
3. √
write time complexity in each case and draw a
comparative chart of number of input versus steps
executed by sequential search for each case.
Compare the performance of linear search and binary
4. √ √
search for Best case, Average case and Worst case inputs.
Analysis and Design of Algorithms (3150703)

Implement functions to print nth Fibonacci number using


iteration and recursive method. Compare the
performance of two methods by counting number of
5. √ √
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).
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
6. √ √
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).
Implement program to solve problem of making a change
7. √
using dynamic programming.
Implement program of chain matrix multiplication using
8. √
dynamic programming.
Implement program to solve LCS problem using
9. √
dynamic programming.
Implement program to solve Knapsack problem using
10. √
dynamic programming.
Implement program for solution of fractional Knapsack
11. √
problem using greedy design technique.
Implement program for solution of Making Change
12. √
problem using greedy design technique
Implement program for Kruskal's algorithm to find
13. √ √
minimum spanning tree.
Implement program for Prim's algorithm to find
14. √ √
minimum spanning tree.
Implement DFS and BFS graph traversal techniques and
15. √
write its time complexities.

16. Implement Rabin-Karp string matching algorithm. √


Analysis and Design of Algorithms (3150703)

Industry Relevant Skills

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

Guidelines for Faculty members


1. Teacher should provide the guideline with demonstration of practical to the students with
all features.
2. Teacher shall explain basic concepts/theory related to the experiment to the students before
starting of each practical
3. Involve all the students in performance of each experiment.
4. Teacher is expected to share the skills and competencies to be developed in the students and
ensure that the respective skills and competencies are developed in the students after the
completion of the experimentation.
5. Teachers should give opportunity to students for hands-on experience after the
demonstration.
6. Teacher may provide additional knowledge and skills to the students even though not
covered in the manual but are expected from the students by concerned industry.
7. Give practical assignment and assess the performance of students based on task assigned to
check whether it is as per the instructions or not.
8. Teacher is expected to refer complete curriculum of the course and follow the guidelines for
implementation.

Instructions for Students


1. Students are expected to carefully listen to all the theory classes delivered by the faculty
members and understand the COs, content of the course, teaching and examination scheme,
skill set to be developed etc.
2. Students shall organize the work in the group and make record of all observations.
3. Students shall develop maintenance skill as expected by industries.
4. Student shall attempt to develop related hand-on skills and build confidence.
5. Student shall develop the habits of evolving more ideas, innovations, skills etc. apart from
those included in scope of manual.
6. Student shall refer technical magazines and data books.
7. Student should develop a habit of submitting the experimentation work as per the schedule
and s/he should be well prepared for the same.

Common Safety Instructions


1. Switch on the PC carefully (not to use wet hands)
2. Shutdown the PC properly at the end of your Lab
3. Carefully handle the peripherals (Mouse, Keyboard, Network cable etc).
4. Use Laptop in lab after getting permission from Teacher
Analysis and Design of Algorithms (3150703)

Index
(Progressive Assessment Sheet)

Sr. Objective(s) of Experiment Page Date of Date of Assessm Sign. of Remarks


No. No. perfor submiss ent Teacher
mance ion Marks with date
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 time complexity
of each function. Also draw a comparative chart
1. 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.
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 input versus steps executed/time taken for
2.
each cases (random, ascending, and
descending).
1.Selection Sort
2.Bubble Sort
3.Insertion Sort
4.Merge Sort
5.Quick Sort
Implement a function of sequential search and
count the steps executed by function on various
inputs (1000 to 5000) for best case, average case
3. 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.
Compare the performance of linear search and
4. binary search for Best case, Average case and
Worst case inputs.
Implement functions to print nth Fibonacci
number using iteration and recursive method.
5. Compare the performance of two methods by
counting number of steps executed on various
inputs. Also, draw a comparative chart.
Analysis and Design of Algorithms (3150703)

(Fibonacci series 1, 1, 2, 3, 5, 8….. Here 8 is


the 6th Fibonacci number).
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
6. 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).
Implement program to solve problem of making
7.
a change using dynamic programming.
Implement program of chain matrix
8.
multiplication using dynamic programming.
Implement program to solve LCS problem using
9.
dynamic programming.
Implement program to solve Knapsack problem
10.
using dynamic programming.
Implement program for solution of fractional
11. Knapsack problem using greedy design
technique.
Implement program for solution of Making
12.
Change problem using greedy design technique.
Implement program for Kruskal's algorithm to
13.
find minimum spanning tree.
Implement program for Prim's algorithm to find
14.
minimum spanning tree.
Implement DFS and BFS graph traversal
15.
techniques and write its time complexities.
Implement Rabin-Karp string matching
16.
algorithm.
Total
Analysis and Design of Algorithms (3150703) 230213116014

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:

Competency and Practical Skills: Algorithmic thinking, Programming Skills, Performance


analysis, and Mathematical skills

Relevant CO: CO1

Objectives: (a) Compare performance of various algorithms


(b) Judge best algorithm in terms of growth rate or steps executed

Equipment/Instruments: Computer System, Any C language editor

Theory:

1. Below are the steps to calculate sum of 1 to N numbers using loop

1. Take an input value for N.


2. Initialize a variable sum to zero.
3. Start a loop that iterates from i=1 to i=N.
4. In each iteration, add the value of i to the sum variable.
5. After the loop completes, output the value of sum.

2. Below are the steps to calculate sum of 1 to N numbers using equation

1. Take an input value for N.


2. Calculate sum as N*(N+1)/2.
3. Output the value of sum.

3. Below are the steps to calculate sum of 1 to N numbers using recursion

1. Take an input value for N.


2. Define a recursive function sum(n) that takes an integer argument n and returns the sum
of 1 to n.
3. In the sum(n) function, check if n equals 1. If it does, return 1 (the base case).
4. Otherwise, return the sum of n and the result of calling sum(n-1).
5. Output the result.

1
Analysis and Design of Algorithms (3150703) 230213116014
Algorithm :

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.

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.

Number of Steps Executed


Inputs
Loop method Equations Recursion
100

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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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:

Competency and Practical Skills: Algorithmic thinking, Programming Skills, Performance


analysis, and Mathematical skills

Relevant CO: CO1, CO2

Objectives: (a) Compare performance of various algorithms.


(b) Judge best sorting algorithm on sorted and random inputs in terms of growth
rate/time complexity.
(c) Derive time complexity from steps count on various inputs.

Equipment/Instruments: Computer System, Any C language editor

Theory:

1. Selection Sort Function in C

Void SelectionSort (inta[] , int n)


//Here ‘a’ is the array having ‘n’ number of data
{
Int MinIndex,temp;
for(int i =0;i<n-1;i++)
{
MinIndex=i;
for(int j=i+1;j<n;j++)
{
if (a[MinIndex]>a[j])
MinIndex=j;
}
if(MinIndex !=i)
{
temp=a[MinIndex];
a[MinIndex] = a[j];
a[j]=temp;
}

}
}

5
Analysis and Design of Algorithms (3150703) 230213116014

2. Bubble Sort Function in C

Void BubbleSort (inta[], int n)


//Here ‘a’ is the array having ‘n’ number of data
{
Int Swap_flag,temp;
for(inti=0;i<n-1;i++)
{
Swap_flag=0;
for(int j=0; j<n-i-1; j++)
{
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
Swap_flag=1;
}
}
if(swap_flag==0)
return;
}}

3. Insertion Sort Function in C

Void InsertionSort (inta[],int n)


//Here ‘a’ is the array having ‘n’ number of data
{
inti,j,key;
for(j=1;j<n;j++)
{
key=a[j];
i=j-1;
while(i>=0 && a[i]>key)
{
a[i+1] = a[i];
i= i-1;
}
a[i+1] =key;
}}

4. Merge Sort Function in C

void merge(inta[],int low, intmid,int high)


{
int temp[5000];
inti,j,k;
k = low;
j = mid +1;
i = low;
while((k<=mid) && (j<=high))

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);
}}

5. Quick Sort Function in C

int quicksort(int q[],intlb, intub)


{
int flag=1;
inti=0,j,key,t1,t2;
if(lb<ub)
{
i=lb;
j=ub+1;
key=q[lb];

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.

Number of Steps Executed (Random Data)


Inputs
Selection Bubble Insertion Merge Quick
1000
2000
3000
4000
5000
Time
Complexity

11
Analysis and Design of Algorithms (3150703) 230213116014

Chart:

Sorting Functions Analysis on Random Data


300000

250000

200000

150000

100000

50000

0
100 200 300 400 500

Selection Insertion Bubble Quick Merge

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

4. On what kind of data, the best case of Bubble sort occurs?


Answer:

5. On what kind of data, the worst case of Bubble sort occurs?


Answer:

6. On what kind of data, the best case of Quick sort occurs?


Answer:

7. On what kind of data, the worst case of Quick sort occurs?


Answer:

8. Which sorting algorithms are in-place sorting algorithms?


Answer:

9. Which sorting algorithms are stable sorting 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.

13
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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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:

Competency and Practical Skills: Algorithmic thinking, Programming Skills, Performance


analysis, and Mathematical skills

Relevant CO: CO1

Objectives: (a) Identify Best, Worst and Average cases of given problem.
(b) Derive time complexity from steps count on various inputs.

Equipment/Instruments: Computer System, Any C language editor

Theory:

Steps to implement sequential search is as below:

1. Take an input array A of n elements and a key value K.


2. Define a variable pos, initially set to -1.
3. Iterate through the array A, starting from the first element and continuing until either the key
value is found or the end of the array is reached.
4. For each element, compare its value to the key value K.
5. If the values match, set pos to the index of the current element and exit the loop.
6. If the end of the array is reached and the key value has not been found, pos remain equal to -1.
7. Output the value of pos.

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.

Number of Steps Executed


Inputs
Best Case Average Case Worst Case
1000
2000
3000
4000
5000
Time
Complexity

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.

References used by the students:

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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:

Competency and Practical Skills: Algorithmic thinking, Programming Skills, Performance


analysis, and Mathematical skills

Relevant CO: CO1, CO2

Objectives: (a) Identify Best, Worst and Average cases of given problem.
(b) Derive time complexity from steps count for different inputs.

Equipment/Instruments: Computer System, Any C language editor

Theory:

Steps to implement binary search are as below:

1. Take an input sorted array A of n elements and a target value T.


2. Define variables start and end to represent the start and end indices of the search range, initially
set to 0 and n-1 respectively.
3. Repeat the following steps while start <= end:
a. Calculate the midpoint index mid as (start + end) / 2.
b. If the value of the midpoint element A[mid] is equal to the target value T, return the
value of mid.
c. If the value of the midpoint element A[mid] is greater than the target value T, set end
to mid-1.
d. If the value of the midpoint element A[mid] is less than the target value T, set start
to mid+1.
4. If the target value T is not found in the array, return -1.
5. Output the value returned in Step 3, representing the position of the target value T in the array.

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.

Number of Steps Executed (Best Case)


Inputs
Linear Search Binary Search
100
200
300
400
500
Time
Complexity

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:

3. Which algorithm executes faster in worst case?


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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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:

Competency and Practical Skills: Algorithmic thinking, Programming Skills, Performance


analysis

Relevant CO: CO1, CO5

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

Equipment/Instruments: Computer System, Any C language editor

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,

Iterative version to print nth Fibonacci number is as below:


Input: An integer n, where n >= 1.
Output: The nth Fibonacci number.
Steps:
Initialize variables f0 = 1, f1 = 1, and i = 2.
If n is 1 or 2 then
Print 1
While i< n, do
a. Set f2 = f0 + f1.
b. Set f0 = f1.
c. Set f1 = f2.
d. Increment i by 1.
Print f1.

21
Analysis and Design of Algorithms (3150703) 230213116014
th
Recursive version to print n Fibonacci number is as below:

Input: An integer n, where n >= 1.


Output: The nth Fibonacci number.
If n is 1 or 2 then
return1.
else recursively compute next number using the (n-1)th and (n-2)th Fibonacci numbers, and return
their sum.
Print the result.

Algorithm:

Observations:

Result: Complete the below table based on your implementation of sequential search algorithm
and steps executed by the function.

Number of Steps Executed (Random data)


Inputs
Iterative Fibonacci Recursive Fibonacci
10
20
30 (if memory doesn’t permit then reduce the
40 range)
50
Time Complexity

22
Analysis and Design of Algorithms (3150703) 230213116014
Chart:

Conclusion:

Quiz:
1. What is the time complexity of iterative version of Fibonacci function?
Answer:

2. What is the time complexity of recursive 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

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
Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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:

Competency and Practical Skills: Algorithmic thinking, Programming Skills, Performance


analysis

Relevant CO: CO1, CO2

Objectives: (a) Improve the performance of quick sort in worst case.


(b) Compare the performance of both the version of quick sort on various inputs

Equipment/Instruments: Computer System, Any C language editor

Theory:

Steps to implement randomized version of quick sort are as below:

RANDOMIZED-QUICKSORT(A, low, high)


if (low< high) {
pivot= RANDOMIZED_PARTITION(A, low, high);
RANDOMIZED-QUICKSORT(A, low, pivot);
RANDOMIZED-QUICKSORT(A, pivot+1,high);
}
RANDOMIZED_PARTITION (A,low,high) {
pos = Random(low, high)
pivot = A[pos]
swap(pivot, a[low])
left = low
right = high
while ( left <right ) {
/* Move left while item < pivot */
while( A[left] <= pivot ) left++;
/* Move right while item > pivot */
while( A[right] > pivot ) right--;
if ( left < right )
swap(A[left],A[right]);}
/* right is final position for the pivot */
swap(A[right], pivot);
return right; }

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.

Number of Steps Executed (Random data)


Inputs Randomized Quick
Basic Quick Sort
Sort
1000
2000
3000
4000
5000
Time Complexity

26
Analysis and Design of Algorithms (3150703) 230213116014

Number of Steps Executed (Random data)


Inputs
Ascending Descending
1000
2000
3000
4000
5000
Time Complexity

Chart:

Conclusion:

Quiz:

1 What is the time complexity of Randomized Quick Sort in worst case?


Answer:

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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO3

Objectives: (a) Understand Dynamic programming algorithm design method.


(b) Solve the optimization based problem.
(c) Find the time complexity of the algorithm.

Equipment/Instruments: Computer System, Any C language editor

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.

Solution steps are as follow:

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.

Figure: 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]}

Steps to solve making change problem are as below:

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:

2. Does above algorithm always return optimal answer?


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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO3

Objectives: (a) Understand Dynamic programming algorithm design method.


(b) Solve the optimization based problem.
(c) Find the time complexity of the algorithm.

Equipment/Instruments: Computer System, Any C language editor

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

A1 (A2 A3) = (5 x 4) x {(4 x 6) x (6 x 2) }


= (5 x 4 x 2) + (4 x 6 x 2)

= 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.

Where d = {d0, d1, d2, …, dn} is the vector of matrix dimensions.


m[i, j] = Least number of multiplications required to multiply matrix sequence Ai….Aj .

Steps to solve chain matrix multiplication problem are as below:


Algorithm MATRIX_CHAIN_ORDER(p)
// p is sequence of n matrices
n ← length(p) - 1
for i ← 1 to n do
m[i,i] ← 0
end
for l ← 2 to n do
for i ← 1 to n – l + 1 do
j←i+l-1
m[i, j] ← ∞
for k ← i to j – 1 do
q ← m[i, k]+ m[k + 1, j] + di – 1 * dk * dj
if q < m[i, j] then
m[i,j ] ← q
s[i, j] ← k
end

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:

2. Does above algorithm always return optimal answer?


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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO3

Objectives: (a) Understand Dynamic programming algorithm design method.


(b) Solve the optimization based problem.
(c) Find the time complexity of the algorithm.

Equipment/Instruments: Computer System, Any C language editor

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.

longest common subsequence (LCS) using Dynamic Programming:


Let X=<x1,x2,x3....,xm> and Y=<y1,y2,y3....,ym> be the sequences. To compute the length of an
element the following algorithm is used.
Step 1 − Construct an empty adjacency table with the size, n × m, where n = size of sequence X and
m = size of sequence Y. The rows in the table represent the elements in sequence X and columns
represent the elements in sequence Y.
Step 2 − The zeroth rows and columns must be filled with zeroes. And the remaining values are
filled in based on different cases, by maintaining a counter value.
 Case 1 − If the counter encounters common element in both X and Y sequences, increment
the counter by 1.
 Case 2 − If the counter does not encounter common elements in X and Y sequences at T[i,
j], find the maximum value between T[i-1, j] and T[i, j-1] to fill it in T[i, j].

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:

Algorithm: LCS-Length-Table-Formulation (X, Y)


m := length(X)
n := length(Y)
for i =1 to m do
C[i, 0] := 0
for j = 1 to n do
C[0, j] := 0

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

Algorithm: Print-LCS (B, X, i, j)


If i=0 and j=0
return
if B[i, j] = ‘D’
Print-LCS(B, X, i-1, j-1)
Print(xi)
else if B[i, j] = ‘U’
Print-LCS(B, X, i-1, j)
else
Print-LCS(B, X, i, j-1)
Implementation:

39
Analysis and Design of Algorithms (3150703) 230213116014

Observations:

Result

Conclusion:

Quiz:
1. What is the time complexity of above algorithm?
Answer:

2. Does above algorithm always return optimal answer?


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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO3

Objectives: (a) Understand Dynamic programming algorithm design method.


(b) Solve the optimization based problem.
(c) Find the time complexity of the algorithm.

Equipment/Instruments: Computer System, Any C language editor

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.

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.

Algorithm DP_BINARY_KNAPSACK (V, W, M)


// Description: Solve binary knapsack problem using dynamic programming
// Input: Set of items X, set of weight W, profit of items V and knapsack capacity M
// Output: Array V, which holds the solution of problem
for i ← 1 to n do
V[i, 0] ← 0
end
for i ← 1 to M do
V[0, i] ← 0
end
for V[0, i] ← 0 do
for j ← 0 to M do
if w[i] ≤ j then
V[i, j] ← max{V[i-1, j], v[i] + V[i – 1, j – w[i]]}
else
42
Analysis and Design of Algorithms (3150703) 230213116014
V[i, j] ← V[i – 1, j] // w[i]>j
end
end
end

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:

2. Does above algorithm always return optimal answer?


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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO3

Objectives: (a) Understand greedy algorithm design method.


(b) Solve the optimization based problem.
(c) Find the time complexity of the algorithm.

Equipment/Instruments: Computer System, Any C language editor

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.

1. Compute the value-to-weight ratio for each item in the knapsack.


2. Sort the items in decreasing order of value-to-weight ratio.
3. Initialize the total weight and total value to 0.
4. For each item in the sorted list:
a. If the entire item can be added to the knapsack without exceeding the weight
capacity, add it and update the total weight and total value.
b. If the item cannot be added entirely, add a fraction of the item that fits into the
knapsack and update the total weight and total value accordingly.
c. If the knapsack is full, stop the algorithm.
5. Return the total value and the set of items in the knapsack.

Implementation:

46
Analysis and Design of Algorithms (3150703) 230213116014

Observations:

Result

Conclusion:

Quiz:
1. What is the time complexity of above knapsack algorithm?
Answer:

2. Does above algorithm always return optimal answer?


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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO3

Objectives: (a) Understand greedy algorithm design method.


(b) Solve the optimization based problem.
(c) Find the time complexity of the algorithm.

Equipment/Instruments: Computer System, Any C language editor

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

1. Initialize a list of coin denominations in descending order.


2. Initialize a list of coin counts, where each count is initially 0.
3. While the remaining amount is greater than 0:
a. For each coin denomination in the list:
i. If the denomination is less than or equal to the remaining amount, add one
coin to the count and subtract the denomination from the remaining
amount.
ii. If the denomination is greater than the remaining amount, move on to the
next denomination.
4. Return the list of coin counts.

Algorithm:

49
Analysis and Design of Algorithms (3150703) 230213116014

Observations:

Result

Conclusion:

Quiz:
1. What is the time complexity of above knapsack algorithm?
Answer:

2. Does above algorithm always return optimal answer?


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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO3, CO6

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.

Equipment/Instruments: Computer System, Any C language editor

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.

1. Sort all the edges in non-decreasing order of their weight.


2. Initialize an empty set of edges for the minimum spanning tree.
3. For each edge in the sorted order, add the edge to the minimum spanning tree if it does not
create a cycle in the tree. To check if adding the edge creates a cycle, you can use the Union-
Find algorithm or a similar method to keep track of the connected components of the graph.
4. Continue adding edges until there are V-1 edges in the minimum spanning tree, where V is the
number of vertices in the graph.
5. Return the set of edges in the minimum spanning tree.
6. Implement the program based on above steps for the solution of fractional knapsack problem.

Kruskal’s Algorithm is as follow:

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:

2. Does above krushkal’salgorithm always return optimal answer?


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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO3, CO6

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.

Equipment/Instruments: Computer System, Any C language editor

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.

Prim’s Algorithm is as follow:

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:

2. Does above Prim’s algorithm always return optimal answer?


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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO6

Objectives: (a) Understand Graph traversal techniques.


(b) Visit all nodes of the graph.
(c) Find the time complexity of the algorithm.

Equipment/Instruments: Computer System, Any C language editor

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.

Consider the following steps for the implementation of DFS algorithm:


1. Create an empty stack and push the starting node onto it.
2. Mark the starting node as visited.
3. While the stack is not empty, pop a node from the stack and mark it as visited.
4. For each adjacent node to the current node, if the adjacent node has not been visited, mark it as
visited and push it onto the stack.
5. After processing all the adjacent nodes, you can do something with the current node, such as
printing it or storing it.
6. Repeat steps 3 to 5 until the stack is empty.

Consider the following steps for the implementation of BFS algorithm:

1. Create a queue Q and a set visited.


2. Add the starting node to the queue Q and mark it as visited.
3. While the queue is not empty:
a. Dequeue a node from the queue Q and process it.
b. For each adjacent node of the dequeued node:
i. If the adjacent node has not been visited, mark it as visited and enqueue it into the
queue Q.

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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

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

Relevant CO: CO4

Objectives: (a) Find all occurrences of a pattern in a given text.


(b) Improve the performance of the brute force algorithm.
(c) Find a pattern in a given text with less time complexity in the average case.

Equipment/Instruments: Computer System, Any C language editor

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.

Let’s take an example to make it more clear.


Assume the given string S = “cxyzghxyzvjkxyz” and pattern P = “xyz” and we have to find all the
occurrences of ‘P’ in ‘S’.

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.

Steps of Rabin-Karp algorithm are as below:

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:

4. What is the worst-case time complexity of the Rabin-Karp 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.

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

Rubric wise marks obtained:

Rubrics Understanding of Program Documentation Total


problem Implementation &Timely Submission (10)
(3) (5) (2)
Good Avg. Poor Good Avg. Poor Good Avg. Poor
(3) (2-1) (1-0) (5-4) (3-2) (1-0) (2) (1) (0)
Marks

Signature of Faculty:

67

You might also like