Ada Lab Manual
Ada Lab Manual
& Management
Opp. Art of living, Udayapura, Kanakapura road, Bengaluru- 560082
(An Autonomous college under VTU, Belagavi & Approved by
AICTE, New Delhi)
Accredited by NBA, New Delhi
SL.
Lab Programs
No.
Department Mission
of artificial intelligence."
Department Mission
# include <stdio.h>
int a,b,i,j,u,v,n;
int visited[10],edge=1, min, mincost=0, cost[10][10];
void kruskal(int n, int cost[10][10])
{
printf("The edge of the spanning tree are \n");
while(edge<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(visited[u])
u=visited[u];
while(visited[v])
v=visited[v];
if(u!=v)
{
edge++;
printf("\n Edge (%d,%d)=%d", a,b,min);
mincost=mincost+min;
visited[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n The minimum cost =%d", mincost);
}
int main( )
{
printf("Enter the number of vertices:\n");
scanf("%d", &n);
printf("Enter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d", &cost[i][j]);
kruskal(n,cost);
return 0;
}
OUTPUT:
Enter the number of vertices:
4
Enter the cost adjacency matrix:
0 10 999 40 1
10 10
2
10 0 20 999
999 20 0 30 20
40 999 30 0 40
The edge of the spanning tree is 30
4 3
# include<stdio.h>
int n, a[10][10];
void min_spa_tree()
{
int i,j, u,v, min, sum,k;
int t[10][2],p[10],d[10],s[10],source;
min=9999;
source=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i][j]!=0 && a[i][j]<=min)
{
min=a[i][j];
source=i;
}
}
}
for(i=1;i<=n;i++)
{
d[i]=a[source][i];
s[i]=0;
p[i]=source;
}
s[source]=1;
sum=0;
k=1;
for(i=1;i<n;i++)
{
min=9999;
u=-1;
for(j=1;j<=n;j++)
{
if(s[j]==0)
{
if(d[j]<=min)
{
min=d[j];
u=j;
}
}
}
t[k][1]=u;
t[k][2]=p[u];
k++;
sum+=a[u][p[u]];
s[u]=1;
for(v=1;v<=n;v++)
{
if(s[v]==0 && a[u][v]<d[v])
{
d[v]=a[u][v];
p[v]=u;
}
}
}
if(sum>=9999)
printf("spanning tree does not exits\n");
else
{
printf("spanning tree exists and min spanning tree is \n");
for(i=1;i<n;i++)
{
printf("%d", t[i][1]);
printf(" ");
printf("%d", t[i][2]);
printf("\n");
}
printf("The cost of the spanning tree is %d", sum);
printf("\n");
}
}
int main( )
{
int i,j;
printf("Enter the no of nodes\n");
scanf("%d", &n);
printf("Enter the cost adjacency matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d", &a[i][j]);
}
}
min_spa_tree( );
return 0;
}
OUTPUT:
Enter the no of nodes
4
Enter the cost adjacency matrix 1 2 10
0 10 999 40
10 0 20 999
999 20 0 30
40 999 30 0 20 40
4 3
Spanning tree exists and min spanning tree is
12 30
32
43
The cost of the spanning tree is 70
3. a) Design and implement C Program to solve All-Pairs Shortest Paths
problem using Floyd's algorithm.
a) Floyd's algorithm:
#include <stdio.h>
#define INF 99999 // Infinity
int main() {
// Number of vertices in the graph
int V = 4;
b) Warshal’s algorithm:
#include<stdio.h>
int n,i,j;
int a[10][10];
int p[10][10];
int write_data()
{
printf("the path matrix is shown below\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d",p[i][j]);
printf(" ");
}
printf("\n");
}
return 0;
}
void path_matrix()
{
int i,j,k;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
p[i][j]=a[i][j];
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(p[i][k]==1&&p[k][j]==1)
p[i][j]=1;
}
}
}
}
int main()
{
printf("enter the no. of nodes\n");
scanf("%d",&n);
printf("enter the adjacency matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
path_matrix();
write_data();
return 0;
}
OUTPUT
#include<stdio.h>
Void dijstra(int n, int v, int cost[10][10], int d[])
{
int count,u,i,j,s[10],min;
for(i=1;i<=n;i++)
{
s[i]=0;
d[i]=cost[v][i];
}
s[v]=1;
d[v]=1;
count=2;
while(count<=n)
{
min=999;
for(j=1;j<=n;j++)
if(d[j]<min && s[j]==0)
{
min=d[j];
u=j;
}
s[u]=1;
count++;
for(j=1;j<=n;j++)
if((d[u]+cost[u][j]<d[j])&&s[j]==0)
d[j]=d[u]+cost[u][j];
}
}
intmain( )
{
int n,i,j,v,c[10][10],d[10];
printf("Enter the no of nodes");
scanf("%d", &n);
printf("Enter the cost adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d", &c[i][j]);
printf("Enter the source vertex");
scanf("%d", &v);
dijstra(n,v,c,d);
printf("The shortest path");
for(j=1;j<=n;j++)
if(j!=v)
printf("\n%d->%d=%d", v,j,d[j]);
return 0;
}
OUTPUT:
20
#include<stdio.h>
void find_indegree(int n,int a[10][10],int indegree[])
{
int i,j,sum;
for(j=1;j<=n;j++)
{
sum=0;
for(i=1;i<=n;i++)
sum=sum+a[i][j];
indegree[j]=sum;
}
}
void topological(int n,int a[10][10])
{
int i,k,u,v,top,t[10],indegree[10],s[10];
find_indegree(n,a,indegree);
top=-1;
k=1;
for(i=1;i<=n;i++)
if(indegree[i]==0)
s[++top]=i;
while(top!=-1)
{
u=s[top--];
t[k++]=u;
for(v=1;v<=n;v++)
{
if(a[u][v]==1)
{
indegree[v]--;
if(indegree[v]==0)
s[++top]=v;
}
}
}
printf("THE TOPOLOGICAL SEQUENCE IS \n");
for(i=1;i<=n;i++)
printf("%d\t",t[i]);
}
int main()
{
int i,j,n,a[10][10];
printf("ENTER THE NUMBER OF NODES\n");
scanf("%d",&n);
printf("ENTER THE ADJACENCY MATRIX\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
topological(n,a);
return 0;
}
OUTPUT:
1
ENTER THE NUMBER OF NODES
3
ENTER THE ADJACENCY MATRIX
3 2
011
000
010
THE TOPOLOGICAL SEQUENCE IS
1 3 2
6. Design and implement C Program to solve 0/1 Knapsack problem using
Dynamic Programming method.
#include<stdio.h>
#include<stdlib.h>
int w[10], p[10], v[10][10];
intMax(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int knapsack(int n,int c)
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=c;j++)
{
if(i==0||j==0)
v[i][j]=0;
elseif(j-w[i]>=0)
v[i][j]=Max(v[i-1][j],p[i]+v[i-1][j-w[i]]);
else
v[i][j]=v[i-1][j];
}
}
return v[n][c];
}
Void optimalsubset(int n,int c)
{
int i,j;
for(i=n,j=c;i>=1 && j>0;i--)
{
if(v[i][j]!=v[i-1][j])
{
printf("item %d\n",i);
j=j-w[i];
}
}
}
int main( )
{
int n,c,mp,i,j;
printf("enter the number of items \n");
scanf("%d",&n);
printf("enter the weights of each item\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("enter the profit of each item\n");
for(i=1;i<=n;i++)
scanf("%d",&profits[i]);
printf("enter the knapsack capacity\n");
scanf("%d",&c);
mp=knapsack(n,c);
printf("solution of the knapsack \n");
for(i=0;i<=n;i++)
{
for(j=0;j<=c;j++)
printf("%d\t",v[i][j]);
printf("\n");
}
printf("the maximal value is %d \n",mp);
printf("the items of optimal subset are\n");
optimalsubset(n,c);
return 0;
}
OUTPUT:
enter the number of items
4
0 0 0 0 0 0
0 0 12 12 12 12
0 10 12 22 22 22
0 10 12 22 30 32
0 10 15 25 30 37
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
exit(1);
}
srand(time(NULL)); // Seed for random number generation
for (int i = 0; i < n; i++) {
fprintf(file, "%d\n", rand());
}
fclose(file);
}
int main() {
FILE *timeFile = fopen("time_vs_n.csv", "w");
if (timeFile == NULL) {
printf("Error opening file.\n");
exit(1);
}
fprintf(timeFile, "n, time_taken_ms\n");
fclose(timeFile);
OUTPUT:
15
77
83
86
93
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define maxsize 3000
void quicksort(int a[],int l,int h)
{
int j;
if(l<h)
{
usleep(500000);
j=partition(a,l,h);
quicksort(a,l,j-1);
quicksort(a,j+1,h);
}
}
int partition(int a[],int l,int h)
{
int i,j,k,t;
k=a[l];
i=l+1;
j=h;
while(1)
{
while(l<h && k>=a[i])
i++;
while(k<a[j])
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
else
{
t=a[l];
a[l]=a[j];
a[j]=t;
return j;
}
}
}
int main()
{
int a[maxsize],i,n;
double runtime=0;
printf("Enter the size \n");
scanf("%d",&n);
srand(1);// generates random numbers
for(i=1;i<=n;i++)
a[i]=rand()%100;
time_t start=time(NULL);
quicksort(a,1,n);
time_t end=time(NULL);
runtime=difftime(end,start);
printf("The sorted array is");
for(i=1;i<=n;i++)
printf("\n%d\n", a[i]);
printf("Time taken %f\n",runtime);
return 0;
}
OUTPUT:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define maxsize 3000
if(i>mid)
for(p=j;p<=high;p++)
{
b[k]=a[p];
k=k+1;
}
else
for(p=i;p<=mid;p++)
{
b[k]=a[p];
k=k+1;
}
for(i=low;i<=high;i++)
a[i]=b[i];
}
#include<stdio.h>
#include<stdlib.h>
int x[10],n;
intdisplay()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
if(x[i]==j)
printf("\tQ");
else
printf("\t x");
printf("\n\n");
}
printf("\n\n");
return 0;
}
intplace(int k, int i)
{
int j;
for(j=1;j<=k-1;j++)
if((x[j]==i)||(abs(x[j]-i)==abs(j-k)))
return 0;
return 1;
}
intnqueen(int k)
{
int i;
for(i=1;i<=n;i++)
if(place(k,i))
{
x[k]=i;
if(k==n)
display();
nqueen(k+1);
}
}
int main()
{
printf("enter the no. of queens\n");
scanf("%d",&n);
if(n==1||n==2||n==3)
printf(" nqueen solution doesnot exist\n");
else
printf("solution for nqueen\n");
nqueen(1);
}
OUTPUT:
x Q x x
x x x Q
Q x x x
x x Q x
x x Q x
Q x x x
x x x Q
x Q x x