I Need The BFS And DFS Graph Source Code In C++ Using Recursion Can You Help?

5 Answers

Monica Stott Profile
Monica Stott answered
A good example of a program to create a graph using DFS and BDS can be found at www.sourcecodesworld.com.

This code is created by Arun Vishnu and available to select and copy so you can make any adaptions and changes so that it suits your project. There are many other codes and source codes on this website so it may be possible to find the exact code you are looking for my checking out this site.
  • DFS
DFS stands for Depth-first search and involves the process of using a graph to find information. The search begins at the root and then backtracks along each branch to find information. The depth-first search is known as an uninformed search as no knowledge is needed prior to the search but the search goes deeper until the goal is found.
  • BFS
BFS stands for Breadth-first search and is a similar process as it is a graph search that begins at the root and then it explores neighboring information within the graph by exploring and examining sequences. In a breadth-first search, the goal is not considered prior to the search, it is only acknowledged once it has been found.
Anonymous Profile
Anonymous answered
Hi,

C++ code for both DFS and BFS can be found here

 
Code for BFS can also be found here.

In these examples, loop has been used to end the search instead.
You can easily remove the loop correctly and make the function a recursive function as required.
 
Hope it helps?
Anonymous Profile
Anonymous answered
#include
#include
#include

void create();  // For creating a graph
void dfs();  // For Deapth First Search(DFS) Traversal.
Void bfs();  // For Breadth First Search(BFS) Traversal.

Struct node  // Structure for elements in the graph
{
   int data,status;
   struct node *next;
   struct link *adj;
};

struct link  // Structure for adjacency list
{
   struct node *next;
   struct link *adj;
};

struct node *start,*p,*q;
struct link *l,*k;

int main()
{
   int choice;
   clrscr();
   create();
   while(1)
   {
  coutadj=NULL;
  if(flag==0)
  {
start=p;
q=p;
flag++;
  }
  else
  {
q->next=p;
q=p;
  }
   }
   p=start;
   while(p!=NULL)
   {
  coutadj=k;
    l=k;
}
q=start;
while(q!=NULL)
{
    if(q->data==that)
  k->next=q;
    q=q->next;
}
  }
  p=p->next;
   }
   coutnext;
   }
   p=start;
   qu[0]=p->data;
   p->status=1;
   while(1)
   {
  if(qu[j]==0)
break;
  p=start;
  while(p!=NULL)
  {
if(p->data==qu[j])
     break;
p=p->next;
  }
  k=p->adj;
  while(k!=NULL)
  {
q=k->next;
if(q->status==0)
{
    qu[I]=q->data;
    q->status=1;
    qu[I+1]=0;
    I++;
}
k=k->adj;
  }
  j++;
   }
   j=0;
   cout
Sama MODsama Profile
Sama MODsama answered
Void BFS(vertic *recieve,int connectedWith[N][N]/*,string v*/)
{
int I=0;
queue<string> q;
q.push(recieve[I].name);
recieve[0].visited = true;
cout<<"n======nBREADTH FIRSST SEARCH TRAVERSING :";
cout<<"n======n ";
while(!q.empty())
{
int j=0;
cout<<q.front()<<" ";

for(int n=0;n<N; n++)
{
if( recieve[n].name == q.front() )
{
I=n;

break;
}//END OF IF
}//END OF FOR LOOP
q.pop();
for( ;j<N; j++ )
{
if(j==I)
continue;
else if( connectedWith[I][j]==1 && recieve[j].visited==false )
{
recieve[j].visited = true;
q.push(recieve[j].name);

}//END OF ELSE IF
}//END OF FOR LOOP
}//END OF WHILE LOOP
}//END OF FUNCTION
Anonymous Profile
Anonymous answered
#include

#define MAX 10

class DFS
{
private : Int n;
  int adj[MAX][MAX];
  int visited[MAX];
public  : Void dfs(int);
  void readmatrix();
};

void DFS :: Readmatrix()
{
int I,j;
cout > n;
cout

Answer Question

Anonymous