Created
September 24, 2014 06:08
-
-
Save arkanath/2df0bf635dd61b801768 to your computer and use it in GitHub Desktop.
Depth First Traversal, with Stack
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
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