Skip to content

Instantly share code, notes, and snippets.

@arkanath
Created September 24, 2014 06:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkanath/2df0bf635dd61b801768 to your computer and use it in GitHub Desktop.
Save arkanath/2df0bf635dd61b801768 to your computer and use it in GitHub Desktop.
Depth First Traversal, with Stack
vector<int> adjList[N];
int visited[N];
void dfsstack(int u)
{
stack<int> nstack;//stack used in place of recursion stack
visited[u] = 1;
nstack.push(u);
while(!nstack.empty())
{
int tp = nstack.top();
nstack.pop();
for(int i=0;i<adjList[tp].size();i++)
{
if(!visited[adjList[tp][i]])
{
nstack.push(adjList[tp][i]);
visited[adjList[tp][i]] = 1;
//visited that node, the time has come :)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment