-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement BFS algorithm in a directed graph
- Loading branch information
1 parent
c4234d0
commit 4a436fc
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
//Time Complexity O(V+E) | ||
vector<int>bfsOfGraph(int v, vector<int> adj[]) | ||
{ | ||
vector<int>bfs; | ||
vector<int>vis(v,0); | ||
queue<int>q; | ||
q.push(0);//pushing first node in queue | ||
vis[0]=1; //marking it traversed | ||
while(!q.empty()) | ||
{ | ||
int node=q.front(); | ||
q.pop(); | ||
bfs.push_back(node);//putting the node in bfs | ||
for(auto i:adj[node]) //checking adjacent nodes of the bfs | ||
{ | ||
if(vis[i]==0) | ||
{ | ||
q.push(i); //pushing adjacent nodes | ||
vis[i]=1; //marking it | ||
} | ||
} | ||
} | ||
return bfs; | ||
} | ||
int main() | ||
{ | ||
int V, E; | ||
cin >> V >> E; | ||
|
||
vector<int> adj[V]; | ||
|
||
for(int i = 0; i < E; i++) | ||
{ | ||
int u, v; | ||
cin >> u >> v; | ||
adj[u].push_back(v); | ||
//adj[v].push_back(u); here graph is directed | ||
} | ||
vector<int>ans = bfsOfGraph(V, adj); | ||
for(int i=0;i<ans.size();i++) | ||
{ | ||
cout<<ans[i]<<" "; | ||
} | ||
cout<<endl; | ||
return 0; | ||
} |