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.
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.
Hi I need help with a breadth first and depth first search in C++. If you...
Implement Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for a graph in Java.(Can be any graph, just an example of DFS and BFS is sufficient) If it cannot be done for a graph, then just an example of DFS and BFS are enough.
Please clearly explain what are the differences between DFS (Depth First Search) and BFS (Breadth First Search)?
From the given graph discover the structure of the graph using 1. breadth first search(BFS) a. depth first search(DFS) b. Show the steps and techniques used for each method (20 points)
From the given graph discover the structure of the graph using 1. breadth first search(BFS) a. depth first search(DFS) b. Show the steps and techniques used for each method (20 points)
You will be implementing a Breadth-First Search (BFS) and a
Depth-First Search (DFS) algorithm on a graph stored as an
adjacency list. The AdjacencyList class inherits from the Graph
class shown below. class Graph { private: vector _distances; vector
_previous; public: Graph() { } virtual int vertices() const = 0;
virtual int edges() const = 0; virtual int distance(int) const = 0;
virtual void bfs(int) const = 0; virtual void dfs(int) const = 0;
virtual void display() const = 0;...
Depth-first Search vs Breadth-first Search Please explain the difference!
Show the operation of depth-first search (DFS) on the graph of Figure 1 starting from vertex q. Always process vertices in alphabetical order. Show the discovery and finish times for each vertex, and the classification of each edge. (b) A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first search (BFS) tree can also be used to classify the edges reachable from the source of the search into the same four categories....
JAVA
LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents the graph. Your code should contain a function called addEdgelint i, int j). Use this function to add the appropriate edges in your matrix. Write code to implement Depth-First-Search (DFS) and Breadth-First-Search (BFS) of your graph. Let 0 be the source Node . Traverse the graph using DFS, print the Nodes as they are visited. . Traverse the...
refer to the question using c++. if you could not do the bonus
part no problem you don't have too , but if you can so please do it
and let me know
Create an unweighted undirected Graph Class using an Adjacency Matrix with the following functions 1. Graph(int numofV) 3. int noOfOutgoingEdges(int vertex); 4. int noOflncomingEdges (int vertex) 5. void print) You may use vectors/2D dynamic arrays to implement the matrix. Bonus (20) 6. void DFS(); Depth First Search...
Discuss graph representation, Breadth-first search and Depth-first search. Use examples to highlight pros and cons.
#include <iostream>
#include <queue>
using namespace std;
class Graph {
public:
Graph(int n);
~Graph();
void addEdge(int src, int tar);
void BFTraversal();
void DFTraversal();
void printVertices();
void printEdges();
private:
int vertexCount;
int edgeCount;
bool** adjMat;
void BFS(int n, bool marked[]);
void DFS(int n, bool marked[]);
};
Graph::Graph(int n=0) {
vertexCount = n;
edgeCount = 0;
if(n == 0)
adjMat = 0;
else {
adjMat = new bool* [n];
for(int i=0; i < n; i++)
adjMat[i] = new bool [n];
for(int i=0;...