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, π(v)=null, color(v)=white, and depth(v)=0.

Let adj(u)={vV:(u,v)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 vadj(u).

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

When DFS terminates, π can be interpreted as a graph Gπ=(V,Eπ) where (u,v)Eππ(v)=unull. Since π(v) is set to u only for vadj(u), Gπ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