Question

Hi I need help with a breadth first and depth first search in C++. If you...

Hi I need help with a breadth first and depth first search in C++. If you could please help me with the code for dfs, bfs, and dfsRecursiv functions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BFS:

For the graph :

0

/ \

1    2

/

3

If we pick source as node 0 , than in bfs , we go breadth-wise i.e first visit all edges from a parent node and the go the child node

1) 0->1

2)0->2

3)1->3

So the bfs sequence is 0 1 2 3

Overall running time using adjacency matrix is O(N^2) , where N is the number of vertices.

Code:


#include <bits/stdc++.h>
using namespace std;

// itialize number of vertices and edges here so we need not to pass them to bfs method seperately.

   const int vertices = 4, edges = 3;
   int adj[vertices][vertices]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};

void BFS(int source)
{

// initially all the nodes are unvisited , so set vis array to 0 for all nodes.
   int vis[vertices+1];
   for(int i=0;i<vertices;i++)
   vis[i]=0;
  
// we would use a vector to act as a queue for storing nodes, you could use STL queue also
// it would also work.
   vector<int> q;
// push the source node in queue.
   q.push_back(source);

// mark it as visited
  
   vis[source] = true;

   int node;
  
// while the queue is not empty
   while (!q.empty()) {
       node = q[0];

// print the node at front of the queue  
       cout << node << " ";
// erase the node at front of the queue
       q.erase(q.begin());

// iterate on the adjacency matrix , if adj[node][i] ==1 means edge from node->i exists
// and it is not visited then , than push it to queue and mark it as visited.
       for (int i = 0; i < vertices; i++) {
           if (adj[node][i] == 1 && (!vis[i])) {
   q.push_back(i);
   vis[i] = true;
           }
       }
   }
}


int main()
{

// adding edge 0-1
   adj[0][1]=1;
// adding edge 0-2
adj[0][2]=1;
// adding edge 1-3
   adj[1][3]=1;
  
// starting bfs from source node->0
   BFS(0);
}

Screenshots:

Output:

DFS:

For the graph :

0

/ \

1    2

/

3

If we pick the vertex 0 as source node , then we will visit the graph in following sequence if we go depthwise fromsource ->

1)0->1

2)1->3

3)0->2

Therefore dfs sequence is 0 1 3 2

Code:


#include <bits/stdc++.h>
using namespace std;

// itialize number of vertices, edges and visted array here so we need
//not to pass them to dfs method seperately.

   const int vertices = 4, edges = 3;
   int adj[vertices][vertices]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
   int vis[]={0,0,0,0};

void DFS(int node)
{
  
// Print the current node
cout <<node << " ";
  
// mark node as visited after printing i.e assign 1 to vis[node].
vis[node] = 1;
  
// Iterate on the adjacency matrix and if the edge from node->i exists and is not visited
// start a recursive call to dfs method with new node i .
for (int i = 0; i < vertices; i++) {
  
// If some node is adjacent to the current node
// and it has not already been visited
if (adj[node][i] == 1 && (!vis[i])) {
DFS(i);
}
}
}

int main()
{

// adding edge 0-1
   adj[0][1]=1;
// adding edge 0-2
adj[0][2]=1;
// adding edge 1-3
   adj[1][3]=1;
  
// starting dfs from source node->0

   DFS(0);
}

Output:

Note : dfs and dfsRecursive method will be same because i have already implement dfs recursively.

Upvote if you like the answer.

Add a comment
Know the answer?
Add Answer to:
Hi I need help with a breadth first and depth first search in C++. If you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT