Professional Documents
Culture Documents
Instructor Chamber : Faculty Block 102, Coup A, First Floor, Block-C Campus-14
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
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.
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
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.
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
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.
1. (31415, 14142)
2. (56, 32566)
3. (34218, 56)
4. (12,15)
5. (31415, 31415)
6. (12, 12)
---------------------------------------------------------------------------------------------------------------------
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.
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.
After conducting the experiments of Lab Day 3, you will be evaluated out of 2.5 by your
allotted Technical Assistant Sir/ Madam
Time Complexity
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.
Time Complexity
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.
Time Complexity
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
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:
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.
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
---------------------------------------------------------------------------------------------------------------------
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
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 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
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:
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:
Aim of Experiment 3: To implement Floyd Warshall Algorithm for the given graph by user input
Input:
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
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
Total 30
10 * 2= 20
Quiz before Midsem and Sessional (Dates will
be informed in advance)
Total 30
15+ 5 = 20
Writing and Execution of Sessional Question
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