Lab Manual (1)
Lab Manual (1)
LAB MANUAL
Bachelor of Technology
IV Semester
TIRUCHIRAPPALLI
1
List of Experiments
2
Lab 1: Simple Algorithm-Insertion sort
Aim : Write an algorithm for sorting the given elements using Insertion sort
Algorithm:
1. If it is the first element, it is already sorted. return 1;
2. Pick next element
3. Compare with all elements in the sorted sub-list
4. Shift all the elements in the sorted sub-list that is greater than the value to be sorted
5. Insert the value
6. Repeat until list is sorted
Linear Search:
Algorithm Linear Search ( Array Arr, Value a ) // Arr is the name of the array, and a is the
searched element.
Step 1: Start
Step 2: Intialize i = 0
5: Set i to i + 1
Quicksort:
Step 1: Select a pivot element
Step 2: Partition the array around the pivot element.
Move all the elements < pivot to the left of the pivot and move all elements >= pivot to the
Step 3: After Step 2, the pivot element is in its correct position Step 4: Apply the quicksort
recursively on the left partition Step 5: Apply the quicksort recursively on the right partition
Step 6: Stop the recursion when the base case is reached. The base case is an array of size
zero or one
Lab 5 : Multiplication
Aim : Write an algorithm to implement Multiplication
Algorithm:
Algorithm STRASSEN_METHOD (A, B, C, int n) if n == 1 then
C = C + (A) * (B)
else
STRASSEN_METHOD (A, B, C, n/4)
STRASSEN_METHOD (A, B + (n/4), C + (n/4), n/4)
STRASSEN_METHOD (A + 2 * (n/4), B, C + 2 * (n/4), n/4)
STRASSEN_METHOD (A + 2 * (n/4), B + (n/4), C + 3 * (n/4), n/4)
STRASSEN_METHOD (A + (n/4), B + 2 * (n/4), C, n/4)
STRASSEN_METHOD (A + (n/4), B + 3 * (n/4), C + (n/4), n/4) STRASSEN_METHOD
(A + 3 * (n/4), B + 2 * (n/4), C + 2 * (n/4), n/4) STRASSEN_METHOD (A + 3 * (n/4), B +
3 * (n/4), C + 3 * (n/4), n/4)
end
Lab 6: Finding Maximum and Minimum in an array, Convex Hull problem
Aim : Write an algorithm for finding Maximum and Minimum in an array, Convex
Hull problem
4
Algorithm:
Finding Maximum and Minimum in an array
Divide array by calculating mid index i.e. mid = l + (r l)/2
Recursively find the maximum and minimum of left part by calling the same
function i.e. leftMinMax[2] = minMax(X, l, mid)
Recursively find the maximum and minimum of right part by calling the same
function i.e. rightMinMax[2] = minMax(X, mid + 1, r)
Finally, get the overall maximum and minimum by comparing the min and max of
both halves.
Make a line joining these two points, say L. This line will divide the whole set into
two parts. Take both the parts one by one and proceed further.
For a part, find the point P with maximum distance from the line L. P forms a
triangle with the points min_x, max_x. It is clear that the points residing inside this
triangle can never be the part of convex hull.
The above step divides the problem into two sub-problems (solved recursively).
Now the line joining the points P and min_x and the line joining the points P and
max_x are new lines and the points residing outside the triangle is the set of points.
Repeat point no. 3 till there no point left with the line. Add the end points of this
point to the convex hull.
Lab 7: Huffman coding, knapsack and using greedy
Aim : Write an algorithm for Huffman Coding and Knapsack using Greedy Approach
5
Algorithm:
Huffman Coding
Input is an array of unique characters along with their frequency of occurrences and output
is Huffman Tree.
Create a leaf node for each unique character and build a min heap of all leaf nodes
(Min Heap is used as a priority queue. The value of frequency field is used to
compare two nodes in min heap. Initially, the least frequent character is at root)
Extract two nodes with the minimum frequency from the min heap.
Create a new internal node with a frequency equal to the sum of the two nodes
frequencies. Make the first extracted node as its left child and the other extracted
node as its right child. Add this node to the min heap.
Repeat steps#2 and #3 until the heap contains only one node. The remaining node is
the root node and the tree is complete.
Knapsack Problem
Sort items by the ratio value/weight for each item, in descending order.
Start with the highest ratio item. Put items into the bag until the next item on the list
cannot fit.
Try to fill any remaining capacity with the next item on the list that can fit.
Lab 8: Various tree traversals
Aim : Write an algorithm to perform various tree traversals
Algorithm:
Pre-order Traversal
Return the root node value.
6
Algorithm:
Let X=<x1,x2,x3....,xm> and Y=<y1,y2,y3 ,ym> be the sequences. To compute the length of
an element the following algorithm is used.
Construct an empty adjacency table with the size, n × m, where n = size of sequence
X and m = size of sequence Y. The rows in the table represent the elements in
sequence X and columns represent the elements in sequence Y.
The zeroeth rows and columns must be filled with zeroes. And the remaining values
are filled in based on different cases, by maintaining a counter value.
Case 1 If the counter encounters common element in both X and Y sequences,
increment the counter by 1.
Case 2 counter does not encounter common elements in X and Y sequences
at T[i, j], find the maximum value between T[i-1, j] and T[i, j-1] to fill it in T[i, j].
Once the table is filled, backtrack from the last value in the table. Backtracking here
is done by tracing the path where the counter incremented first.
The longest common subsequence obtained by noting the elements in the traced
path.
Lab10: N problem
Aim : Write an algorithm to solve N problem
Algorithm:
Start in the leftmost column
If all queens are placed return true
Try all rows in the current column. Do the following for every row.
If the queen can be placed safely in this row
Then mark this [row, column] as part of the solution and
recursively check if placing queen here leads to a solution.
If placing the queen in [row, column] leads to a solution then
return true.
If placing queen lead to a solution then unmark this [row,
column] then backtrack and try other rows.
If all rows have been tried and valid solution is not found return false to
trigger backtracking.
Lab11: Travelling salesman problem
7
Algorithm:
Travelling salesman problem
Input : G= {V, E} Output Gout = {V,E}
Start
Sort all the edges in the input graph G from the least distance to the largest distance.
The first edge selected is the edge with least distance, and one of the two vertices
(say A and B) being the origin node (say A).
Then among the adjacent edges of the node other than the origin node (B), find the
least cost edge and add it onto the output graph.
Continue the process with further nodes making sure there are no cycles in the
output graph and the path reaches back to the origin node A.
However, if the origin is mentioned in the given problem, then the solution must
always start from that node only. Let us look at some example problems to
understand this better.
End
Aim: Write an algorithm to perform breadth first search and death first search of a graph
8
Algorithm
Start
Initialize the enqueue the starting node into a queue and mark it as visited.
Dequeue a node from the queue and visit it (e.g., print its value).
Stop
End
Lab 13: Randomized quick sort
9
Algorithm:
Start
An array is divided into subarrays by selecting a pivot element.
Divide the array, the pivot element should be positioned in such a way that elements
less than pivot are kept on the left side and elements greater than pivot are on the
right side of the pivot
The left and right subarrays are also divided using the same approach. This process
continues until each subarray contains a single element.
At this point, elements are already sorted. Finally, elements are combined to form a
sorted array.
Stop
Lab14: String matching algorithms
Start
Get the input string and substring
Repeat
Compare the pattern with every substring of the text until a match is found.
Print output
End
10