Beruflich Dokumente
Kultur Dokumente
#include<stdio.h>
#include<omp.h>
#include<time.h>
void mergesort(int a[100],int low,int high);
void merge(int a[100],int low,int mid,int high);
int total=0;
void main()
{
int i,n,a[100];
clock_t start=0,end=0;
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("\n Enter the elements to be generated\n");
for(i=0;i<n;i++)
{
a[i]=rand()%100;
printf("%d ",a[i]);
}
start=clock();
mergesort(a,0,n-1);
end=clock();
printf("The sorted elements are\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("The time taken for sorting process is : %lf", (end-start)/
(double)CLOCKS_PER_SEC);
printf("The total no. of threads are %d ",total);
}
void mergesort(int a[100],int low,int high)
{
int i,j,mid;
int nthreads,tid;
for(i=1;i<=10000;i++)
for(j=1;j<=1000;j++);
#pragma omp parallel shared(a,low,mid,high,nthreads,tid)
{
if(low<high)
{
#pragma omp sections nowait
4
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
tid=omp_get_thread_num();
if(tid==0)
{
nthreads=omp_get_num_threads();
if(total<nthreads)
total=nthreads;
printf("\n the total threads are %d ",nthreads);
}
}
}
void merge(int a[100],int low,int mid,int high)
{
int i=low,j=mid+1,k=low,c[20];
while(i<=mid && j<=high)
{
if(a[i]<a[j])
5
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<=high;i++)
a[i]=c[i];
}
int i,j;
for(i=1;i<=n;i++)
{
if(flag[i]==0)
{
printf("%d ",i);
flag[i]=-1;
for(j=1;j<=n;j++)
{
if(a[i][j]==1)
flag[j]=flag[j]-1;
}
i=0;
}
}
}
{
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] ;
}
10
return v[n][m];
}
void optimalsubset(int n,int m,int v[][],int w[][])
{
int i,j;
i=n;
j=m;
while((i!=0)&&(j!=0))
{
if(v[i][j]!=v[i-1][j])
{
printf("\n Item %d \n",i);
j=j-w[i];
}
}
}
void main()
{
int n,m,p[10][10],w[10][10],i,j;
printf("\n Enter the number of items:");
scanf("%d",&n);
printf("\n Enter the weights of each item:");
12
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("\n Enter the values of each item:");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("\n Enter knapsack capacity:");
scanf("%d",&m);
value=knapsack(n,m,p,w);
printf("\n Solution
programming\n");
of
the
knapsack
problem
by
dynamic
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
printf("%d\t",v[i][j]);
printf("\n");
}
printf("\n The maximal value is %d ",value);
printf("\n The items of optimal subset are \n");
optimalsubset(n,m,v,w);
}
13
s[i]=0;
d[i]=a[source][i];
}
s[source]=1;
for(i=1;i<=n;i++)
{
minm=999;
for(j=1;j<=n;j++)
if(s[j]==0)
if(d[j]<minm)
{
minm=d[j];
u=j;
}
s[u]=1;
for(v=1;v<=n;v++)
if(s[v]==0)
if(d[v]>(d[u]+a[u][v]))
{
d[v]=d[u]+a[u][v];
}
}
printf("\nShortest distance from source is \n");
15
for(i=1;i<=n;i++)
16
#include<stdio.h>
#include<stdlib.h>
struct edge
{
Int begv,endv,cost;
};
typedef struct edge E;
int getparent(int parent[20],int v)
{
while(parent[v]!=1)
V=parent[v];
return v;
}
void kruskal(int a[][],int n)
{
E e[200],temp;
Int count=1,i,j,parent[20],sum=0,parenti,parentj,nedges=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
17
if((a[i][j]!=0)&&(a[i][j]!=999))
{
e[count].begv=i;
e[count].endv=j;
e[count].cost=a[i][j];
cost++;
}
count=count-1;
for(i=1;i<=count-1;i++)
for(j=1;j<=count-1;j++)
if(e[j+1].cost<e[j].cost)
{
temp.begv=e[j+1].begv;
temp.endv=e[j+1].endv;
temp.cost=e[j+1].cost;
e[j+1].begv=e[j].begv;
e[j+1].endv=e[j].endv;
e[j+1].cost=e[j].cost;
e[j].begv=temp.begv;
e[j].endv=temp.endv;
e[j].cost=temp.cost;
}
for(i=1;i<=n;i++)
18
parent[i]=-1;
for(i=1;i<=count;i++)
{
parenti=getparent(parent,e[i].begv);
parentj=getparent(parent,e[i].endv);
if(parenti!=parentj)
{
printf(%d%d=%d\n,e[i].begv,e[i].endv,e[i].cost);
sum=sum+e[i].cost;
parent[parentj]=parenti;
nedges++;
}
}
if(nedges==n-1)
printf(Cost is %d,sum);
else
exit(0);
}
void main()
{
int n,a[10][10],I,j;
19
20
7a. Print all the nodes reachable from a given starting node in a
digraph using BFS method.
#include<stdio.h>
void bfs(int n,int a[10][10],int source,int s[10])
{
int i,f=0,r=-1,q[10],v;
printf("%d -->",source);
s[source]=1;
q[++r]=source;
while(f<=r)
{
v=q[f++];
for(i=1;i<=n;i++)
if(s[i]==0&&a[v][i])
{
q[++r]=i;
printf("%d -->",i);
s[i]=1;
}
}
}
void main()
21
{
int n,a[10][10],i,j,source,s[10];
printf("\n Enter the number of nodes in the graph");
scanf("%d",&n);
printf("\n Enter the adjacency matrix for the graph");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the source node");
scanf("%d",&source);
for(i=1;i<=n;i++)
s[i]=0;
printf("\n The BFS traversal is");
bfs(n,a,source,s);
printf("\n the nodes reachable are \n");
for(i=1;i<=n;i++)
if(s[i])
printf("%d\n",i);
}
22
if(s[i]==0) flag=1;
}
if(flag==0) printf("\ngraph connected");
else printf("\ngraph not connected");
}
24
scanf("%d",&s[i]);
rem=rem+s[i];
x[i]=0;
}
printf("Enter Max subset value\n");
scanf("%d",&total);
if(total>rem)
printf("Subset not possible\n");
else
{
subset(1,0,r);
if(count==0)
printf("Solution does not exist\n");
else
printf("\nNumber of subsets is %d\n",count);
}
}
26
flag=1;
visited[i]=1;
path[no]=i;
distance(a,path,dist+a[s][i],no+1,i,visited);
visited[i]=0;
}
}
if(flag==0)
{
printf("\nSolution %d \n",solno);
for(i=1;i<=n;i++)
{
printf("%d->",path[i]);
paths[solno][i]=path[i];
}
printf("%d\t",source);
printf("Distance:: %d\n",dist+a[s][source]);
dis[solno]=dist+a[s][source];
solno++;
}
}
float optimal()
{
int minm=dis[0],solnno=0,i;
for(i=1;i<solno;i++)
if( dis[i]<minm)
{
minm=dis[i];
solnno=i;
}
printf("\nOptimal path is ");
for(i=1;i<=n;i++)
printf("%d->",paths[solnno][i]);
printf("%d",source);
printf("\nOptimal cost is %d",minm);
return minm;
}
28
float nearest()
{
int i,j,x,y,cost=0,minm=999,count=0 ;
printf("\n\nPath using nearest\n");
printf("%d ",source);
vis[source]=1;
while(count<n-1)
{
minm=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(vis[i] && !vis[j] && a[i][j]<minm)
{
minm=a[i][j];
x=i;
y=j;
}
}
cost=cost+a[x][y];
printf("->%d",y);
vis[y]=1;
count++;
}
printf("->%d",source);
cost=cost+a[y][source];
printf("\nMinimum cost is %d",cost);
return cost;
}
29
#include<stdio.h>
void main()
{
int n,a[10][10],i,j,sum;
printf("Enter the no. of nodes\n");
scanf("%d",&n);
printf("Enter the cost adj. matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
sum=prims(a,n,1);
if(sum>=999)
printf("spanning does not exist\n");
else
printf("cost of MST is %d",sum)
}
for(i=1;i<=n;i++)
{
s[i]=0;
d[i]=a[source][i];
iner[i]=0;
}
sum=0;
s[source]=1;
for(i=1;i<=n-1;i++)
{
minm=999;
for(j=1;j<=n;j++)
{
if(s[j]==0)
if(d[j]<minm)
{
minm=d[j];
u=j;
}
}
sum=sum+d[u];
s[u]=1;
for(v=1;v<=n;v++)
31
if(s[v]==0)if(d[v]>a[u][v])
{
d[v]=a[u][v];
inter[v]=u;
}
}
printf(Path of the MST is sourcedestination=cost:\n);
for(i=1;i<=n;i++)
if(inter[i]!=i)
printf(%d%d=%d\n,i,a[inter[i]][i]);
return sum;
}
32
33
scanf("%d",&cost[i][j]);
a[i][j]=cost[i][j];
}
start=clock();
tot=floyds(cost,n);
end=clock();
printf("\nthe path matrix is \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",cost[i][j]);
printf("\n");
}
printf("\n the time taken for sorting process is: %lf",(end-start)/
(double)CLOCKS_PER_SEC);
}
for(i=1;i<=10000;i++)
34
for(j=1;j<=1000;j++);
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
tid=omp_get_thread_num();
if(tid==0)
{
nthreads=omp_get_num_threads();
if(total<nthreads)
total=nthreads;
printf("\n total threads are: %d ",nthreads);
}
}
return total;
}
35
{
x[k]=x[k]+1;
while( x[k] <=n && place(x,k) ==0 )
x[k]=x[k]+1;
if(x[k]<=n)
{
if(k==n)
{
count++;
printf("\nSolution %d is\n",count);
for(i=1;i<=n;i++)
printf("%d ",x[i]);
printf("\nChessBoard Representation is\n");
printsol(x,n);
}
else
{
k=k+1;
x[k]=0;
}
}
else
k=k-1;
37
}
return;
}
for(i=1;i<=n;i++)
c[i][x[i]]='Q';
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%c ",c[i][j]);
printf("\n");
}
return;
}
39