Design and Analysis of Algorithm Lab (BSCS2351) Lab Manual
Design and Analysis of Algorithm Lab (BSCS2351) Lab Manual
Lab Manual
DURATION OF
CREDITS 1 24 Weeks
SEMESTER
PREREQUISITE SESSION
Data Structure Using C/C++ 2 Hrs per Week
SUBJECTS DURATION
Prepared by:
DEAN/HOD
Department of Computer Science and Engineering
Contact Details
Contact Person Name of the person Contact No. Room No.
Dean Dr. S Raju +91 73584 54577
HOD Dr. Tapas Kumar +91 98183 39510
LabCourseCoordinator Dr. S.P.S. Chauhan 9871498875 C-331
Lab Course Faculty Dr. S.P.S. Chauhan 9871498875 C-331
Lab Technician
Course Detail
L TP 002
Course Type CORE
Semester Offered 5th
Academic Year 2019-2020
Slot
Class Room C-306
Faculty Details:
Name Dr. S.P.S. Chauhan
Website link
Designation Associate Professor
School Department of Computer Science and Engineering
Cabin No C-331
Intercom -------
Open Hours 9.00 AM to 11.00 AM(Tuesday)
VISION
MISSION
➢ Involve in societal outreach programs to identify concerns and provide sustainable ethical solu-
tions.
➢ Encourage life-long learning and team-based problem solving through an enabling environment.
2. VISION AND MISSION OF DEPARTMENT
VISION
➢ To be known globally as a premier department of computer science and engineering for value-
based education, multi-disciplinary research and innovation.
MISSION
➢ Create a strong foundation on fundamentals of computer science and engineering through out-
come based teaching- learning process.
➢ Establish state-of-art facilities for analysis design and implementation to develop sustainable
ethical solution.
➢ Involve the students in group activity including that of professional bodies to develop leadership
and communication skills.
3. PROGRAM EDUCATIONAL OBJECTIVES
Graduates of Computer Science and Engineering will be globally competent and provide
PEO1 sustainable solutions for interdisciplinary problems as team players
Graduates of Computer Science and Engineering will engage in professional activities with ethical
PEO2 practices in the field of Computer Science and Engineering to enhance their own stature to
contribute towards society
Graduates of Computer Science and Engineering will acquire specialized knowledge in emerging
PEO3 technologies for research, innovation and product development.
4. PROGRAMME OUTCOMES
Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
PO5 engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
The engineer and society: Apply reasoning informed by the contextual knowledge to assess
PO6 societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
PO8 of the engineering practice.
Individual and team work: Function effectively as an individual, and as a member or leader in
PO9 diverse teams, and in multidisciplinary settings.
Life-long learning: Recognize the need for, and have the preparation and ability to engage in
PO12 independent and life-long learning in the broadest context of technological change.
Able to analyze, design and implement sustainable and ethical solutions in the field of computer
PSO1 science.
PSO2 Able to use problem solving skills to develop efficient algorithmic solutions.
4.
Hardware Requirements
➢ No specific requirements. Any computer Hardware capable of running DOS can be used for this
course.
EXPERIMENT LIST
S.
No Title of Lab Experiments
.
1. Write a program to sort given set of numbers in ascending/descending order using Bub-
1
ble sort and also search a number using binary search.
Write a program to sort given set of numbers in ascending/descending order using Insertion
2
sort and also search a number using linear search.
3 Quick sort and any other sorting algorithm. Also record the time taken by these two
programs and compare them.
CO2 Develop algorithms for sorting, searching, insertion and matching. (K2)
7.
7. CO-PO-PSO MAPPING:
CO/PO Mapping
Programme Outcomes(POs)
CO’s
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
CO1 3 1 1 3 1 2
CO2 3 3 2 2 3 2
CO3 3 3 3 3 2 3 2
CO4 3 2 2 2 3 2
CO5 3 3 3 2 1 3 2 2
8.
7.1 Relationship between the COs and Program Outcomes Pos with experiments:
Continuous evaluation R0 20
Total: 100
9.
S.
Rubrics - Parts Marks
No.
3 Implementation 5
5 Viva – voce 5
Total 20
8.2 R1 : Internal Lab Test
Maximum Marks: 20
Level of Achievement
Adequate Superficial
Lack of
Knowledge of Knowledge of
Demonstrates deep information
most algorithms
Knowledge of about most of
Algorithms and and
Identify appropriate algorithms and the algorithms
procedures procedures;
a tests, procedures and Procedure; Answer and procedures; CO1
answer the able to answer
algorithms/tools the related questions cannot even
related only some of
with explanations answer basic
questions, but the related
and elaboration related
fails to basic
questions
elaborate Questions
Define the
Define the Define the
problems with
Define the problems problems with problems with
full
with full full insufficient
justification
Implementation of justification and justification systems
b and CO2
problem Statement implement the and implement knowledge and
implement the
systems that works the system that implement the
system that
perfectly alright does not give system that does
does not give
100% results not give results
results
Adequate
insight but
missed some Little insight
important and analyzed No insight and
Excellent insight
points in only the most entirely missed
and well focused
results and basic points; the point of the
result and
discussion; Interpreted experiment;
Result Analysis and discussion; Data
c interpreted some data little or no CO3
Data Interpretation completely and
most data correctly but attempt to
appropriately
correctly but significant interpret data or
interpreted and no
some errors, over-interpreted
over- interpretation
conclusions omissions still data.
may be suspect present
or over-
interpreted
Maximum Marks: 20
Level of Achievement
Assessment Mapped
Excellent(4) Very Good (3) Fair (2) Poor (1) CO
Parameter
Coding is Coding is
complete, relevant complete and Coding is brief
(Achieved all the Executed and missing
Program and
requirements) and successfully significant No program
a Result CO4
Executed but failed in requirement of reported
Representation
successfully. result representing problem
discussion was result statement;
clear. discussion.
Organization of
Report and Lab report is
Lab report is well Report contains Poor
Timely well organized
organized as few errors and organization
b Submission but not CO5
directed and not submitted on and late
Result submitted on
submitted on time time submission
Representation time
and Discussion
Level of Achievement
Knowledge of
a theory of practical CO1
problems
Implementation
b CO2
logics
c Execution CO5
8.5 Internal lab Assessment format
GALGOTIAS UNIVERSITY
Department of Computer Science and Engineering
Assessment of Internal lab Test
Knowledge of
S. Enrollment Execution and
Name of the Student Algorithms and Total (20)
No Result (10)
No. Procedures (10)
1.
2.
3.
4.
5.
6.
7.
1.
2.
3.
4.
5.
6.
7.
8.
GALGOTIAS UNIVERSITY
10
11
12
13
14
15
16
9. EXPERIMENT WRITE UPS
Experiment No:1
Write a program to sort given set of numbers in ascending/descending order using Bub-
Title
ble sort and also search a number using binary search.
Pre-
Knowledge of Array and Control Statements in C programming language
requisite
Assumptions NA
Bubble Sort Algorithm:
Input to the algorithm is A: Array in which key is to searched, key: the key is to searched,
imin: lower index of array, imax: upper index of array.
int binary_search(int A[],int key,int imin,int imax)
Algorithm
{
// test if array is empty
if(imax < imin)
// set is empty, so return value showing not found
return KEY_NOT_FOUND;
else
{
// calculate midpoint to cut set in half
int imid = midpoint(imin, imax);
// three-way comparison
if(A[imid]> key)
// key is in lower subset
return binary_search(A, key, imin, imid-1);
elseif(A[imid]< key)
// key is in upper subset
return binary_search(A, key, imid+1, imax);
else
// key has been found
return imid;
}
}
Sample
execution
Post Lab
Assignment ----------------------
(If Any)
Experiment No:2
Write a program to sort given set of numbers in ascending/descending order
Title using Insertion sort and also search a number using linear search.
Pre- Knowledge of
requisite Array Data Structure
Assumptions —————————
Insertion Sort Algorithm:
Experiment No:3
Write a program to sort given set of numbers in ascending/descending order
Title using Quick sort and any other sorting algorithm. Also record the time taken by
these two programs and compare them.
Knowledge of
Pre-
Array Data Structure
requisite
Divide and Conquer Technique
Assumptions —————————————
Quick Sort Algorithm:
Sample
execution
Post Lab
Assignment(if -------------------------------------------------------
any)
Experiment No:4
Write a program to sort given set of numbers using Heap sort.
Title
Knowledge of
Pre-
Array Data Structure
requisite
Tree
Assumptions
The heap sort algorithm can be divided into two parts.
In the first step, a heap is built out of the data.
In the second step, a sorted array is created by repeatedly removing the largest element from
the heap, and inserting it into the array. The heap is reconstructed after each removal. Once
all objects have been removed from the heap, we have a sorted array. The direction of the
sorted elements can be varied by choosing a min-heap or max-heap in step one.
Input:- A is the array to be sorted
HEAPSORT(A)
1 BUILD-MAX-HEAP(A)
2 for i ← length[A] down to 2
3 do exchange A[1] ↔ A[i ]
4 heap-size[A] ← heap-size[A] − 1
5 MAX-HEAPIFY(A, 1)
MAX-HEAPIFY are an important subroutine for manipulating max-heaps. Its inputs are an
array A and an index i into the array. When MAX-HEAPIFY is called, it is assumed that the
binary trees rooted at LEFT(i ) and RIGHT(i ) are max-heaps, but that A[i ] may be smaller than
its children, thus violating the max-heap property. The function of MAX-HEAPIFY is to let the
Algorithm value at A[i ] “float down” in the maxheap
So that the subtree rooted at index i become a max-heap.
MAX-HEAPIFY(A, i )
1 l ← LEFT(i )
2 r ← RIGHT(i )
3 if l ≤ heap-size[A] and A[l] > A[i ]
4 then largest ←l
5 else largest ←i
6 if r ≤ heap-size[A] and A[r] > A[largest]
7 then largest ←r
8 if largest _= i
9 then exchange A[i ] ↔ A[largest]
10 MAX-HEAPIFY(A, largest)
BUILD-MAX-HEAP(A)
1 heap-size[A] ← length[A]
2 for i ← _length[A]/2down to 1
3 do MAX-HEAPIFY(A, i )
Sample
Execution
Post Lab
Assignment -------------------------------------------------------
(if any)
Experiment No:5
Knowledge of
Pre-
Array Data Structure
requisite
Divide and Conquer Technique
Assumptions
Merge Sort Algorithm:
MERGE-SORT(A, p, r)
1 if p < r
2 then q ← _(p + r)/2_
3 MERGE-SORT(A, p, q)
4 MERGE-SORT(A, q + 1, r)
5 MERGE(A, p, q, r)
MERGE(A, p, q, r)
1 n1 ← q − p + 1
2 n2 ←r − q
Algorithm 3 create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]
4 for i ← 1 to n1
5 do L[i ] ← A[p + i − 1]
6 for j ← 1 to n2
7 do R[ j ]← A[q + j ]
8 L[n1 + 1]←infinity
9 R[n2 + 1]←infinity
10 i ← 1
11 j ← 1
12 for k ← p to r
13 do if L[i ] ≤ R[ j ]
14 then A[k] ← L[i ]
15 i ← i + 1
16 else A[k] ← R[ j ]
17 j ← j + 1
Sample
execution
Post Lab
Assignment ----------------------
(If Any)
Experiment No:6
Knowledge of
Pre-requisite
Array Data Structure
Sample Output
Post Lab
Assignment(if ------------------------------------------
any)
Experiment No:7
Knowledge of
Pre-requisite
Array concept
Assumptions
Begin
Read two arrays A and B of size nxn
MMult(A, B, n)
1. If n = 1 Output A × B
2. Else
3. Compute A11, B11, . . ., A22, B22 % by computing m = n/2
4. X1 ← MMult(A11, B11, n/2)
5. X2 ← MMult(A12, B21, n/2)
6. X3 ← MMult(A11, B12, n/2)
Algorithm/ 7. X4 ← MMult(A12, B22, n/2)
Experiment 8. X5 ← MMult(A21, B11, n/2)
Specifications 9. X6 ← MMult(A22, B21, n/2)
10. X7 ← MMult(A21, B12, n/2)
11. X8 ← MMult(A22, B22, n/2)
12. C 11 ← X1 + X2
13. C 12 ← X3 + X4
14. C 21 ← X5 + X6
15. C 22 ← X7 + X8
16. Output C
17. End If
End
Sample
Input/Out put
Post Lab
Assignment(if ---------------------------------------------
any)
Experiment No:8
Knowledge of
Pre-requisite Array Data Structure
Greedy Programming
Assumptions
Knapsack Problem:
Two main kinds of Knapsack Problems:
1. 0-1 Knapsack:
▪ N items (can be the same or different)
▪ Have only one of each
▪ Must leave or take (i.e. 0-1) each item (e.g. ingots of gold)
▪ DP works, greedy does not
2. Fractional Knapsack:
▪ N items (can be the same or different)
▪ Can take fractional part of each item (eg bags of gold dust)
▪ Greedy works and DP algorithms work
Fractional Knapsack: Greedy Solution
Algorithm:
Experimental o Assume knapsack holds weight W and items have value vi and
specifications weight wi
o Rank items by value/weight ratio: vi / wi
▪ Thus: vi / wi ≥ vj / wj, for all i ≤ j
o Consider items in order of decreasing ratio
o Take as much of each item as possible
Example: Knapsack Capacity W = 30 and
Item A B C D
Value 50 140 60 60
Size 5 20 10 12
Ratio 10 7 6 5
Solution:
o All of A, all of B, and ((30-25)/10) of C (and none of D)
o Size: 5 + 20 + 10*(5/10) = 30
o Value: 50 + 140 + 60*(5/10) = 190 + 30 = 220
Post Lab
Assignment(if -------------------------------------------------------
any)
Experiment No:9
Knowledge of
Pre-requisite Array Data Structure
Dynamic Programming
Assumptions
Knapsack Problem:
Two main kinds of Knapsack Problems:
1. 0-1 Knapsack:
▪ N items (can be the same or different)
▪ Have only one of each
▪ Must leave or take (i.e. 0-1) each item (e.g. ingots of gold)
▪ DP works, greedy does not
2. Fractional Knapsack:
▪ N items (can be the same or different)
▪ Can take fractional part of each item (eg bags of gold dust)
▪ Greedy works and DP algorithms work
0-1 Knapsack: Dynamic Solution
Here is a dynamic programming algorithm to solve the 0-1 Knapsack problem:
Experiment
Input: S, a set of n items as described earlier, W the total weight of the knapsack. (As-
Specifications
sume that the weights and values are stored in separate arrays named w and v, respec-
tively.)
int w, k;
for (w=0; w <= W; w++)
B[w] = 0
for (k=0; k<n; k++) {
for (w = W; w>= w[k]; w--) {
if (B[w – w[k]] + v[k]> B[w])
B[w] = B[w – w[k]] + v[k]
}
}
Sample
input/output
Experiment No:10
Knowledge of
Pre-requisite Array Data Structure
Backtracking
Assumptions
n-Queen Problem :
The n-queens problem consists in placing n non-attacking queens on an n-by-n
chess board. A queen can attack another queen vertically, horizontally, or diag-
onally. E.g. placing a queen on a central square of the board blocks the row and
column where it is placed, as well as the two diagonals (rising and falling) at
whose intersection the queen was placed.
The algorithm to solve this problem uses backtracking, but we will unroll the
recursion. The basic idea is to place queens column by column, starting at the
left. New queens must not be attacked by the ones to the left that have already
been placed on the board. We place another queen in the next column if a con-
sistent position is found. All rows in the current column are checked. We have
Experiment found a solution if we placed a queen in the rightmost column.
Specifications Following is a solution for 4 Queen problem.
Enter the no. of queens:- 4
. . . Q
Q . . .
Sample Input/ . . Q .
Output
Q . . .
. . . Q
. Q . .
Experiment No:11
Objective 1.To study and Implement String Matching using Rabin-Karp algorithm
Knowledge of
Pre- String Data Structure
requisite
The Rabin–Karp algorithm seeks to speed up the testing of equality of the pattern to the sub-
strings in the text by using a hash function. A hash function is a function which converts every
string into a numeric value, called its hash value; for example, we might have hash ("hello") =5.
Rabin–Karp exploits the fact that if two strings are equal, their hash values are also equal. Thus,
it would seem all we have to do is compute the hash value of the substring we're searching for,
and then look for a substring with the same hash value.
However, there are two problems with this. First, because there are so many different strings,
to keep the hash values small we have to assign some strings the same number. This means that
if the hash values match, the strings might not match; we have to verify that they do, which can
take a long time for long substrings. Luckily, a good hash function promises us that on most
reasonable inputs, this won't happen too often, which keeps the average search time good.
The algorithm is as shown:
Sl.
Experiment beyond the syllabus CO PO & PSO
No
Implement Closest Pair problem using Divide and Conquer PO1, PO4, PO7,
2. CO4
technique. (K5) PSO1
3.
4.
5.
11.MINI PROJECTS
1. Median finding, Order Statistics and Quick Sort: Median Finding : Implement the following order
statistics algorithms : a. Median of Median with groups of 3,5 and 7. b. Randomized median finding
algorithm. Make your code generic enough so that it can answer order statistics for any k. For eg k =
n/2 gives the median and k=n gives the max.
BST and Heap : Huffman coding and decoding Huffman Encoding is one of the simplest
algorithms to compress data. Even though it is very old and simple , it is still widely used (eg : in
few stages of JPEG, MPEG etc). In this project you will implement huffman encoding and
decoding.
3. Red-Black Trees : Shared Memory de-duplication Red black trees are one of the most important
data structures used extensively in the Linux Kernel. For eg, recent kernels use Completely Fair
Scheduler for process scheduling which depends on Red Black tree.
4. Minimum Spanning Tree : Solving TSP for Metric Graphs using MST Heuristic Given an
arbitrary metric graph, construct its Minimum spanning tree using Kruskal's algorithm. You can
assume adjacency matrix representation of graphs.
5. Network Flow : Task allocation using Bipartite Graph In this project, you are given two sets : set
of employees and tasks. For each task , you are also given the list of employees who can
complete the task. Model this scenario as a bipartite graph and allocate work in such a way that
the job is completed as soon as possible. You must also utilize your workforce as much as
possible. Solve this problem using Network flow .
12. INDIRECT ASSESSMENT OF COs
Galgotias Univer-
sity, Greater Noida
Course End Survey #
B. Tech. – Sem :
2018-19
Name of Sub- DAA Lab
ject
Develop
algorithms
Analyse the running time of
CO Statement: sorting, searchi
algorithm. (K3)
insertion a
matching. (K2)
Enroll/Admn
S.No.
No.
Students name Section
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1
14 1
15 1