In implementation of DFS and BFS, CLRS authors distinguish 3 colors for each vertex -- grey, black and white. I understand that black and white signify whether node was visited or not. Why do we need grey color?
My guess is to detect cycles, but can we also detect cycles with just black & white (i.e. w/o the grey)?
The main reason is to help the reader understand the concept better. But there are some algorithms that actually make use of grey nodes. For instance for finding cycles in a directed graph you need grey nodes since having a black neighbor doesn't indicate a cycle and only grey neighbors create cycles.
A->B, B->C, A->C
A->C
doesn't create a cycle despite C
being black.