0% found this document useful (0 votes)
372 views

Design and Analysis of Algorithm Lab (BSCS2351) Lab Manual

This document provides information about the lab manual for the Design and Analysis of Algorithms course at Galgotias University's Department of Computer Science and Engineering. It includes details about the course such as credits, duration, prerequisites, and contact information. It also outlines the course's vision and mission statements, program outcomes, list of experiments to be conducted in the lab, and mapping of course outcomes to program outcomes and program specific outcomes.

Uploaded by

vint
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
372 views

Design and Analysis of Algorithm Lab (BSCS2351) Lab Manual

This document provides information about the lab manual for the Design and Analysis of Algorithms course at Galgotias University's Department of Computer Science and Engineering. It includes details about the course such as credits, duration, prerequisites, and contact information. It also outlines the course's vision and mission statements, program outcomes, list of experiments to be conducted in the lab, and mapping of course outcomes to program outcomes and program specific outcomes.

Uploaded by

vint
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

GALGOTIAS UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Lab Manual

SUBJECT Design and Analysis of Algorithm PROGRAMME B. Tech.


Lab

SUBJECT CODE BSCS2351 SEMESTER V

DURATION OF
CREDITS 1 24 Weeks
SEMESTER

PREREQUISITE SESSION
Data Structure Using C/C++ 2 Hrs per Week
SUBJECTS DURATION

Prepared by:

Dr. S.P.S. Chauhan APPROVED 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)

1. VISION AND MISSION OF GALGOTIAS UNIVERSITY

VISION

➢ To be known globally for value-based education, research, creativity and innovation

MISSION

➢ Establish state-of-the-art facilities for world class education and research.

➢ Collaborate with industry and society to align the curriculum.

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

➢ Conduct multi-disciplinary research for developing innovative 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

Engineering knowledge: Apply the knowledge of mathematics, science, engineering


PO1 fundamentals, and an engineering specialization to the solution of complex engineering problems.
Problem analysis: Identify, formulate, review research literature, and analyze complex
PO2 engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.

Design/development of solutions: Design solutions for complex engineering problems and


design system components or processes that meet the specified needs with appropriate
PO3 consideration for the public health and safety, and the cultural, societal, and environmental
considerations.

Conduct investigations of complex problems: Use research-based knowledge and research


PO4 methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions

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.

Environment and sustainability: Understand the impact of the professional engineering


PO7 solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development

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.

Communication: Communicate effectively on complex engineering activities with the


engineering community and with society at large, such as, being able to comprehend and write
PO10 effective reports and design documentation, make effective presentations, and give and receive
clear instructions.

Project management and finance: Demonstrate knowledge and understanding of the


PO11 engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.

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.

Programme specifics Outcome (PSO)

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.

5. EXPERIMENTAL SETUP DETAILS FOR THE COURSE


Software Requirements
➢ Turbo C 2.0/ Turbo C++3.0+

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.

1. Write a program to sort given set of numbers in ascending/descending order using

3 Quick sort and any other sorting algorithm. Also record the time taken by these two
programs and compare them.

4 1. Write a program to sort given set of numbers using Heap sort.

5 1. Write a program to sort given set of numbers Merge Sort.

6 1. Write a program to sort given set of numbers Counting Sort.

7 Write a program to implement Strassen's Matrix Multiplication by Divide and Conquer.

8 Write a program to implement Knapsack using Greedy technique.

9 Write a program to implement Knapsack using Dynamic programming.


10 Write a program to implement Dijkstra’s Algorithm.

11 Write a program to implement n-Queen Problem using backtracking.

12 Write a program to implement String Matching using Rabin-Karp algorithm.

Experiments beyond curriculum:

1 Implement Coin Change problem using Dynamic Programming.

2 Implement Closest Pair problem using Divide and Conquer technique.

6. COURSE OUTCOMES (COs):

Course outcomes (COs)

CO1 Analyse the running time of algorithm. (K3)

CO2 Develop algorithms for sorting, searching, insertion and matching. (K2)

CO3 Identify and apply the concept of computational intractability. (K3)

CO4 Apply the algorithms and design techniques to solve problems(K5)

CO5 Analyze the complexities of various problems in different domains. (K6)

7.

7. CO-PO-PSO MAPPING:

CO/PO Mapping

(S/M/W indicates strength of correlation) S-Strong, M-Medium, L-Low

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:

S. Mapped Program Mapping of Rubrics for


Course Outcomes
No. Outcomes mapping course outcomes

Analyse the running time of Exp No. 1,2,3,4,5


PO1, PO2, PO7, PO9,
1 algorithm. (K3) Mid-term internal evaluation,
PO12, PSO2
End-term external evaluation

Develop algorithms for sorting, Exp No. 6, 7


2 searching, insertion and PO1, PO2, PSO3, PO5,
Mid-term internal evaluation,
matching. (K2) PO9, PO12, PSO1
End-term external evaluation

Identify and apply the concept of Exp No. 8


3 PO1, PO2, PSO3, PO5,
computational intractability. (K3) End- term internal evaluation,
PO8, PO9, PO12, PSO2
End-term external evaluation

Exp No. 9, 10, 11


4 Apply the algorithms and design PO1, PO2, PSO3, PO5,
End- term internal evaluation,
techniques to solve problems(K5) PO9, PO12, PSO1
End-term external evaluation

Analyze the complexities of Exp No. 11, 12


5 various problems in PO1, PO2, PSO3, PO5,
End- term internal evaluation,
different domains. (K6) PO7, PO9, PO12, PSO2
End-term external evaluation
8. LAB EVALUATION SCHEME

Component of evaluation Internal/external Rubric for CO Marks

Continuous evaluation R0 20

Pre-final lab test Internal R1 20

R3(Theory, tools, Result


Internal viva 10 (2+4+4)
interpretation )

ETE Lab test R1 20

Lab Report External R2 20

R3(Theory, tools, Result


External Viva 10(2+4+4)
interpretation)

Total: 100

9.

8.1 R0 : Rubrics for continuous evaluation

S.
Rubrics - Parts Marks
No.

1 Able to define the problem statement 2

2 Able to convert the problem statement into program 3

3 Implementation 5

4 Compile and Interpret the results 5

5 Viva – voce 5

Total 20
8.2 R1 : Internal Lab Test
Maximum Marks: 20

Level of Achievement

Assessment Parameter Map


Excellent (4) Very Good (3) Fair (2) Poor (1) ped
CO

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

8.3 R2: Lab Report

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

8.4 R3: Viva

Level of Achievement

Very Good Mapped


Assessment Parameter Excellent (4) Fair (2) Poor (1)
(3) CO

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

Subject Code : Subject Name :


Session : Class :
Date : Max. Marks :

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.

8.6 Continuous Assessment Format

Internal Lab Assessment (End Semester)


S. Name of the Continuous Internal Total Marks
Enrol. No. assessment
No. Student Assessment (50) (in words)
(30) Test (20)

1.

2.

3.

4.

5.

6.

7.

8.
GALGOTIAS UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LABEVALUATION SHEET 2019-20 (ODD SEM)

FACULTY NAME PROGRAMME COURSE CODE/COURSE NAME

Dr. S.P.S. Chauhan B. Tech BSCS2351-DAA-Lab

S. EXP1 EXP2 EXP3 EXP4 EXP5 EXP


NAME OF STUDENT ADMISSION/ ENROLL NO.
NO. 24-Jul 31-Jul 7-Aug 14-Aug 28-Aug 4-Se

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.

Objective Implementation of Bubble Sort and Binary Search

Pre-
Knowledge of Array and Control Statements in C programming language
requisite

Assumptions NA
Bubble Sort Algorithm:

Inputtothefunctionis array A: The array to be sorted


procedure bubbleSort( A : list of sort able items )
repeat
swapped = false
for i = 1 to length(A) - 1 inclusive do:
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure

Binary Search 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.

1.To study and Implement Insertion Sort Algorithm


Objective
2.To study and Implement Linear Search Algorithm

Pre- Knowledge of
requisite  Array Data Structure

Assumptions —————————
Insertion Sort Algorithm:

Input to the function is array A: The array to be sorted


for i ← 1 to length(A)
x ← A[i]
j←i
while j > 0 and A[j-1] > x
A[j] ← A[j-1]
j←j-1
A[j] ← x

Linear Search Algorithm:


Algorithm

Input to the algorithm is A: Array in which key is to searched, x: the key is to


searched
LinearSearch(A, x)
Set i to n.
Repeat this loop:
If i< 0, then exit the loop.
If A[i] = x, then exit the loop.
Set i to i − 1.
Return i.
If returned value is –ve then it means item not found otherwise search successful
at location i.
Sample
Execution
Post Lab
Assignment ----------------------
(If Any)

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.

Objective 1.To study and Implement Quick SortAlgorithm

Knowledge of
Pre-
 Array Data Structure
requisite
 Divide and Conquer Technique

Assumptions —————————————
Quick Sort Algorithm:

Input to the function is array A: The array to be sorted


The following procedure implements quick sort.
QUICKSORT(A, p, r)
1 if p < r
2 then q ← PARTITION(A, p, r)
3 QUICKSORT(A, p, q − 1)
4 QUICKSORT(A, q + 1, r)

To sort an entire array A, the initial call is QUICKSORT (A, 1, length[A]).


Algorithm Partitioning the array
The key to the algorithm is the PARTITION procedure, which rearranges the subarray
A [p . . . r] in place.
PARTITION(A, p, r)
1 x ← A[r]
2i←p−1
3 for j ← p to r − 1
4 do if A[ j ] ≤ x
5 then i ←i + 1
6 exchange A[i ] ↔ A[ j ]
7 exchange A[i + 1] ↔ A[r]
8 return i + 1

Sample
execution

Post Lab
Assignment(if -------------------------------------------------------
any)
Experiment No:4
Write a program to sort given set of numbers using Heap sort.
Title

To study and Implement Heap Sort Algorithm.


Objective

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

Title Write a program to sort given set of numbers Merge Sort.

Objective 1.To study and Implement Merge SortAlgorithm

Knowledge of
Pre-
 Array Data Structure
requisite
 Divide and Conquer Technique

Assumptions
Merge Sort Algorithm:

Inputtothefunctionis array A: The array to be sorted

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

Write a program to sort given set of numbers Counting Sort.


Title

Objective 1.To study and Implement Counting SortAlgorithm

Knowledge of
Pre-requisite
 Array Data Structure

Inputtothefunctionis array A: The array to be sorted


In the code for counting sort, we assume that the input is an array A [1 . . . n], and Thus
length [A] = n. We require two other arrays: the array B [1. . . n] holds the sorted output,
and the array C[0 . . k] provides temporary working storage.
COUNTING-SORT(A, B, k)
1 for i ← 0 to k
Study 2 do C[i ] ← 0
3 for j ← 1 to length[A]
4 do C[A[ j ]] ← C[A[ j ]] + 1
5 C[i] now contains the number of elements equal to i .
6 for i ← 1 to k
7 do C[i ] ← C[i ] + C[i − 1]
8 C[i] now contains the number of elements less than or equal to i .
9 for j ← length[A] down to 1
10 do B[C[A[ j ]]] ← A[ j ]
11 C[A[ j ]] ← C[A[ j ]] − 1

Sample Output

Post Lab
Assignment(if ------------------------------------------
any)
Experiment No:7

Write a program to implement Strassen's Matrix Multiplication by Divide and Con-


Title quer

Objective 1. To study and Implement Matrix multiplication problem.

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

Write a program to implement Knapsack using Greedy technique.


Title

Objective 1. To study and Implement Knapsack Algorithm.

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

Write a program to implement Knapsack using Dynamic programming.


Title

Objective 1. To study and Implement Knapsack Algorithm.

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

Output: The maximal value of items in a valid knapsack.

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

Write a program to implement n-Queen Problem using backtracking.


Title

Objective 1.To study and Implement n-Queen Problem

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

The solution is:-


. Q . .

. . . Q

Q . . .

Sample Input/ . . Q .
Output

The solution is:-


. . Q .

Q . . .

. . . Q

. Q . .
Experiment No:11

Title Write a program to implement String Matching using Rabin-Karp algorithm.

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:

function RabinKarp(string s[1..n], string sub[1..m])


hsub := hash(sub[1..m]); hs := hash(s[1..m])
for i from 1 to n-m+1
if hs = hsub
if s[i..i+m-1] = sub
return i
hs := hash(s[i+1..i+m])
return not found
10. EXPERIMENTS BEYOND CURRICULUM

Sl.
Experiment beyond the syllabus CO PO & PSO
No

Implement Coin Change problem using Dynamic


1. CO2 PO1, PO3, PSO1
Programming.(K5)

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

Name of Faculty: Dr. S.P.S. Chauhan


Please tick ()
at appropriate
place for each
CO :
CO1 CO2

Develop
algorithms
Analyse the running time of
CO Statement: sorting, searchi
algorithm. (K3)
insertion a
matching. (K2)

SIGN V.Good Good Poor V.Good Good Po

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

You might also like