DAA - Lab Manual
DAA - Lab Manual
LTP 0 0 2
Course Type Core
Semester Offered BCA 3rd Sem
Faculty Details:
Name Dr. Vikas Chaudhary
Designation Professor
School School of Computer Applications and Technology
___________________
Vision and Mission
SCHOOL OF COMPUTER APPLICATIONS AND TECHNOLOGY
Vision
To be known globally for value-based education, research, creativity and innovation
Mission
Vision
To be recognized globally as a premier School of Computer Applications and Technology imparting
quality and value based education engaged in multi-disciplinary and collaborative research.
Mission
The mission of the department is to:
M2: Establishing state-of-the-art facilities and adopt education 4.0 practices to analyze, develop, test
and deploy sustainable ethical IT solutions by involving multiple stakeholders.
CO1 Describe basic techniques for designing algorithms, including the techniques of
recursion, divide-and-conquer, and greedy.
CO2
Analyze algorithmic complexity for efficient problem-solving.
CO3
Develop problem-solving skills through algorithmic approaches.
CO4
Apply classical sorting, searching, optimization and graph algorithms.
PO2: Analyze and synthesis computing systems through quantitative and qualitative techniques.
PO3: Design and develop computer programs in the areas related to algorithm, web design, networking
and AI.
PO4: Inculcating essential skills as demanded by Indian and Global Software industries through latest
tools of learning. This also includes team-building skills, audio- visual presentations and personality
development programs.
PO5: To develop inter-twining competence in the field of Digital Marketing and Commerce, Computing
Skill and Computational tools.
PO6: To Develop practical skills to provide solutions to industry, society and business.
PO7: Understand environmental issues and lead a life with concerns for environment.
PO8: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
computing science practice.
PO9: To make graduates understand cross cultural, societal, professional, legal and ethical issues
prevailing in industry.
PO11: To apply standard software engineering practices and strategies in software project development
using open source programming environment to deliver a quality of product for business success
PO12 Recognize the need for lifelong learning for continuous enhancement and up gradation of
technological changes in society.
PROGRAM EDUCATIONAL OBJECTIVES
PEO1: be engaged with leading Global Software Services companies handling projects in
contemporary technologies.
PEO2: serve in technical or managerial roles at Government firms, Corporates and contributing to the
society as successful entrepreneurs through startup.
PSO1: Have the ability to work with emerging technologies in computing requisite to Industry 4.0.
PSO2: Demonstrate application development skills learned through technical training and projects tosolve
real world problems
Hardware Requirements
No specific requirements. Any computer Hardware capable of running DOS can be
used for this course.
LIST OF EXPERIMENTS
Title Write a program to sort given set of numbers in ascending/descending order using
Bubble sort and also search a number using binary search.
Pre Knowledge of
requisite • Array Data Structure
Algorithm 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)
{
// 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)
Title Write a program to sort given set of numbers in ascending/descending order using
Insertion sort and also search a number using linear search.
Pre-requi Knowledge of
site • Array Data Structure
Title Write a program to sort given set of numbers in ascending/descending order using
Quick sort and any other sorting algorithm. Also record the time taken by these two
programs and compare them.
Pre-requi Knowledge of
site • Array Data Structure
• Divide and Conquer Technique
Sample
Output
Experiment No:4
Title Write a program to sort given set of numbers using Heap sort.
Pre-req Knowledge of
uisite • Array Data Structure
• Tree
Algori The heap sort algorithm can be divided into two parts.
thm 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 re moval. 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 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
Output
Experiment No:5
Pre-requi Knowledge of
site • Array Data Structure
• Divide and Conquer Technique
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
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 Out
put
Experiment No:6
Pre-requi Knowledge of
site • Array Data Structure
Sample Out
put
Experiment No:7
Pre-requi Knowledge of
site • Dynamic Programming
Algorithm Matrix-chain-multiplication Problem:-
The way we parenthesize a chain of matrices can have a dramatic impact on the
cost of evaluating the product. Consider first the cost of multiplying two matrices.
The stand ard algorithm is given by the following pseudocode. The attributes rows
and columns are the numbers of rows and columns in a matrix.
MATRIX-MULTIPLY(A, B)
1 if columns[A] _= rows[B]
2 then error “incompatible dimensions”
3 else for i ← 1 to rows[A]
4 do for j ← 1 to columns[B]
5 do C[i, j ]← 0
6 for k ← 1 to columns[A]
7 do C[i, j ] ← C[i, j ] + A[i, k] · B[k, j ]
8 return C
We can multiply two matrices A and B only if they are compatible: the number of
columns of A must equal the number of rows of B. If A is a p ×q matrix and B is a
q ×r matrix, the resulting matrix C is a p ×r matrix. The time to compute C is
dominated by the number of scalar multiplications in line 7, which is pqr.
MATRIX-CHAIN-ORDER(p)
1 n ← length[p] − 1
2 for i ← 1 to n
3 do m[i, i ] ← 0
4 for l ← 2 to n \\ l is the chain length.
5 do for i ← 1 to n − l + 1
6 do j ←i + l − 1
7 m[i, j ]←infinity
8 for k ←i to j − 1
9 do q ← m[i, k] + m[k + 1, j ] + pi−1pk pj
10 if q < m[i, j ]
11 then m[i, j ]← q
12 s[i, j ] ← k
13 return m and s
The algorithm first computes m [i, i]← 0 for i = 1, 2. . . n (the minimum costs for
chains of length 1) in lines 2–3. During the first execution of the loop in lines 4–12.
The second time through the loop, it computes m [i, i +2] for i = 1, 2. . . n−2 (the
minimum costs for chains of length l = 3), and so forth. At each step, the m [i, j]
cost computed in lines 9–12 depends only on table entries m [i, k] and m [k + 1, j ]
already computed.
Sample Out
put
Experiment No:8
Pre Knowledge of
requi • Array Data Structure
site • Greedy Programming
Pre-requi Knowledge of
site • Array Data Structure
• Dynamic Programming
Input: S, a set of n items as described earlier, W the total weight of the knapsack.
(Assume that the weights and values are stored in separate arrays named w and v,
re spectively.)
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 Out
put
Experiment No:10
Pre-requi Knowledge of
site • Tree Data Structure
• Graph
DIJKSTRA(G,w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 S ← NULL
3 Q ← V[G]
4 while Q != NULL
5 do u ← EXTRACT-MIN(Q)
6 S ← S UNION {u}
7 for each vertex v € Adj[u]
8 do RELAX(u, v,w)
INITIALIZE-SINGLE-SOURCE(G, s)
1 for each vertex v €V[G]
2 do d[v]←∞
3 π[v]← NIL
4 d[s] ← 0
RELAX(u, v,w)
1 if d[v] > d[u] + w(u, v)
2 then d[v] ← d[u] + w(u, v)
3 π[v]← u
Sample Out
999 is representing infinity. It means no paths exist.
put 0 in the input cost matrix denotes no direct paths exist.
Experiment No:11
Pre-requi Knowledge of
site • Tree Data Structure
• Graph
INITIALIZE-SINGLE-SOURCE(G, s)
1 for each vertex v € V[G]
2 do d[v]←∞
3 π[v]← NIL
4 d[s] ← 0
RELAX(u, v, w)
1 if d[v] > d[u] + w(u, v)
2 then d[v] ← d[u] + w(u, v)
3 π[v]← u
Experiment No:12
Pre-requisite Knowledge of
• Array Data Structure
• Backtracking
...Q
Q...
..Q.
Q...
...Q
.Q..
Experiment No:13
Pre-requi Knowledge of
site • String
Algorithm/ The simplest and least efficient way to see where one string occurs inside another is to
Theory check each place it could be, one by one, to see if it's there. So first we see if there's a copy
of the needle in the first character of the haystack; if not, we look to see if there's a copy of
the needle starting at the second character of the haystack; if not, we look starting at the
third character, and so forth. In the normal case, we only have to look at one or two
characters for each wrong position to see that it is a wrong position, so in the average case,
this takes O(n + m) steps, where n is the length of the haystack and m is the length of the
needle; but in the worst case, searching for a string like "aaaab" in a string like
"aaaaaaaaab", it takes O(nm)
Sample Out
put
Experiment No:14
Pre-requisite Knowledge of
• String Data Structure
Algo The Rabin–Karp algorithm seeks to speed up the testing of equality of the pattern
to the substrings in the text by using a hash function. A hash function is a
rithm/Theory 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 sub string 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 num ber. 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 of ten, which keeps the average search time good.
The algorithm is as shown: