ArticleslgStudy

computer science

Tarjan's off-line lowest common ancestors algorithm

Tarjan's off-line lowest common ancestors 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 off-line lowest common ancestors algorithm rather than just read about it. In short: In computer science, Tarjan's off-line lowest common ancestors algorithm is an algorithm for computing lowest common ancestors for pairs of nodes in a tree, based on the union-find data structure. The lowest common ancestor of two nodes d and e in a rooted tree T is the node g that is an ancestor of both d and e and that has the greatest depth in T.

Key takeaways

  • Tarjan's off-line lowest common ancestors 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 off-line lowest common ancestors algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Tarjan's off-line lowest common ancestors algorithm from memory before moving on to harder problems.

Reference excerpt

In computer science, Tarjan's off-line lowest common ancestors algorithm is an algorithm for computing lowest common ancestors for pairs of nodes in a tree, based on the union-find data structure. The lowest common ancestor of two nodes d and e in a rooted tree T is the node g that is an ancestor of both d and e and that has the greatest depth in T. It is named after Robert Tarjan, who discovered the technique in 1979. Tarjan's algorithm is an offline algorithm; that is, unlike other lowest common ancestor algorithms, it requires that all pairs of nodes for which the lowest common ancestor is desired must be specified in advance. The simplest version of the algorithm uses the union-find data structure, which unlike other lowest common ancestor data structures can take more than constant time per operation when the number of pairs of nodes is similar in magnitude to the number of nodes. A later refinement by Gabow & Tarjan (1983) speeds the algorithm up to linear time.

Pseudocode The pseudocode below determines the lowest common ancestor of each pair in P, given the root r of a tree in which the children of node n are in the set n.children. For this offline algorithm, the set P must be specified in advance. It uses the MakeSet, Find, and Union functions of a disjoint-set data structure. MakeSet(u) removes u to a singleton set, Find(u) returns the standard representative of the set containing u, and Union(u,v) merges the set containing u with the set containing v. TarjanOLCA(r) is first called on the root r.

function TarjanOLCA(u) is MakeSet(u) u.ancestor := u for each v in u.children do TarjanOLCA(v) Union(u, v) Find(u).ancestor := u u.color := black for each v such that {u, v} in P do if v.color == black then print "Tarjan's Lowest Common Ancestor of " + u + " and " + v + " is " + Find(v).ancestor + "."

Each node is initially white, and is colored black after it and all its children have been visited. For each node pair {u,v} to be investigated:

When v is already black (viz. when v comes before u in a post-order traversal of the tree): After u is colored black, the lowest common ancestor of this pair is available as Find(v).ancestor, but only while the LCA of u and v is not colored black. Otherwise: Once v is colored black, the LCA will be available as Find(u).ancestor, while the LCA is not colored black. According to Tarjan (1979) the time complexity is O((m + n)a(m + n, n)), where m is the number of edges and n the number of vertices and a is the inverse Ackerman function, provided that the time to find the vertex pairs corresponding to u takes constant time per vertex. The paper recommends using an adjacency list (called adjacency structure in the paper). For reference, here are optimized versions of MakeSet, Find, and Union for a disjoint-set forest:

function MakeSet(x) is x.parent := x x.rank := 1 function Union(x, y) is xRoot := Find(x) yRoot := Find(y) if xRoot.rank > yRoot.rank then yRoot.parent := xRoot else if xRoot.rank < yRoot.rank then xRoot.parent := yRoot else if xRoot.rank == yRoot.rank then yRoot.parent := xRoot xRoot.rank := xRoot.rank + 1 function Find(x) is if x.parent != x then x.parent := Find(x.parent) return x.parent

Speeding up the algorithm It is possible to preprocess the input LCA queries in such a manner, that the algorithm works faster by an order of magnitude.

function Preprocess(P) is m := empty map for each {u, v} in P do if u is not mapped in m m[u] := ∅ m[u] := m[u] ∪ {(u, v)} if v is not mapped in m m[v] := ∅ m[v] := m[v] ∪ {(u, v)} return m

function GetOpposite(q, u) is if q[0] == u then return q[1] return q[0]

function FasterTarjanOLCA(u, m) is MakeSet(u) u.ancestor := u for each v in u.children do FasterTarjanOLCA(v) Union(u, v) Find(u).ancestor := u u.color := black if m[u] == nil then return for each q in m[u] do v := GetOpposite(q, u) if v != nil and v.color == black then print "LCA of " + u + " and " + v + " is " + Find(v).ancestor + "."

The idea in the optimization is associating nodes with their counterparts in the list of input queries.

References Gabow, H. N.; Tarjan, R. E. (1983), "A linear-time algorithm for a special case of disjoint set union", Proceedings of the 15th ACM Symposium on Theory of Computing (STOC), pp. 246–251, doi:10.1145/800061.808753, ISBN 0-89791-099-0. Tarjan, R. E. (1979), "Applications of path compression on balanced trees", Journal of the ACM, 26 (4): 690–715, doi:10.1145/322154.322161.

External links Optimized Java implementation

Worked examples

Example 1 — a first encounter with Tarjan's off-line lowest common ancestors algorithm

Start with the simplest possible case. Write down what Tarjan's off-line lowest common ancestors 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 off-line lowest common ancestors 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 off-line lowest common ancestors 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 off-line lowest common ancestors algorithm

In research
Tarjan's off-line lowest common ancestors 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 off-line lowest common ancestors 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 off-line lowest common ancestors algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Graph algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Tarjan's off-line lowest common ancestors 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.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Tarjan's off-line lowest common ancestors algorithm” →

Affiliate

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

How to study Tarjan's off-line lowest common ancestors algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Tarjan's off-line lowest common ancestors 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 off-line lowest common ancestors algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Tarjan's off-line lowest common ancestors algorithm in simple terms?

In computer science, Tarjan's off-line lowest common ancestors algorithm is an algorithm for computing lowest common ancestors for pairs of nodes in a tree, based on the union-find data structure. The lowest common ancestor of two nodes d and e in a rooted tree T is the node g that is an ancestor o…

Why does Tarjan's off-line lowest common ancestors 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 off-line lowest common ancestors 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 off-line lowest common ancestors algorithm.

Tags

  • Graph algorithms

Keep exploring