Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The lca package #597

Merged
merged 39 commits into from
Aug 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1a449c3
Add interface
AlexandruValeanu May 23, 2018
4fe4eaf
Add interface
AlexandruValeanu May 23, 2018
9e26b03
Update old implementations
AlexandruValeanu May 23, 2018
edf9900
Add EulerTourRMQLCAFinder and tests for all implementations
AlexandruValeanu May 28, 2018
091dc67
Fix bug with disconnected graphs.
AlexandruValeanu May 28, 2018
176e867
Add more tests
AlexandruValeanu May 28, 2018
c6b36be
Fix Java related issues
AlexandruValeanu Jun 6, 2018
3b842e9
Add support for multiple roots
AlexandruValeanu Jun 6, 2018
88b67ed
Add Heavy Path Decomposition
AlexandruValeanu Jun 6, 2018
2336275
Add forest generator
AlexandruValeanu Jun 7, 2018
861b239
Add performance tests
AlexandruValeanu Jun 9, 2018
b3b3eb0
Small fixes
AlexandruValeanu Jun 9, 2018
b1765b0
docs + opt-heavy-path
AlexandruValeanu Jun 9, 2018
da2e4ab
Update docs
AlexandruValeanu Jun 11, 2018
0586b4d
Update heavy-path tests
AlexandruValeanu Jun 12, 2018
6c66f66
Refactor heavy-path decomposition
AlexandruValeanu Jun 12, 2018
1dda979
Fix checkstyle errors
AlexandruValeanu Jun 12, 2018
3b91111
Fix checkstyle errors
AlexandruValeanu Jun 12, 2018
e4b14a0
Merge branch 'lca' of https://github.com/AlexandruValeanu/jgrapht int…
AlexandruValeanu Jun 12, 2018
21aabb1
Fix checkstyle errors
AlexandruValeanu Jun 12, 2018
fefe40e
Update docs
AlexandruValeanu Jun 12, 2018
756c6d2
Merge remote-tracking branch 'remotes/upstream/master' into lca
AlexandruValeanu Jun 21, 2018
3a4295f
Update docs
AlexandruValeanu Jun 21, 2018
9a2037b
Merge branch 'master' of https://github.com/jgrapht/jgrapht into lca
AlexandruValeanu Jun 21, 2018
ad24696
Merge branch 'master' of https://github.com/jgrapht/jgrapht into lca
AlexandruValeanu Jun 21, 2018
3c36ba6
Fix imports
AlexandruValeanu Jun 21, 2018
1fabf8b
Fix imports
AlexandruValeanu Jun 21, 2018
72d4292
Update docs + tests
AlexandruValeanu Jun 22, 2018
b00ec07
Fix doc
AlexandruValeanu Jun 23, 2018
ac712d4
Merge branch 'master' of https://github.com/jgrapht/jgrapht into lca
AlexandruValeanu Jul 18, 2018
c238f0e
Add docs and reorganize lca package
AlexandruValeanu Jul 21, 2018
06d917f
Merge branch 'master' of https://github.com/jgrapht/jgrapht into lca
AlexandruValeanu Aug 4, 2018
50002cd
Refactor lca package
AlexandruValeanu Aug 4, 2018
736f4cf
Fix broken javadoc
AlexandruValeanu Aug 4, 2018
71d4dc2
Update documentation of BinaryLifting
AlexandruValeanu Aug 4, 2018
61cb2a4
Rename interface to LowestCommonAncestorAlgorithm
AlexandruValeanu Aug 7, 2018
751066e
Update method names
AlexandruValeanu Aug 7, 2018
8a89b81
Small fixes + documentation update
AlexandruValeanu Aug 13, 2018
7ca0aeb
Restore vanished tests
AlexandruValeanu Aug 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
*/
package org.jgrapht.alg;

import org.jgrapht.*;
import org.jgrapht.Graph;
import org.jgrapht.GraphTests;

import java.util.*;

Expand Down Expand Up @@ -61,8 +62,10 @@
*
* @param <V> the graph vertex type
* @param <E> the graph edge type
*
*
* @deprecated Replaced by {@link org.jgrapht.alg.lca.NaiveLCAFinder}
*/
@Deprecated
public class NaiveLcaFinder<V, E>
{
private Graph<V, E> graph;
Expand Down Expand Up @@ -259,4 +262,4 @@ private V overlappingMember(Set<V> x, Set<V> y)
}
}

// End NaiveLcaFinder.java
// End NaiveLCAFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package org.jgrapht.alg;

import org.jgrapht.*;
import org.jgrapht.alg.util.*;
import org.jgrapht.Graph;
import org.jgrapht.alg.util.UnionFind;

import java.util.*;

Expand All @@ -29,8 +29,10 @@
* @param <E> the graph edge type
*
* @author Leo Crawford
*
* @deprecated Replaced by {@link org.jgrapht.alg.lca.TarjanLCAFinder}
*/
public class TarjanLowestCommonAncestor<V, E>
@Deprecated public class TarjanLowestCommonAncestor<V, E>
{
private Graph<V, E> g;

Expand Down Expand Up @@ -232,4 +234,4 @@ public Set<LcaRequestResponse<V>> getOrCreate(V key)
}
}

// End TarjanLowestCommonAncestor.java
// End TarjanLCAFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
* into a set of disjoint paths.
*
* <p>
* The techniques was first introduced in <i>Sleator, D. D.; Tarjan, R. E. (1983).
* "A Data Structure for Dynamic Trees". Proceedings of the thirteenth annual ACM symposium on Theory of computing
* - STOC '81 doi:10.1145/800076.802464 </i>
* </p>
*
* <p>
* In a heavy path decomposition, the edges set is partitioned into two sets, a set of heavy edges and a
* set of light ones according to the relative number of nodes in the vertex's subtree.
*
Expand Down Expand Up @@ -80,7 +86,7 @@ public class HeavyPathDecomposition<V, E> implements TreeToPathDecompositionAlgo
/**
* Create an instance with a reference to the tree that we will decompose and to the root of the tree.
*
* Note: The constructor will NOT check if the input tree is a valid tree.
* Note: The constructor will NOT check if the input graph is a valid tree.
*
* @param tree the input tree
* @param root the root of the tree
Expand All @@ -94,7 +100,7 @@ public HeavyPathDecomposition(Graph<V, E> tree, V root) {
* forest (one root per tree).
*
* Note: If two roots appear in the same tree, an error will be thrown.
* Note: The constructor will NOT check if the input forest is a valid forest.
* Note: The constructor will NOT check if the input graph is a valid forest.
*
* @param forest the input forest
* @param roots the set of roots of the graph
Expand Down Expand Up @@ -512,5 +518,16 @@ public int[] getPositionInPathArray(){
public int[] getFirstNodeInPathArray(){
return firstNodeInPath;
}

/**
* Return the internal parent array.
* For each vertex $v \in V$, $parentArray[normalizeVertex(v)] = normalizeVertex(u)$ if $getParent(v) = u$ or
* $-1$ if $getParent(v) = null$.
*
* @return internal parent array
*/
public int[] getParentArray(){
return parent;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* (C) Copyright 2018-2018, by Alexandru Valeanu and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* This program and the accompanying materials are dual-licensed under
* either
*
* (a) the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation, or (at your option) any
* later version.
*
* or (per the licensee's choosing)
*
* (b) the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
package org.jgrapht.alg.interfaces;

import org.jgrapht.alg.util.Pair;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Algorithm to compute a <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor">lowest common ancestor</a>
* in a tree, forest or DAG.
*
* @param <V> vertex the graph vertex type
*
* @author Alexandru Valeanu
*/
public interface LowestCommonAncestorAlgorithm<V> {

/**
* Return the LCA of a and b
*
* @param a the first element to find LCA for
* @param b the other element to find the LCA for
*
* @return the LCA of a and b, or null if there is no LCA.
*/
V getLCA(V a, V b);

/**
* Return a list of LCAs for a batch of queries
*
* @param queries a list of pairs of vertices
* @return a list L of LCAs where L(i) is the LCA for pair queries(i)
*/
default List<V> getBatchLCA(List<Pair<V, V>> queries){
return queries.stream().map(p -> getLCA(p.getFirst(), p.getSecond())).collect(Collectors.toList());
}

/**
* Return the computed set of LCAs of a and b
*
* @param a the first element to find LCA for
* @param b the other element to find the LCA for
*
* @return the set LCAs of a and b, or empty set if there is no LCA computed.
* @throws UnsupportedOperationException - if the operation is not supported by the implementing class
*/
Set<V> getLCASet(V a, V b);

/**
* Return a list of computed sets of LCAs for a batch of queries
*
* @param queries a list of pairs of vertices
* @return a list L of LCAs where L(i) is the computed set of LCAs for pair queries(i)
*/
default List<Set<V>> getBatchLCASet(List<Pair<V, V>> queries){
return queries.stream().map(p -> getLCASet(p.getFirst(), p.getSecond())).collect(Collectors.toList());
}
}
Loading