C program for Graph Traversals: DFS and BFS
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int push(int s[],int top,int item)
{
top=top+1;
s[top]=item;
return top;
}
int pop(int s[],int *top)
{
int i;
i=s[*top];
*top=*top-1;
return i;
}
int check(int visit[],int item)
{
int i=0;
while(i<100)
{
if(visit[i]==item)
{
return 1;
}
i++;
}
return 0;
}
void enq(int open2[],int item,int *front,int *rear)
{
if((*front==-1)&&(*rear==-1))
{
*front=*front+1;
*rear=*rear+1;
open2[*front]=item;
}
else
{
*rear=*rear+1;
open2[*rear]=item;
}
}
int dq(int open2[],int *front,int *rear)
{
int i;
i=open2[*front];
if(*front==*rear)
{
*front=-1;
*rear=-1;
}
else
{
*front=*front+1;
}
return i;
}
void bfs(int g[][20],int n)
{
int k=0,q=0,i=0,j=0,visit2[100],front=-1,rear=-1,open2[100],item;
while(i<100)
{
visit2[i]=0;
i++;
}
enq(open2,1,&front,&rear);
while(front!=-1)
{
item=dq(open2,&front,&rear);
i=check(visit2,item);
if(!i)
{
printf(" %d",item);
visit2[j]=item;
j++;
for(k=1;k<=n;k++)
{
if(g[item][k]==1)
{
enq(open2,k,&front,&rear);
}
}
}
}
}
void dfs(int g[][20],int n)
{
int k=0,q=0,i=0,j=0,visit1[100],top=-1,open1[100],item;
while(i<100)
{
visit1[i]=0;
i++;
}
top=push(open1,top,1);
while(top!=-1)
{
item=pop(open1,&top);
i=check(visit1,item);
if(!i)
{
printf(" %d",item);
visit1[j]=item;
j++;
for(k=1;k<=n;k++)
{
if(g[item][k]==1)
{
top=push(open1,top,k);
}
}
}
}
}
int main()
{
int i,j,n,g[20][20];
char op;
printf("\nEnter the number of vertices: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\nIs %d neigbour of %d (Y/N)",i,j);
fflush(stdin);
scanf("%c",&op);
if((op=='Y')||(op=='y'))
{
g[i][j]=1;
}
else
{
g[i][j]=0;
}
}
}
printf("\n\nDFS: ");
dfs(g,n);
printf("\n\nBFS: ");
bfs(g,n);
getch();
}
#include <stdlib.h>
#include <conio.h>
int push(int s[],int top,int item)
{
top=top+1;
s[top]=item;
return top;
}
int pop(int s[],int *top)
{
int i;
i=s[*top];
*top=*top-1;
return i;
}
int check(int visit[],int item)
{
int i=0;
while(i<100)
{
if(visit[i]==item)
{
return 1;
}
i++;
}
return 0;
}
void enq(int open2[],int item,int *front,int *rear)
{
if((*front==-1)&&(*rear==-1))
{
*front=*front+1;
*rear=*rear+1;
open2[*front]=item;
}
else
{
*rear=*rear+1;
open2[*rear]=item;
}
}
int dq(int open2[],int *front,int *rear)
{
int i;
i=open2[*front];
if(*front==*rear)
{
*front=-1;
*rear=-1;
}
else
{
*front=*front+1;
}
return i;
}
void bfs(int g[][20],int n)
{
int k=0,q=0,i=0,j=0,visit2[100],front=-1,rear=-1,open2[100],item;
while(i<100)
{
visit2[i]=0;
i++;
}
enq(open2,1,&front,&rear);
while(front!=-1)
{
item=dq(open2,&front,&rear);
i=check(visit2,item);
if(!i)
{
printf(" %d",item);
visit2[j]=item;
j++;
for(k=1;k<=n;k++)
{
if(g[item][k]==1)
{
enq(open2,k,&front,&rear);
}
}
}
}
}
void dfs(int g[][20],int n)
{
int k=0,q=0,i=0,j=0,visit1[100],top=-1,open1[100],item;
while(i<100)
{
visit1[i]=0;
i++;
}
top=push(open1,top,1);
while(top!=-1)
{
item=pop(open1,&top);
i=check(visit1,item);
if(!i)
{
printf(" %d",item);
visit1[j]=item;
j++;
for(k=1;k<=n;k++)
{
if(g[item][k]==1)
{
top=push(open1,top,k);
}
}
}
}
}
int main()
{
int i,j,n,g[20][20];
char op;
printf("\nEnter the number of vertices: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\nIs %d neigbour of %d (Y/N)",i,j);
fflush(stdin);
scanf("%c",&op);
if((op=='Y')||(op=='y'))
{
g[i][j]=1;
}
else
{
g[i][j]=0;
}
}
}
printf("\n\nDFS: ");
dfs(g,n);
printf("\n\nBFS: ");
bfs(g,n);
getch();
}
No comments