Skip to content

Instantly share code, notes, and snippets.

@ryanwarsaw
Last active May 3, 2018 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanwarsaw/d58b3bb4ce652807418ad5e738abfe58 to your computer and use it in GitHub Desktop.
Save ryanwarsaw/d58b3bb4ce652807418ad5e738abfe58 to your computer and use it in GitHub Desktop.
// TODO: Write a comment here.
void Dgraph::Dfs(int vertex) {
auto *has_been_visited = new bool[this->vertices_];
// set them all to unvisited.
for (int index = 0; index < this->vertices_; index++) {
has_been_visited[index] = false;
}
has_been_visited[vertex] = true;
stack<int> stack;
stack.push(vertex);
cout << vertex << " ";
while(!stack.empty()) {
int *index = nullptr;
list<int>::iterator iterator;
for (iterator = this->list_[stack.top()].begin();
iterator != this->list_[stack.top()].end();
iterator++) {
if (!has_been_visited[*iterator] && index == nullptr) {
index = &*iterator;
}
}
if (index == nullptr) {
stack.pop(); // No such vertex available.
} else {
has_been_visited[*index] = true;
cout << *index << " ";
stack.push(*index);
}
index = nullptr;
}
delete[] has_been_visited;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment