Cs3401_algorithm Lab Record
Cs3401_algorithm Lab Record
REG.No. ………………………………
LABORATORY RECORD
Course Code: CS3401
by………..…………..……………………………………………of………………..
KCG College of Technology aspires to become a globally recognized centre of excellence for
science, technology & engineering education, committed to quality teaching, learning, and
research while ensuring for every student a unique educational experience which will promote
leadership, job creation, social commitment and service to nation-building
Nurture the virtue of service and an ethical value system in the young minds
PSO
No.
Use compiler tool, CASE tool, graphic tool, app development tools, network simulator,
PSO 2 security and analysis tools, cloud and grid tool kits, database management tools, web
development frameworks for providing appropriate solutions.
PSO 3 Demonstrate professional & ethical behavior while providing IT based solutions.
PEO
Description of PEO
No.
Have successful career as software professional (or) entrepreneur (or) researcher in compute
PEO1
science and relevant disciplines.
Analyze, design, develop, test and deploy appropriate solutions for real world computing
PEO2
problems.
PEO3 Apply software engineering principles at process, project and product levels.
After the completion of this course, the student will be able to:
CO Blooms
Description of the Course Outcome
No level
CO 2 K3
Analyze the efficiency of algorithms using various frameworks
Make use of algorithm design techniques like divide and conquer, dynamic
CO 4 K3
programming, and greedy techniques to solve problems.
CO 5 K3
Apply State Space Tree Analysis to Problem-Solving.
CO 6 K3
Solve problems using approximation algorithms and randomized algorithms
ANNA UNIVERSITY
REGULATIONS 2021
PRACTICAL EXERCISES
Implement Linear Search. Determine the time required to search for an element. Repeat the
experiment for different values of n, the number of elements in the list to be searched and
plot a graph of the time taken versus n.
Implement recursive Binary Search. Determine the time required to search an element.
Repeat the experiment for different values of n, the number of elements in the list to be
searched and plot a graph of the time taken versus n.
Given a text txt [0...n-1] and a pattern pat [0...m-1], write a function search (char pat [ ],
char txt [ ]) that prints all occurrences of pat [ ] in txt [ ]. You may assume that n > m.
Develop a program to implement Heap sort with graph.
Find the minimum cost spanning tree of a given undirected graph using Prim’s algorithm.
Implement Floyd’s algorithm for the All-Pairs- Shortest-Paths problem.
Compute the transitive closure of a given directed graph using Warshall's algorithm.
Develop a program to find out the maximum and minimum numbers in a given list of n.
Implement Merge sort and Quick sort methods to sort an array of elements and determine
the time required to sort.
Implement N Queens problem using Backtracking.
Implement any scheme to find the optimal solution for the Traveling Salesperson problem
and then solve the same problem instance using any approximation algorithm and determine
the error in the approximation.
Software Tools
Turbo C
CS3401 ALGORITHMS LABORATORY
Index
Expt
Date Name of the Experiment Page No. Marks Signature
No.
2 Pattern Searching
7 Prim’s algorithm.
8 Floyd’s algorithm
Compute the transitive closure of a given directed
9 graph using Warshall's algorithm.
Implement Merge sort and Quick sort methods to sort
10 an array of elements
AIM:
To implement Linear search and Binary search and determine the time required to
search for an element
ALGORITHM:
Binary Search
Linear Search
Set i to 0.
If Li = T, the search terminates successfully; return i.
Increase i by 1.
If i < n, go to step 2. Otherwise, the search terminates unsuccessfully.
PROGRAM
OUTPUT
Result
Evaluation by Faculty Members:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS
3. If we have two arrays, one sorted and the other unsorted, which one would you
recommend for implementing a linear search?
4. In what ways does multi-threading help improve the performance of a linear search?
Ex No: 2 PATTERN SEARCHING
Date:
AIM:
To write a C program for a given text and pattern and write a search function
that prints all occurrences of pattern in text.
ALGORITHM:
Algorithm-NAVE_STRING_MATCHING (T, P)
for i←0 to n-m do
if P[1......m] == T[i+1.....i+m] then
print "Match Found"
end
end
PROGRAM:
OUTPUT:
RESULT:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS:
1. How pattern matching different from substring searching?
AIM:
To Implement Heap sort methods and determine the time required to sort the elements..
ALGORITHM:
The Heap sort algorithm to arrange a list of elements in ascending order is performed using
following steps...
Step 1 - Construct a Binary Tree with given list of Elements.
Step 2 - Transform the Binary Tree into Min Heap.
Step 3 - Delete the root element from Min Heap using Heapify method.
Step 4 - Put the deleted element into the Sorted list.
Step 5 - Repeat the same until Min Heap becomes empty.
Step 6 - Display the sorted lis
PROGRAM:
Output
RESULT:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS
3. Is it possible to sort an array using Heapsort without building a heap? If yes, then how?
4. Why isn’t a binary tree used for implementing heaps instead of arrays?
Ex No: 4 BREADTH FIRST SEARCH
Date:
AIM:
ALGORITHM
Step 2: Select a starting node (visiting a node) and insert it into the Queue.
Step 3: Provided that the Queue is not empty, extract the node from the Queue and
insert its child nodes (exploring a node) into the Queue.
PROGRAM
OUTPUT
RESULT:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS:
Date:
AIM:
ALGORITHM
Step 4: If there are undiscovered neighbour nodes, switch to that node and repeat the steps
(recursively or using a chunk data structure).
Step 5: When there are no undiscovered neighbour nodes, go back and return to the previous
node.
Step 6: Repeat the steps until all nodes have been visited.
PROGRAM
OUTPUT
RESULT
Evaluation by Faculty Members:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS
3.If the depth of a tree is 3 levels, then what is the size of the Tree ?
AIM
To form a given vertex in a weighted connected graph and to find shortest paths to other vertices using Dijkstra’s
algorithm.
ALGORITHM
Create a shortest path tree set that keeps track of vertices included in shortest path tree,
i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.
- Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE.
- Assign distance value as 0 for the source vertex so that it is picked first.
- While shortest path tree set doesn’t include all vertices
a) Pick a vertex u which is not there in shortest path tree set and has minimum distance value.
b) Include u to shortest path tree set
c) Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertice
For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance
value of v, then update the distance value of v.
PROGRAM
OUTPUT
RESULT
Faculty Signature
REVIEW QUESTIONS
AIM:
To find the minimum spanning tree of given undirected graph using prims's algorithm.
ALGORITHM
Step 1: start the program
Step 2: Initialize the algorithm by choosing the
source vertex.
Step 3:Find the minimum weight edge connected to
the source node and another node and add
it to the tree.
Step 4:Keep repeating this process until we find minimum spanning tree. To write a shell
program to
find the sum of n numbers.
PROGRAM
OUTPUT
RESULT
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks…………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS
AIM:
Step 1: Initialize the shortest paths between any 2 vertices with Infinity.
Step 2: Find all pair shortest paths that use 0 intermediate vertices, then find the shortest paths
that use 1 intermediate vertex and so on.. until using all N vertices as intermediate nodes.
Step 3: Minimize the shortest paths between any 2 pairs in the previous operation.
Step 4: For any 2 vertices (i,j) , one should actually minimize the distances between this pair
using the first K nodes, so the shortest path will be: min(dist[i][k]+dist[k][j],dist[i][j]).
dist[i][k] represents the shortest path that only uses the first K vertices, dist[k][j] represents the
shortest path between the pair k,j. As the shortest path will be a concatenation of the shortest
path from i to k, then from k to j.
PROGRAM:
OUTPUT
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS:
2.Which algorithms are used to find the shortest path from a source node to all other nodes in a
weighted graph?
4.What is the best time complexity we can achieve to precompute all-pairs shortest paths in a
weighted graph?
Ex No: 9 TRANSITIVE CLOSURE OF A GIVEN DIRECTED GRAPH USING
WARSHALL'S ALGORITHM
Date:
AIM:
ALGORITHM:
path exists between two vertices i, j,
• there is an edge from i to j; or
• there is a path from i to j going through vertex 1; or
• there is a path from i to j going through vertex 1 and/or 2; or
• there is a path from i to j going through vertex 1, 2, and/or 3; or
• there is a path from i to j going through any of the other vertices 3 3 3
Recurrence relating elements R(k) to elements of
R(k-1) is: R(k) [i,j] = R(k-1)[i,j] or (R(k-1)[i,k] and R(k-1)[k,j])
It implies the following rules for generating R(k) from R(k-1):
PROGRAM:
OUTPUT:
RESULT:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS:
1. How does the Floyds algorithm work for a negative edge graph?
Aim
To sort an array of N numbers using Quick sort.
Algorithm
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Select an pivot element x from Ai
5. Divide the array into 3 sequences: elements < x, x, elements > x
6. Recursively quick sort both sets (Ai < x and Ai > x)
7. Stop
PROGRAM
OUTPUT:
RESULT
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS
1. Is it possible to Quick Sort suitable for use on large datasets? Why or why not?
AIM
ALGORITHM
Step 1 - Place the queen row-wise, starting from the left-most cell.
Step 2 - If all queens are placed then return true and print the solution matrix.
Condition 1 - Check if the queen can be placed safely in this column then mark the
current cell [Row, Column] in the solution matrix as 1 and try to check the rest of the problem
recursively by placing the queen here leads to a solution or not.
Condition 2 - If placing the queen [Row, Column] can lead to the solution return true and
print the solution for each queen's position.
Condition 3 - If placing the queen cannot lead to the solution then unmark this [row,
column] in the solution matrix as 0, BACKTRACK, and go back to condition 1 to try other rows.
Step 4 - If all the rows have been tried and nothing worked, return false to trigger backtracking.
PROGRAM
OUTPUT:
RESULT
Evaluation by Faculty Members:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
Review Questions
1. How would you use a backtracking algorithm to solve a constraint-satisfaction
problem?
AIM:
To Implement any scheme to find the optimal solution for the Traveling Salesperson
problem and then solve the same problem instance using any approximation algorithm and
determine the error in the approximation.
ALGORITHM:
Step1: start the program.
o First of them is a list that can hold the indices of the cities in terms of the input
matrix of distances between cities
o And the Second one is the array which is our result
Step3: Perform traversal on the given adjacency matrix tsp[][] for all the city and if the cost of
reaching any city from the current city is less than the current cost the update the cost.
Step4: Generate the minimum path cycle using the above step and return their minimum cost.
PROGRAM
OUTPUT
RESULT:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS
AIM:
ALGORITHM:
10. Exit
PROGRAM:
OUTPUT
RESULT:
Evaluation by Faculty Members:
Particulars Marks
Program/Execution (7)
Output &Results(3)
Viva (10)
Total (20)
Faculty Remarks……………………………………………………………………...
Faculty Signature
REVIEW QUESTIONS: