Professional Documents
Culture Documents
BFS:
BFS stands for Breadth First Search is a vertex based technique for finding a shortest path in graph. It
uses a Queue data structure which follows first in first out. In BFS, one vertex is selected at a time when
it is visited and marked then its adjacent are visited and stored in the queue.
DFS:
DFS stands for Depth First Search is a edge based technique. It uses the Stack data structure, performs
two stages, first visited vertices are pushed into stack and second if there is no vertices then visited
vertices are popped.
CODE:
//implementing BFS and DFS
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
adj[u].push_back(v);
//using a queue
queue<int>q;
q.push(s);
visit[s]=true;
while(!q.empty()){
int u=q.front();
cout<<u<<" ";
q.pop();
for(int i=0;i<adj[u].size();i++){
if(!visit[adj[u][i]]){
q.push(adj[u][i]);
visit[adj[u][i]]=true;
//using a stack
stack<int>stk
stk.push(s);
visit[s]=true;
while(!stk.empty()){
int u=stk.top();
cout<<u<<" ";
stk.pop();
for(int i=0;i<adj[u].size();i++){
if(!visit[adj[u][i]]){
stk.push(adj[u][i]);
visit[adj[u][i]]=true;
//main function
int main(){
vector<int>adj[5];
bool visit[5];
for(int i=0;i<5;i++){
visit[i]=false;
edge(adj,0,2);
edge(adj,0,1);
edge(adj,1,3);
edge(adj,2,0);
edge(adj,2,3);
edge(adj,2,4);
cout<<endl;
for(int i=0;i<5;i++){
visit[i]=false;
OUTPUT: