Last active
May 3, 2018 06:05
-
-
Save ryanwarsaw/d58b3bb4ce652807418ad5e738abfe58 to your computer and use it in GitHub Desktop.
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
// 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