0% found this document useful (0 votes)
28 views

2022 Scheme Ada LAB Manual-1

Uploaded by

rayangurkar01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

2022 Scheme Ada LAB Manual-1

Uploaded by

rayangurkar01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Design and implement C/C++ Program to sort a given set of n integer elements using
Selection Sort method and compute its time complexity. Run the program for varied values
of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n. The
elements can be read from a file or can be generated using the random number generator.

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int a[50000],temp,i,j,min,n;

void selectionsort()
{
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
void main()
{

clock_t start,end;
printf("Enter the number of Elements\n");
scanf("%d",&n);
printf("Reading elements using random number generator\n");
for(i=0;i<n;i++)
a[i]=rand();
start=clock();
selectionsort();
end=clock();
printf("The Sorted Element are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf(" \nTime complexity=%f ",(double)(end-start)/(CLOCKS_PER_SEC));

OUTPUT:

Enter the number of Elements


5
Reading elements using random number generator
The Sorted Element are
846930886 1681692777 1714636915 1804289383 1957747793
Time complexity=0.000001

Note: Run the program for varied values of n> 5000, and record the time taken to sort. Plot a
graph of the time taken versus n
2. Design and implement C/C++ Program to sort a given set of n integer elements using Merge
Sort method and compute its time complexity. Run the program for varied values of n> 5000,
and record the time taken to sort. Plot a graph of the time taken versus n. The elements can
be read from a file or can be generated using the random number generator.

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int c[50000], a[50000];
void merge(int low, int mid, int high)
{
int i,k,j;
k=low;
i=low;
j=mid+1;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
c[k++]=a[i++];
else
c[k++]=a[j++];
}
while(i<=mid)
c[k++]=a[i++];
while(j<=high)
c[k++]=a[j++];
for(i=low;i<=k-1;i++)
a[i]=c[i];
}
void msort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
msort(low,mid);
msort(mid+1,high);
merge(low,mid,high);
}
}
void main()
{
int i,n;
clock_t start,end;
printf("Enter the number of Elements\n");
scanf("%d",&n);
printf("Reading elements using random number generator\n");
for(i=0;i<n;i++)
a[i]=rand();
start=clock();
msort(0,n-1);
end=clock();
printf("The Sorted Element are\n");
for(i=0;i<n;i++)
printf("%d\t",c[i]);
printf(" \nTime complexity=%f",(double)(end-start)/CLOCKS_PER_SEC);

}
OUTPUT:
Enter the number of Elements
5
Reading elements using random number generator
The Sorted Element are
846930886 1681692777 1714636915 1804289383 1957747793
Time complexity=0.000003
Note: Run the program for varied values of n> 5000, and record the time taken to sort. Plot a
graph of the time taken versus n
3. Design and implement C/C++ Program to sort a given set of n integer elements using Quick
Sort method and compute its time complexity. Run the program for varied values of n> 5000
and record the time taken to sort. Plot a graph of the time taken versus n. The elements can
be read from a file or can be generated using the random number generator.

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int a[50000],n,i,j,temp;
int part(int low, int high)
{
int i,j,key;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i<high&&key>a[i])
i++;
while(a[j]>key)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}

void qs(int low, int high)


{
int j;
if(low<=high)
{
j=part(low, high);
qs(low,j-1);
qs(j+1,high);
}
}

void main()
{

clock_t start,end;

printf("Enter the number of Elements\n");


scanf("%d",&n);

printf("Reading elements using random number generator\n");


for(i=0;i<n;i++)
a[i]=rand();
start=clock();
qs(0,n-1);
end=clock();
printf("The Sorted Element are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf(" \nTime complexity=%f ",(double)(end-start)/(CLOCKS_PER_SEC));

}
OUTPUT:
Enter the number of Elements
5
Reading elements using random number generator
The Sorted Element are
846930886 1681692777 1714636915 1804289383 1957747793
Time complexity=0.000001

Note: Run the program for varied values of n> 5000, and record the time taken to sort. Plot a
graph of the time taken versus n
4. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
given digraph.

#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
int adj[MAX_VERTICES][MAX_VERTICES];
int in_degree[MAX_VERTICES];
int visited[MAX_VERTICES];
int top_order[MAX_VERTICES];
int n;

void add_edge(int u, int v)


{
adj[u][v] = 1;
in_degree[v]++;
}

void topological_sort()
{
int i, j, k = 0;

for (i = 0; i < n; i++)


{
for (j = 0; j < n; j++)
{
if (in_degree[j] == 0 && !visited[j])
{
top_order[k++] = j;
visited[j] = 1;
for (int l = 0; l < n; l++)
{
if (adj[j][l]) {
in_degree[l]--;
}
}
}
}
}
printf("Topological order of vertices:\n");
for (i = 0; i < n; i++)
{
printf("%d ", top_order[i]);
}
printf("\n");
}
int main()
{
int u, v, e;

printf("Enter the number of vertices: ");


scanf("%d", &n);

printf("Enter the number of edges: ");


scanf("%d", &e);

for (int i = 0; i < e; i++) {


printf("Enter edge %d (u, v): ", i + 1);
scanf("%d %d", &u, &v);
add_edge(u, v);
}

topological_sort();

return 0;
}

OUTPUT:

Enter the number of vertices: 5


Enter the number of edges: 5
Enter edge 1 (u, v): 0 2
Enter edge 2 (u, v): 1 2
Enter edge 3 (u, v): 2 3
Enter edge 4 (u, v): 2 4
Enter edge 5 (u, v): 3 4
Topological order of vertices:
01234
Algorithm:

The algorithm is based on a direct implementation of the decrease-(by one)-and- conquer technique:

1. Repeatedly, identify in a remaining digraph a source, which is a vertex with no incoming edges,
and delete it along with all the edges outgoing from it. (If there are several sources, break the tie
arbitrarily. If there are none, stop because the problem cannot be solved.)

2. The order in which the vertices are deleted yields a solution to the topological sorting problem.
5. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected
undirected graph using Kruskal's algorithm.

Program:
#include<stdio.h>
int parent[10], min, ne=1, mincost=0, cost[10][10];
int i,j,a,b,u,v,n;
void main()
{
printf("Enter the number of vertices\n”);
scanf("%d",&n);
printf(“Enter graph data\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i,b=v=j;
}
while(parent[u]) u=parent[u];
while(parent[v]) v=parent[v];

if(u!=v)
{
printf("\n%d\tEdge\t(%d,%d)=%d",ne++,a,b,min);
mincost+=min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMINCOST=%d\n",mincost);
}
OUTPUT:
Enter the number of vertices
6
Enter graph data
020240
203204
030107
221030
400300
047000

1 Edge (3,4)=1
2 Edge (1,2)=2
3 Edge (1,4)=2
4 Edge (4,5)=3
5 Edge (2,6)=4
MINCOST=12

Note: Draw Graph and its Minimum Spanning Tree


6. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.
Program:
#include<stdio.h>
int a,b,u,v,i,n,j,ne=1;
int vis[10],min,mincost=0,cost[10][10];
void main()
{
printf("Enter the no of vertices\n");
scanf("%d",&n);
printf("Enter adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
for(i=2;i<=n;i++)
vis[i]=0;
printf("The edges of spanning tree are\n");
vis[1]=1;
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(vis[i]==0)
continue;
else
{
min=cost[i][j];
a=u=i,b=v=j;
}
if(vis[u]==0||vis[v]==0)
{
printf("%d\t edge\t (%d, %d)=%d\n",ne++,a,b,min);
mincost+=min;
vis[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\t MINCOST=%d\n",mincost);
}
OUTPUT:
Enter the no of vertices
6
Enter adjacency matrix
020240
203204
030107
221030
400300
047000
The edges of spanning tree are
1 edge (1, 2)=2
2 edge (1, 4)=2
3 edge (4, 3)=1
4 edge (4, 5)=3
5 edge (2, 6)=4

MINCOST=12

Algorithm:

Note: Draw Graph and its Minimum Spanning Tree


Example:

You might also like