Monthly Archives: September 2013

Breadth First Search – C Program

#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{
int v;
clrscr();
printf(“\n Enter the number of vertices:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf(“\n Enter graph data in matrix form:\n”);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf(“%d”,&a[i][j]);
printf(“\n Enter the starting vertex:”);
scanf(“%d”,&v);
bfs(v);
printf(“\n The node which are reachable are:\n”);
for(i=1;i<=n;i++)
if(visited[i])
printf(“%d\t”,i);
else
printf(“\n Bfs is not possible”);
getch();
}

Depth First Search – C Program

#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf(“\n %d->%d”,v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
clrscr();
printf(“\n Enter number of vertices:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf(“\n Enter the adjacency matrix:\n”);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf(“%d”,&a[i][j]);
dfs(1);
printf(“\n”);
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf(“\n Graph is connected”);
else
printf(“\n Graph is not connected”);
getch();
}

N Queen’s Problem – PPTs

Click following links to download N Queen’s Problem PPTs.

N-Queen PPT1

N-Queen PPT2

N Queen’s Problem – C Program

#include<stdio.h>
#include<conio.h>
#include<math.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 print_sol(int n)
{
int i,j;
count++;
printf(“\n\nSolution #%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)
print_sol(n);
else
{
k++;
a[k]=0;
}
}
else
k–;
}
}
void main()
{
int i,n;
clrscr();
printf(“Enter the number of Queens\n”);
scanf(“%d”,&n);
queen(n);
printf(“\nTotal solutions=%d”,count);
getch();
}

Knapsack Problem using Greedy Method – C Program

# include<stdio.h>
# include<conio.h>

void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp= 0;
int i, j, u;
u=capacity;

for (i=0;i<n;i++)
x[i]=0.0;

for (i=0;i<n;i++)
{
if(weight[i]>u)
break;
else
{
x[i]=1.0;
tp= tp+profit[i];
u=u-weight[i];
}
}

if(i<n)
x[i]=u/weight[i];

tp= tp + (x[i]*profit[i]);

printf(“The result vector is:- “);
for(i=0;i<n;i++)
printf(“%.2f   “,x[i]);

printf(“\nMaximum profit(value) is:- %.2f”, tp);

}

void main()
{
float weight[20], profit[20], capacity;
int n, i ,j;
float ratio[20], temp;
clrscr();

printf (“Enter the No. of Objects:- “);
scanf (“%d”, &n);

printf (“Enter the Weights and Values of each object:- “);
for (i=0; i<n; i++)
{
scanf(“%f %f”, &weight[i], &profit[i]);
}

printf (“Enter the capacity of knapsack:- “);
scanf (“%f”, &capacity);

for (i=0; i<n; i++)
{
ratio[i]=profit[i]/weight[i];
}

for(i=0; i<n; i++)
{
for(j=i+1;j< n; j++)
{
if(ratio[i]<ratio[j])
{
temp= ratio[j];
ratio[j]= ratio[i];
ratio[i]= temp;

temp= weight[j];
weight[j]= weight[i];
weight[i]= temp;

temp= profit[j];
profit[j]= profit[i];
profit[i]= temp;
}
}
}

knapsack(n, weight, profit, capacity);
getch();
}