CIL47 CYL47 AlgorithmLab Manual
CIL47 CYL47 AlgorithmLab Manual
Algorithms Laboratory
COUSE CODE:CIL47/ CYL47 COURSE COORDINATOR: Dr. SINI ANNA ALEX
CREDITS : 0:0:1 SEM: 4
I.A. Marks: 50 SEE Exam Hours: 03 SEE Exam Marks: 50
Develop the following program using C/C++ Programming Language
Sl. List of Programs CO PO
No.
1. Design and Implement an algorithm for computing Greatest Common divisor (GCD) of 2 numbers, 1 1,2
say m and n, using the following approaches.
i. Middle school procedure
ii. Euclid’s Algorithm by Division
iii. Euclid’s Algorithm by Subtraction
Compute the Time Complexity for each and Display the GCD (m, n) where m>n.
2. Design and Implement algorithm for searching techniques Linear Search and Binary Search. Com- 1 1,2
pute the Time Complexity and Display.
3. Consider the problem: You have a row of binary digits arranged randomly. Arrange them in such an 1,2 1,2
order that all 0’s precede all 1’s or vice-versa. The only constraint in arranging them is that you are ,3
allowed to interchange the positions of binary digits if they are not similar.
Implement an algorithm for Bubble sort and Insertion sort for binary value as input like
1 1 0 1 0 1 0 0 0.
Compute the Time Complexity and Display the output as 0 0 0 0 0 1 1 1 1.
4. Design and Implement a Merge Sort algorithm to sort a given set of elements and determine the 1,2 1,2
time required to sort the elements. Repeat the experiment for different values of n, the number of ,3
elements in the list to be sorted. The elements can be read from the user or can be generated using
the random number generator.
5. Design and Implement Quick sort algorithm to sort a given list of unsorted elements in ascending 1,2 1,2
order and determine the time required to sort the elements. Repeat the experiment for different ,3
values of n, the number of elements in the 1st to be sorted and plot a graph of the time taken versus
n. The elements can be read from the user or can be generated using the random number genera-
tor.
6. Design and Implement an algorithm to Print all the nodes reachable from a given starting node in a 1,2 1,2
graph using Breadth First Search (BFS). Use Queue for constructing BFS spanning tree and Display ,3
the BFS traversal order.
7. Design and Implement an algorithm to check whether a given graph is connected or not using Depth 1,2 1,2
First Search (DFS). Use stack for constructing DFS spanning tree traversal and Display the DFS tra- ,3
versal order.
8. Design and Implement Prim’s algorithm using Greedy Technique to display the minimum cost 1,2 1,2
achieved considering the scenario given. ,3
A car driver is given a set of locations to be covered with their distances by a company. Now the
company gives a privilege for the car driver to start at any arbitrary location. The constraint is entire
driving route chosen by the driver should be minimum.
9. Design and Implement an algorithm to find Minimum Cost Spanning Tree of a given undirected 1,2 1,2
graph using Prim’s algorithm. ,3
10. Design and Implement an algorithm to Find Minimum Cost Spanning Tree of a given undirected 1,2 1,2
graph using Kruskal’s algorithm ,3
11. Design and Implement an algorithm to find the single source shortest path to other vertices using 2,3 1,2
Dijkstra’s algorithm from a given vertex in a weighted connected graph. ,3
12. Design and Implement the Branch and Bound Technique: Find the optimal solution for the Travel- 3 2,3
ling salesman problem.
PROGRAM:
#include <iostream>
using namespace std;
#include<time.h>
long int middle_school(long int m,long int n){
long int i,gcd;
clock_t start,end;
start=clock();
for(i=1; i <= m && i <= n; ++i)
{
// Checks if i is factor of both integers
if(m%i==0 && n%i==0)
gcd = i;
}
end=clock();
cout<<endl<<"Time taken:"<<(end-start)/CLOCKS_PER_SEC<<" sec , GCD ";
return gcd;
}
while(m != n)
{
if(m > n)
{
m = m - n;
}
else
{
n = n - m;
}
}
end=clock();
cout<<"Time taken :"<<(end-start)/CLOCKS_PER_SEC<<" sec , GCD ";
return m;
} /*End of the function sub*/
main()
{
long int x,y;
cout<<"\t\t ANALYSIS OF THE TWO ALGORITHMS"<<endl<<endl;
cout<<"GCD : "<<endl;
cout<<"enter two numbers:";
cin>>x>>y;
cout<<endl<<endl<<"GCD-Middle School : "<<middle_school(x,y);
cout<<endl<<endl<<"GCD-Euclid : "<<euclid(x,y);
cout<<endl<<endl<<"------------------------------------------------";
cout<<endl<<endl<<"GCD- subtraction : "<<sub(x,y);
}
2. Design and Implement algorithm for searching techniques Linear Search and Bi-
nary Search. Compute the Time Complexity and Display.
PROGRAM:
#include<iostream>
using namespace std;
#include<time.h>
int Linear_search()
{
int arr[10], n, i, num, index;
clock_t start,end;
cout<<"Linear Search: Enter Number of elements: ";
cin>>n;
cout<<"Enter the Elements";
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"\n Enter a Number to Search: ";
cin>>num;
start=clock();
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
index = i;
break;
}
}
cout<<"\nFound at Index No."<<index;
end=clock();
int main()
{
Binary_search();
Linear_search();
}
3. Consider the problem: You have a row of binary digits arranged randomly. Ar-
range them in such an order that all 0’s precede all 1’s or vice-versa. The only
constraint in arranging them is that you are allowed to interchange the positions
of binary digits if they are not similar.
Implement an algorithm for Bubble sort and Insertion sort for binary value
as input like 1 1 0 1 0 1 0 0 0.
Compute the Time Complexity and Display the output as 0 0 0 0 0 1 1 1 1.
PROGRAM:
#include<iostream>
using namespace std;
#include<time.h>
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
4. Design and Implement a Merge Sort algorithm to sort a given set of elements and
determine the time required to sort the elements. Repeat the experiment for dif-
ferent values of n, the number of elements in the list to be sorted. The elements
can be read from the user or can be generated using the random number genera-
tor.
/* C++ Program to implement Merge Sorting using Divide and Conquer Algo-
rithm */
PROGRAM:
#include <iostream>
using namespace std;
#include<time.h>
int main()
{
int n, i;
clock_t start,end;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
start=clock();
MergeSort(arr, 0, n-1);
end=clock();
cout<<"Time taken for Merge sort: "<<(double)(end-
start)/CLOCKS_PER_SEC<<" sec"<< endl;
return 0;
}
5. Design and Implement Quick sort algorithm to sort a given list of unsorted ele-
ments in ascending order and determine the time required to sort the elements.
Repeat the experiment for different values of n, the number of elements in the
1st to be sorted and plot a graph of the time taken versus n. The elements can be
read from the user or can be generated using the random number generator.
/* C++ Program to implement Quick Sorting using Divide and Conquer Algorithm
*/
PROGRAM:
#include<iostream>
using namespace std;
#include<time.h>
int main()
{
int n ;
clock_t start,end;
#include <stdio.h>
#include <stdlib.h>
int a[20][20],vis[20],stack[20], top=-1;
void dfs(int s,int n);
void push(int item);
int pop();
void main()
{
int n,i,s,j;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
for(i=1;i<=n;i++)
vis[i]=0;
printf("\n D.F.S");
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);
dfs(s,n);
}
7. Design and Implement an algorithm to Print all the nodes reachable from a
given starting node in a graph using Breadth First Search (BFS). Use Queue for
constructing BFS spanning tree and Display the BFS traversal order.
BFS program
#include<stdio.h>
int a[20][20],q[20],visited[20],n,f=-1,r=-1;
void bfs(int v)
{
int i;
for (i=0;i<n;i++) // check all the vertices in the graph
{
if(a[v][i] != 0 && visited[i] == 0) // adjacent to v and not visited
{
r=r+1;
q[r]=i; // insert them into queue
visited[i]=1; // mark the vertex visited
printf("%d ",i);
}
}
f=f+1; // remove the vertex at front of the queue
if(f<=r) // as long as there are elements in the queue
bfs(q[f]); // peform bfs again on the vertex at front of the queue
8. Design and Implement Prim’s algorithm using Greedy technique to display the
minimum cost achieved considering the scenario given.
A car driver is given a set of locations to be covered with their distances by
a company. Now the company gives a privilege for the car driver to start at any
arbitrary location. The constraint is entire driving route chosen by the driver
should be minimum.
PROGRAM 8 & 9:
#include<stdio.h>
#include<stdlib.h>
int G[MAX][MAX],spanning[MAX][MAX],n;
int prims();
int main()
{
int i,j,total_cost;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
total_cost=prims();
int prims()
{
int cost[MAX][MAX];
int u,v,min_distance,distance[MAX],from[MAX];
int visited[MAX],no_of_edges,i,min_cost,j;
//create cost[][] matrix,spanning[][]
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(G[i][j]==0)
cost[i][j]=infinity;
else
cost[i][j]=G[i][j];
spanning[i][j]=0;
}
//initialise visited[],distance[] and from[]
distance[0]=0;
visited[0]=1;
Output:
Enter no. of vertices:5
Enter the adjacency matrix:
01200
10204
22030
00302
04020
spanning tree matrix:
0 1 2 0 0
1 0 0 0 0
2 0 0 3 0
0 0 3 0 2
0 0 0 2 0
10. Design and Implement an algorithm to Find Minimum Cost Spanning Tree of a
given undirected graph using Kruskal’s algorithm
PROGRAM:
#include<iostream>
#include<string.h>
using namespace std;
class Graph
{
char vertices[10][10];
int cost[10][10],no;
public:
Graph();
void creat_graph();
void display();
int Position(char[]);
void kruskal_algo();
};
/* Initialzing adj matrix with 999 */
/* 999 denotes infinite distance */
Graph::Graph()
{
no=0;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
cost[i][j]=999;
}
}
/* Taking inputs for creating graph */
void Graph::creat_graph()
{
char ans,Start[10],End[10];
int wt,i,j;
cout<<"Enter the number of vertices: ";
cin>>no;
cout<<"\nEnter the vertices: ";
for(i=0;i<no;i++)
cin>>vertices[i];
do
{
cout<<"\nEnter Start and End vertex of the edge: ";
cin>>Start>>End;
cout<<"Enter weight: ";
cin>>wt;
Input/Output:
Enter the number of vertices: 5
Enter the vertices: 0 1 2 3 4
Cost matrix:
999 2 999 6 999
2 999 3 8 5
999 3 999 999 7
6 8 999 999 9
999 5 7 9 999
KRUSKAL’s using C
#include <stdio.h>
#include <stdlib.h>
int i, j, k, a, b, u, v, n, ne = 1;
int min, mincost = 0, cost[9][9], parent[9];
int find(int);
int uni(int, int);
void main()
{
printf("Kruskal's algorithm in C\n");
printf("========================\n");
u = find(u);
v = find(v);
if (uni(u, v))
int find(int i)
{
while (parent[i])
i = parent[i];
return i;
}
return 0;
}
Output
Kruskal's algorithm in C
========================
Enter the no. of vertices:
5
Enter the cost adjacency matrix:
01201
10301
23065
00600
11500
The edges of Minimum Cost Spanning Tree are
1 edge (1,2) =1
2 edge (1,5) =1
3 edge (1,3) =2
4 edge (3,4) =6
Minimum cost = 10
11. Design and Implement an algorithm to find the single source shortest path to
other vertices using Dijkstra’s algorithm from a given vertex in a weighted con-
nected graph.
PROGRAM:
#include<stdio.h>
#define INFINITY 9999
#define MAX 10
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
int cost[MAX][MAX],distance[MAX],pred[MAX];
12. Design and Implement the Branch and Bound Technique: Find the optimal solu-
tion for the Travelling salesman problem.
PROGRAM:
/*Branch and Bound Algorithm for Travelling Sales Person*/
#include<stdio.h>
#include<conio.h>
int a[10][10],visited[10],n,cost=0;
void get()
{
int i,j;
printf(“Enter No. of Cities: “);
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i<n;i++)
{
if((a[c][i]!=0)&&(visited[i]==0))
if(a[c][i]<min)
{
min=a[i][0]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
void put()
{
printf(“\n\nMinimum cost:”);
printf(“%d”,cost);
}
Cost Matrix:
99 10 15 20 99 8
5 99 9 10 8 99
6 13 99 12 99 5
8 8 9 99 6 99
99 10 99 6 99 99
10 99 5 99 99 99