Gujarat Technological University
C. K. Pithawalla College of Engineering and Technology
Surat
Department of Computer Engineering
Lab Manual
Name of Subject : Analysis and Design of Algorithms
Subject Code : 2150703
Subject coordinator : Prof. Yogesh M Kapuriya
2 Lab Manual – Algorithms
Practical Assignment
Prob. No Problem Statement
1.1 Implementation and Time analysis of sorting algorithms : Bubble sort
1.2 Implementation and Time analysis of sorting algorithms : Selection sort
1.3 Implementation and Time analysis of sorting algorithms : Insertion sort
Implementation and Time analysis of factorial program using iterative and recursive
1.4
method.
2.1 Implementation and Time analysis of Linear search algorithm.
2.2 Implementation and Time analysis of Binary search algorithm.
2.3 Implementation and Time analysis of Merge sort
2.4 Implementation and Time analysis of Quick sort
2.5 Implementation of Max-Heap sort algorithm
3.1 Implementation of a knapsack problem using dynamic programming.
3.2 Implementation of chain matrix multiplication using dynamic programming.
3.3 Implementation of making a change problem using dynamic programming
3.4 Implement LCS problem.
4.1 Implementation of a Knapsack problem using greedy algorithm
4.2 Implement Prim’s algorithm
4.3 Implement Kruskal’s algorithm.
5.1 Implementation of Graph and Searching : Breadth First Search
5.2 Implementation of Graph and Searching : Depth First Search
Prof. Yogesh M Kapuriya 2|Page
3 Lab Manual – Algorithms
Solutions
1.1 Bubble sort
BubbleSort (A, n)
//A – array of numbers
//n – number of elements
for i = 1 to n-1
for j = 1 to n-1
if A[j] > A[j+1]
temp = A[j]
A[j] = A[j+1]
A[j+1] = temp
return A
1.2 Selection sort
SelectionSort (A, n)
//A – array of numbers
//n – number of elements
for i = 1 to n-1
for j = i+1 to n-1
if A[j] > A[i]
temp = A[j]
A[j] = A[i]
A[i] = temp
return A
1.3 Insertion sort
Insertion_Sort(A, n)
//A – array of numbers
//n – number of elements
for j=2 to n do
key = A[j]
i = j-1
While i>0 and A[i] > key
A[i+1] = A[i]
i = i-1
A[i+1] = key
return A
1.4 Factorial program using iterative and recursive method.
IterativeFactorial(n)
// n – nth factorial number
fact = 1
For I = 1 to n
fact = fact * i
Prof. Yogesh M Kapuriya 3|Page
4 Lab Manual – Algorithms
return fact
RecursiveFactorial(n)
// n – nth factorial number
if n <= 1
return n
else
return RecursiveFactorial(n-1) * n
2.1 Linear search
LinearSearch(A, n, key)
// A – Array of elements
// n – size of an array
// key – element to be searched
for i = 1 to n
if A[i] = key then
return i
return -1
2.2 Binary search
IterativeBinSearch(A, n, key)
// Given Array – A[1:n] in increasing order, n>0,
// If key is present in A it returns p such that A[p] = key.
low=1;
high=n;
While( low ≤ high)
{
mid = (low + high)/2 ;
If (key < A[mid]) then
high= mid -1;
Else if If (key > A[mid]) then
low = mid + 1;
Else
Return mid;
}
Return 0;
RecursiveBinSearch(A, low, high, key)
// Given Array – A[low:high] in increasing order, 1 ≤ low ≤ high,
// If key is present in A it returns p such that A[p] = key.
If (low = high ) // If Small(P)
{
If (key = A[low]) then
Return low;
Else
Return 0;
}
Else
Prof. Yogesh M Kapuriya 4|Page
5 Lab Manual – Algorithms
{
mid = (low + high )/2;
If ( key = A[mid]) then
Return mid;
Else if (key < A[mid]) then
RBinSerach(A, low, mid – 1, key);
Else
RBinSerach(A, mid + 1, high, key);
}
2.3 Merge sort
MergeSort(low, high)
// Global Array – A[low:high] to be sorted.
If (low ≤ high) then
{
// Divide P into two SP
mid = (low + high)/2 ;
// Solve SP
MergeSort(low, mid);
MergeSort(mid+1, high);
// Combine The Solutions
Merge(low, mid, high);
}
Merge(low, mid, high)
// A[low:high] is global array containing 2 sorted subsets in A[low:mid] and
A[mid+1:high]
// Combine these 2 sets into a 1 set residing in a A[low:high].
// B[] is an auxiliary global array
h = low;
i = low;
j = mid + 1;
while(( h ≤ mid) and (j ≤ high)) do
if (A[h] ≤ A[j]) then
B[i] = A[h] ;
h = h+1;
else
B[i] = A[j] ;
j = j+1;
end While
if (h>mid) then
for k = j to high do
B[i] = A[k];
i=i+1;
else
for k = h to mid do
B[i] = A[k];
i=i+1;
end if
for k = low to high do
A[k] = B[k];
Prof. Yogesh M Kapuriya 5|Page
6 Lab Manual – Algorithms
2.4 Quick sort
QuickSort(p, r)
// Sorts the elements A[p],…A[r] which resides in Global Array – A[1:n].
If (p < r) then
{
// Divide P into two SP
q = Partition(A, p, r+1)
// q is the position of the partitioning element
// Solve SP
QuickSort(p, q-1);
QuickSort(q+1, r);
}
Partition(A, l, r)
// Returns correct position of pivot
pivot = A[l]
i = l
j = r
While i < j
While A[i] < pivot && i <= r
i = i + 1
End While
While A[j] > pivot && j >= l
j = j – 1
End While
If (i < j ) then
Interchange(A, i, j)
End While
A[l] = A[j]
A[j] = pivot
Return j
Interchange(A, i, j)
// Swaps values of A[i] and A[j]
x = A[i]
A[i] = A[j]
A[j] = x
2.5 Max-Heap sort
Heapify (A, i)
l ← left [i]
r ← right [i]
if l ≤ heap-size [A] and A[l] > A[i]
then largest ← l
else largest ← i
if r ≤ heap-size [A] and A[i] > A[largest]
then largest ← r
if largest ≠ i
then exchange A[i] ↔ A[largest]
Heapify (A, largest)
Prof. Yogesh M Kapuriya 6|Page
7 Lab Manual – Algorithms
BuildHeap (A)
heap-size (A) ← length [A]
For i ← floor(length[A]/2) down to 1 do
Heapify (A, i)
Heapsort (A)
BuildHeap (A)
for i ← length (A) down to 2 do
exchange A[1] ↔ A[i]
heap-size [A] ← heap-size [A] - 1
Heapify (A, 1)
3.1 Knapsack problem using dynamic programming
KnapsackDP(n, v, w)
For j = 0 to W
V[0, j] = 0
For i = 1 to n
For j = 1 to W
if (wi > j)
V[i, j] = V[i-1, j]
else
V[i, j] = max {V[i-1,j], vi + V[i-1, j-wi}
Return V[n, W]
3.2 Chain matrix multiplication using dynamic programming
MatrixChainMultiplocation(p, n)
// p – represents chain of matrices such that matrix Ai has
// the dimension p[i-1] x p[i]
// n – length of p i.e. Total matrices – (n-1)
For i = 1 to n
M[i][i] = 0
For L = 2 to n
For i = 1 to n-L+1
j = i+L-1
m[i][j] = ∞
For k = i to j-1
cost = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]
if (cost < m[i][j])
m[i][j] = cost
s[i][j] = k
Return m[1][n-1]
3.3 Making a change problem using dynamic programming
MakeChange(SetofCoins n)
// Making change for N units using coinages from d[1…n]
d[1…n] = [1,4,6]
for i=1 to n
C[i, 0] = 0
for i=1 to n
for j=1 to N
Prof. Yogesh M Kapuriya 7|Page
8 Lab Manual – Algorithms
if i=1 and j<d[i] then
else if i=1 then
C[i, j] = 1 + C[1, j-d[1]]
else if j<d[i] then
C[i, j] = C[i-1, j]
else
C[i, j] = min {C[i-1, j] , 1 + C[1, j-d[1]] }
return c[n, N]
3.4 LCS problem
LongestCommonSequence(X, Y, m, n)
//X – sequence 1 of length m
//Y – sequence 2 of length n
For i = 0 to m
For j = 0 to n
if i=0 or j=0
L[i, j] = 0
else if X[i] = Y[j]
L[i, j] = 1 + L[i-1, j-1]
else
L[i, j] = max {L[i-1, j], L[i, j-1]}
Return L[m, n]
4.1 Knapsack problem using greedy algorithm
FractionalKanpsack(v,w,W,n)
// v[i] and w[i] represents Value and Weight of an Object i respectively
// W = Weight a knapsack can occupy
For i = 1 to n do
x[i] = 0
Weight = 0
For i = 1 to n do
ValueDensity[i] = v[i] / w[i]
T = Allocate_Memory(n)
IndirectSort(ValueDensity, T, n)
For i = 1 to n And Weight < W
If Weight + w[T[i]] ≤ W then
x[T[i]] = 1
Weight = Weight + w[T[i]]
Else
x[T[i]] = (W – Weight) / w[T[i]]
Weight = W
Return x
4.2 Prim’s algorithm
MST_Prim(G, w, r)
// G – Graph with vertices set V and edges set E
// w – weight function
// r – source vertex
for each u V[G]
Prof. Yogesh M Kapuriya 8|Page
9 Lab Manual – Algorithms
key[u] =
parent[u] = Ø
key[r] = 0
Q = InitQueue(V[G]) //Maintain set V as min-priority heap in queue
while Q ≠ Ø
u = ExtractMin(Q) // Extract Min
for each v Adj[u]
if v Q and w[u,v] < key[v]
parent[v] = u
key[v] = w[u,v] // Updates Queue
4.3 Kruskal’s algorithm
MST_Kruskal(G, w)
// G – Graph with vertices set V and edges set E
// w – weight function
A = Ø
for each vertex v V[G]
MakeSet(v)
sort the edges E into ascending order by weight w
for each edge (u, v) E, taken in ascending order by weight
if FindSet(u) ≠ FindSet(v)
A = A {(u, v)}
Union(u, v)
5.1 Breadth First Search
Breadth-First-Search (G, r)
// G – Graph of set V and E
// r – root vertex
for each node n in G
n.distance = INFINITY
n.parent = NIL
create empty queue Q
r.distance = 0
Q.enqueue(r)
while Q is not empty
current = Q.dequeue()
for each node n that is adjacent to current:
if n.distance == INFINITY
n.distance = current.distance + 1
n.parent = current
Q.enqueue(n)
5.2 Depth First Search
DFS-Iterative(G, r)
// G – Graph of set V and E
// r – root vertex
let S be a stack
Prof. Yogesh M Kapuriya 9|Page
10 Lab Manual – Algorithms
S.push(r)
while S is not empty
v = S.pop()
if v is not labeled as discovered:
label v as discovered
for all edges from v to w in G.adjacentEdges(v) do
S.push(w)
Prof. Yogesh M Kapuriya 10 | P a g e