- java.lang.Object
-
- org.jgrapht.alg.lca.TarjanLCAFinder<V,E>
-
- Type Parameters:
V- the graph vertex typeE- the graph edge type
- All Implemented Interfaces:
LowestCommonAncestorAlgorithm<V>
public class TarjanLCAFinder<V,E> extends java.lang.Object implements LowestCommonAncestorAlgorithm<V>
Tarjan's offline algorithm for computing lowest common ancestors in rooted trees and forests.See the article on wikipedia for more information on the algorithm.
The original algorithm can be found in 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
Preprocessing Time complexity: $O(1)$
Preprocessing Space complexity: $O(1)$
Query Time complexity: $O(|V| log^{*}(|V|) + |Q|)$ where $|Q|$ is the number of queries
Query Space complexity: $O(|V| + |Q|)$ where $|Q|$ is the number of queries
For small (i.e. less than 100 vertices) trees or forests, all implementations behave similarly. For larger trees/forests with less than 50,000 queries you can use either
BinaryLiftingLCAFinder,HeavyPathLCAFinderorEulerTourRMQLCAFinder. Fo more than that useEulerTourRMQLCAFindersince it provides $O(1)$ per query.
Space-wise,HeavyPathLCAFinderandTarjanLCAFinderonly use a linear amount whileBinaryLiftingLCAFinderandEulerTourRMQLCAFinderrequire linearithmic space.
For DAGs, useNaiveLCAFinder.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<V,V>ancestorsprivate java.util.Set<V>blackNodesprivate Graph<V,E>graphprivate java.util.List<V>lowestCommonAncestorsprivate java.util.List<Pair<V,V>>queriesprivate java.util.HashMap<V,java.util.Set<java.lang.Integer>>queryOccursprivate java.util.Set<V>rootsprivate UnionFind<V>unionFind
-
Constructor Summary
Constructors Constructor Description TarjanLCAFinder(Graph<V,E> graph, java.util.Set<V> roots)Construct a new instance of the algorithm.TarjanLCAFinder(Graph<V,E> graph, V root)Construct a new instance of the algorithm.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidclear()private java.util.List<V>computeTarjan(java.util.List<Pair<V,V>> queries)private voidcomputeTarjanOLCA(V u, V p, java.util.Set<V> visited)java.util.List<V>getBatchLCA(java.util.List<Pair<V,V>> queries)Return a list of LCAs for a batch of queriesVgetLCA(V a, V b)Return the LCA of a and bjava.util.Set<V>getLCASet(V a, V b)Note: This operation is not supported.
Return the computed set of LCAs of a and bprivate voidinitialize()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jgrapht.alg.interfaces.LowestCommonAncestorAlgorithm
getBatchLCASet
-
-
-
-
Constructor Detail
-
TarjanLCAFinder
public TarjanLCAFinder(Graph<V,E> graph, V root)
Construct a new instance of the algorithm.Note: The constructor will NOT check if the input graph is a valid tree.
- Parameters:
graph- the input graphroot- the root of the graph
-
TarjanLCAFinder
public TarjanLCAFinder(Graph<V,E> graph, java.util.Set<V> roots)
Construct a new instance of the algorithm.Note: If two roots appear in the same tree, an error will be thrown.
Note: The constructor will NOT check if the input graph is a valid forest.
- Parameters:
graph- the input graphroots- the set of roots of the graph
-
-
Method Detail
-
getLCA
public V getLCA(V a, V b)
Return the LCA of a and b- Specified by:
getLCAin interfaceLowestCommonAncestorAlgorithm<V>- Parameters:
a- the first element to find LCA forb- the other element to find the LCA for- Returns:
- the LCA of a and b, or null if there is no LCA.
-
getBatchLCA
public java.util.List<V> getBatchLCA(java.util.List<Pair<V,V>> queries)
Return a list of LCAs for a batch of queries- Specified by:
getBatchLCAin interfaceLowestCommonAncestorAlgorithm<V>- Parameters:
queries- a list of pairs of vertices- Returns:
- a list L of LCAs where L(i) is the LCA for pair queries(i)
-
initialize
private void initialize()
-
clear
private void clear()
-
getLCASet
public java.util.Set<V> getLCASet(V a, V b)
Note: This operation is not supported.
Return the computed set of LCAs of a and b- Specified by:
getLCASetin interfaceLowestCommonAncestorAlgorithm<V>- Parameters:
a- the first element to find LCA forb- the other element to find the LCA for- Returns:
- the set LCAs of a and b, or empty set if there is no LCA computed.
- Throws:
java.lang.UnsupportedOperationException- if the method is called
-
-