You are on page 1of 28

School of Computer Engineering

KIIT deemed to be University


Laboratory Lesson Plan – Autumn’2023 (5th Semester)

Discipline: CS / IT / CSCE / CSSE


Course Name and Code: Algorithms Laboratory-CS2098 (L-T-P-Cr: 0-0-2-1)
Instructor Name : Dr. Pratyusa Mukherjee, Email :: pratyusa.mukherjeerfcs@kiit.ac.in

Instructor Chamber : Faculty Block 102, Coup A, First Floor, Block-C Campus-14

Technical Assistants Names : ______________________________________

Course Contents:
Review of Fundamentals of Few simple programs involving arrays
Data Structure
Fundamentals of Algorithmic Analysis of time complexity of small algorithms through
Problem Solving step/frequency count method. (Sort Algorithms, GCD, Prime
No, Etc)

Divide and Conquer Method Binary Search, Merge Sort, Quick Sort, Randomized Quick
Sort

Heap & Priority Queues Building a heap, Heap sort algorithm, Min-Priority queue,
Max-Priority queue

Greedy Technique Fractional knapsack problem, Activity selection problem,


Huffman’s code

Dynamic Programming Matrix Chain Multiplication, Longest Common Subsequence

Graph Algorithms Dis-joint Set Data Structure, Representation Of Graph, Graph


Traversals (BFS DFS), Single Source Shortest Path
(Dijkstra’s Algorithm, Bellman-Ford Algorithm), All Pair
Shortest Path (Floyd-Warshall Algorithm), Minimum Cost
Spanning Tree (Kruskal’s Algorithm, Prim’s Algorithm),
List of Experiments (Day Wise)
Lab Day 1 :: Revision of Fundamentals of Data Structures
Aim of the Experiment 1: To store random numbers into an array of N integers and then find out
the smallest and largest number stored in it. N is the user input.

Input: Your Roll Number as the array


Output: The smallest and largest number
Sample Screenshot:

Aim of the Experiment 2: To store random numbers into an array of N integers, where the array
must contains some duplicates and then do the following:
a) Find out the total number of duplicate elements.
b) Find out the most repeating element in the array.

Input: Your Roll Number as the array


Output: The total number of duplicate elements and the most repeating element in the array
Sample Screenshot:

Aim of the Experiment 3: To rearrange the elements of an array of N integers such that all even
numbers are followed by all odd numbers

Input: Your Roll Number as the array


Output: The rearranged array
Sample Screenshot:
Aim of the Experiment 4: To take three variable (a, b, c) as separate parameters and swap the
values stored so that value a goes to b, b to c and c to a by using a SWAP(x,y) function that
swaps/exchanges the numbers x & y.

Input: Last 3 Digits of your Roll Number


Output: The swapped Numbers
Sample Screenshot:

Aim of the Experiment 5: To take A an n*n square matrix array as an input and then WAP by
using appropriate user defined functions for the following:
a) Find the number of nonzero elements in A
b) Find the sum of the elements above the leading diagonal.
c) Display the elements below the minor diagonal.
d) Find the product of the diagonal elements.

Input: The 3*3 sqaure matrix by filling your Roll Number row-wise. In case you are short of
digits in your roll number, fill it with 0
Output: The number of nonzero elements, sum of the elements above the diagonal, elements
below the minor diagonal, product of diagonal elements
Sample Screenshot:

After conducting the experiments of Lab Day 1, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam
Practice Questions for Lab Day 1:
1) Write a program to find out the second smallest and second largest element stored in an array
of n integers. n is the user input. The array takes input through random number generation within
a given range. How many different ways you can solve this problem. Write your approaches &
strategy for solving this problem.
2) Write a program to swap pair of elements of an array of n integers from starting. If n is odd,
then last number will be remain unchanged.
3) Write a program to display an array of n integers (n>1), where at every index of the array
should contain the product of all elements in the array except the element at the given index.
Solve this problem by taking single loop and without an additional array.
Input Array : 3 4 5 1 2
Output Array :40 30 24 120 60
4) Write a program using a function for computing ⌊√n⌋ for any positive integer. Besides
assignment and comparison, your algorithm may only use the four basic arithmetic operations.
------------------------------------------------------------------------------------------------------------------------------------------
Lab Day 2 :: Fundamentals of Algorithmic Problem Solving
Aim of the Experiment 1: To test whether a number n, entered through keyboard is prime or not
by using different algorithms you know for at-least 10 inputs and note down the time complexity
by step/frequency count method for each algorithm & for each input. Finally make a comparison
of time complexities found for different inputs, plot an appropriate graph & decide which
algorithm is faster.

Input: The input numbers


Output: The following table for 10 different inputs,the resultant graph plot, the conclusion drawn

Time by frequency(Prime Number Testing)

Sl. No. Input(n)


Algorithm-I Algorithm-II Algorithm-III

1. 7 7 9 16

2. 13 9 15 29

3. 16 9 9 36

4. 23 11 25 49

5. 45 11 11 95

6. 77 19 19 157

7. 89 21 91 181

8. 97 21 99 197

9. 112 9 9 233

10. 193 29 195 389

The conclusion about which algorithm is the fastest.

Aim of the Experiment 2: To find out GCD (greatest common divisor) using the following three
algorithms.

a) Euclid’s algorithm
b) Consecutive integer checking algorithm.
c) Middle school procedure which makes use of common prime factors. For finding
list of primes implement sieve of Eratosthenes algorithm
Find out which algorithm is faster for the following data. Estimate how many times it will be
faster than the other two by step/frequency count method in each case.

i. Find GCD of two numbers when both are very large i.e.GCD(31415, 14142) by applying each
of the above algorithms.
ii. Find GCD of two numbers when one of them can be very large i.e.GCD(56, 32566) or
GCD(34218, 56) by applying each of the above algorithms.
iii. Find GCD of two numbers when both are very small i.e.GCD(12,15) by applying each of the
above algorithms.
iv. Find GCD of two numbers when both are same i.e.GCD(31415, 31415) or GCD(12, 12) by
applying each of the above algorithms.
Write the above data in the following format and decide which algorithm is faster for
the particular data.

Input: As given in the table below


Input Frequency Count(GCD Algorithm)

GCD(x, y) Euclid’s Consecutive Middle school Remarks


Sl. No. integer procedure
algorithm (which
checking algorithm.
algorithm is
faster)

1. (31415, 14142)

2. (56, 32566)

3. (34218, 56)

4. (12,15)

5. (31415, 31415)

6. (12, 12)

Output: Individual pair wise GCD outcomes followed by the table


Input Frequency Count(GCD Algorithm)

Sl. No. GCD(x, y) Euclid’s Consecutive Middle school Remarks


integer checking procedure
algorithm(I) algorithm(II) algorithm(III) (which is
faster)

1. (31415, 14142) 43 28285 631 I

2. (56, 32566) 19 111 50 I

Complete the table for the remaining inputs

The conclusion about which method is the fastest.

---------------------------------------------------------------------------------------------------------------------
Lab Day 3 :: Fundamentals of Algorithmic Problem Solving

Aim of the Experiment 1: Write a menu driven program as given below, to sort an array of n
integers in ascending order by insertion sort algorithm and determine the time required (in terms
of step/frequency count) to sort the elements. Repeat the experiment for different values of n and
different nature of data (i.e.apply insertion sort algorithm on the data of array that are already
sorted, reversely sorted and random data). Finally plot a graph of the time taken versus n for each
type of data. The elements can be read from a file or can be generated using the random number
generator. Depending on the nature of data what is your conclusion about the time taken by each
set of data. Can you justify your conclusion.

INSERTION SORT MENU


0. Quit
1. n Random numbers=>Array
2. Display the Array
3. Sort the Array in Ascending Order by using Insertion Sort Algorithm
4. Sort the Array in Descending Order by using any sorting algorithm
5. Time Complexity to sort ascending of random data
6. Time Complexity to sort ascending of data already sorted in ascending order
7. Time Complexity to sort ascending of data already sorted in descending order
8. Time Complexity to sort ascending of data for all Cases (Data Ascending, Data in Descending
& Random Data) in Tabular form for values n=5000 to 50000, step=5000

Enter your choice:


If the choice is option 8, the it will display the tabular form as follows:

Input: As given in the table below for different types of data


Time Complexity

Sl. No. Value of n Random Data Data in Ascending Data in Descending

1. 5000

2. 10000

3. 15000

4. 20000

5. 25000

6. 30000

7. 35000

8. 40000

9. 45000

10. 50000

Output: The filled table for 10 different inputs, the resultant graph plot, the conclusion drawn

Aim of the Experiment 2: Rewrite the program for Experiment 1 (Insertion Sort) with the
following details.
i. Compare the best case, worst case and average case time complexity with the same data except
time complexity will be calculated by the count of the CPU clock time.
ii. Plot a graph showing the above comparison (n, the input data Vs. CPU times for best, worst &
average case)
iii. Compare manually Experiment 1 graph vs Experiment 2 graph and draw your inference.

Input and Output: Similar to that of Experiment 1


Aim of the Experiment 3: To define an user defined function that is used to produce the rounded
matrix as described in the below example. Find out the time complexity of your
algorithm/function. Consider an n × n matrix A = (aij), each of whose elements aij is a non-
negative real number, and suppose that each row and column of A sums to an integer value. We
wish to replace each element aij with either ⌊a ij⌋ or ⌈aij⌉ without disturbing the row and column
sums. Here is an example:
Input: Output:

After conducting the experiments of Lab Day 3, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam

Practice Questions for Lab Day 3:


1) Let A be a list of n (not necessarily distinct) integers. Write a program by using User Defined
Function(UDF)s to test whether any item occurs more than n/2 times in A.
a) UDF should take O(n2) time and use no additional space.
b) UDF should take O(n) time and use O(1) additional space.
---------------------------------------------------------------------------------------------------------------------
Before conducting the experiments of Lab Day 4, your lab record will be evaluated out of
2.5 by your allotted Technical Assistant Sir/ Madam

Lab Day 4 :: Divide and Conquer Method


Aim of the Experiment 1: Write a program to search an element x in an array of n integers using
binary search algorithm that uses divide and conquer technique. Find out the best case, worst
case and average case time complexities for different values of n and plot a graph of the time
taken versus n. The n integers can be generated randomly and x can be chosen randomly, or any
element of the array or middle or last element of the array depending on type of time complexity
analysis.

Input: Array of Random Numbers of different sizes


Output: The following table, the resultant graph plot and the conclusion drawn

Time Complexity

Sl. No. Value of n Best Case Average Case Worst Case

1. 5000

2. 10000

3. 15000

4. 20000
5. 25000

6. 30000

7. 35000

8. 40000

9. 45000

10. 50000

Aim of the Experiment 2: Write a program to sort a list of n elements using the merge sort
method and determine the time required to sort the elements. Repeat the experiment for different
values of n and different nature of data (random data, sorted data, reversely sorted data) in the
list. n is the user input and n integers can be generated randomly. Finally plot a graph of the time
taken versus n.

Input: Array of Random Numbers of different sizes


Output: The following table, the resultant graph plot and the conclusion drawn

Time Complexity

Sl. No. Value of n Best Case Average Case Worst Case

1. 5000

2. 10000

3. 15000

4. 20000

5. 25000
6. 30000

7. 35000

8. 40000

9. 45000

10. 50000

Aim of the Experiment 3: Write a program to sort a list of n elements using the quick sort method
and determine the time required to sort the elements. Repeat the experiment for different values
of n and different nature of data (random data, sorted data, reversely sorted data, all same
elements data) in the list. n is the user input and n integers can be generated randomly. Finally
plot a graph of the time taken versus n.

Input: Array of Random Numbers of different sizes


Output: The following table, the resultant graph plot and the conclusion drawn

Time Complexity

Sl. No. Value of n Best Case Average Case Worst Case

1. 5000

2. 10000

3. 15000

4. 20000

5. 25000

6. 30000

7. 35000

8. 40000
9. 45000

10. 50000

After conducting the experiments of Lab Day 4, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam

Practice Questions for Lab Day 4:


1) Write a program to sort a list of n elements using the randomized quick sort method and
determine the time required to sort the elements. Repeat the experiment for different values of n
and different nature of data (random data, sorted data, reversely sorted data, all same elements
data) in the list. n is the user input and n integers can be generated randomly. The chosen pivot
location will be last digit of your roll number. Finally plot a graph of the time taken versus n.
Comment on how the location of the randomly chosen pivot affects the complexity of the
algorithm.

2) Write a program to use divide and conquer method to determine the time required to find the
maximum and minimum element in a list of n elements. The data for the list can be generated
randomly. Compare this time with the time taken by straight forward algorithm or brute force
algorithm for finding the maximum and minimum element for the same list of n elements. Show
the comparison by plotting a required graph for this problem.

3) Write a program that uses a divide-and-conquer algorithm/user defined function for the
exponentiation problem of computing an where a > 0 and n is a positive integer. How does this
algorithm compare with the brute-force algorithm in terms of number of multiplications made by
both algorithms.

4) A and B are playing a guessing game where B first thinks up an integer X (positive, negative
or zero, and could be of arbitrarily large magnitude) and A tries to guess it. In response to A’s
guess, B gives exactly one of the following three replies:

a) Try a bigger number


b) Try a smaller number or
c) You got it.
Write a program by designing an efficient algorithm to minimize the number of guesses A has to
make. An example (not necessarily an efficient one) below: Let B thinks up the number 35

A’s guess B’s response


10 Try a bigger number

20 Try a bigger number

30 Try a bigger number

40 Try a smaller number

35 You got it

5) In a social gathering, there are b boys and g girls (b > g) of different ages. You have two
unsorted arrays giving their ages (one for the boys, the other for the girls). Write a program by
devising an efficient O(b log g) algorithm to find out the ages that are common between both the
boys and girls.

Example:
If Arrayboy = {10, 20, 11, 89, 23, 21} and Arraygirl = {12, 30, 11, 20},
Then Arraycommon = {11, 20}

6) Refer the following new sort algorithm for sorting an array A of n numbers. The
algorithm is described below:
(i) If there is only one number, return.
(ii) If there are two numbers, perform a single comparison to determine the order.
(iii) If there are more than two numbers, then first sort the top two-thirds of the elements
recursively. Follow this by sorting the bottom two-thirds of the elements recursively and then
sorting the top two-thirds of the elements again.
a) Write a program that uses a recursive algorithm to implement the above strategy.
b) What is the comparison complexity of this new-sort algorithm? Formulate a recurrence
relation and solve the same to justify your answer.

-------------------------------------------------------------------------------------------------------------------
Lab Day 5 :: Heap and Priority Queues
Aim of the Experiment 1: To write a menu (given as follows) driven program to sort an array of n
integers in ascending order by heap sort algorithm and perform the operations on max heap.
Determine the time required to sort the elements. Repeat the experiment for different values of n,
the number of elements in the array to be sorted and plot a graph of the time taken versus n. The
elements can be read from a file or can be generated using the random number generator.

MAX-HEAP & PRIORITY QUEUE MENU


0. Quit
1. n Random numbers=>Array
2. Display the Array
3. Sort the Array in Ascending Order by using Max-Heap Sort technique
4. Sort the Array in Descending Order by using any algorithm
5. Time Complexity to sort ascending of random data
6. Time Complexity to sort ascending of data already sorted in ascending order
7. Time Complexity to sort ascending of data already sorted in descending order
8. Time Complexity to sort ascending all Cases (Data Ascending, Data in Descending & Random
Data) in Tabular form for values n=5000 to 50000, step=5000
9. Extract largest element
10. Replace value at a node with new value
11. Insert a new element
12. Delete an element

Enter your choice:


If the choice is option 8, the it will display the tabular form as follows:

Input: As given in the table below for different types of data


Time Complexity

Sl. No. Value of n Sorted Data Random Data Reversely Sorted Data

1. 5000

2. 10000

3. 15000

4. 20000

5. 25000

6. 30000

7. 35000

8. 40000

9. 45000

10. 50000

Output: The filled table for 10 different inputs, the resultant graph plot, the conclusion drawn

After conducting the experiments of Lab Day 5, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam

Practice Questions for Lab Day 5:


1) Write a program with the following two functions.
a) MaxMin() - Takes a heap, returns ‘MAX’ if it is a max-heap and converts it into a mean-heap
in linear (to the number of elements) time.
b) MinMax() - Takes a heap, returns ‘MIN’ if it is a min-heap and converts it into a max-heap in
linear (to the number of elements) time.

---------------------------------------------------------------------------------------------------------------------
Before conducting the experiments of Lab Day 6, your lab record will be evaluated out of
2.5 by your allotted Technical Assistant Sir/ Madam

Lab Day 6 :: Greedy Techniques


Aim of the Experiment 1: To write a program to implementation of Fractional Knapsack
algorithm.
Input:
Weight: <10, 5, 15, 7, 6 ,18, 3> Value: <2, 3, 5, 7, 1, 4, 1>
Capacity of Knapsack: 5 * Your Roll MOD 10

Output: Fraction of Items to be kept in the sack and the final profit earned.
Sample Screenshot:
Aim of the Experiment 2: To write a program to implement the activity-selection problem. You
are given n activities with their start and finish times. Select the maximum number of activities
that can be performed by a single person, assuming that a person can only work on a single
activity at a time.
Input: Consider the following 6 activities ( 0, 1, 2, 3, 4, 5). start[] = {1, 3, 0, 5, 8, 5}; finish[] =
{2, 4, 6, 7, 9, 9}
Output: The maximum set of activities that can be executed by a single person is {0, 1, 3, 4}.

Aim of the Experiment 3: To write a program to implement the Huffman Coding


Input: Let N = (Last digit of your roll + 1).
Character A B C D E
Frequency 5 N+1 10 15 2N

Output: The resultant Huffman Codes


After conducting the experiments of Lab Day 6, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam

Practice Questions for Lab Day 6:


1) You are given n activities with their start and finish times, input through keyboard. Write a
program to select all possible maximum number of activities that can be performed by a single
person including one activity of his/her choice, assuming that a person can only work on a single
activity at a time.
Example:
Enter maximum number of activities: 11
Enter the name of activities with their start and finish time: (p, 3, 5) (q, 0,6) (r, 3, 9) (s, 5, 9)
(t, 6, 10) (u, 8, 11) (v, 2, 14) (w, 12, 16) (x, 5, 7) (y, 8, 12) (z, 1, 4)
Enter the name of activity of person’s choice: y
Maximum number of activity selected = 4
Selected Activity Set (s): {p, x, y w} or {z, x, y w}
-----------------------------------------------------------------------------------------------------------------
Lab Day 7 :: Dynamic Programming
Aim of Experiment 1: To write a C program to implement the Longest Common Subsequence of
two given strings.
Input: String 1: Your First Name, String 2: Your Last Name
Output: The resultant tables needed to calculate the LCS, the length of the LCS and the final
sequence.
Sample Screenshot:

Aim of Experiment 2: To write a program to implement the matrix chain multiplication problem
using m-table & s-table to find optimal ordering of matrix multiplication.
Input: Dimensions of the matrices {A1, A2, A3, A4, A5} = {4 x 6, 6 x 2, 2 x N, N x 4, 4 x 1}
where N = Last digit of your roll + 1.
Output: The resultant m-table & s-table, the optimal parenthesization and the number of
multiplications required.
Sample Screenshot:
After conducting the experiments of Lab Day 7, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam

Practice Questions for Lab Day 7:


1) Write a program to find the Longest palindromic subsequence
2) Given n elements input through keyboard, write a program that prints the length of the longest
increasing subsequence whose adjacent element difference is one.
Example:
Enter maximum size of array: 10
Enter 10 integers (separated by space): 3 10 3 11 4 5 6 7 8 12
The longest increasing subsequence whose adjacent element differs by one: {3, 4, 5, 6, 7, 8}
The length of increasing subsequence: 6

3) Given an array of n positive integers, input through keyboard. Write a program to find the sum
of maximum sum subsequence of the given array such that the integers in the subsequence are
sorted in increasing order.
Example:
Enter maximum size of array: 7
Enter 10 integers (separated by space): 1 101 2 3 100 4 5
The maximum sum subsequence : {1, 2, 3,100} with sum 106

---------------------------------------------------------------------------------------------------------------------
Before conducting the experiments of Lab Day 6, your lab record will be evaluated out of
2.5 by your allotted Technical Assistant Sir/ Madam
Lab Day 8 :: Graph Algorithms
Aim of Experiment 1: To implement DFS for a given graph as adjacency matrix.
Input Output:
Consider the starting point be Your Roll MOD 5

Sample Screenshot:

Aim of Experiment 2: To implement BFS for a given graph as adjacency matrix.


Input Output:
Consider the starting point be Your Roll MOD 5

Sample Screenshot:

Aim of Experiment 3: To implement Kruskal’s Algorithm for the given graph by user input
Input: Sample Output Screenshot:
Consider the N = Last Digit of your Roll + 1

Aim of Experiment 4: To implement Prim’s Algorithm for the given graph by user input
Input: Sample Output Screenshot:
Consider the N = Last Digit of your Roll + 1

---------------------------------------------------------------------------------------------------------------------
After conducting the experiments of Lab Day 8, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam
Lab Day 9 :: Graph Algorithms
Aim of Experiment 1: To implement Dijkstra’s Algorithm for the given graph by user input
Input:

Sample Output Screenshot:


Aim of Experiment 2: To implement Bellman-Ford Algorithm for the given graph by user input
Input:

Sample Output Screenshot:

Aim of Experiment 3: To implement Floyd Warshall Algorithm for the given graph by user input
Input:

Sample Output Screenshot:


--------------------------------------------------------------------------------------------------------------------
After conducting the experiments of Lab Day 9, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam
Lab Day 10: On Lab Day 10, your lab record will be evaluated out of 2.5 by your allotted
Technical Assistant Sir/ Madam
Grading Policies:

The process of evaluation is distributed over two main components: continuous evaluation and
end-semester examination. Continuous evaluation further consists of several repetitions of same
evaluation scheme.

60
Continuous Evaluation

End Semester Evaluation 40

Total 100
Continuous Evaluation components by Allotted Technical Assistant::

2.5 * 8= 20
Regular Program Execution Evaluation on
Lab Day 1,3,4,5,6,7,8,9

Lab Record Evaluation on Lab Day 4,6,8,10 2.5 * 4 = 10

Total 30

Continuous Evaluation components by Instructor::

10 * 2= 20
Quiz before Midsem and Sessional (Dates will
be informed in advance)

Surprise Viva on any Lab Day before Midsem 5 * 2 = 10


and Sessional

Total 30

End semester evaluation ::

15+ 5 = 20
Writing and Execution of Sessional Question

Quiz On the day of the Sessional 20

Total 40

2 Defaulter Labs will be given before Midsem and Sessional, however each student will be
given only one defaulter chance.
Reference Materials :- Class Notes, Lab Manual
1. T. H. Coreman, C. E. Leiserson, R. L. Rivest, “Introduction to Algorithms, PHI

2. Sara Basse, A. V. Gelder, “Computer Algorithms”, Eddison Wesley.

3. Michael Goodrich, Roberto Tamassia, “Algorithm Design: Foundations, Analysis &


Internet Examples”, John Wiley & Sons.

You might also like