0% found this document useful (0 votes)
5 views15 pages

ADA Programs

Uploaded by

Nayana Kulkarni
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)
5 views15 pages

ADA Programs

Uploaded by

Nayana Kulkarni
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/ 15

Program1

#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);

printf("Enter the cost of Matrix\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
}
printf("\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++)
{

if(!parent[j] && cost[i][j]<min && cost[i][j]!=0)


{
min = cost[i][j];
a=i;
b=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()
{

printf("\n Enter the number of nodes: ");


scanf("%d",&n);

printf("\n Enter the 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;
}
}

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

int min(int a, int b)


{
return (a<b)?a:b;
}

void floyd(int p[10][10], int n)


{
int i,j,k;
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
}
}
}

void main()
{

int a[10][10],n,i,j;

printf("\n Enter the n value: ");


scanf("%d",&n);

printf("\nEnter the graph data: \n");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[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 warsh(int p[10][10],int n)


{
int i,j,k;
for(k=1; k<=n;k++)
{
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++)
{
p[i][j]=p[i][j] || (p[i][k] && p[k][j]);
}
}
}
}

void main()
{
int a[10][10], n, i, j;

printf("\nEnter the n value: ");


scanf("%d",&n);

printf("\n Enter the graph data:\n");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}

warsh(a,n);

printf("\n Resultant path matrix:\n");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d \t",a[i][j]);
}
printf("\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;

printf("enter the value of n: ");


scanf("%d",&n);

printf("\nEnter the graph data:\n");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&c[i][j]);
}
}

printf("\n enter the source node:");


scanf("%d",&s);

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 sort(int a[10][10], int id[10], int n)


{
int i,j;
for(i=1;i<=n;i++)
{
if(id[i]==0)
{
id[i]=-1;
temp[++k]=i;
for(j=1;j<=n;j++)
{
if(a[i][j] == 1 && id[j]!=-1)
id[j]--;
}
i=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;

printf("\nEnter the graph data:\n");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j] == 1)
{
id[j]++;
}
}
}

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;

int max(int a, int b)


{
return (a>b)?a:b;
}

int knap(int i,int m)


{
if(i==n) return w[i]>m?0:p[i];
if(w[i]>m) return knap(i+1,m);
return max(knap(i+1,m), knap(i+1,m-w[i])+p[i]);
}

void main()
{
int m,i,max_profit;

printf("\n enter the no of objects:");


scanf("%d",&n);

printf("\nEnter the knapsack capacity:");


scanf("%d", &m);

printf("\n Enter the profit followed by weight:\n");


for(i=1;i<=n;i++)
{
scanf("%d %d",&p[i],&w[i]);
}

max_profit = knap(1,m);

printf("\n Max profit=%d",max_profit);

Program 7

#include<stdio.h>
#define MAX 50

int p[MAX],w[MAX], x[MAX];


double maxprofit;
int n,m,i;

void greedyknapsack(int n, int w[],int p[], int m)


{
double ratio[MAX];
int j;

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;

int temp2 = w[i];


w[i]=w[j];
w[j]=temp2;

temp2=p[i];
p[i]=p[j];
p[j]=temp2;
}
}
}

int currentWeight =0;


maxprofit = 0.0;

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;
}
}

printf("Optimal solution for greedy method: %lf\n",maxprofit);


printf("solution vector for greedy method:");
for(i=0;i<n;i++)
printf("%d\t",x[i]);
}

void main()
{
printf("Enter the number of objects:");
scanf("%d",&n);

printf("enter the objects' weights:");


for(i=0;i<n;i++)
scanf("%d",&w[i]);

printf("enter the objects' profits:");


for(i=0;i<n;i++)
scanf("%d",&p[i]);

printf("Enter the maximum capacity:");


scanf("%d",&m);

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];

printf("Enter the elements S1,S2,....");


for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
}
printf("enter the target sum(d):");
scanf("%d",&d);

printf("Subsets of S with sum%d:\n",d);


findSubset(s,n,d,0,0,subset,0);
}

Program 9

#include<stdio.h>
#include<sys/time.h>

void selsort(int a[], int n)


{
int i,j,small,pos,temp;
for(j=0;j<n-1;j++)
{
small=a[j];
pos=j;
for(i=j+1;i<n;i++)
{
if(a[i]<small)
{
small=a[i];
pos=i;
}
}

temp=a[j];
a[j]=a[pos];
a[pos]=temp;
}
}

void main()
{
int a[10000],i,n;
struct timeval start,end;

printf("\n enter the n value:");


scanf("%d",&n);

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;
}

printf("\n Time taken is %ld seconds and %ld


microseconds\n",seconds,microseconds);

/*printf("\n Sorted array is:");

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>

void Exch(int *p, int *q)


{
int temp= *p;
*p=*q;
*q=temp;
}

void Quicksort(int a[], int low, int high)


{
int i,j,key;
if(low>=high)
return;

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;

printf("\n Enter how many numbers:");

scanf("%d",&n);

int *a = (int *)malloc(n*sizeof(int));

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);

long seconds = end.tv_sec - start.tv_sec;


long microseconds = end.tv_usec - start.tv_usec;

if(microseconds<0)
{
seconds--;
microseconds += 1000000;
}

/*printf("\n Sorted Numbers are:\n");


for(k=0;k<n;k++)
{
printf("%d",a[k]);
}*/

printf("\n The time taken is %ld sec %ld


Microsec\n",seconds,microseconds);
free(a);
return 0;
}

Program 11

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>

void Merge(int a[], int low, int mid, int high)


{
int i,j,k;
int b[high-low+1];
i=low;
j=mid+1;
k=0;

while(i<=mid && j<=high)


{
if(a[i]<=a[j])
{
b[k++]=a[i++];
}
else
{
b[k++]=a[j++];
}
}

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 Mergesort(int a[], int low, int high)


{
if(low<high)
{
int mid=(low+high)/2;
Mergesort(a,low,mid);
Mergesort(a,mid+1,high);
Merge(a,low,mid,high);
}
}

void main()
{
int n,a[2000],k,i;
struct timeval start,end;
double ts;

printf("\n Enter how many numbers:");


scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand(); //average
//a[i]=i; //best
//a[i]=n-i; //worst
}

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;
}

/*printf("\n Sorted Numbers are:\n");


for(k=0;k<n;k++)
{
printf("%d\t",a[k]);
}*/
printf("\n Time taken is %ld seconds and %ld
microsec\n",seconds,microsec);
}

Program 12

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int a[30], count = 0;

int place(int pos)


{
int i;
for(i=1;i<pos;i++)
{
if((a[i]==a[pos])||(abs(a[i]-a[pos])==abs(i-pos)))
return 0;
}
return 1;
}

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);
}

You might also like