ArticleslgStudy

computer science

Tarjan's strongly connected components algorithm

Tarjan's strongly connected components algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Tarjan's strongly connected components algorithm rather than just read about it. In short: Tarjan's strongly connected components algorithm is an algorithm in graph theory for finding the strongly connected components (SCCs) of a directed graph. It runs in linear time, matching the time bound for alternative methods including Kosaraju's algorithm and the path-based strong component algorithm.

Tarjan's strongly connected components algorithm — main illustration
Tarjan's strongly connected components algorithm — illustration

Key takeaways

  • Tarjan's strongly connected components algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Tarjan's strongly connected components algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Tarjan's strongly connected components algorithm from memory before moving on to harder problems.

Reference excerpt

Tarjan's strongly connected components algorithm is an algorithm in graph theory for finding the strongly connected components (SCCs) of a directed graph. It runs in linear time, matching the time bound for alternative methods including Kosaraju's algorithm and the path-based strong component algorithm. The algorithm is named for its inventor, Robert Tarjan.

Overview The algorithm takes a directed graph as input, and produces a partition of the graph's vertices into the graph's strongly connected components. Each vertex of the graph appears in exactly one of the strongly connected components. Any vertex that is not on a directed cycle forms a strongly connected component all by itself—i.e., any vertex whose in-degree or out-degree is 0, or every vertex of a directed acyclic graph. The basic idea of the algorithm is this: a depth-first search (DFS) begins from an arbitrary start node (and subsequent depth-first searches are conducted on any nodes that have not yet been found). As usual with depth-first search, the search visits every node of the graph exactly once, refusing to revisit any node that has already been visited. Thus, the collection of search trees is a spanning forest of the graph. The strongly connected components will be recovered as certain subtrees of this forest. The roots of these subtrees are called the "roots" of the strongly connected components. Any node of a strongly connected component might serve as a root, if it happens to be the first node of a component that is discovered by search.

Stack invariant The root of a strongly connected component with respect to a depth first search traversal is the first node in the component visited by the depth first search component. The root is therefore the last node in the component that is backtracked out of during the traversal. The key idea used in Tarjans algorithm is that a root can also be expressed as a node from which no previously visited node is reachable. As in standard depth first search, nodes are placed on a stack in the order in which they are visited. Unlike in depth-first search, when the depth-first search recursively visits a node v and its descendants, those nodes are not all necessarily popped from the stack when this recursive call returns. The crucial invariant property is that a node remains on the stack after it has been visited if and only if there exists a path in the input graph from it to some node earlier on the stack, and nodes are removed when backtracking out of a root. In other words, a node is only removed from the DFS stack when all of its connected paths have been traversed. At the end of the call that visits v and its descendants, we know whether v itself has a path to any node earlier on the stack. If so, the call returns, leaving v on the stack to preserve the invariant. If not, then v must be the root of its strongly connected component, which consists of v together with any nodes later on the stack than v (such nodes all have paths back to v but not to any earlier node, because if they had paths to earlier nodes then v would also have paths to earlier nodes which is false). The connected component rooted at v is then popped from the stack and returned, again preserving the invariant.

Bookkeeping Each node v is assigned a unique integer v.index, which numbers the nodes consecutively in the order in which they are discovered. It also maintains a value v.lowlink that represents the smallest index of any node on the stack known to be reachable from v through v's DFS subtree, including v itself. Therefore v must be left on the stack if v.lowlink < v.index, whereas v must be removed as the root of a strongly connected component if v.lowlink == v.index. The value v.lowlink is computed during the depth-first search from v, as this finds the nodes that are reachable from v. The lowlink is different from the lowpoint, which is the smallest index reachable from v through any part of the graph.

The algorithm in pseudocode algorithm tarjan is input: graph G = (V, E) output: set of strongly connected components (sets of vertices) index := 0 S := empty stack for each v in V do if v.index is undefined then strongconnect(v) function strongconnect(v) // Set the depth index for v to the smallest unused index v.index := index v.lowlink := index index := index + 1 S.push(v) v.onStack := true // Consider successors of v for each (v, w) in E do if w.index is undefined then // Successor w has not yet been visited; recurse on it strongconnect(w) v.lowlink := min(v.lowlink, w.lowlink) else if w.onStack then // Successor w is in stack S and hence in the current SCC // If w is not on stack, then (v, w) is an edge pointing to an SCC already found and must be ignored // See below regarding the next line v.lowlink := min(v.lowlink, w.index) // If v is a root node, pop the stack and generate an SCC if v.lowlink = v.index then start a new strongly connected component repeat w := S.pop() w.onStack := false add w to current strongly connected component while w ≠ v output the current strongly connected component

… excerpt ends here. Continue reading the full article.

Illustrations

Tarjan's strongly connected components algorithm illustration

Worked examples

Example 1 — a first encounter with Tarjan's strongly connected components algorithm

Start with the simplest possible case. Write down what Tarjan's strongly connected components algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Tarjan's strongly connected components algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Tarjan's strongly connected components algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Tarjan's strongly connected components algorithm

In research
Tarjan's strongly connected components algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Tarjan's strongly connected components algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Tarjan's strongly connected components algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Graph algorithms, Graph connectivity, so understanding it makes those chapters shorter.
In everyday life
Look for Tarjan's strongly connected components algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Tarjan's strongly connected components algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Tarjan's strongly connected components algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Tarjan's strongly connected components algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Tarjan's strongly connected components algorithm in simple terms?

Tarjan's strongly connected components algorithm is an algorithm in graph theory for finding the strongly connected components (SCCs) of a directed graph. It runs in linear time, matching the time bound for alternative methods including Kosaraju's algorithm and the path-based strong component algor…

Why does Tarjan's strongly connected components algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Tarjan's strongly connected components algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Tarjan's strongly connected components algorithm.

Tags

  • Graph algorithms
  • Graph connectivity

Keep exploring