skip to content

What is a strongly connected component, and why does ignoring edge direction give the wrong answer?

level: juniorimportance: must knowfreq 50%

answer

  1. direction is not decoration
  2. reachability has to run both ways
  3. maximal set, not any mutually reachable pair
  4. two vertices, one one-way edge
  5. equivalence classes partition every vertex

basics

~20 s

A strongly connected component is a maximal set of vertices in which every vertex can reach every other by following edge directions. Ignoring direction only proves the vertices hang together somehow; a one-way edge destroys mutual reachability without disconnecting anything.

solid answer

~40 s

Two vertices `u` and `v` belong to the same strongly connected component when there is a directed path `u -> v` **and** a directed path `v -> u`. Mutual reachability is an equivalence relation, so its classes partition the vertex set: every vertex sits in exactly one component, including vertices that lie on no cycle at all, which form singletons. The word *maximal* matters — a mutually reachable pair inside a larger mutually reachable group is not its own component. Dropping edge directions instead answers a much weaker question. Take two services where checkout calls billing and billing never calls back: erasing direction says one blob, but billing cannot reach checkout, so there are two components, not one. Weak connectivity says "joined"; strong connectivity says "joined both ways".

go deeper

for a junior

Be ready to state the definition in one sentence and produce the two-vertex, one-edge counterexample on demand. Know that every vertex lands in exactly one component, singletons included.

for a middle

Explain why mutual reachability being an equivalence relation forces a partition, and why maximality is part of the definition rather than a detail. Know the linear O(V + E) cost of finding all components.

for a senior

Show you spot the wrong question being asked: when someone reports "one connected blob" over a directed call graph, name what was actually computed and what decision it would mislead.

for a principal

Own the framing: strong connectivity is the property that says which parts of a system cannot be reasoned about, versioned, or extracted independently. Be able to explain why weak connectivity is a useless answer for that purpose.

## The definition, stated carefully Work in a **directed graph**: vertices joined by edges that point one way. Vertex `v` is **reachable** from `u` if some sequence of edges leads from `u` to `v`, always travelling in the direction each edge points. Reachability is one-directional; `v` reachable from `u` says nothing about getting back. `u` and `v` are **mutually reachable** when both `u -> ... -> v` and `v -> ... -> u` exist. A **strongly connected component** (SCC) is a *maximal* set of pairwise mutually reachable vertices — maximal meaning you cannot add another vertex to the set and keep the property. ## Why the components partition the graph Mutual reachability is an equivalence relation: - **Reflexive** — every vertex reaches itself by the empty path. - **Symmetric** — the definition already demands both directions. - **Transitive** — if `u` and `v` are mutually reachable and `v` and `w` are, concatenating paths gives `u <-> w`. Equivalence relations carve their domain into disjoint classes, and those classes are exactly the SCCs. Three consequences follow immediately, and each is a place interviews probe: 1. **Every vertex belongs to exactly one component.** Components never overlap and never leave a vertex out. 2. **A vertex on no directed cycle is its own component**, a singleton, because reflexivity alone qualifies it. Candidates who say "that vertex has no component" have quietly swapped the definition for "cycle". 3. **The count of components runs from 1 to V.** Exactly one component means the whole graph is strongly connected: everything reaches everything. Exactly `V` components means no directed cycle exists anywhere — the graph is acyclic. A useful equivalent phrasing: `u` and `v` share a component precisely when some closed directed walk passes through both. Note *walk*, not *simple cycle* — the `u -> v` path and the `v -> u` path may share vertices, so what you get is a closed walk, which is enough. ## The mistake: treating direction as decoration The common wrong move is to erase arrows and run ordinary connected-component labelling. That computes **weak connectivity**: whether the underlying undirected graph hangs together. It is a genuinely different question, and it is strictly weaker. The minimal counterexample is two vertices with one edge, `A -> B`. Erase direction and you see one piece. Respect direction and `B` reaches nothing, so `A` and `B` are two separate components. Scale that up to a call graph of services: an ordering service calls a pricing service, which calls a currency service, which calls nobody. Ignoring direction reports one clump of three. The truth is three components — nothing downstream can call back upstream, and that asymmetry is precisely the information you wanted. The reverse error also appears: assuming that because the graph is *weakly* connected it must be *strongly* connected "if there are enough edges". Edge count is irrelevant; a graph with a million edges arranged as a long one-way chain still has a million-plus singleton components. ## Related but distinct notions - **Weakly connected** — connected once directions are dropped. Implied by strongly connected, never the reverse. - **Reachable from a root** — one traversal from a chosen vertex reaches everything. That is weaker than strong connectivity too, because it says nothing about paths back to the root. - **Acyclic** — no directed cycles, which is the same as "every component is a singleton". ## What it costs Computing all SCCs is a linear-time job, O(V + E), using either a two-pass finish-order method or a one-pass low-link method. That is the same order as a single traversal, which is why an interviewer expects you to reach for it without hesitation rather than proposing anything pairwise. Checking reachability for every pair separately would cost O(V * (V + E)) and is the answer to avoid. ## How to say it in an interview "A strongly connected component is a maximal group of vertices where every vertex reaches every other along directed edges. Mutual reachability is an equivalence relation, so the components partition the vertices — a vertex on no cycle is its own singleton. Ignoring direction answers a weaker question: two vertices joined by a single one-way edge look connected but form two components. Finding all of them is linear, O(V + E)."

  • Can a vertex with no outgoing edges belong to a strongly connected component?
    Yes — it forms a singleton component of its own. Every vertex reaches itself by the empty path, so mutual reachability is reflexive and the components cover all V vertices. Saying such a vertex has no component confuses "component" with "lies on a cycle".
  • How many strongly connected components can a directed graph on V vertices have, and what do the extremes mean?
    Between 1 and V. Exactly one means the graph is strongly connected — every vertex reaches every other. Exactly V means every component is a singleton, which happens precisely when the graph contains no directed cycle at all.
  • If two vertices share a component, what does that tell you about cycles through them?
    Some closed directed walk passes through both: concatenate the path from u to v with the path back from v to u. It need not be a simple cycle, since the two paths may reuse vertices, but a closed walk through both always exists.

saying these in an interview costs you the question

  • Says edge direction does not matter for connectivity
  • Calls any mutually reachable pair a component, ignoring maximality
  • Claims a vertex on no cycle belongs to no component
  • Treats weakly connected and strongly connected as the same thing
  • Thinks components can overlap or share vertices

context