0% found this document useful (0 votes)
125 views

DFS Using STACK and BFS Using Queue

This document contains C code to implement depth-first search (DFS) using a stack and breadth-first search (BFS) using a queue on a graph. It defines functions for DFS, BFS, enqueue, and dequeue operations. The main function gets graph input from the user and allows them to choose between DFS or BFS, calling the appropriate function to search the graph from a given starting node.

Uploaded by

chetna001
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

DFS Using STACK and BFS Using Queue

This document contains C code to implement depth-first search (DFS) using a stack and breadth-first search (BFS) using a queue on a graph. It defines functions for DFS, BFS, enqueue, and dequeue operations. The main function gets graph input from the user and allows them to choose between DFS or BFS, calling the appropriate function to search the graph from a given starting node.

Uploaded by

chetna001
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//DFS using STACK and BFS using Queue

#include<stdio.h>
#include<conio.h>
#define MAX 20
void dfs(int v);
void bfs(int v);
int dequeue();
void enqueue ( int item );
typedef enum boolean{false,true}bool;
int n,a[10][10];
int q[MAX];
bool visited[10];
int front = -1, rear = -1 ;

void main()
{
int i,j,v;
int ch;
printf("\n DFS and BFS");
while(1)
{
printf("\n 1. DFS");
printf("\n 2. BFS");
printf("\n 3. Quit");
printf ("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: {
printf("Enter the no. of nodes in the graph\n");
scanf("%d",&n);
printf("Enter the adjacency matrix \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The adjacency matrix shown as\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Enter the starting node for Depth First search\n");
scanf("%d",&v);
for(i=1;i<=n;i++)
visited[i]=false;
dfs(v);
}
break;
case 2: {
printf("Enter the no. of nodes in the graph\n");
scanf("%d",&n);
printf("Enter the adjacency matrix \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The adjacency matrix shown as\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Enter the starting node for Depth First search\n");
scanf("%d",&v);
for(i=1;i<=n;i++)
visited[i]=false;
bfs(v);
};
break;
case 3: exit(1);
default : printf("\n Invalid choice");

}
}
}
void dfs(int v)
{
//RECURSION METHOD
/* int count;
printf("%d", v);
visited[v] = 1;
for(count = 1; count <=n; count++)
{
if(a[v][count] == 1 && visited[count] == 0)
{
dfs(count);
}
} */

int i,stack[10],top=-1,pop;
top++;
stack[top]=v;
while(top>=0)
{
pop=stack[top];
top--;
if(visited[pop]==false)
{
printf("%d",pop);
visited[pop]=true;
}
else
continue;
for(i=1;i<=n;i++)
{
if(a[pop][i]==1 && visited[i]==false)
{
top++;// push all the unvisited neighbours in the stack
stack[top]=i;
}
}
}
}
void bfs( int s)
{
int p, i;
enqueue( s );
p = dequeue();
while ( p != 0 )
{
if(visited[p]==0)
{visited[p]= 1;
printf("%d ",p);
}
for ( i = 1;i <= n;i++ )
{
//if edge between two nodes (find adjacent nodes) and not visited
if ( ( a[ p ][ i ] != 0 ) && ( visited[ i ] == 0 ) )
{
enqueue( i );

}
}
p = dequeue();
}

//for ( i = 1;i <= n;i++ )


//if ( vis[ i ] == 0 )
// bfs( i, n );
}
void enqueue(int element)
{
if(rear==MAX)
{
printf("\n Queue OverFlow Occured");
}
else
{
if(front==-1 && rear==-1)

front=rear=0;
q[rear]=element;
else
rear++;
q[rear]=element;
}
}
int dequeue()
{
int element;
if(front==-1 && rear==-1)
{
//printf("\n Underflow");
return 0;
}
else
{element=q[front];

if(front == rear)
front=rear=-1;
else
front++;
return element;
}

You might also like