ADA Programs
ADA Programs
#include<stdio.h>
int main()
{
int n,i,j,min,a,b,u,v,cost[20][20],parent[20];
int ne=1, mincost=0;
printf("enter the no of vertex: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d",cost[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
parent[i]=0;
}
parent[0]=1;
printf("\n The edges of spanning tree are\n");
while(ne<n)
{
min=999;
for(i=0;i<n;i++)
{
if(parent[i])
{
for(j=0;j<n;j++)
{
parent[b]=1;
printf("Edge %d\t (%d -> %d) = %d\n",ne++,a,b,min);
mincost += min;
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimum cost = %d\n", mincost);
return 0;
}
Program2
#include<stdio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0}, min, mincost=0,cost[10][10];
void main()
{
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j] < min)
{
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
}
if(visited[u]==0||visited[v]==0)
{
printf("\n Edge %d: (%d %d) cost: %d", ne++,a,b,min);
mincost +=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n minimum cost=%d\n",mincost);
}
Program 3.a
#include<stdio.h>
#define INF 999
void main()
{
int a[10][10],n,i,j;
floyd(a,n);
printf("\n Shortest path matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d \t",a[i][j]);
}
printf("\n");
}
}
Program 3.b
#include<stdio.h>
void main()
{
int a[10][10], n, i, j;
warsh(a,n);
Program 4
#include<stdio.h>
#define INF 999
int main()
{
int c[10][10],d[10],v[10],min,u,i,j,s,n;
for(i=1;i<=n;i++)
{
d[i]=c[s][i];
v[i]=0;
}
v[s]=1;
for(i=1;i<n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(v[j]==0 && d[j] < min)
{
min = d[j];
u=j;
}
}
v[u]=1;
for(j=1;j<=n;j++)
{
if(v[j]==0 && (d[u] + c[u][j] < d[j]))
{
d[j]=d[u]+c[u][j];
}
}
}
printf("\nshortest distance from source node %d:\n",s);
for(i=1;i<=n;i++)
{
printf("Node %d: %d\n",i,d[i]);
}
return 0;
}
Program 5
#include<stdio.h>
int temp[10],k=0;
void main()
{
int a[10][10], id[10],n,i,j;
printf("\n Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
id[i]=0;
sort(a,id,n);
if(k!=n)
printf("\nTopological ordering not possiblr");
else
{
printf("\nTopological ordering is: ");
for(i=1;i<=k;i++)
printf("%d",temp[i]);
}
}
Program 6
#include<stdio.h>
int w[10],p[10],n;
void main()
{
int m,i,max_profit;
max_profit = knap(1,m);
Program 7
#include<stdio.h>
#define MAX 50
for(i=0;i<n;i++)
{
ratio[i]=(double)p[i]/w[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(ratio[i] < ratio[j])
{
double temp=ratio[i];
ratio[i]=ratio[j];
ratio[j]=temp;
temp2=p[i];
p[i]=p[j];
p[j]=temp2;
}
}
}
for(i=0;i<n;i++)
{
if(currentWeight + w[i]<=m)
{
x[i]=1;
currentWeight += w[i];
maxprofit += p[i];
}
else
{
x[i]=(m-currentWeight)/(double)w[i];
maxprofit += x[i]*p[i];
break;
}
}
void main()
{
printf("Enter the number of objects:");
scanf("%d",&n);
greedyknapsack(n,w,p,m);
}
Program 8
#include<stdio.h>
void findSubset(int s[], int n,int d, int sum, int index, int subset[],
int subindex)
{
int i;
if(sum ==d)
{
printf("subset:");
for(i=0;i<subindex;i++)
{
printf("%d\t",subset[i]);
}
printf("\n");
return;
}
if(index==n||sum>d)
{
return;
}
subset[subindex]=s[index];
findSubset(s,n,d,sum+s[index],index+1,subset, subindex+1);
findSubset(s,n,d,sum,index+1,subset,subindex);
}
void main()
{
int n,d,i;
printf("enter the number of elements (n):");
scanf("%d",&n);
int s[n],subset[n];
Program 9
#include<stdio.h>
#include<sys/time.h>
temp=a[j];
a[j]=a[pos];
a[pos]=temp;
}
}
void main()
{
int a[10000],i,n;
struct timeval start,end;
if(n>10000){
printf("Array size too large\n");
n=10000;}
for(i=0;i<n;i++)
{
a[i]=rand(); //average
//a[i]=i; //best
//a[i]=n-i; //worst
}
gettimeofday(&start, NULL);
selsort(a,n);
gettimeofday(&end, NULL);
long seconds = end.tv_sec - start.tv_sec;
long microseconds = end.tv_usec - start.tv_usec;
if(microseconds<0)
{
seconds--;
microseconds += 1000000;
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}*/
printf("\n");
}
Program 10
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
key = low;
i=low+1;
j=high;
while(i<=j)
{
while(a[i] <= a[key] && i<=high) i++;
while(a[j] > a[key] && j >= low) j--;
if(i<j)
Exch(&a[i],&a[j]);
}
Exch(&a[j],&a[key]);
Quicksort(a,low,j-1);
Quicksort(a,j+1,high);
}
int main()
{
int n,k,i;
struct timeval start,end;
double ts;
scanf("%d",&n);
srand(time(NULL));
for(i=0;i<n;i++)
{
a[i]=rand(); //average
//a[i]=i; //best
//a[i]=n-i; //worst
}
gettimeofday(&start, NULL);
Quicksort(a,0,n-1);
gettimeofday(&end, NULL);
if(microseconds<0)
{
seconds--;
microseconds += 1000000;
}
Program 11
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
while(i<=mid)
{
b[k++]=a[i++];
}
while(j<=high)
{
b[k++]=a[j++];
}
for(i=low,k=0;i<=high;i++,k++)
{
a[i]=b[k];
}
}
void main()
{
int n,a[2000],k,i;
struct timeval start,end;
double ts;
gettimeofday(&start,NULL);
Mergesort(a,0,n-1);
gettimeofday(&end, NULL);
long seconds=end.tv_sec-start.tv_sec;
long microsec=end.tv_usec-start.tv_usec;
if(microsec<0)
{
seconds--;
microsec += 1000000;
}
Program 12
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void printsol(int n)
{
int i,j;
count++;
printf("\n solution #%d:\n", count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i]==j)
{
printf("Q\t");
}
else
{
printf("*\t");
}
}
printf("\n");
}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while((a[k]<=n) && !place(k))
a[k]++;
if(a[k]<=n)
{
if(k==n)
printsol(n);
else{
k++;
a[k]=0;
}
}
else
{
k--;
}
}
}
void main()
{
int n;
printf("Enter the number of Queens");
scanf("%d",&n);
queen(n);
printf("\nTotal solution=%d",count);
}