Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views12 pages

Ada 1

Uploaded by

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

Ada 1

Uploaded by

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

program 1 Kruskal

#include<stdio.h>
int cost[10][10],n,i,j;
void kruskal()
{
int par[n];
int a=0,b=0,u=0,v=0,min, mincost = 0, ne = 0;
for(i=0;i<n;i++)
par[i]=-1;

printf("the minimum spanning tree edges are...");


while(ne < n-1)
{
min = 999;
for( i=0;i<n;i++)
for( j=0;j<n;j++)
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
while(par[u]!=-1)
u=par[u];
while(par[v]!=-1)
v=par[v];
if(u!=v)
{
printf("From vertex %d to vertex %d and the cost = %d\n",a,b,min);
mincost+=min;
par[v]=u;
ne++;
}
cost[a][b]=cost[b][a]=999;
}
printf("Cost of MST = %d", mincost);
}

int main()
{
printf("Enter the no. of vertices:");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=0;i<n;i++)
for( j=0;j<n;j++)
scanf("%d",&cost[i][j]);
kruskal();
}

------------------------------------------------------------------

program 2

#include<stdio.h>
int cost[10][10],n,i,j;
void prim()
{
int vt[10]={0};
int a=0,b=0,min, mincost = 0, ne = 0;

vt[0] = 1;
while(ne < n-1)
{
min = 999;
for ( i = 0; i<n; i++)
{
if(vt[i]==1)
for(j = 0;j <n; j++)
if(cost[i][j] < min && vt[j]==0)
{
min = cost[i][j];
a = i;
b = j;
}
}
printf("Edge from vertex %d to vertex %d and the cost %d\n",a,b,min);
vt[b] = 1;
ne++;
mincost += min;
cost[a][b] = cost[b][a] = 999;
}
printf("minimum spanning tree cost is %d",mincost);
}

int main()
{
printf("Enter the no. of vertices: ");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for( i=0;i<n;i++)
for( j=0;j<n;j++)
scanf("%d",&cost[i][j]);
prim();
}
----------------------------------------------------------------
program 3A

#include<stdio.h>
int min(int a, int b)
{
return(a < b ? a : b);
}
void floyd(int D[][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++)
D[i][j]=min(D[i][j],D[i][k]+D[k][j]);
}

int main()
{
int n,i,j,cost[10][10];
printf("Enter no. of Vertices: ");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=1;i<=n;i++)
for( j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
floyd(cost,n);
printf("All pair shortest path\n");
for(i=1;i<=n;i++)
{
for( j=1;j<=n;j++)
printf("%d ",cost[i][j]);
}
}
--------------------------------------------------------------------

program 3B

#include<stdio.h>
void warshal(int A[][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++)
A[i][j]=A[i][j] || (A[i][k] && A[k][j]);
}

void main()
{
int n,i,j, adj[10][10];
printf("Enter no. of Vertices: ");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for( i=1;i<=n;i++)
for( j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
warshal(adj,n);

printf("Transitive closure of the given graph is\n");


for(i=1;i<=n;i++)
{
for(i=1;j<=n;j++)
printf("%d ",adj[i][j]);
printf("\n");
}
}
---------------------------------------------------------------------
PROGRAM 4

#include<stdio.h>
int cost[10][10],n,dist[10];
int minm(int m, int n)
{
return(( m < n) ? m: n);
}

void dijkstra(int source)


{
int i,j,v;
int s[10]={0};
int min, w=0;

for(i=0;i<n;i++)
dist[i]=cost[source][i];

dist[source] = 0;
s[source] = 1;
for(i=0; i < n-1; i++)
{
min = 999;
for(j = 0; j < n; j++)
{
if ((s[j] == 0 ) && (min > dist[j]))
{
min = dist[j];
w = j;
}
}
s[w]=1;
for( v=0;v<n;v++)
{
if(s[v]==0 && cost[w][v]!=999)
{
dist[v]= minm(dist[v],dist[w]+cost[w][v]);
}
}
}
}

int main()
{

int source,i,j;

printf("Enter the no.of vertices:");


scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=0;i<n;i++)
for( j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("Enter the source vertex:");
scanf("%d",&source); dijkstra(source);

printf("the shortest distance is...");


for(i=0; i<n; i++)
printf("Cost from %d to %d is %d\n",source,i,dist[i]);
}

--------------------------------------------------------------------
PROGRAM 5

#include<stdio.h>
int cost[10][10],n,colsum[10]; void cal_colsum()
{
int j,i;
for(j=0;j<n;j++)
{
colsum[j]=0;
for( i=0;i<n;i++)
colsum[j]+=cost[i][j];
}
}

void source_removal()
{
int i,j,k,select[10]={0};
printf("Topological ordering is:");
for(i=0;i<n;i++)
{
cal_colsum();
for(j=0;j<n;j++)
{
if(select[j]==0 && colsum[j]==0)
break;
}
printf("%d ",j);
select[j]=1;
for(k=0;k<n;k++)
cost[j][k]=0;
}
}

void main()
{
int i,j;
printf("Enter no. of Vertices: ");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=0;i<n;i++)
for( j=0;j<n;j++)
scanf("%d",&cost[i][j]);
source_removal();
}
--------------------------------------------------------------------
PROGRAM 6

#include<stdio.h>
int n,m,p[10],w[10];
int max(int a, int b)
{
return(a>b?a:b);
}
void knapsack_DP()
{
int V[10][10],i,j;
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
if(i==0 || j==0)
V[i][j]=0;
else if(j<w[i])
V[i][j]=V[i-1][j];
else
V[i][j]=max(V[i-1][j],p[i]+V[i-1][j-w[i]]);

for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
printf("%d ",V[i][j]);
printf("\n");
}
printf("Items included are:");
while(n > 0)
{
if(V[n][m] != V[n-1][m])
{
printf("%d ",n);
m = m - w[n];
}
n--;
}
}

int main()
{
int i;
printf("Enter the no. of items: ");
scanf("%d",&n);
printf("Enter the weights of n items: ");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("Enter the prices of n items: ");
for(i=1;i<=n;i++)

scanf("%d",&p[i]);
printf("Enter the capacity of Knapsack: ");
scanf("%d",&m);
knapsack_DP();
}

------------------------------------------------------------------
PROGRAM 7

#include<stdio.h>
int n,m,p[10],w[10];
void greedy_knapsack()
{
float max, profit=0;
int k=0,i,j;
printf("item included is :");
for(i=0;i<n;i++)
{
max=0;
for(j=0;j<n;j++)
{
if(((float)p[j])/w[j] > max)
{
k=j;
max=((float)p[j])/w[j];
}
}
if(w[k] <= m )
{
printf("%d",k); m = m - w[k];
profit=profit+p[k]; p[k]=0;
}
else
break;
}
printf("Discrete Knapsack profit = %f\n",profit);
printf("Continuous Knapsack also includes item %d with portion: %f\n", k,
((float)m)/w[k]);
profit = profit + ((float)m)/w[k] * p[k];
printf("Continuous Knapsack profit = %f\n",profit);
}

int main()
{
int i;
printf("Enter the no. of items: ");
scanf("%d",&n);
printf("Enter the weights of n items: ");
for(i=0;i<n;i++)
scanf("%d",&w[i]);
printf("Enter the prices of n items: ");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter the capacity of Knapsack: ");
scanf("%d",&m);
greedy_knapsack();
}

-----------------------------------------------------------------------------------
-----------------------------------------------------
EXPERMINET 8

#include<stdio.h>
int x[10], w[10], count, d;
void sum_of_subsets(int s, int k, int rem)
{
x[k] = 1;
if( s + w[k] == d)
{

printf("subset = %d\n", ++count);


for(int i=0 ; i <= k ; i++)
if ( x[i] == 1)
printf("%d ",w[i]);
printf("\n");
}
else if ( s + w[k] + w[k+1] <= d )
sum_of_subsets(s+w[k], k+1, rem-w[k]);

if( ( s+rem-w[k] >= d) && ( s + w[k+1]) <= d)


{
x[k] = 0;
sum_of_subsets(s,k+1,rem-w[k]);
}
}

int main()
{
int sum = 0,n;
printf("enter no of elements:");
scanf("%d",&n);
printf("enter the elements in increasing order:");
for( int i = 0; i < n ; i++)
{
scanf("%d",&w[i]);
sum=sum+w[i];
}
printf("eneter the sum:");
scanf("%d",&d);

if ( ( sum < d ) || ( w[0] > d ) )


printf("No subset possible\n");
else
sum_of_subsets(0,0,sum);
}

-----------------------------------------------------------------------------------
--------------------------------------------------------------------
PROGRAM 9 SELECION SORT

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int a[10000],n,count;
void selection_sort()
{
int i,j;
for( i=0;i<n-1;i++)
{
int min = i;
for( j=i+1;j<n;j++)
{
count++;
if(a[j]<a[min])
min=j;
}
int temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}

int main()
{
int i;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
printf("All the elements:");
srand(time(0));
for(i=0;i<n;i++)
{
a[i]=rand();
printf("%d ",a[i]);
}
selection_sort();
printf("\nAfter sorting\n");
for(i=0;i<n;i++)
printf("%d ", a[i]);
printf("\nNumber of basic operations = %d\n",count);
}

-----------------------------------------------------------------------------------
---------------------------------------
program 10 QUICKSORT

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int count=0;
int partition(int a[], int low,int high)
{
int pivot=a[low],temp,i=low+1,j=high;
while(1)
{
while(i<=high && a[i]<=pivot)
{
i++;
count++;
}
while(j>0 && a[j]>pivot)
{
j--;
count++;
}
count+=2;
if(i<j)
{
temp = a[i]; a[i] = a[j];
a[j] =temp;
}
else if(i>j)
{
temp = a[low]; a[low] = a[j]; a[j] = temp;
return j;
}
else
return j;
}
}

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


{
int s;
if(low<high)
{
s = partition(a,low,high);

quicksort(a,low,s-1);
quicksort(a,s+1,high);
}
}
int main()
{
int a[10000],i,n;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
printf("All the elements:");
srand(time(0));
for( i=0;i<n;i++)
{
a[i]=rand();
printf("%d ",a[i]);
}
quicksort(a,0,n-1);
printf("\nAfter sorting\n");
for( i=0;i<n;i++)
printf("%d ", a[i]);
printf("\nNumber of basic operations = %d\n",count);
}

-----------------------------------------------------------------------------------
------------------------------------------------------
PROGRAM 10- MERGE SORT

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int count=0;
void merge(int a[], int low,int mid,int high)
{
int i,j,k,c[10000];
i=low, j=mid+1, k=0;
while((i<=mid) && (j<=high))
{
count++;
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,j=0;j<k;i++, j++)
a[i]=c[j];
}
void merge_sort(int a[], int low, int high)
{
int mid;
if(low < high)
{
mid=(low+high)/2;
merge_sort(a,low,mid);
merge_sort(a,mid+1,high);
merge(a,low,mid,high);
}
}
int main()
{
int *a,n,i;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
a=(int *) malloc(n*sizeof(int));
printf("All the elements:");
srand(time(0));
for( i=0;i<n;i++)
{
a[i]=rand()%1001;
printf("%d ",a[i]);
}
merge_sort(a,0,n-1);
printf("\nAfter sorting\n");
for(i=0;i<n;i++)
printf("%d ", a[i]);
printf("\nNumber of basic operations = %d\n",count);
}

-----------------------------------------------------------------------------------
---------------------------------------------
program 12 -nqueen prob

#include<stdio.h>
#include<math.h> //for abs() function
int place(int x[],int k)
{
int i,j;
for(i=1;i<k;i++)
{
if( (x[i] == x[k]) || ( abs(x[i]-x[k]) == abs(i-k)) )
return 0;
}
return 1;
}

int nqueens(int n)
{
int x[10], k, count=0,i,j;
k=1;
x[k]=0;
while(k != 0)
{
x[k]++;
while((x[k] <= n) && (!place(x,k)))
x[k]++;
if(x[k] <= n)
{
if(k == n)
{
printf("\nSolution %d\n",++count);
for(i=1;i <= n;i++)
{
for( j=1;j <= n;j++)
printf("%c ",j==x[i]?'Q':'X');
printf("\n");
}
}
else
{
++k;
x[k]=0;
}
}
else
k--;
}
return count;
}

void main()

{
int n;
printf("Enter the size of chessboard: ");
scanf("%d",&n);
printf("\nThe number of possibilities are %d",nqueens(n));
}

-----------------------------------------------------------------------------------
------------------------------------------------------------

You might also like