diff --git a/Advanced/dfs.cpp b/Advanced/dfs.cpp new file mode 100644 index 00000000..58e32a40 --- /dev/null +++ b/Advanced/dfs.cpp @@ -0,0 +1,52 @@ +// A Quick implementation of dfs using vectors + +// Its time complexity is O(v+e).where v is number of vertex and e is number of edges. + +#include +#define pb push_back + +using namespace std; + +bool v[10]={0}; +vector g[20]; + +void edge(int a, int b) +{ + g[a].pb(b); + + g[b].pb(a); // for undirected graph add this line +} + +void dfs(int u) +{ + + v[u]=true; + +cout<>n; + cout<<"Number of edges"<>e; + + int a, b; + for (int i = 0; i < e; i++) { + cin >> a >> b; + edge(a, b); + } + dfs(1); + + + return 0; +}