Search code examples
algorithmgraphdepth-first-search

A claim regarding DFS, is it true?


While solving questions regarding DFS I have noticed something with I couldn't formally prove or find a contradict example.

let's consider a directed graph g

u, v are in the same SCC (strong connect component) iff u, v are in the same DFS tree in every run of DFS.

is my claim correct?


Solution

  • Yes, it's true.

    If u and v are in the same strongly connected component, then there's paths from u to v, and from v to u. Consider any DFS in which u appears, and consider (for contradiction) the first vertex on the path from u to v that doesn't appear in this run of a DFS. But then there's a vertex on the path just before this one that does appear, and an edge from that vertex to the one that doesn't appear. But that's a contradiction because of how DFS works, so every vertex on the path from u to v appears in the DFS, and so v appears in the DFS. (We can make the same argument with u and v swapped).

    Conversely, suppose in every DFS that u appears v (and vice versa). But then the DFS that starts from u includes v, which means there's a path from u to v. (And again, we can apply symmetry to get the same with u and v swapped).