Skip to content

Commit

Permalink
Merge pull request jgrapht#652 from d-michail/jheaps-dependency
Browse files Browse the repository at this point in the history
Added jheaps dependency
  • Loading branch information
jsichi authored Aug 3, 2018
2 parents 384ca0b + d0ad0cb commit 31193fe
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 36 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ A local copy of the Javadoc HTML files is included in the distribution. The late
## Dependencies ##

- JGraphT requires JDK 1.8 or later to build starting with version 1.0.0.
- [JHeaps](http://www.jheaps.org/) is a library with priority queues. JHeaps is licensed under the terms of the Apache License, Version 2.0.
- [JUnit](http://www.junit.org) is a unit testing framework. You need JUnit only if you want to run the unit tests. JUnit is licensed under the terms of the IBM Common Public License. The JUnit tests included with JGraphT have been created using JUnit 4.
- [XMLUnit](http://xmlunit.sourceforge.net) extends JUnit with XML capabilities. You need XMLUnit only if you want to run the unit tests. XMLUnit is licensed under the terms of the BSD License.
- [JGraphX](http://www.jgraph.com/jgraph.html) is a graph visualizations and editing component (the successor to the older JGraph library). You need JGraphX only if you want to use the JGraphXAdapter to visualize the JGraphT graph interactively via JGraphX. JGraphX is licensed under the terms of the BSD license.
- [Touchgraph](http://sourceforge.net/projects/touchgraph) is a graph visualization and layout component. You need Touchgraph only if you want to create graph visualizations using the JGraphT-to-Touchgraph converter. Touchgraph is licensed under the terms of an Apache-style License.
- [ANTLR](http://www.antlr.org) is a parser generator. It is used for reading text files containing graph representations, and is only required by the jgrapht-io module. ANTLR v4 is licensed under the terms of the [BSD license](http://www.antlr.org/license.html).
- [Guava](https://github.com/google/guava) is Google's core libraries for Java. You need Guava only if you are already using Guava's graph data-structures and wish to use our adapter classes in order to execute JGraphT's algorithms.
- [Guava](https://github.com/google/guava) is Google's core libraries for Java. You need Guava only if you are already using Guava's graph data-structures and wish to use our adapter classes in order to execute JGraphT's algorithms. Only required by the jgrapht-guava module.

## Online Resources ##

Expand Down
42 changes: 41 additions & 1 deletion jgrapht-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
</license>
</licenses>
<dependencies>
<dependency>
<groupId>org.jheaps</groupId>
<artifactId>jheaps</artifactId>
<version>0.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -33,7 +38,7 @@
<dependency>
<groupId>com.googlecode.junit-toolbox</groupId>
<artifactId>junit-toolbox</artifactId>
<version>2.4</version>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -51,6 +56,41 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<!-- Use unique OSGi bundle symbolic name for uber artifact. Otherwise
e.g. jgrapht-core-x.y.z.jar and jgrapht-core-x.y.z-uber.jar could not be deployed
to the same P2 repository. -->
<Bundle-SymbolicName>org.jgrapht.core.uber</Bundle-SymbolicName>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/jheaps-license</resource>
<file>${main.basedir}/etc/licenses/apache-license-2.0.txt</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer">
<addHeader>false</addHeader>
</transformer>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>uber</shadedClassifierName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,26 @@
*/
package org.jgrapht.alg.matching;

import org.jgrapht.*;
import org.jgrapht.alg.interfaces.*;
import org.jgrapht.util.*;

import java.math.*;
import java.util.*;
import java.math.BigDecimal;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

import org.jgrapht.Graph;
import org.jgrapht.GraphTests;
import org.jgrapht.Graphs;
import org.jgrapht.alg.interfaces.MatchingAlgorithm;
import org.jheaps.AddressableHeap;
import org.jheaps.tree.FibonacciHeap;

/**
* Maximum weight matching in bipartite graphs.
Expand Down Expand Up @@ -51,15 +65,16 @@ public class MaximumWeightBipartiteMatching<V, E>
private final Set<V> partition2;

private final Comparator<BigDecimal> comparator;
private final Function<Comparator<BigDecimal>, AddressableHeap<BigDecimal,V>> heapSupplier;

// vertex potentials
private Map<V, BigDecimal> pot;
// the matched edge of a vertex, also used to check if a vertex is free
private Map<V, E> matchedEdge;

// shortest path related data structures
private GenericFibonacciHeap<BigDecimal, V> heap;
private Map<V, GenericFibonacciHeap<BigDecimal, V>.Node> nodeInHeap;
private AddressableHeap<BigDecimal, V> heap;
private Map<V, AddressableHeap.Handle<BigDecimal, V>> nodeInHeap;
private Map<V, E> pred;
private Map<V, BigDecimal> dist;

Expand All @@ -76,11 +91,26 @@ public class MaximumWeightBipartiteMatching<V, E>
* @throws IllegalArgumentException if the graph is not undirected
*/
public MaximumWeightBipartiteMatching(Graph<V, E> graph, Set<V> partition1, Set<V> partition2)
{
this(graph, partition1, partition2, (comparator) -> new FibonacciHeap<>(comparator));
}

/**
* Constructor.
*
* @param graph the input graph
* @param partition1 the first partition of the vertex set
* @param partition2 the second partition of the vertex set
* @param heapSupplier a supplier for the addressable heap to use in the algorithm.
* @throws IllegalArgumentException if the graph is not undirected
*/
public MaximumWeightBipartiteMatching(Graph<V, E> graph, Set<V> partition1, Set<V> partition2, Function<Comparator<BigDecimal>, AddressableHeap<BigDecimal,V>> heapSupplier)
{
this.graph = GraphTests.requireUndirected(graph);
this.partition1 = Objects.requireNonNull(partition1, "Partition 1 cannot be null");
this.partition2 = Objects.requireNonNull(partition2, "Partition 2 cannot be null");
this.comparator = Comparator.<BigDecimal> naturalOrder();
this.heapSupplier = Objects.requireNonNull(heapSupplier, "Heap supplier cannot be null");
}

/**
Expand Down Expand Up @@ -112,7 +142,7 @@ public Matching<V, E> getMatching()
pot = new HashMap<>();
dist = new HashMap<>();
matchedEdge = new HashMap<>();
heap = new GenericFibonacciHeap<>(comparator);
heap = heapSupplier.apply(comparator);
nodeInHeap = new HashMap<>();
pred = new HashMap<>();
graph.vertexSet().forEach(v -> {
Expand Down Expand Up @@ -192,7 +222,7 @@ private void augment(V a)
pred.put(b1, e1);
reachedB.push(b1);

GenericFibonacciHeap<BigDecimal, V>.Node node = heap.insert(db1, b1);
AddressableHeap.Handle<BigDecimal, V> node = heap.insert(db1, b1);
nodeInHeap.put(b1, node);
} else {
if (comparator.compare(db1, dist.get(b1)) < 0) {
Expand All @@ -211,7 +241,7 @@ private void augment(V a)
V b = null;
BigDecimal db = BigDecimal.ZERO;
if (!heap.isEmpty()) {
b = heap.removeMin().getData();
b = heap.deleteMin().getValue();
nodeInHeap.remove(b);
db = dist.get(b);
}
Expand Down Expand Up @@ -252,8 +282,7 @@ private void augment(V a)
pred.put(b1, e1);
reachedB.push(b1);

GenericFibonacciHeap<BigDecimal, V>.Node node =
heap.insert(db1, b1);
AddressableHeap.Handle<BigDecimal, V> node = heap.insert(db1, b1);
nodeInHeap.put(b1, node);
} else {
if (comparator.compare(db1, dist.get(b1)) < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@
*/
package org.jgrapht.alg.shortestpath;

import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import org.jgrapht.Graph;
import org.jgrapht.GraphPath;
import org.jgrapht.Graphs;
import org.jgrapht.graph.GraphWalk;
import org.jheaps.Heap;
import org.jheaps.array.DaryArrayHeap;

/**
* Martin's algorithm for the multi-objective shortest paths problem.
Expand Down Expand Up @@ -52,8 +63,8 @@ public class MartinShortestPath<V, E>
// final labels for each node
private final Map<V, LinkedList<Label>> nodeLabels;
// temporary labels ordered lexicographically
private final GenericFibonacciHeap<Label, Void> heap;

private final Heap<Label> heap;
/**
* Create a new shortest path algorithm
*
Expand All @@ -67,9 +78,9 @@ public MartinShortestPath(Graph<V, E> graph, Function<E, double[]> edgeWeightFun
Objects.requireNonNull(edgeWeightFunction, "Function cannot be null");
this.objectives = validateEdgeWeightFunction(edgeWeightFunction);
this.nodeLabels = new HashMap<>();
this.heap = new GenericFibonacciHeap<>(new LabelComparator());
this.heap = new DaryArrayHeap<>(3, new LabelComparator());
}

@Override
public List<GraphPath<V, E>> getPaths(V source, V sink)
{
Expand Down Expand Up @@ -107,10 +118,10 @@ private void runAlgorithm(V source)
nodeLabels.put(v, new LinkedList<>());
}
nodeLabels.get(source).add(sourceLabel);
heap.insert(sourceLabel, null);
heap.insert(sourceLabel);

while (!heap.isEmpty()) {
Label curLabel = heap.removeMin().getKey();
Label curLabel = heap.deleteMin();
V v = curLabel.node;
for (E e : graph.outgoingEdgesOf(v)) {
V u = Graphs.getOppositeVertex(graph, e, v);
Expand All @@ -132,7 +143,7 @@ private void runAlgorithm(V source)
}
if (!isDominated) {
uLabels.add(newLabel);
heap.insert(newLabel, null);
heap.insert(newLabel);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
*
* @author Nathan Fiedler
* @author Dimitrios Michail
*
* @deprecated In favor of using data structures from the jheaps library.
*/
@Deprecated
public class GenericFibonacciHeap<K, T>
{
private static final double ONEOVERLOGPHI = 1.0 / Math.log((1.0 + Math.sqrt(5.0)) / 2.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
*/
package org.jgrapht.alg.shortestpath;

import org.jgrapht.*;
import org.jgrapht.alg.interfaces.MultiObjectiveShortestPathAlgorithm.*;
import org.jgrapht.graph.*;
import org.junit.*;
import static org.junit.Assert.assertEquals;

import java.util.*;
import java.util.stream.*;
import java.util.List;
import java.util.stream.IntStream;

import static org.junit.Assert.*;
import org.jgrapht.GraphPath;
import org.jgrapht.alg.interfaces.MultiObjectiveShortestPathAlgorithm.MultiObjectiveSingleSourcePaths;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DefaultEdgeFunction;
import org.jgrapht.graph.DirectedPseudograph;
import org.junit.Test;

/**
* Test {@link MartinShortestPath}.
Expand Down Expand Up @@ -105,5 +107,5 @@ public void testNoPaths()
List<GraphPath<Integer, DefaultEdge>> paths22 = paths2.getPaths(2);
assertEquals(1, paths22.size());
}

}
6 changes: 6 additions & 0 deletions jgrapht-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<groupId>${project.groupId}</groupId>
<artifactId>jgrapht-core</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jgrapht-core</artifactId>
<classifier>uber</classifier>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jgrapht-io</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions jgrapht-dist/src/assembly/assembly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
<outputDirectory>3rd-party-licenses</outputDirectory>
<destName>commons-lang3-license.txt</destName>
</file>
<file>
<source>${main.basedir}/etc/licenses/apache-license-2.0.txt</source>
<outputDirectory>3rd-party-licenses</outputDirectory>
<destName>jheaps-license.txt</destName>
</file>
</files>

<fileSets>
Expand Down

0 comments on commit 31193fe

Please sign in to comment.