DFS(Depth First Search) Algorithm(Part-3)

DFS(Depth First Search) Algorithm(Part-3)

Posted By 1 year ago

Depth First Search or DFS for a Graph in c++

#include<bits/stdc++.h>
using namespace std;
bool vis[10000];

void dfs(int n,vector<int>adj[])
{

vis[n]=true;
for(int i=0;i<adj[n].size();i++)
{
if(!vis[adj[n][i]])
{
cout<<adj[n][i]<<" ";
dfs(adj[n][i],adj);
}
}
}
int main()
{
int n,e;
cin>>n>>e;
vector<int>adj[20000];
for(int i=0;i<e;i++)
{
int x,y;
cin>>x>>y;
adj[x].push_back(y);

}
int s;
cin>>s;
dfs(s,adj);
}

Blog Topics
  • PHP (php: Hypertext preprocessor)

    8 Article
  • Agile

    2 Article
  • Freelancing

    3 Article
  • JavaScript

    7 Article
  • AngularJs

    1 Article
  • Programming Language C

    22 Article
  • Object Oriented Programming(C++)

    1 Article
  • Algorithm Design

    14 Article
  • Subject

    Python

    682 Article
  • Computer

    756 Article
  • Subject

    Technology Tips & Tricks

    3 Article