DEPTH FIRST SEARCH ALGORITHM IN C:
#include <stdio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=0;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("\n%d->%d",v,i);
dfs(i);
}
}
int main()
{
int i,j,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
reach[i]=0;
for(j=0;j<n;j++)
a[i][j]=0;
}
for(i=0;i<n;i++)//adjacency matrix
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
dfs(1);
return 0;
}
#include <stdio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=0;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("\n%d->%d",v,i);
dfs(i);
}
}
int main()
{
int i,j,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
reach[i]=0;
for(j=0;j<n;j++)
a[i][j]=0;
}
for(i=0;i<n;i++)//adjacency matrix
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
dfs(1);
return 0;
}
No comments:
Post a Comment