PrepStack · Markdown HTML

DFS components and reachability

Traversal and connectivity · HTML

Formal shape

For a graph G=(V,E) and eligible set A, start DFS from every unvisited v in A. The vertices reached by one launch are exactly v's connected component.

Recognition signals

False friends

SignalWhy it is different
Unweighted shortest pathThe first DFS arrival need not use the fewest edges; use bfs-unweighted-shortest.
Dependency orderingDirected prerequisites require indegrees or recursion colors, not plain component traversal.

Core invariant

Once marked visited, a vertex belongs to the component represented by the current search and can never join a later component.

State, transition, and processing order

ItemDefinition
StateCurrent vertex u, global visited set, and an optional component accumulator such as size, sum, or boundary flags.
TransitionFor every neighbor v of u, if v is eligible and unseen, mark it before recursing or pushing it.
Frontier / orderThe call stack or an explicit LIFO stack of discovered vertices whose adjacency is not fully expanded.

Python skeleton

def components(graph):
    seen, groups = set(), []
    for start in graph:
        if start in seen:
            continue
        seen.add(start)
        stack, group = [start], []
        while stack:
            u = stack.pop()
            group.append(u)
            for v in graph[u]:
                if v not in seen:
                    seen.add(v)
                    stack.append(v)
        groups.append(group)
    return groups

Correctness sketch

  1. Soundness: every pushed vertex is connected to start through actual graph edges.
  2. Completeness: choose a path to any reachable vertex; inductively, expanding each predecessor discovers the next vertex.
  3. Partition: marking on discovery permits at most one push per vertex, while the outer loop launches exactly for vertices not assigned earlier.

Complexity

TimeSpaceParameters
O(V+E)O(V)V is the number of vertices and E the number of stored adjacency entries.

Edge cases

Error patterns

PatternWhy it fails / fix
Marking only after popSeveral parents can push the same vertex; mark it when discovered.
Searching from one source onlyThat finds one component, not a partition of the entire graph.
Wrong edge directionOmitting reverse edges for an undirected graph silently turns connectivity into one-way reachability.

Selection and exclusion rules

RelationTemplateBoundary
recursive reversal alternativeLinked-list pointer rewiringRewiring on recursive return is acceptable with O(n) call-stack space.
traversal-foundationTree dynamic programmingDFS supplies parent tracking and postorder.
dynamic-connectivity alternativeUnion-Find dynamic connectivityEdges arrive over time and connectivity is queried repeatedly.

Original micro-example

PartDetail
ResultThe components are {p,q,r} and {s}.
Setupp—q—r are linked; s is isolated.
TraceStarting at p discovers q and r. The outer loop skips them and later launches from s.

Recall prompts

My notes