lowest_common_ancestors

lowest_common_ancestors#

graph_tool.structure.lowest_common_ancestors(g, pairs)[git][source]#

Return the lowest common ancestors of pairs of vertices of a rooted tree or of a directed acyclic graph.

Parameters:
gGraph

Directed acyclic graph, with the edges pointing away from the roots.

pairsiterable of pairs of int

The pairs of vertices.

Returns:
lcanumpy.ndarray

The lowest common ancestor of every pair, or -1 if the two vertices have none.

See also

dominator_tree

Immediate dominator of every vertex

topological_sort

Topological ordering of a directed acyclic graph

Notes

Every vertex is its own ancestor. If every vertex has at most one in-edge (a rooted forest), the ancestors are found by binary lifting, in time \(O(V\log V)\) for the preprocessing and \(O(\log V)\) per pair [bender-lca-2000]. Otherwise, the common ancestors of each pair are found by backward searches, in time \(O(V + E)\) per pair (with the pairs processed in parallel), and the one that comes last in topological order is returned, which has no other common ancestor as a descendant (there may be several such vertices).

References

[bender-lca-2000]

Michael A. Bender and Martín Farach-Colton, “The LCA problem revisited”, LATIN 2000: Theoretical Informatics, Lecture Notes in Computer Science 1776, 88-94 (2000), DOI: 10.1007/10719839_9 [sci-hub, @tor]

Examples

>>> g = gt.Graph([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5)])
>>> print(gt.lowest_common_ancestors(g, [(3, 4), (3, 5), (4, 1)]))
[1 0 1]