Properties of DFS

Dependencies:

  1. Graph
  2. Transpose of a graph
  3. Forest
  4. Rooted tree

Depth-first search (DFS) is an algorithm which systematically explores a graph $G = (V, E)$.

It returns a DFS forest of $G$ and discovery-start-time and discovery-finish-time for each vertex.

The algorithm

DFS maintains 5 attributes for every vertex:

Initially, for every vertex $v$, $s(v) = f(v) = 0$, $\pi(v) = \textrm{null}$, $\color(v) = \textrm{white}$, and $\depth(v) = 0$.

Let $\adj(u) = \{v \in V: (u, v) \in E\}$.

DFS algorithm on $G$:

for u in G.V:
    color[u] = white
    π[u] = null
time = 0

def visit(u, d):
    assert(color[u] == white)
    depth[u] = d
    color[u] = gray
    time += 1
    s[u] = time
    for v in adj[u]:
        if color[v] == white:
            π[v] = u
            visit(v, d+1)
    color[u] = black
    time += 1
    f[u] = time

for u in G.V:
    if color[u] == white:
        visit(u, 0)

As visit(u) iterates over adj[u], we say that the edge $(u, v)$ is explored for $v \in \adj(u)$.

Vertices with depth 0 are called root vertices. When DFS terminates, root vertices are the only vertices to have $\pi(u) = \textrm{null}$, since $\pi(v)$ is set to a vertex before all non-root visit(v) calls.

When DFS terminates, $\pi$ can be interpreted as a graph $G_π = (V, E_π)$ where $(u, v) \in E_π \iff π(v) = u \neq \textrm{null}$. Since $\pi(v)$ is set to $u$ only for $v \in \adj(u)$, $G_π \subseteq G$. Therefore, $G_π$ is called the predecessor subgraph of $G$.

Properties of DFS

Dependency for:

  1. DFS: Parenthesis theorem
  2. DFS: Edge classification
  3. DFS: White-path theorem
  4. DFS: low

Info:

Transitive dependencies:

  1. /sets-and-relations/equivalence-classes
  2. /sets-and-relations/relation-composition-is-associative
  3. /sets-and-relations/equivalence-relation
  4. Graph
  5. Transpose of a graph
  6. Path in Graph
  7. Rooted tree
  8. Forest