Created
February 20, 2019 02:11
-
-
Save awave1/179a61b13cbd80c5126e4973cca55106 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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