Dfs
Mon Jan 10 2022 06:49:45 GMT+0000 (Coordinated Universal Time)
Saved by
@vaibhav_55
vector graph[100001];
bool visited[100001];
//DFS traversal function of graph
/*time complexity = O(n+m)
where,n = no of vertices and m = no of edges
-->Stack data structure is used
*/
void dfs(int s)
{
if (visited[s])
return;
visited[s] = true;
cout << s << " ";
for (int i = 0; i < graph[s].size(); ++i)
{
if (visited[graph[s][i]] == false)
dfs(graph[s][i]);
}
}
content_copyCOPY
Comments