Skip to content

Instantly share code, notes, and snippets.

@awave1
Created February 20, 2019 02:11
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 awave1/179a61b13cbd80c5126e4973cca55106 to your computer and use it in GitHub Desktop.
Save awave1/179a61b13cbd80c5126e4973cca55106 to your computer and use it in GitHub Desktop.
bool Graph::dfs_util(int v, int *colors) {
colors[v] = GRAY;
for (auto *u : n_map[v]->adj) {
if (colors[u->name] == GRAY) {
return true;
} else if (colors[u->name] == WHITE && dfs_util(u->name, colors)) {
return true;
}
}
colors[v] = BLACK;
return false;
}
bool Graph::has_cycle() {
int *colors = new int[n_map.size()];
for (int i = 0; i < n_map.size(); i++) {
colors[i] = WHITE;
}
for (int i = 0; i < n_map.size(); i++) {
if (colors[i] == WHITE && dfs_util(i, colors)) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment