You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/11/03 14:17:14 UTC
(commons-graph) branch master updated (e795895 -> 178a2fa)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-graph.git
from e795895 Update .asf.yaml
new 783a9c5 Normalize test method names
new 178a2fa Sort members
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../graph/scc/SCCAlgorithmBenchmarkTestCase.java | 16 +-
.../UniVsBiDijkstraBenchmarkTestCase.java | 46 +-
.../MinimumSpanningTreeBenchmarkTestCase.java | 36 +-
.../org/apache/commons/graph/CommonsGraph.java | 150 ++---
.../org/apache/commons/graph/DirectedGraph.java | 20 +-
src/main/java/org/apache/commons/graph/Graph.java | 76 +--
.../org/apache/commons/graph/GraphException.java | 14 +-
.../org/apache/commons/graph/MutableGraph.java | 28 +-
.../commons/graph/SynchronizedDirectedGraph.java | 16 +-
.../apache/commons/graph/SynchronizedGraph.java | 78 +--
.../commons/graph/SynchronizedMutableGraph.java | 16 +-
.../java/org/apache/commons/graph/VertexPair.java | 44 +-
.../graph/builder/AbstractGraphConnection.java | 48 +-
.../commons/graph/builder/DefaultGrapher.java | 12 +-
.../commons/graph/builder/GraphConnector.java | 18 +-
.../commons/graph/collections/DisjointSet.java | 54 +-
.../commons/graph/collections/DisjointSetNode.java | 34 +-
.../commons/graph/collections/FibonacciHeap.java | 608 ++++++++++-----------
.../graph/collections/FibonacciHeapNode.java | 112 ++--
.../commons/graph/coloring/ColoredVertices.java | 24 +-
.../graph/coloring/ColoringAlgorithmsSelector.java | 14 +-
.../DefaultColoringAlgorithmsSelector.java | 66 +--
.../graph/connectivity/ConnectivityBuilder.java | 10 +-
.../connectivity/DefaultConnectivityBuilder.java | 12 +-
.../org/apache/commons/graph/elo/Category.java | 12 +-
.../commons/graph/elo/DefaultKFactorBuilder.java | 46 +-
.../commons/graph/export/AbstractExporter.java | 88 +--
.../apache/commons/graph/export/DotExporter.java | 120 ++--
.../commons/graph/export/GraphMLExporter.java | 140 ++---
.../flow/DefaultMaxFlowAlgorithmSelector.java | 116 ++--
.../commons/graph/flow/FlowNetworkHandler.java | 110 ++--
.../graph/flow/MaxFlowAlgorithmSelector.java | 12 +-
.../org/apache/commons/graph/model/BaseGraph.java | 176 +++---
.../commons/graph/model/BaseMutableGraph.java | 88 +--
.../commons/graph/model/DirectedMutableGraph.java | 54 +-
.../apache/commons/graph/model/InMemoryPath.java | 148 ++---
.../commons/graph/model/InMemoryWeightedPath.java | 52 +-
.../commons/graph/model/MutableSpanningTree.java | 16 +-
.../apache/commons/graph/model/RevertedGraph.java | 54 +-
.../graph/model/UndirectedMutableGraph.java | 18 +-
.../graph/scc/CheriyanMehlhornGabowAlgorithm.java | 58 +-
.../graph/scc/DefaultSccAlgorithmSelector.java | 30 +-
.../commons/graph/scc/KosarajuSharirAlgorithm.java | 100 ++--
.../commons/graph/scc/SccAlgorithmSelector.java | 12 +-
.../apache/commons/graph/scc/TarjanAlgorithm.java | 74 +--
.../commons/graph/scc/TarjanVertexMetaInfo.java | 10 +-
.../shortestpath/AllVertexPairsShortestPath.java | 28 +-
.../shortestpath/DefaultPathSourceSelector.java | 18 +-
.../DefaultShortestPathAlgorithmSelector.java | 118 ++--
.../NegativeWeightedCycleException.java | 8 +-
.../graph/shortestpath/ShortestDistances.java | 50 +-
.../ShortestPathAlgorithmSelector.java | 8 +-
.../DefaultSpanningTreeSourceSelector.java | 38 +-
.../commons/graph/spanning/ReverseDeleteGraph.java | 64 +--
.../commons/graph/spanning/ShortestEdges.java | 74 +--
.../graph/spanning/SpanningTreeSourceSelector.java | 30 +-
.../apache/commons/graph/spanning/SuperVertex.java | 44 +-
.../org/apache/commons/graph/utils/Assertions.java | 60 +-
.../org/apache/commons/graph/utils/Objects.java | 20 +-
.../commons/graph/visit/BaseGraphVisitHandler.java | 14 +-
.../visit/DefaultVisitAlgorithmsSelector.java | 8 +-
.../commons/graph/visit/GraphVisitHandler.java | 34 +-
.../graph/visit/VisitAlgorithmsSelector.java | 12 +-
.../commons/graph/visit/VisitGraphBuilder.java | 20 +-
.../org/apache/commons/graph/weight/Monoid.java | 14 +-
.../primitive/BigDecimalWeightBaseOperations.java | 24 +-
.../primitive/BigIntegerWeightBaseOperations.java | 24 +-
.../primitive/DoubleWeightBaseOperations.java | 24 +-
.../primitive/FloatWeightBaseOperations.java | 24 +-
.../primitive/IntegerWeightBaseOperations.java | 24 +-
.../weight/primitive/LongWeightBaseOperations.java | 24 +-
.../graph/builder/GraphBuilderTestCase.java | 2 +-
.../graph/collections/FibonacciHeapTestCase.java | 140 ++---
.../graph/coloring/AbstractColoringTest.java | 42 +-
.../GraphColoringBackTrackingTestCase.java | 120 ++--
.../graph/coloring/GraphColoringTestCase.java | 118 ++--
.../FindConnectedComponetTestCase.java | 86 +--
.../org/apache/commons/graph/elo/EloTestCase.java | 2 +-
.../commons/graph/elo/SimplePlayersRank.java | 10 +-
.../commons/graph/export/ExportTestCase.java | 6 +-
.../commons/graph/flow/EdmondsKarpTestCase.java | 98 ++--
.../commons/graph/flow/FordFulkersonTestCase.java | 110 ++--
.../commons/graph/model/BaseLabeledEdge.java | 34 +-
.../commons/graph/model/BaseLabeledVertex.java | 34 +-
.../graph/model/BaseLabeledWeightedEdge.java | 34 +-
.../graph/model/BaseMutableGraphTestCase.java | 346 ++++++------
.../graph/model/GraphSerializationTestCase.java | 184 +++----
.../graph/scc/CheriyanMehlhornGabowTestCase.java | 16 +-
.../commons/graph/scc/KosarajuSharirTestCase.java | 30 +-
.../apache/commons/graph/scc/TarjanTestCase.java | 14 +-
.../commons/graph/shortestpath/AStarTestCase.java | 220 ++++----
.../graph/shortestpath/BellmannFordTestCase.java | 158 +++---
.../graph/shortestpath/BidirDijkstraTestCase.java | 242 ++++----
.../graph/shortestpath/DijkstraTestCase.java | 128 ++---
.../graph/shortestpath/FloydWarshallTestCase.java | 86 +--
.../commons/graph/spanning/BoruvkaTestCase.java | 44 +-
.../commons/graph/spanning/KruskalTestCase.java | 250 ++++-----
.../commons/graph/spanning/PrimTestCase.java | 78 +--
.../graph/spanning/ReverseDeleteTestCase.java | 40 +-
.../org/apache/commons/graph/utils/GraphUtils.java | 58 +-
.../graph/utils/MultiThreadedTestRunner.java | 16 +-
.../org/apache/commons/graph/utils/TestRunner.java | 14 +-
.../apache/commons/graph/visit/VisitTestCase.java | 4 +-
103 files changed, 3328 insertions(+), 3328 deletions(-)
(commons-graph) 02/02: Sort members
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-graph.git
commit 178a2fac10ae12ebe09e0513fddbc78ee99feb88
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Nov 3 10:17:09 2023 -0400
Sort members
---
.../graph/scc/SCCAlgorithmBenchmarkTestCase.java | 14 +-
.../UniVsBiDijkstraBenchmarkTestCase.java | 46 +-
.../MinimumSpanningTreeBenchmarkTestCase.java | 30 +-
.../org/apache/commons/graph/CommonsGraph.java | 150 ++---
.../org/apache/commons/graph/DirectedGraph.java | 20 +-
src/main/java/org/apache/commons/graph/Graph.java | 76 +--
.../org/apache/commons/graph/GraphException.java | 14 +-
.../org/apache/commons/graph/MutableGraph.java | 28 +-
.../commons/graph/SynchronizedDirectedGraph.java | 16 +-
.../apache/commons/graph/SynchronizedGraph.java | 78 +--
.../commons/graph/SynchronizedMutableGraph.java | 16 +-
.../java/org/apache/commons/graph/VertexPair.java | 44 +-
.../graph/builder/AbstractGraphConnection.java | 48 +-
.../commons/graph/builder/DefaultGrapher.java | 12 +-
.../commons/graph/builder/GraphConnector.java | 18 +-
.../commons/graph/collections/DisjointSet.java | 54 +-
.../commons/graph/collections/DisjointSetNode.java | 34 +-
.../commons/graph/collections/FibonacciHeap.java | 608 ++++++++++-----------
.../graph/collections/FibonacciHeapNode.java | 112 ++--
.../commons/graph/coloring/ColoredVertices.java | 24 +-
.../graph/coloring/ColoringAlgorithmsSelector.java | 14 +-
.../DefaultColoringAlgorithmsSelector.java | 66 +--
.../graph/connectivity/ConnectivityBuilder.java | 10 +-
.../connectivity/DefaultConnectivityBuilder.java | 12 +-
.../org/apache/commons/graph/elo/Category.java | 12 +-
.../commons/graph/elo/DefaultKFactorBuilder.java | 46 +-
.../commons/graph/export/AbstractExporter.java | 88 +--
.../apache/commons/graph/export/DotExporter.java | 120 ++--
.../commons/graph/export/GraphMLExporter.java | 140 ++---
.../flow/DefaultMaxFlowAlgorithmSelector.java | 116 ++--
.../commons/graph/flow/FlowNetworkHandler.java | 110 ++--
.../graph/flow/MaxFlowAlgorithmSelector.java | 12 +-
.../org/apache/commons/graph/model/BaseGraph.java | 176 +++---
.../commons/graph/model/BaseMutableGraph.java | 88 +--
.../commons/graph/model/DirectedMutableGraph.java | 54 +-
.../apache/commons/graph/model/InMemoryPath.java | 148 ++---
.../commons/graph/model/InMemoryWeightedPath.java | 52 +-
.../commons/graph/model/MutableSpanningTree.java | 16 +-
.../apache/commons/graph/model/RevertedGraph.java | 54 +-
.../graph/model/UndirectedMutableGraph.java | 18 +-
.../graph/scc/CheriyanMehlhornGabowAlgorithm.java | 58 +-
.../graph/scc/DefaultSccAlgorithmSelector.java | 30 +-
.../commons/graph/scc/KosarajuSharirAlgorithm.java | 100 ++--
.../commons/graph/scc/SccAlgorithmSelector.java | 12 +-
.../apache/commons/graph/scc/TarjanAlgorithm.java | 74 +--
.../commons/graph/scc/TarjanVertexMetaInfo.java | 10 +-
.../shortestpath/AllVertexPairsShortestPath.java | 28 +-
.../shortestpath/DefaultPathSourceSelector.java | 18 +-
.../DefaultShortestPathAlgorithmSelector.java | 118 ++--
.../NegativeWeightedCycleException.java | 8 +-
.../graph/shortestpath/ShortestDistances.java | 50 +-
.../ShortestPathAlgorithmSelector.java | 8 +-
.../DefaultSpanningTreeSourceSelector.java | 38 +-
.../commons/graph/spanning/ReverseDeleteGraph.java | 64 +--
.../commons/graph/spanning/ShortestEdges.java | 74 +--
.../graph/spanning/SpanningTreeSourceSelector.java | 30 +-
.../apache/commons/graph/spanning/SuperVertex.java | 44 +-
.../org/apache/commons/graph/utils/Assertions.java | 60 +-
.../org/apache/commons/graph/utils/Objects.java | 20 +-
.../commons/graph/visit/BaseGraphVisitHandler.java | 14 +-
.../visit/DefaultVisitAlgorithmsSelector.java | 8 +-
.../commons/graph/visit/GraphVisitHandler.java | 34 +-
.../graph/visit/VisitAlgorithmsSelector.java | 12 +-
.../commons/graph/visit/VisitGraphBuilder.java | 20 +-
.../org/apache/commons/graph/weight/Monoid.java | 14 +-
.../primitive/BigDecimalWeightBaseOperations.java | 24 +-
.../primitive/BigIntegerWeightBaseOperations.java | 24 +-
.../primitive/DoubleWeightBaseOperations.java | 24 +-
.../primitive/FloatWeightBaseOperations.java | 24 +-
.../primitive/IntegerWeightBaseOperations.java | 24 +-
.../weight/primitive/LongWeightBaseOperations.java | 24 +-
.../graph/collections/FibonacciHeapTestCase.java | 134 ++---
.../graph/coloring/AbstractColoringTest.java | 42 +-
.../GraphColoringBackTrackingTestCase.java | 120 ++--
.../graph/coloring/GraphColoringTestCase.java | 118 ++--
.../FindConnectedComponetTestCase.java | 76 +--
.../commons/graph/elo/SimplePlayersRank.java | 10 +-
.../commons/graph/flow/EdmondsKarpTestCase.java | 98 ++--
.../commons/graph/flow/FordFulkersonTestCase.java | 110 ++--
.../commons/graph/model/BaseLabeledEdge.java | 34 +-
.../commons/graph/model/BaseLabeledVertex.java | 34 +-
.../graph/model/BaseLabeledWeightedEdge.java | 34 +-
.../graph/model/BaseMutableGraphTestCase.java | 346 ++++++------
.../graph/model/GraphSerializationTestCase.java | 182 +++---
.../graph/scc/CheriyanMehlhornGabowTestCase.java | 14 +-
.../commons/graph/scc/KosarajuSharirTestCase.java | 30 +-
.../apache/commons/graph/scc/TarjanTestCase.java | 14 +-
.../commons/graph/shortestpath/AStarTestCase.java | 218 ++++----
.../graph/shortestpath/BellmannFordTestCase.java | 156 +++---
.../graph/shortestpath/BidirDijkstraTestCase.java | 238 ++++----
.../graph/shortestpath/DijkstraTestCase.java | 126 ++---
.../graph/shortestpath/FloydWarshallTestCase.java | 86 +--
.../commons/graph/spanning/BoruvkaTestCase.java | 42 +-
.../commons/graph/spanning/KruskalTestCase.java | 250 ++++-----
.../commons/graph/spanning/PrimTestCase.java | 74 +--
.../graph/spanning/ReverseDeleteTestCase.java | 32 +-
.../org/apache/commons/graph/utils/GraphUtils.java | 58 +-
.../graph/utils/MultiThreadedTestRunner.java | 16 +-
.../org/apache/commons/graph/utils/TestRunner.java | 14 +-
99 files changed, 3295 insertions(+), 3295 deletions(-)
diff --git a/src/benchmarks/java/org/apache/commons/graph/scc/SCCAlgorithmBenchmarkTestCase.java b/src/benchmarks/java/org/apache/commons/graph/scc/SCCAlgorithmBenchmarkTestCase.java
index 24c5c0f..a5e62ee 100644
--- a/src/benchmarks/java/org/apache/commons/graph/scc/SCCAlgorithmBenchmarkTestCase.java
+++ b/src/benchmarks/java/org/apache/commons/graph/scc/SCCAlgorithmBenchmarkTestCase.java
@@ -50,9 +50,6 @@ public final class SCCAlgorithmBenchmarkTestCase
private static final int NODES = 5000;
private static final int EDGES = 5000;
- @Rule
- public BenchmarkRule benchmarkRun = new BenchmarkRule();
-
private static DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> graph;
@BeforeClass
@@ -85,17 +82,20 @@ public final class SCCAlgorithmBenchmarkTestCase
} );
}
+ @Rule
+ public BenchmarkRule benchmarkRun = new BenchmarkRule();
+
@Test
- public void testPerformKosaraju()
+ public void testPerformCheriyanMehlhornGabow()
{
- Set<Set<BaseLabeledVertex>> actual = findStronglyConnectedComponent( graph ).applyingKosarajuSharir();
+ Set<Set<BaseLabeledVertex>> actual = findStronglyConnectedComponent( graph ).applyingCheriyanMehlhornGabow();
assertTrue( actual.size() > 0 );
}
@Test
- public void testPerformCheriyanMehlhornGabow()
+ public void testPerformKosaraju()
{
- Set<Set<BaseLabeledVertex>> actual = findStronglyConnectedComponent( graph ).applyingCheriyanMehlhornGabow();
+ Set<Set<BaseLabeledVertex>> actual = findStronglyConnectedComponent( graph ).applyingKosarajuSharir();
assertTrue( actual.size() > 0 );
}
diff --git a/src/benchmarks/java/org/apache/commons/graph/shortestpath/UniVsBiDijkstraBenchmarkTestCase.java b/src/benchmarks/java/org/apache/commons/graph/shortestpath/UniVsBiDijkstraBenchmarkTestCase.java
index 914ce52..c109757 100644
--- a/src/benchmarks/java/org/apache/commons/graph/shortestpath/UniVsBiDijkstraBenchmarkTestCase.java
+++ b/src/benchmarks/java/org/apache/commons/graph/shortestpath/UniVsBiDijkstraBenchmarkTestCase.java
@@ -58,9 +58,6 @@ public final class UniVsBiDijkstraBenchmarkTestCase
private static final int NODES = 5000;
private static final int EDGES = 100000;
- @Rule
- public BenchmarkRule benchmarkRun = new BenchmarkRule();
-
private static DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph;
private static Mapper<BaseLabeledWeightedEdge<Double>, Double> weightedEdges;
@@ -96,6 +93,18 @@ public final class UniVsBiDijkstraBenchmarkTestCase
{
Random r = new Random();
+ private boolean addEdge( BaseLabeledVertex src, BaseLabeledVertex dst )
+ {
+ try {
+ addEdge( new BaseLabeledWeightedEdge<Double>( format( "%s -> %s", src, dst ),
+ 10.0 * r.nextDouble() + 1.0 ) ).from( src ).to( dst );
+ return true;
+ } catch (GraphException e) {
+ // ignore duplicate edge exceptions
+ return false;
+ }
+ }
+
public void connect()
{
vertices = new ArrayList<BaseLabeledVertex>();
@@ -123,18 +132,6 @@ public final class UniVsBiDijkstraBenchmarkTestCase
}
}
}
-
- private boolean addEdge( BaseLabeledVertex src, BaseLabeledVertex dst )
- {
- try {
- addEdge( new BaseLabeledWeightedEdge<Double>( format( "%s -> %s", src, dst ),
- 10.0 * r.nextDouble() + 1.0 ) ).from( src ).to( dst );
- return true;
- } catch (GraphException e) {
- // ignore duplicate edge exceptions
- return false;
- }
- }
} );
sourceListUni = new LinkedList<BaseLabeledVertex>();
@@ -157,10 +154,13 @@ public final class UniVsBiDijkstraBenchmarkTestCase
}
}
+ @Rule
+ public BenchmarkRule benchmarkRun = new BenchmarkRule();
+
@Test
- public void testPerformUnidirectionalDijkstra() {
- BaseLabeledVertex source = sourceListUni.removeFirst();
- BaseLabeledVertex target = targetListUni.removeFirst();
+ public void testPerformBidirectionalDijkstra() {
+ BaseLabeledVertex source = sourceListBi.removeFirst();
+ BaseLabeledVertex target = targetListBi.removeFirst();
try {
WeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> path =
@@ -168,7 +168,7 @@ public final class UniVsBiDijkstraBenchmarkTestCase
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
.from( source )
.to( target )
- .applyingDijkstra( weightOperations );
+ .applyingBidirectionalDijkstra( weightOperations );
assertTrue( path.getSize() > 0 );
assertTrue( path.getWeight() > 0D );
@@ -180,9 +180,9 @@ public final class UniVsBiDijkstraBenchmarkTestCase
}
@Test
- public void testPerformBidirectionalDijkstra() {
- BaseLabeledVertex source = sourceListBi.removeFirst();
- BaseLabeledVertex target = targetListBi.removeFirst();
+ public void testPerformUnidirectionalDijkstra() {
+ BaseLabeledVertex source = sourceListUni.removeFirst();
+ BaseLabeledVertex target = targetListUni.removeFirst();
try {
WeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> path =
@@ -190,7 +190,7 @@ public final class UniVsBiDijkstraBenchmarkTestCase
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
.from( source )
.to( target )
- .applyingBidirectionalDijkstra( weightOperations );
+ .applyingDijkstra( weightOperations );
assertTrue( path.getSize() > 0 );
assertTrue( path.getWeight() > 0D );
diff --git a/src/benchmarks/java/org/apache/commons/graph/spanning/MinimumSpanningTreeBenchmarkTestCase.java b/src/benchmarks/java/org/apache/commons/graph/spanning/MinimumSpanningTreeBenchmarkTestCase.java
index f42135c..1d06a8d 100644
--- a/src/benchmarks/java/org/apache/commons/graph/spanning/MinimumSpanningTreeBenchmarkTestCase.java
+++ b/src/benchmarks/java/org/apache/commons/graph/spanning/MinimumSpanningTreeBenchmarkTestCase.java
@@ -52,9 +52,6 @@ public final class MinimumSpanningTreeBenchmarkTestCase
private static final int NODES = 1000;
private static final int EDGES = 6000;
- @Rule
- public BenchmarkRule benchmarkRun = new BenchmarkRule();
-
private static UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph;
private static Mapper<BaseLabeledWeightedEdge<Double>, Double> weightedEdges;
@@ -76,6 +73,18 @@ public final class MinimumSpanningTreeBenchmarkTestCase
{
Random r = new Random();
+ private boolean addEdge( BaseLabeledVertex src, BaseLabeledVertex dst )
+ {
+ try {
+ addEdge( new BaseLabeledWeightedEdge<Double>( format( "%s -> %s", src, dst ),
+ (double) r.nextInt(10) ) ).from( src ).to( dst );
+ return true;
+ } catch (GraphException e) {
+ // ignore duplicate edge exceptions
+ return false;
+ }
+ }
+
public void connect()
{
List<BaseLabeledVertex> vertices = new ArrayList<BaseLabeledVertex>();
@@ -102,21 +111,12 @@ public final class MinimumSpanningTreeBenchmarkTestCase
}
}
}
-
- private boolean addEdge( BaseLabeledVertex src, BaseLabeledVertex dst )
- {
- try {
- addEdge( new BaseLabeledWeightedEdge<Double>( format( "%s -> %s", src, dst ),
- (double) r.nextInt(10) ) ).from( src ).to( dst );
- return true;
- } catch (GraphException e) {
- // ignore duplicate edge exceptions
- return false;
- }
- }
} );
}
+ @Rule
+ public BenchmarkRule benchmarkRun = new BenchmarkRule();
+
@Test
public void testPerformBoruvka()
{
diff --git a/src/main/java/org/apache/commons/graph/CommonsGraph.java b/src/main/java/org/apache/commons/graph/CommonsGraph.java
index ef460d8..6850ea4 100644
--- a/src/main/java/org/apache/commons/graph/CommonsGraph.java
+++ b/src/main/java/org/apache/commons/graph/CommonsGraph.java
@@ -53,63 +53,78 @@ public final class CommonsGraph
{
/**
- * Export the graph in DOT or GraphML format.
+ * Create a color builder.
*
* @param <V> the Graph vertices type
* @param <E> the Graph edges type
* @param <G> the Graph type
* @param graph the input graph
- * @return an instance of {@link NamedExportSelector}
+ * @return an instance of {@link ColorsBuilder}
*/
- public static <V, E, G extends Graph<V, E>> NamedExportSelector<V, E> export( G graph )
+ public static <V, E, G extends UndirectedGraph<V, E>> ColorsBuilder<V, E> coloring( G graph )
{
- graph = checkNotNull( graph, "Null graph can not be exported" );
- return new DefaultExportSelector<V, E>( graph );
+ graph = checkNotNull( graph, "Coloring can not be calculated on null graph" );
+ return new DefaultColorsBuilder<V, E>( graph );
}
/**
- * Create a color builder.
+ * Ranks the players (vertices) that took part in a tournament (graph) depending on the game results (edges),
+ * applying the <a href="http://en.wikipedia.org/wiki/Elo_rating_system.">Elo Rating System</a>.
+ *
+ * @param <P> the players involved in the tournament
+ * @param <TG> the Tournament Graph type
+ * @param tournamentGraph the graph representing the tournament
+ * @return the builder for the functor which returns/update the players ranking
+ */
+ public static <P, TG extends DirectedGraph<P, GameResult>> RankingSelector<P> eloRate( TG tournamentGraph )
+ {
+ tournamentGraph = checkNotNull( tournamentGraph, "ELO ranking can not be applied on null graph!" );
+ return new DefaultRankingSelector<P>( tournamentGraph );
+ }
+
+ /**
+ * Export the graph in DOT or GraphML format.
*
* @param <V> the Graph vertices type
* @param <E> the Graph edges type
* @param <G> the Graph type
* @param graph the input graph
- * @return an instance of {@link ColorsBuilder}
+ * @return an instance of {@link NamedExportSelector}
*/
- public static <V, E, G extends UndirectedGraph<V, E>> ColorsBuilder<V, E> coloring( G graph )
+ public static <V, E, G extends Graph<V, E>> NamedExportSelector<V, E> export( G graph )
{
- graph = checkNotNull( graph, "Coloring can not be calculated on null graph" );
- return new DefaultColorsBuilder<V, E>( graph );
+ graph = checkNotNull( graph, "Null graph can not be exported" );
+ return new DefaultExportSelector<V, E>( graph );
}
/**
- * Find the maximum flow on the input {@link Graph}.
+ * Calculates the input graph Connected Component.
*
- * @param <V> the Graph vertices type
- * @param <WE> the Graph edges type
- * @param <G> the Graph type
- * @param graph the input edge-weighted graph
- * @return an instance of {@link FlowWeightedEdgesBuilder}
+ * @param <V> the Graph vertices type.
+ * @param <E> the Graph edges type.
+ * @param <G> the directed graph type
+ * @param graph the Graph which connected component has to be verified.
+ * @return the Connectivity algorithm builder
*/
- public static <V, WE, G extends DirectedGraph<V, WE>> FlowWeightedEdgesBuilder<V, WE> findMaxFlow( G graph )
+ public static <V, E, G extends Graph<V, E>> ConnectivityBuilder<V, E> findConnectedComponent( G graph )
{
- graph = checkNotNull( graph, "Max flow can not be calculated on null graph" );
- return new DefaultFlowWeightedEdgesBuilder<V, WE>( graph );
+ graph = checkNotNull( graph, "Connected Component cannot be calculated from a null graph" );
+ return new DefaultConnectivityBuilder<V, E>( graph );
}
/**
- * Find the minimum spanning tree on the input {@link Graph}
+ * Find the maximum flow on the input {@link Graph}.
*
* @param <V> the Graph vertices type
* @param <WE> the Graph edges type
* @param <G> the Graph type
* @param graph the input edge-weighted graph
- * @return the caluculated minimun spanning tree
+ * @return an instance of {@link FlowWeightedEdgesBuilder}
*/
- public static <V, WE, G extends Graph<V, WE>> SpanningWeightedEdgeMapperBuilder<V, WE> minimumSpanningTree( G graph )
+ public static <V, WE, G extends DirectedGraph<V, WE>> FlowWeightedEdgesBuilder<V, WE> findMaxFlow( G graph )
{
- graph = checkNotNull( graph, "Minimum spanning tree can not be calculated on null graph" );
- return new DefaultSpanningWeightedEdgeMapperBuilder<V, WE>( graph );
+ graph = checkNotNull( graph, "Max flow can not be calculated on null graph" );
+ return new DefaultFlowWeightedEdgesBuilder<V, WE>( graph );
}
/**
@@ -143,48 +158,18 @@ public final class CommonsGraph
}
/**
- * Calculates the input graph Connected Component.
- *
- * @param <V> the Graph vertices type.
- * @param <E> the Graph edges type.
- * @param <G> the directed graph type
- * @param graph the Graph which connected component has to be verified.
- * @return the Connectivity algorithm builder
- */
- public static <V, E, G extends Graph<V, E>> ConnectivityBuilder<V, E> findConnectedComponent( G graph )
- {
- graph = checkNotNull( graph, "Connected Component cannot be calculated from a null graph" );
- return new DefaultConnectivityBuilder<V, E>( graph );
- }
-
- /**
- * Allows select a series of algorithms to apply on input graph.
+ * Find the minimum spanning tree on the input {@link Graph}
*
* @param <V> the Graph vertices type
- * @param <E> the Graph edges type
+ * @param <WE> the Graph edges type
* @param <G> the Graph type
- * @param graph the Graph instance to apply graph algorithms
- * @return the graph algorithms selector
- */
- public static <V, E, G extends Graph<V, E>> VisitSourceSelector<V, E, G> visit( G graph )
- {
- graph = checkNotNull( graph, "No algorithm can be applied on null graph!" );
- return new DefaultVisitSourceSelector<V, E, G>( graph );
- }
-
- /**
- * Ranks the players (vertices) that took part in a tournament (graph) depending on the game results (edges),
- * applying the <a href="http://en.wikipedia.org/wiki/Elo_rating_system.">Elo Rating System</a>.
- *
- * @param <P> the players involved in the tournament
- * @param <TG> the Tournament Graph type
- * @param tournamentGraph the graph representing the tournament
- * @return the builder for the functor which returns/update the players ranking
+ * @param graph the input edge-weighted graph
+ * @return the caluculated minimun spanning tree
*/
- public static <P, TG extends DirectedGraph<P, GameResult>> RankingSelector<P> eloRate( TG tournamentGraph )
+ public static <V, WE, G extends Graph<V, WE>> SpanningWeightedEdgeMapperBuilder<V, WE> minimumSpanningTree( G graph )
{
- tournamentGraph = checkNotNull( tournamentGraph, "ELO ranking can not be applied on null graph!" );
- return new DefaultRankingSelector<P>( tournamentGraph );
+ graph = checkNotNull( graph, "Minimum spanning tree can not be calculated on null graph" );
+ return new DefaultSpanningWeightedEdgeMapperBuilder<V, WE>( graph );
}
/**
@@ -230,7 +215,7 @@ public final class CommonsGraph
}
/**
- * Returns a synchronized (thread-safe) {@link Graph} backed by the specified Graph.
+ * Returns a synchronized (thread-safe) {@link DirectedGraph} backed by the specified Graph.
*
* It is imperative that the user manually synchronize on the returned graph when iterating over iterable collections:
* <pre>
@@ -253,13 +238,13 @@ public final class CommonsGraph
* @param graph the input {@link Graph}
* @return the syncronyzed graph
*/
- public static <V, E> Graph<V, E> synchronize( Graph<V, E> graph )
+ public static <V, E> Graph<V, E> synchronize( DirectedGraph<V, E> graph )
{
- return new SynchronizedGraph<V, E>( graph );
+ return new SynchronizedDirectedGraph<V, E>( graph );
}
/**
- * Returns a synchronized (thread-safe) {@link DirectedGraph} backed by the specified Graph.
+ * Returns a synchronized (thread-safe) {@link Graph} backed by the specified Graph.
*
* It is imperative that the user manually synchronize on the returned graph when iterating over iterable collections:
* <pre>
@@ -282,13 +267,13 @@ public final class CommonsGraph
* @param graph the input {@link Graph}
* @return the syncronyzed graph
*/
- public static <V, E> Graph<V, E> synchronize( DirectedGraph<V, E> graph )
+ public static <V, E> Graph<V, E> synchronize( Graph<V, E> graph )
{
- return new SynchronizedDirectedGraph<V, E>( graph );
+ return new SynchronizedGraph<V, E>( graph );
}
/**
- * Returns a synchronized (thread-safe) {@link UndirectedGraph} backed by the specified Graph.
+ * Returns a synchronized (thread-safe) {@link MutableGraph} backed by the specified Graph.
*
* It is imperative that the user manually synchronize on the returned graph when iterating over iterable collections:
* <pre>
@@ -309,15 +294,15 @@ public final class CommonsGraph
* @param <V> the Graph vertices type
* @param <E> the Graph edges type
* @param graph the input {@link Graph}
- * @return the syncronyzed graph
+ * @return the synchronized graph
*/
- public static <V, E> Graph<V, E> synchronize( UndirectedGraph<V, E> graph )
+ public static <V, E> Graph<V, E> synchronize( MutableGraph<V, E> graph )
{
- return new SynchronizedUndirectedGraph<V, E>( graph );
+ return new SynchronizedMutableGraph<V, E>( graph );
}
/**
- * Returns a synchronized (thread-safe) {@link MutableGraph} backed by the specified Graph.
+ * Returns a synchronized (thread-safe) {@link UndirectedGraph} backed by the specified Graph.
*
* It is imperative that the user manually synchronize on the returned graph when iterating over iterable collections:
* <pre>
@@ -338,11 +323,26 @@ public final class CommonsGraph
* @param <V> the Graph vertices type
* @param <E> the Graph edges type
* @param graph the input {@link Graph}
- * @return the synchronized graph
+ * @return the syncronyzed graph
*/
- public static <V, E> Graph<V, E> synchronize( MutableGraph<V, E> graph )
+ public static <V, E> Graph<V, E> synchronize( UndirectedGraph<V, E> graph )
{
- return new SynchronizedMutableGraph<V, E>( graph );
+ return new SynchronizedUndirectedGraph<V, E>( graph );
+ }
+
+ /**
+ * Allows select a series of algorithms to apply on input graph.
+ *
+ * @param <V> the Graph vertices type
+ * @param <E> the Graph edges type
+ * @param <G> the Graph type
+ * @param graph the Graph instance to apply graph algorithms
+ * @return the graph algorithms selector
+ */
+ public static <V, E, G extends Graph<V, E>> VisitSourceSelector<V, E, G> visit( G graph )
+ {
+ graph = checkNotNull( graph, "No algorithm can be applied on null graph!" );
+ return new DefaultVisitSourceSelector<V, E, G>( graph );
}
/**
diff --git a/src/main/java/org/apache/commons/graph/DirectedGraph.java b/src/main/java/org/apache/commons/graph/DirectedGraph.java
index 079d564..8cf3c33 100644
--- a/src/main/java/org/apache/commons/graph/DirectedGraph.java
+++ b/src/main/java/org/apache/commons/graph/DirectedGraph.java
@@ -33,14 +33,6 @@ public interface DirectedGraph<V, E>
extends Graph<V, E>
{
- /**
- * For a vertex, the number of head endpoints adjacent to a node is called the indegree.
- *
- * @param v the vertex which indegree has to be returned.
- * @return the number of head endpoints adjacent to a vertex.
- */
- int getInDegree( V v );
-
/**
* Returns the set of edges which are inbound to the vertex.
*
@@ -50,12 +42,12 @@ public interface DirectedGraph<V, E>
Iterable<V> getInbound( V v );
/**
- * For a vertex, the number of tail endpoints adjacent to a node is called the outdegree.
+ * For a vertex, the number of head endpoints adjacent to a node is called the indegree.
*
* @param v the vertex which indegree has to be returned.
* @return the number of head endpoints adjacent to a vertex.
*/
- int getOutDegree( V v );
+ int getInDegree( V v );
/**
* Returns the set of vertexs which lead away from the vertex.
@@ -65,4 +57,12 @@ public interface DirectedGraph<V, E>
*/
Iterable<V> getOutbound( V v );
+ /**
+ * For a vertex, the number of tail endpoints adjacent to a node is called the outdegree.
+ *
+ * @param v the vertex which indegree has to be returned.
+ * @return the number of head endpoints adjacent to a vertex.
+ */
+ int getOutDegree( V v );
+
}
diff --git a/src/main/java/org/apache/commons/graph/Graph.java b/src/main/java/org/apache/commons/graph/Graph.java
index c9e37c1..204fb93 100644
--- a/src/main/java/org/apache/commons/graph/Graph.java
+++ b/src/main/java/org/apache/commons/graph/Graph.java
@@ -34,111 +34,111 @@ public interface Graph<V, E>
{
/**
- * Returns the total set of Vertices in the graph.
+ * Returns true if the edge is contained into the graph
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @return the total set of Vertices in the graph.
+ * @param e the edge to be checked
+ * @return Returns true if the edge is contained into the graph, false otherwise
*/
- Iterable<V> getVertices();
+ boolean containsEdge( E e );
/**
- * Returns the <i>order</i> of a Graph (the number of Vertices);
+ * Returns true if the vertex is contained into the graph
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @return the <i>order</i> of a Graph (the number of Vertices);
+ * @param v the vertex to be checked
+ * @return Returns true if the vertex is contained into the graph, false otherwise
*/
- int getOrder();
+ boolean containsVertex( V v );
/**
- * Returns the total set of Edges in the graph.
+ * Returns all vertices which touch this vertex.
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @return the total set of Edges in the graph.
+ * @param v the vertex which connected vertices have to be returned.
+ * @return all vertices which touch this vertex.
*/
- Iterable<E> getEdges();
+ Iterable<V> getConnectedVertices( V v );
/**
- * Returns the <i>size</i> of a Graph (the number of Edges)
+ * The degree (or valency) of a vertex of a {@link Graph}
+ * is the number of edges incident to the vertex.
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @return the <i>size</i> of a Graph (the number of Edges)
+ * @param v the vertex which degree has to be returned.
+ * @return the number of edges incident to the vertex.
*/
- int getSize();
+ int getDegree( V v );
/**
- * The degree (or valency) of a vertex of a {@link Graph}
- * is the number of edges incident to the vertex.
+ * Returns the edge with vertex source and target.
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param v the vertex which degree has to be returned.
- * @return the number of edges incident to the vertex.
+ * @param source the source vertex
+ * @param target the target vertex
+ * @return the edge with vertex source and target.
*/
- int getDegree( V v );
+ E getEdge( V source, V target );
/**
- * Returns all vertices which touch this vertex.
+ * Returns the total set of Edges in the graph.
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param v the vertex which connected vertices have to be returned.
- * @return all vertices which touch this vertex.
+ * @return the total set of Edges in the graph.
*/
- Iterable<V> getConnectedVertices( V v );
+ Iterable<E> getEdges();
/**
- * Returns the edge with vertex source and target.
+ * Returns the <i>order</i> of a Graph (the number of Vertices);
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param source the source vertex
- * @param target the target vertex
- * @return the edge with vertex source and target.
+ * @return the <i>order</i> of a Graph (the number of Vertices);
*/
- E getEdge( V source, V target );
+ int getOrder();
/**
- * Return the set of vertex on the input edge (2 for normal edges, > 2 for HyperEdges)
+ * Returns the <i>size</i> of a Graph (the number of Edges)
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param e the input edge
- * @return the set of vertex on this Edge.
+ * @return the <i>size</i> of a Graph (the number of Edges)
*/
- VertexPair<V> getVertices( E e );
+ int getSize();
/**
- * Returns true if the vertex is contained into the graph
+ * Returns the total set of Vertices in the graph.
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param v the vertex to be checked
- * @return Returns true if the vertex is contained into the graph, false otherwise
+ * @return the total set of Vertices in the graph.
*/
- boolean containsVertex( V v );
+ Iterable<V> getVertices();
/**
- * Returns true if the edge is contained into the graph
+ * Return the set of vertex on the input edge (2 for normal edges, > 2 for HyperEdges)
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param e the edge to be checked
- * @return Returns true if the edge is contained into the graph, false otherwise
+ * @param e the input edge
+ * @return the set of vertex on this Edge.
*/
- boolean containsEdge( E e );
+ VertexPair<V> getVertices( E e );
}
diff --git a/src/main/java/org/apache/commons/graph/GraphException.java b/src/main/java/org/apache/commons/graph/GraphException.java
index b0b7964..361a320 100644
--- a/src/main/java/org/apache/commons/graph/GraphException.java
+++ b/src/main/java/org/apache/commons/graph/GraphException.java
@@ -51,24 +51,24 @@ public class GraphException
}
/**
- * Constructs a new graph exception with the specified cause.
+ * Constructs a new graph exception with the specified detail message and cause.
*
+ * @param msg the detail message.
* @param cause the cause
*/
- public GraphException( Throwable cause )
+ public GraphException( String msg, Throwable cause )
{
- super( cause );
+ super( msg, cause );
}
/**
- * Constructs a new graph exception with the specified detail message and cause.
+ * Constructs a new graph exception with the specified cause.
*
- * @param msg the detail message.
* @param cause the cause
*/
- public GraphException( String msg, Throwable cause )
+ public GraphException( Throwable cause )
{
- super( msg, cause );
+ super( cause );
}
}
diff --git a/src/main/java/org/apache/commons/graph/MutableGraph.java b/src/main/java/org/apache/commons/graph/MutableGraph.java
index e4f3830..1473114 100644
--- a/src/main/java/org/apache/commons/graph/MutableGraph.java
+++ b/src/main/java/org/apache/commons/graph/MutableGraph.java
@@ -30,45 +30,45 @@ public interface MutableGraph<V, E>
{
/**
- * Adds a feature to the vertex attribute of the {@code MutableGraph} object.
+ * Adds a feature to the edge attribute of the {@code MutableGraph} object
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param v the vertex has to be added in this {@code MutableGraph} instance.
+ * @param head the head vertex
+ * @param e the edge has to be added in this {@code MutableGraph} instance.
+ * @param tail the tail vertex
*/
- void addVertex( V v );
+ void addEdge( V head, E e, V tail );
/**
- * Removes the vertex from the {@code MutableGraph} object.
+ * Adds a feature to the vertex attribute of the {@code MutableGraph} object.
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param v the vertex has to be removed from this {@code MutableGraph} instance.
+ * @param v the vertex has to be added in this {@code MutableGraph} instance.
*/
- void removeVertex( V v );
+ void addVertex( V v );
/**
- * Adds a feature to the edge attribute of the {@code MutableGraph} object
+ * Removed the edge from the {@code MutableGraph} object.
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param head the head vertex
- * @param e the edge has to be added in this {@code MutableGraph} instance.
- * @param tail the tail vertex
+ * @param e the edge has to be removed from this {@code MutableGraph} instance.
*/
- void addEdge( V head, E e, V tail );
+ void removeEdge( E e );
/**
- * Removed the edge from the {@code MutableGraph} object.
+ * Removes the vertex from the {@code MutableGraph} object.
*
* <b>NOTE</b>: implementors have to take in consideration throwing a {@link GraphException}
* if an error occurs while performing that operation.
*
- * @param e the edge has to be removed from this {@code MutableGraph} instance.
+ * @param v the vertex has to be removed from this {@code MutableGraph} instance.
*/
- void removeEdge( E e );
+ void removeVertex( V v );
}
diff --git a/src/main/java/org/apache/commons/graph/SynchronizedDirectedGraph.java b/src/main/java/org/apache/commons/graph/SynchronizedDirectedGraph.java
index 89c776a..2ede733 100644
--- a/src/main/java/org/apache/commons/graph/SynchronizedDirectedGraph.java
+++ b/src/main/java/org/apache/commons/graph/SynchronizedDirectedGraph.java
@@ -44,44 +44,44 @@ final class SynchronizedDirectedGraph<V, E>
/**
* {@inheritDoc}
*/
- public int getInDegree( V v )
+ public Iterable<V> getInbound( V v )
{
synchronized ( lock )
{
- return directedGraph.getInDegree( v );
+ return directedGraph.getInbound( v );
}
}
/**
* {@inheritDoc}
*/
- public Iterable<V> getInbound( V v )
+ public int getInDegree( V v )
{
synchronized ( lock )
{
- return directedGraph.getInbound( v );
+ return directedGraph.getInDegree( v );
}
}
/**
* {@inheritDoc}
*/
- public int getOutDegree( V v )
+ public Iterable<V> getOutbound( V v )
{
synchronized ( lock )
{
- return directedGraph.getOutDegree( v );
+ return directedGraph.getOutbound( v );
}
}
/**
* {@inheritDoc}
*/
- public Iterable<V> getOutbound( V v )
+ public int getOutDegree( V v )
{
synchronized ( lock )
{
- return directedGraph.getOutbound( v );
+ return directedGraph.getOutDegree( v );
}
}
diff --git a/src/main/java/org/apache/commons/graph/SynchronizedGraph.java b/src/main/java/org/apache/commons/graph/SynchronizedGraph.java
index 8811060..c037c85 100644
--- a/src/main/java/org/apache/commons/graph/SynchronizedGraph.java
+++ b/src/main/java/org/apache/commons/graph/SynchronizedGraph.java
@@ -47,110 +47,129 @@ class SynchronizedGraph<V, E>
/**
* {@inheritDoc}
*/
- public Iterable<V> getVertices()
+ public boolean containsEdge( E e )
{
synchronized ( lock )
{
- return g.getVertices();
+ return g.containsEdge( e );
}
}
/**
* {@inheritDoc}
*/
- public int getOrder()
+ public boolean containsVertex( V v )
{
synchronized ( lock )
{
- return g.getOrder();
+ return g.containsVertex( v );
}
}
+ @Override
+ public boolean equals( Object obj )
+ {
+ if ( this == obj )
+ {
+ return true;
+ }
+
+ if ( obj == null || getClass() != obj.getClass() )
+ {
+ return false;
+ }
+
+ @SuppressWarnings( "unchecked" )
+ // test against any Graph typed instance
+ SynchronizedGraph<Object, Object> other = (SynchronizedGraph<Object, Object>) obj;
+ return eq( g, other.g );
+ }
+
/**
* {@inheritDoc}
*/
- public Iterable<E> getEdges()
+ public Iterable<V> getConnectedVertices( V v )
{
synchronized ( lock )
{
- return g.getEdges();
+ return g.getConnectedVertices( v );
}
}
/**
* {@inheritDoc}
*/
- public int getSize()
+ public int getDegree( V v )
{
synchronized ( lock )
{
- return g.getSize();
+ return g.getDegree( v );
}
}
/**
* {@inheritDoc}
*/
- public int getDegree( V v )
+ public E getEdge( V source, V target )
{
synchronized ( lock )
{
- return g.getDegree( v );
+ return g.getEdge( source, target );
}
}
/**
* {@inheritDoc}
*/
- public Iterable<V> getConnectedVertices( V v )
+ public Iterable<E> getEdges()
{
synchronized ( lock )
{
- return g.getConnectedVertices( v );
+ return g.getEdges();
}
}
/**
* {@inheritDoc}
*/
- public E getEdge( V source, V target )
+ public int getOrder()
{
synchronized ( lock )
{
- return g.getEdge( source, target );
+ return g.getOrder();
}
}
/**
* {@inheritDoc}
*/
- public VertexPair<V> getVertices( E e )
+ public int getSize()
{
synchronized ( lock )
{
- return g.getVertices( e );
+ return g.getSize();
}
}
/**
* {@inheritDoc}
*/
- public boolean containsVertex( V v )
+ public Iterable<V> getVertices()
{
synchronized ( lock )
{
- return g.containsVertex( v );
+ return g.getVertices();
}
}
/**
* {@inheritDoc}
*/
- public boolean containsEdge( E e )
+ public VertexPair<V> getVertices( E e )
{
synchronized ( lock )
{
- return g.containsEdge( e );
+ return g.getVertices( e );
}
}
@@ -164,25 +183,6 @@ class SynchronizedGraph<V, E>
return result;
}
- @Override
- public boolean equals( Object obj )
- {
- if ( this == obj )
- {
- return true;
- }
-
- if ( obj == null || getClass() != obj.getClass() )
- {
- return false;
- }
-
- @SuppressWarnings( "unchecked" )
- // test against any Graph typed instance
- SynchronizedGraph<Object, Object> other = (SynchronizedGraph<Object, Object>) obj;
- return eq( g, other.g );
- }
-
@Override
public String toString()
{
diff --git a/src/main/java/org/apache/commons/graph/SynchronizedMutableGraph.java b/src/main/java/org/apache/commons/graph/SynchronizedMutableGraph.java
index cb2a369..12f1c7c 100644
--- a/src/main/java/org/apache/commons/graph/SynchronizedMutableGraph.java
+++ b/src/main/java/org/apache/commons/graph/SynchronizedMutableGraph.java
@@ -44,44 +44,44 @@ final class SynchronizedMutableGraph<V, E>
/**
* {@inheritDoc}
*/
- public void addVertex( V v )
+ public void addEdge( V head, E e, V tail )
{
synchronized ( lock )
{
- mutableGraph.addVertex( v );
+ mutableGraph.addEdge( head, e, tail );
}
}
/**
* {@inheritDoc}
*/
- public void removeVertex( V v )
+ public void addVertex( V v )
{
synchronized ( lock )
{
- mutableGraph.removeVertex( v );
+ mutableGraph.addVertex( v );
}
}
/**
* {@inheritDoc}
*/
- public void addEdge( V head, E e, V tail )
+ public void removeEdge( E e )
{
synchronized ( lock )
{
- mutableGraph.addEdge( head, e, tail );
+ mutableGraph.removeEdge( e );
}
}
/**
* {@inheritDoc}
*/
- public void removeEdge( E e )
+ public void removeVertex( V v )
{
synchronized ( lock )
{
- mutableGraph.removeEdge( e );
+ mutableGraph.removeVertex( v );
}
}
diff --git a/src/main/java/org/apache/commons/graph/VertexPair.java b/src/main/java/org/apache/commons/graph/VertexPair.java
index 738af4d..b101ab1 100644
--- a/src/main/java/org/apache/commons/graph/VertexPair.java
+++ b/src/main/java/org/apache/commons/graph/VertexPair.java
@@ -54,6 +54,28 @@ public final class VertexPair<V>
this.tail = checkNotNull( tail, "Impossible to construct a Vertex with a null tail" );
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals( Object obj )
+ {
+ if ( this == obj )
+ {
+ return true;
+ }
+
+ if ( obj == null || getClass() != obj.getClass() )
+ {
+ return false;
+ }
+
+ @SuppressWarnings( "unchecked" ) // equals() invoked against only same VertexPair type
+ VertexPair<V> other = (VertexPair<V>) obj;
+ return eq( head, other.getHead() )
+ && eq( tail, other.getTail() );
+ }
+
/**
* @return the source
*/
@@ -80,28 +102,6 @@ public final class VertexPair<V>
return hash( 1, prime, head, tail );
}
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals( Object obj )
- {
- if ( this == obj )
- {
- return true;
- }
-
- if ( obj == null || getClass() != obj.getClass() )
- {
- return false;
- }
-
- @SuppressWarnings( "unchecked" ) // equals() invoked against only same VertexPair type
- VertexPair<V> other = (VertexPair<V>) obj;
- return eq( head, other.getHead() )
- && eq( tail, other.getTail() );
- }
-
@Override
public String toString()
{
diff --git a/src/main/java/org/apache/commons/graph/builder/AbstractGraphConnection.java b/src/main/java/org/apache/commons/graph/builder/AbstractGraphConnection.java
index 6daae73..d8b281f 100644
--- a/src/main/java/org/apache/commons/graph/builder/AbstractGraphConnection.java
+++ b/src/main/java/org/apache/commons/graph/builder/AbstractGraphConnection.java
@@ -34,21 +34,15 @@ public abstract class AbstractGraphConnection<V, E>
private GraphConnector<V, E> connector;
/**
- * {@inheritDoc}
+ * Adds a new edge to graph connector.
+ *
+ * @param <A> the Graph edges type
+ * @param arc the edge to add.
+ * @return the {@link HeadVertexConnector}
*/
- public final void connect( GraphConnector<V, E> connector )
+ protected final <A extends E> HeadVertexConnector<V, E> addEdge( A arc )
{
- checkState( this.connector == null, "Re-entry not allowed!" );
- this.connector = connector;
-
- try
- {
- connect();
- }
- finally
- {
- this.connector = null;
- }
+ return connector.addEdge( arc );
}
/**
@@ -64,20 +58,26 @@ public abstract class AbstractGraphConnection<V, E>
}
/**
- * Adds a new edge to graph connector.
- *
- * @param <A> the Graph edges type
- * @param arc the edge to add.
- * @return the {@link HeadVertexConnector}
+ * Connects the graph.
*/
- protected final <A extends E> HeadVertexConnector<V, E> addEdge( A arc )
- {
- return connector.addEdge( arc );
- }
+ public abstract void connect();
/**
- * Connects the graph.
+ * {@inheritDoc}
*/
- public abstract void connect();
+ public final void connect( GraphConnector<V, E> connector )
+ {
+ checkState( this.connector == null, "Re-entry not allowed!" );
+ this.connector = connector;
+
+ try
+ {
+ connect();
+ }
+ finally
+ {
+ this.connector = null;
+ }
+ }
}
diff --git a/src/main/java/org/apache/commons/graph/builder/DefaultGrapher.java b/src/main/java/org/apache/commons/graph/builder/DefaultGrapher.java
index 961ae8a..06081bf 100644
--- a/src/main/java/org/apache/commons/graph/builder/DefaultGrapher.java
+++ b/src/main/java/org/apache/commons/graph/builder/DefaultGrapher.java
@@ -34,6 +34,12 @@ final class DefaultGrapher<V, E>
this.graph = graph;
}
+ public <A extends E> HeadVertexConnector<V, E> addEdge( A arc )
+ {
+ arc = checkNotNull( arc, "Null edge not admitted" );
+ return new DefaultHeadVertexConnector<V, E>( graph, arc );
+ }
+
public <N extends V> N addVertex( N node )
{
node = checkNotNull( node, "Null vertex not admitted" );
@@ -41,10 +47,4 @@ final class DefaultGrapher<V, E>
return node;
}
- public <A extends E> HeadVertexConnector<V, E> addEdge( A arc )
- {
- arc = checkNotNull( arc, "Null edge not admitted" );
- return new DefaultHeadVertexConnector<V, E>( graph, arc );
- }
-
}
diff --git a/src/main/java/org/apache/commons/graph/builder/GraphConnector.java b/src/main/java/org/apache/commons/graph/builder/GraphConnector.java
index 790f124..f956aa0 100644
--- a/src/main/java/org/apache/commons/graph/builder/GraphConnector.java
+++ b/src/main/java/org/apache/commons/graph/builder/GraphConnector.java
@@ -28,15 +28,6 @@ package org.apache.commons.graph.builder;
public interface GraphConnector<V, E>
{
- /**
- * Adds a new vertex to graph.
- *
- * @param <N> the Graph vertices type
- * @param node the vertex to add
- * @return the vertex added
- */
- <N extends V> N addVertex( N node );
-
/**
* Adds a new edge to graph.
*
@@ -46,4 +37,13 @@ public interface GraphConnector<V, E>
*/
<A extends E> HeadVertexConnector<V, E> addEdge( A arc );
+ /**
+ * Adds a new vertex to graph.
+ *
+ * @param <N> the Graph vertices type
+ * @param node the vertex to add
+ * @return the vertex added
+ */
+ <N extends V> N addVertex( N node );
+
}
diff --git a/src/main/java/org/apache/commons/graph/collections/DisjointSet.java b/src/main/java/org/apache/commons/graph/collections/DisjointSet.java
index 5833cd5..47b31ca 100644
--- a/src/main/java/org/apache/commons/graph/collections/DisjointSet.java
+++ b/src/main/java/org/apache/commons/graph/collections/DisjointSet.java
@@ -35,6 +35,21 @@ public final class DisjointSet<E>
*/
private final Map<E, DisjointSetNode<E>> disjointSets = new HashMap<E, DisjointSetNode<E>>();
+ /**
+ * Performs the @code{ find} operation by applying the <i>path compression</i>.
+ *
+ * @param node the input DisjointSet node for the @code{ find} operation
+ * @return the root node of the path
+ */
+ private DisjointSetNode<E> find( DisjointSetNode<E> node )
+ {
+ if ( node == node.getParent() )
+ {
+ return node;
+ }
+ return find( node.getParent() );
+ }
+
/**
* Performs the {@code find} operation applying the <i>path compression</i>.
*
@@ -56,18 +71,23 @@ public final class DisjointSet<E>
}
/**
- * Performs the @code{ find} operation by applying the <i>path compression</i>.
+ * Retrieves the {@code DisjointSetNode} from the {@link #disjointSets},
+ * if already previously set, creates a new one and push it in {@link #disjointSets} otherwise.
*
- * @param node the input DisjointSet node for the @code{ find} operation
- * @return the root node of the path
+ * @param e the element which related subset has to be returned
+ * @return the input element {@code DisjointSetNode}
*/
- private DisjointSetNode<E> find( DisjointSetNode<E> node )
+ private DisjointSetNode<E> getNode( E e )
{
- if ( node == node.getParent() )
+ DisjointSetNode<E> node = disjointSets.get( e );
+
+ if ( node == null )
{
- return node;
+ node = new DisjointSetNode<E>( e );
+ disjointSets.put( e, node );
}
- return find( node.getParent() );
+
+ return node;
}
/**
@@ -102,24 +122,4 @@ public final class DisjointSet<E>
}
}
- /**
- * Retrieves the {@code DisjointSetNode} from the {@link #disjointSets},
- * if already previously set, creates a new one and push it in {@link #disjointSets} otherwise.
- *
- * @param e the element which related subset has to be returned
- * @return the input element {@code DisjointSetNode}
- */
- private DisjointSetNode<E> getNode( E e )
- {
- DisjointSetNode<E> node = disjointSets.get( e );
-
- if ( node == null )
- {
- node = new DisjointSetNode<E>( e );
- disjointSets.put( e, node );
- }
-
- return node;
- }
-
}
diff --git a/src/main/java/org/apache/commons/graph/collections/DisjointSetNode.java b/src/main/java/org/apache/commons/graph/collections/DisjointSetNode.java
index 7577c91..1674dfa 100644
--- a/src/main/java/org/apache/commons/graph/collections/DisjointSetNode.java
+++ b/src/main/java/org/apache/commons/graph/collections/DisjointSetNode.java
@@ -53,6 +53,14 @@ final class DisjointSetNode<E>
this.element = element;
}
+ /**
+ * {@inheritDoc}
+ */
+ public int compareTo( DisjointSetNode<E> o )
+ {
+ return rank.compareTo( o.getRank() );
+ }
+
/**
* Returns the adapted element by this node.
*
@@ -73,16 +81,6 @@ final class DisjointSetNode<E>
return parent;
}
- /**
- * Sets the reference to a new parent node.
- *
- * @param parent the reference to a new parent node.
- */
- public void setParent( DisjointSetNode<E> parent )
- {
- this.parent = parent;
- }
-
/**
* Returns this node rank.
*
@@ -102,21 +100,23 @@ final class DisjointSetNode<E>
}
/**
- * Sets a new different rank.
+ * Sets the reference to a new parent node.
*
- * @param rank the new rank to this node.
+ * @param parent the reference to a new parent node.
*/
- public void setRank( int rank )
+ public void setParent( DisjointSetNode<E> parent )
{
- this.rank = rank;
+ this.parent = parent;
}
/**
- * {@inheritDoc}
+ * Sets a new different rank.
+ *
+ * @param rank the new rank to this node.
*/
- public int compareTo( DisjointSetNode<E> o )
+ public void setRank( int rank )
{
- return rank.compareTo( o.getRank() );
+ this.rank = rank;
}
}
diff --git a/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java b/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java
index 7040594..74ac325 100644
--- a/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java
+++ b/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java
@@ -106,42 +106,6 @@ public final class FibonacciHeap<E>
this.comparator = comparator;
}
- /**
- * Moves the target node in the {@code H} root nodes.
- *
- * @param node the node has to be moved in the {@code H} root nodes
- * @see #add(Object)
- * @see #consolidate()
- * @see #cut(FibonacciHeapNode, FibonacciHeapNode)
- */
- private void moveToRoot( FibonacciHeapNode<E> node )
- {
- // 8' if min[H] = NIL
- if ( isEmpty() )
- {
- // then min[H] <- x
- minimumNode = node;
- }
- else
- {
- // 7 concatenate the root list containing x with root list H
- node.getLeft().setRight( node.getRight() );
- node.getRight().setLeft( node.getLeft() );
-
- node.setLeft( minimumNode );
- node.setRight( minimumNode.getRight() );
- minimumNode.setRight( node );
- node.getRight().setLeft( node );
-
- // 8'' if key[x] < key[min[H]]
- if ( compare( node, minimumNode ) < 0 )
- {
- // 9 then min[H] <- x
- minimumNode = node;
- }
- }
- }
-
/**
* {@inheritDoc}
*
@@ -188,6 +152,44 @@ public final class FibonacciHeap<E>
return true;
}
+ /**
+ * Implements the {@code CASCADING-CUT(H,y)} function.
+ *
+ * <pre>CASCADING-CUT(H,y)
+ * 1 z ← p[y]
+ * 2 if z ≠ NIL
+ * 3 then if mark[y] = FALSE
+ * 4 then mark[y] ← TRUE
+ * 5 else CUT(H,y,z)
+ * 6 CASCADING-CUT(H,z)</pre>
+ *
+ * @param y the target node to apply CASCADING-CUT
+ */
+ private void cascadingCut( FibonacciHeapNode<E> y )
+ {
+ // z <- p[y]
+ FibonacciHeapNode<E> z = y.getParent();
+
+ // if z != NIL
+ if ( z != null )
+ {
+ // if mark[y] = FALSE
+ if ( !y.isMarked() )
+ {
+ // then mark[y] TRUE
+ y.setMarked( true );
+ markedNodes++;
+ }
+ else
+ {
+ // else CUT(H,y,z)
+ cut( y, z );
+ // CASCADING-CUT(H,z)
+ cascadingCut( z );
+ }
+ }
+ }
+
/**
* {@inheritDoc}
*/
@@ -200,6 +202,151 @@ public final class FibonacciHeap<E>
elementsIndex.clear();
}
+ /**
+ * Compare the given objects according to to the specified comparator if not null,
+ * according to their natural ordering otherwise.
+ *
+ * @param o1 the first {@link FibonacciHeap} node to be compared
+ * @param o2 the second {@link FibonacciHeap} node to be compared
+ * @return a negative integer, zero, or a positive integer as the first argument is
+ * less than, equal to, or greater than the second
+ */
+ private int compare( FibonacciHeapNode<E> o1, FibonacciHeapNode<E> o2 )
+ {
+ if ( comparator != null )
+ {
+ return comparator.compare( o1.getElement(), o2.getElement() );
+ }
+ @SuppressWarnings( "unchecked" ) // it will throw a ClassCastException at runtime
+ Comparable<? super E> o1Comparable = (Comparable<? super E>) o1.getElement();
+ return o1Comparable.compareTo( o2.getElement() );
+ }
+
+ /**
+ * Implements the {@code CONSOLIDATE(H)} function.
+ *
+ * <pre>CONSOLIDATE(H)
+ * 1 for i ← 0 to D(n[H])
+ * 2 do A[i] ← NIL
+ * 3 for each node w in the root list of H
+ * 4 do x ← w
+ * 5 d ← degree[x]
+ * 6 while A[d] ≠ NIL
+ * 7 do y ← A[d]
+ * 8 if key[x] > key[y]
+ * 9 then exchange x ↔ y
+ * 10 FIB-HEAP-LINK(H,y,x)
+ * 11 A[d] ← NIL
+ * 12 d ← d + 1
+ * 13 A[d] ← x
+ * 14 min[H] ← NIL
+ * 15 for i ← 0 to D(n[H])
+ * 16 do if A[i] ≠ NIL
+ * 17 then add A[i] to the root list of H
+ * 18 if min[H] = NIL or key[A[i]] ≤ key[min[H]]
+ * 19 then min[H] ← A[i]</pre>
+ */
+ private void consolidate()
+ {
+ if ( isEmpty() )
+ {
+ return;
+ }
+
+ // D( n[H] ) <= log_phi( n[H] )
+ // -> log_phi( n[H] ) = log( n[H] ) / log( phi )
+ // -> D( n[H] ) = log( n[H] ) / log( phi )
+ int arraySize = ( (int) floor( log( size ) / LOG_PHI ) );
+
+ // 1 for i <- 0 to D(n[H])
+ List<FibonacciHeapNode<E>> nodeSequence = new ArrayList<FibonacciHeapNode<E>>( arraySize );
+ for ( int i = 0; i < arraySize; i++ )
+ {
+ // 2 do A[i] <- NIL
+ nodeSequence.add( i, null );
+ }
+
+ int numRoots = 0;
+
+ // 3 for each node x in the root list of H
+ // 4 do x ← w
+ FibonacciHeapNode<E> x = minimumNode;
+
+ if ( x != null )
+ {
+ numRoots++;
+ x = x.getRight();
+
+ while ( x != minimumNode )
+ {
+ numRoots++;
+ x = x.getRight();
+ }
+ }
+
+ while ( numRoots > 0 )
+ {
+ // 5 d <- degree[x]
+ int degree = x.getDegree();
+ FibonacciHeapNode<E> next = x.getRight();
+
+ // 6 while A[d] != NIL
+ while ( nodeSequence.get( degree ) != null )
+ {
+ // 7 do y <- A[d]
+ FibonacciHeapNode<E> y = nodeSequence.get( degree );
+
+ // 8 if key[x] > key[y]
+ if ( compare( x, y ) > 0 )
+ {
+ // 9 exchange x <-> y
+ FibonacciHeapNode<E> pointer = y;
+ y = x;
+ x = pointer;
+ }
+
+ // 10 FIB-HEAP-LINK(H,y,x)
+ link( y, x );
+
+ // 11 A[d] <- NIL
+ nodeSequence.set( degree, null );
+
+ // 12 d <- d + 1
+ degree++;
+ }
+
+ // 13 A[d] <- x
+ nodeSequence.set( degree, x );
+
+ x = next;
+ numRoots--;
+ }
+
+ // 14 min[H] <- NIL
+ minimumNode = null;
+
+ // 15 for i <- 0 to D(n[H])
+ for ( FibonacciHeapNode<E> pointer : nodeSequence )
+ {
+ if ( pointer == null )
+ {
+ continue;
+ }
+ if ( minimumNode == null )
+ {
+ minimumNode = pointer;
+ }
+
+ // 16 if A[i] != NIL
+ // We've got a live one, add it to root list.
+ if ( minimumNode != null )
+ {
+ // First remove node from root list.
+ moveToRoot( pointer );
+ }
+ }
+ }
+
/**
* {@inheritDoc}
*/
@@ -235,79 +382,135 @@ public final class FibonacciHeap<E>
}
/**
- * {@inheritDoc}
+ * Implements the {@code CUT(H,x,y)} function.
+ *
+ * <pre>CUT(H,x,y)
+ * 1 remove x from the child list of y, decrementing degree[y]
+ * 2 add x to the root list of H
+ * 3 p[x] ← NIL
+ * 4 mark[x] ← FALSE</pre>
+ *
+ * @param x the node has to be removed from {@code y} children
+ * @param y the node has to be updated
*/
- public boolean isEmpty()
+ private void cut( FibonacciHeapNode<E> x, FibonacciHeapNode<E> y )
{
- return minimumNode == null;
+ // add x to the root list of H
+ moveToRoot( x );
+
+ // remove x from the child list of y, decrementing degree[y]
+ y.decraeseDegree();
+ // p[x] <- NIL
+ x.setParent( null );
+
+ // mark[x] <- FALSE
+ x.setMarked( false );
+ markedNodes--;
}
/**
* {@inheritDoc}
*/
- public Iterator<E> iterator()
+ public E element()
{
- throw new UnsupportedOperationException();
+ if ( isEmpty() )
+ {
+ throw new NoSuchElementException();
+ }
+ return peek();
}
/**
* {@inheritDoc}
*/
- public boolean remove( Object o )
+ public boolean isEmpty()
{
- throw new UnsupportedOperationException();
+ return minimumNode == null;
}
/**
* {@inheritDoc}
*/
- public boolean removeAll( Collection<?> c )
+ public Iterator<E> iterator()
{
throw new UnsupportedOperationException();
}
/**
- * {@inheritDoc}
+ * Implements the {@code FIB-HEAP-LINK(H, y, x)} function.
+ *
+ * <pre>FIB-HEAP-LINK(H, y, x)
+ * 1 remove y from the root list of H
+ * 2 make y a child of x, incrementing degree[x]
+ * 3 mark[y] FALSE</pre>
+ *
+ * @param y the node has to be removed from the root list
+ * @param x the node has to to become fater of {@code y}
*/
- public boolean retainAll( Collection<?> c )
+ private void link( FibonacciHeapNode<E> y, FibonacciHeapNode<E> x )
{
- throw new UnsupportedOperationException();
- }
+ // 1 remove y from the root list of H
+ y.getLeft().setRight( y.getRight() );
+ y.getRight().setLeft( y.getLeft() );
- /**
- * {@inheritDoc}
- */
- public int size()
- {
- return size;
- }
+ y.setParent( x );
- /**
- * {@inheritDoc}
- */
- public Object[] toArray()
- {
- throw new UnsupportedOperationException();
- }
+ if ( x.getChild() == null )
+ {
+ // 2 make y a child of x, incrementing degree[x]
+ x.setChild( y );
+ y.setRight( y );
+ y.setLeft( y );
+ }
+ else
+ {
+ y.setLeft( x.getChild() );
+ y.setRight( x.getChild().getRight() );
+ x.getChild().setRight( y );
+ y.getRight().setLeft( y );
+ }
- /**
- * {@inheritDoc}
- */
- public <T> T[] toArray( T[] a )
- {
- throw new UnsupportedOperationException();
+ x.incraeseDegree();
+
+ // 3 mark[y] <- FALSE
+ y.setMarked( false );
+ markedNodes++;
}
/**
- * {@inheritDoc}
+ * Moves the target node in the {@code H} root nodes.
+ *
+ * @param node the node has to be moved in the {@code H} root nodes
+ * @see #add(Object)
+ * @see #consolidate()
+ * @see #cut(FibonacciHeapNode, FibonacciHeapNode)
*/
- public E element()
+ private void moveToRoot( FibonacciHeapNode<E> node )
{
+ // 8' if min[H] = NIL
if ( isEmpty() )
{
- throw new NoSuchElementException();
+ // then min[H] <- x
+ minimumNode = node;
+ }
+ else
+ {
+ // 7 concatenate the root list containing x with root list H
+ node.getLeft().setRight( node.getRight() );
+ node.getRight().setLeft( node.getLeft() );
+
+ node.setLeft( minimumNode );
+ node.setRight( minimumNode.getRight() );
+ minimumNode.setRight( node );
+ node.getRight().setLeft( node );
+
+ // 8'' if key[x] < key[min[H]]
+ if ( compare( node, minimumNode ) < 0 )
+ {
+ // 9 then min[H] <- x
+ minimumNode = node;
+ }
}
- return peek();
}
/**
@@ -405,6 +608,17 @@ public final class FibonacciHeap<E>
return minimum;
}
+ /**
+ * The potential of Fibonacci heap {@code H} is then defined by
+ * {@code t(H) + 2m(H)}.
+ *
+ * @return The potential of this Fibonacci heap.
+ */
+ public int potential()
+ {
+ return trees + 2 * markedNodes;
+ }
+
/**
* {@inheritDoc}
*/
@@ -421,265 +635,51 @@ public final class FibonacciHeap<E>
}
/**
- * Implements the {@code CONSOLIDATE(H)} function.
- *
- * <pre>CONSOLIDATE(H)
- * 1 for i ← 0 to D(n[H])
- * 2 do A[i] ← NIL
- * 3 for each node w in the root list of H
- * 4 do x ← w
- * 5 d ← degree[x]
- * 6 while A[d] ≠ NIL
- * 7 do y ← A[d]
- * 8 if key[x] > key[y]
- * 9 then exchange x ↔ y
- * 10 FIB-HEAP-LINK(H,y,x)
- * 11 A[d] ← NIL
- * 12 d ← d + 1
- * 13 A[d] ← x
- * 14 min[H] ← NIL
- * 15 for i ← 0 to D(n[H])
- * 16 do if A[i] ≠ NIL
- * 17 then add A[i] to the root list of H
- * 18 if min[H] = NIL or key[A[i]] ≤ key[min[H]]
- * 19 then min[H] ← A[i]</pre>
+ * {@inheritDoc}
*/
- private void consolidate()
+ public boolean remove( Object o )
{
- if ( isEmpty() )
- {
- return;
- }
-
- // D( n[H] ) <= log_phi( n[H] )
- // -> log_phi( n[H] ) = log( n[H] ) / log( phi )
- // -> D( n[H] ) = log( n[H] ) / log( phi )
- int arraySize = ( (int) floor( log( size ) / LOG_PHI ) );
-
- // 1 for i <- 0 to D(n[H])
- List<FibonacciHeapNode<E>> nodeSequence = new ArrayList<FibonacciHeapNode<E>>( arraySize );
- for ( int i = 0; i < arraySize; i++ )
- {
- // 2 do A[i] <- NIL
- nodeSequence.add( i, null );
- }
-
- int numRoots = 0;
-
- // 3 for each node x in the root list of H
- // 4 do x ← w
- FibonacciHeapNode<E> x = minimumNode;
-
- if ( x != null )
- {
- numRoots++;
- x = x.getRight();
-
- while ( x != minimumNode )
- {
- numRoots++;
- x = x.getRight();
- }
- }
-
- while ( numRoots > 0 )
- {
- // 5 d <- degree[x]
- int degree = x.getDegree();
- FibonacciHeapNode<E> next = x.getRight();
-
- // 6 while A[d] != NIL
- while ( nodeSequence.get( degree ) != null )
- {
- // 7 do y <- A[d]
- FibonacciHeapNode<E> y = nodeSequence.get( degree );
-
- // 8 if key[x] > key[y]
- if ( compare( x, y ) > 0 )
- {
- // 9 exchange x <-> y
- FibonacciHeapNode<E> pointer = y;
- y = x;
- x = pointer;
- }
-
- // 10 FIB-HEAP-LINK(H,y,x)
- link( y, x );
-
- // 11 A[d] <- NIL
- nodeSequence.set( degree, null );
-
- // 12 d <- d + 1
- degree++;
- }
-
- // 13 A[d] <- x
- nodeSequence.set( degree, x );
-
- x = next;
- numRoots--;
- }
-
- // 14 min[H] <- NIL
- minimumNode = null;
-
- // 15 for i <- 0 to D(n[H])
- for ( FibonacciHeapNode<E> pointer : nodeSequence )
- {
- if ( pointer == null )
- {
- continue;
- }
- if ( minimumNode == null )
- {
- minimumNode = pointer;
- }
-
- // 16 if A[i] != NIL
- // We've got a live one, add it to root list.
- if ( minimumNode != null )
- {
- // First remove node from root list.
- moveToRoot( pointer );
- }
- }
+ throw new UnsupportedOperationException();
}
/**
- * Implements the {@code FIB-HEAP-LINK(H, y, x)} function.
- *
- * <pre>FIB-HEAP-LINK(H, y, x)
- * 1 remove y from the root list of H
- * 2 make y a child of x, incrementing degree[x]
- * 3 mark[y] FALSE</pre>
- *
- * @param y the node has to be removed from the root list
- * @param x the node has to to become fater of {@code y}
+ * {@inheritDoc}
*/
- private void link( FibonacciHeapNode<E> y, FibonacciHeapNode<E> x )
+ public boolean removeAll( Collection<?> c )
{
- // 1 remove y from the root list of H
- y.getLeft().setRight( y.getRight() );
- y.getRight().setLeft( y.getLeft() );
-
- y.setParent( x );
-
- if ( x.getChild() == null )
- {
- // 2 make y a child of x, incrementing degree[x]
- x.setChild( y );
- y.setRight( y );
- y.setLeft( y );
- }
- else
- {
- y.setLeft( x.getChild() );
- y.setRight( x.getChild().getRight() );
- x.getChild().setRight( y );
- y.getRight().setLeft( y );
- }
-
- x.incraeseDegree();
-
- // 3 mark[y] <- FALSE
- y.setMarked( false );
- markedNodes++;
+ throw new UnsupportedOperationException();
}
/**
- * Implements the {@code CUT(H,x,y)} function.
- *
- * <pre>CUT(H,x,y)
- * 1 remove x from the child list of y, decrementing degree[y]
- * 2 add x to the root list of H
- * 3 p[x] ← NIL
- * 4 mark[x] ← FALSE</pre>
- *
- * @param x the node has to be removed from {@code y} children
- * @param y the node has to be updated
+ * {@inheritDoc}
*/
- private void cut( FibonacciHeapNode<E> x, FibonacciHeapNode<E> y )
+ public boolean retainAll( Collection<?> c )
{
- // add x to the root list of H
- moveToRoot( x );
-
- // remove x from the child list of y, decrementing degree[y]
- y.decraeseDegree();
- // p[x] <- NIL
- x.setParent( null );
-
- // mark[x] <- FALSE
- x.setMarked( false );
- markedNodes--;
+ throw new UnsupportedOperationException();
}
/**
- * Implements the {@code CASCADING-CUT(H,y)} function.
- *
- * <pre>CASCADING-CUT(H,y)
- * 1 z ← p[y]
- * 2 if z ≠ NIL
- * 3 then if mark[y] = FALSE
- * 4 then mark[y] ← TRUE
- * 5 else CUT(H,y,z)
- * 6 CASCADING-CUT(H,z)</pre>
- *
- * @param y the target node to apply CASCADING-CUT
+ * {@inheritDoc}
*/
- private void cascadingCut( FibonacciHeapNode<E> y )
+ public int size()
{
- // z <- p[y]
- FibonacciHeapNode<E> z = y.getParent();
-
- // if z != NIL
- if ( z != null )
- {
- // if mark[y] = FALSE
- if ( !y.isMarked() )
- {
- // then mark[y] TRUE
- y.setMarked( true );
- markedNodes++;
- }
- else
- {
- // else CUT(H,y,z)
- cut( y, z );
- // CASCADING-CUT(H,z)
- cascadingCut( z );
- }
- }
+ return size;
}
/**
- * The potential of Fibonacci heap {@code H} is then defined by
- * {@code t(H) + 2m(H)}.
- *
- * @return The potential of this Fibonacci heap.
+ * {@inheritDoc}
*/
- public int potential()
+ public Object[] toArray()
{
- return trees + 2 * markedNodes;
+ throw new UnsupportedOperationException();
}
/**
- * Compare the given objects according to to the specified comparator if not null,
- * according to their natural ordering otherwise.
- *
- * @param o1 the first {@link FibonacciHeap} node to be compared
- * @param o2 the second {@link FibonacciHeap} node to be compared
- * @return a negative integer, zero, or a positive integer as the first argument is
- * less than, equal to, or greater than the second
+ * {@inheritDoc}
*/
- private int compare( FibonacciHeapNode<E> o1, FibonacciHeapNode<E> o2 )
+ public <T> T[] toArray( T[] a )
{
- if ( comparator != null )
- {
- return comparator.compare( o1.getElement(), o2.getElement() );
- }
- @SuppressWarnings( "unchecked" ) // it will throw a ClassCastException at runtime
- Comparable<? super E> o1Comparable = (Comparable<? super E>) o1.getElement();
- return o1Comparable.compareTo( o2.getElement() );
+ throw new UnsupportedOperationException();
}
/**
diff --git a/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java b/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java
index 154f708..b66e3c9 100644
--- a/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java
+++ b/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java
@@ -88,143 +88,143 @@ final class FibonacciHeapNode<E>
}
/**
- * Returns the reference to the parent node, if any.
+ * Decreases the degree of current node.
*
- * @return the reference to the parent node, if any.
+ * @see #getDegree()
*/
- public FibonacciHeapNode<E> getParent()
+ public void decraeseDegree()
{
- return parent;
+ degree--;
}
/**
- * Sets the reference to the parent node.
+ * Returns the reference to the first child node.
*
- * @param parent the reference to the parent node
+ * @return the reference to the first child node.
*/
- public void setParent( FibonacciHeapNode<E> parent )
+ public FibonacciHeapNode<E> getChild()
{
- this.parent = parent;
+ return child;
}
/**
- * Returns the left node reference.
+ * Returns the number of children in the child list of node {@code x} is stored in {@code degree[x]}.
*
- * @return the left node reference.
+ * @return the number of children in the child list of node {@code x} is stored in {@code degree[x]}.
*/
- public FibonacciHeapNode<E> getLeft()
+ public int getDegree()
{
- return left;
+ return degree;
}
/**
- * Sets the left node reference.
+ * Returns the adapted element by this node.
*
- * @param left the left node reference.
+ * @return the adapted element by this node.
*/
- public void setLeft( FibonacciHeapNode<E> left )
+ public E getElement()
{
- this.left = left;
+ return element;
}
/**
- * Returns the right node reference.
+ * Returns the left node reference.
*
- * @return the right node reference.
+ * @return the left node reference.
*/
- public FibonacciHeapNode<E> getRight()
+ public FibonacciHeapNode<E> getLeft()
{
- return right;
+ return left;
}
/**
- * Sets the right node reference.
+ * Returns the reference to the parent node, if any.
*
- * @param left the right node reference.
+ * @return the reference to the parent node, if any.
*/
- public void setRight( FibonacciHeapNode<E> right )
+ public FibonacciHeapNode<E> getParent()
{
- this.right = right;
+ return parent;
}
/**
- * Returns the reference to the first child node.
+ * Returns the right node reference.
*
- * @return the reference to the first child node.
+ * @return the right node reference.
*/
- public FibonacciHeapNode<E> getChild()
+ public FibonacciHeapNode<E> getRight()
{
- return child;
+ return right;
}
/**
- * Sets the reference to the first child node.
+ * Increases the degree of current node.
*
- * @param child the reference to the first child node.
+ * @see #getDegree()
*/
- public void setChild( FibonacciHeapNode<E> child )
+ public void incraeseDegree()
{
- this.child = child;
+ degree++;
}
/**
- * Returns the number of children in the child list of node {@code x} is stored in {@code degree[x]}.
+ * Returns the current node mark status.
*
- * @return the number of children in the child list of node {@code x} is stored in {@code degree[x]}.
+ * @return true, if the node is marked, false otherwise.
*/
- public int getDegree()
+ public boolean isMarked()
{
- return degree;
+ return marked;
}
/**
- * Increases the degree of current node.
+ * Sets the reference to the first child node.
*
- * @see #getDegree()
+ * @param child the reference to the first child node.
*/
- public void incraeseDegree()
+ public void setChild( FibonacciHeapNode<E> child )
{
- degree++;
+ this.child = child;
}
/**
- * Decreases the degree of current node.
+ * Sets the left node reference.
*
- * @see #getDegree()
+ * @param left the left node reference.
*/
- public void decraeseDegree()
+ public void setLeft( FibonacciHeapNode<E> left )
{
- degree--;
+ this.left = left;
}
/**
- * Returns the current node mark status.
+ * Flags the current node as marked.
*
- * @return true, if the node is marked, false otherwise.
+ * @param marked the current node mark status.
*/
- public boolean isMarked()
+ public void setMarked( boolean marked )
{
- return marked;
+ this.marked = marked;
}
/**
- * Flags the current node as marked.
+ * Sets the reference to the parent node.
*
- * @param marked the current node mark status.
+ * @param parent the reference to the parent node
*/
- public void setMarked( boolean marked )
+ public void setParent( FibonacciHeapNode<E> parent )
{
- this.marked = marked;
+ this.parent = parent;
}
/**
- * Returns the adapted element by this node.
+ * Sets the right node reference.
*
- * @return the adapted element by this node.
+ * @param left the right node reference.
*/
- public E getElement()
+ public void setRight( FibonacciHeapNode<E> right )
{
- return element;
+ this.right = right;
}
/**
diff --git a/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java b/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java
index f3d10c6..f6126c1 100644
--- a/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java
+++ b/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java
@@ -68,14 +68,14 @@ public final class ColoredVertices<V, C>
}
/**
- * Remove the input vertex color.
- *
- * @param v the vertex for which storing the color.
+ * Tests if the 'vertex' is colored.
+ *
+ * @param vertex the vertex
+ * @return true if the colored vertex is contained into the map, false otherwise
*/
- void removeColor( V v )
+ public boolean containsColoredVertex( V vertex )
{
- C color = coloredVertices.remove( v );
- usedColor.remove( color );
+ return coloredVertices.containsKey( vertex );
}
/**
@@ -102,14 +102,14 @@ public final class ColoredVertices<V, C>
}
/**
- * Tests if the 'vertex' is colored.
- *
- * @param vertex the vertex
- * @return true if the colored vertex is contained into the map, false otherwise
+ * Remove the input vertex color.
+ *
+ * @param v the vertex for which storing the color.
*/
- public boolean containsColoredVertex( V vertex )
+ void removeColor( V v )
{
- return coloredVertices.containsKey( vertex );
+ C color = coloredVertices.remove( v );
+ usedColor.remove( color );
}
}
diff --git a/src/main/java/org/apache/commons/graph/coloring/ColoringAlgorithmsSelector.java b/src/main/java/org/apache/commons/graph/coloring/ColoringAlgorithmsSelector.java
index c4171cf..d2eb300 100644
--- a/src/main/java/org/apache/commons/graph/coloring/ColoringAlgorithmsSelector.java
+++ b/src/main/java/org/apache/commons/graph/coloring/ColoringAlgorithmsSelector.java
@@ -30,13 +30,6 @@ package org.apache.commons.graph.coloring;
public interface ColoringAlgorithmsSelector<V, E, C>
{
- /**
- * Colors the graph such that no two adjacent vertices share the same color.
- *
- * @return The color - vertex association.
- */
- ColoredVertices<V, C> applyingGreedyAlgorithm();
-
/**
* Graph m-coloring algorithm. This algorithm uses a brute-force backtracking
* procedure to find a graph color.
@@ -54,4 +47,11 @@ public interface ColoringAlgorithmsSelector<V, E, C>
*/
ColoredVertices<V, C> applyingBackTrackingAlgorithm( ColoredVertices<V, C> partialColoredVertex );
+ /**
+ * Colors the graph such that no two adjacent vertices share the same color.
+ *
+ * @return The color - vertex association.
+ */
+ ColoredVertices<V, C> applyingGreedyAlgorithm();
+
}
diff --git a/src/main/java/org/apache/commons/graph/coloring/DefaultColoringAlgorithmsSelector.java b/src/main/java/org/apache/commons/graph/coloring/DefaultColoringAlgorithmsSelector.java
index 485bf4e..6bd055b 100644
--- a/src/main/java/org/apache/commons/graph/coloring/DefaultColoringAlgorithmsSelector.java
+++ b/src/main/java/org/apache/commons/graph/coloring/DefaultColoringAlgorithmsSelector.java
@@ -49,6 +49,39 @@ final class DefaultColoringAlgorithmsSelector<V, E, C>
this.colors = colors;
}
+ /**
+ * {@inheritDoc}
+ */
+ public ColoredVertices<V, C> applyingBackTrackingAlgorithm()
+ {
+ return applyingBackTrackingAlgorithm( new ColoredVertices<V, C>() );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public ColoredVertices<V, C> applyingBackTrackingAlgorithm( ColoredVertices<V, C> partialColoredVertex )
+ {
+ partialColoredVertex = checkNotNull( partialColoredVertex, "PartialColoredVertex must be not null" );
+
+ final List<V> verticesList = new ArrayList<V>();
+
+ for ( V v : g.getVertices() )
+ {
+ if ( !partialColoredVertex.containsColoredVertex( v ) )
+ {
+ verticesList.add( v );
+ }
+ }
+
+ if ( backtraking( -1, verticesList, partialColoredVertex ) )
+ {
+ return partialColoredVertex;
+ }
+
+ throw new NotEnoughColorsException( colors );
+ }
+
/**
* {@inheritDoc}
*/
@@ -111,39 +144,6 @@ final class DefaultColoringAlgorithmsSelector<V, E, C>
return coloredVertices;
}
- /**
- * {@inheritDoc}
- */
- public ColoredVertices<V, C> applyingBackTrackingAlgorithm()
- {
- return applyingBackTrackingAlgorithm( new ColoredVertices<V, C>() );
- }
-
- /**
- * {@inheritDoc}
- */
- public ColoredVertices<V, C> applyingBackTrackingAlgorithm( ColoredVertices<V, C> partialColoredVertex )
- {
- partialColoredVertex = checkNotNull( partialColoredVertex, "PartialColoredVertex must be not null" );
-
- final List<V> verticesList = new ArrayList<V>();
-
- for ( V v : g.getVertices() )
- {
- if ( !partialColoredVertex.containsColoredVertex( v ) )
- {
- verticesList.add( v );
- }
- }
-
- if ( backtraking( -1, verticesList, partialColoredVertex ) )
- {
- return partialColoredVertex;
- }
-
- throw new NotEnoughColorsException( colors );
- }
-
/**
* This is the recursive step.
*
diff --git a/src/main/java/org/apache/commons/graph/connectivity/ConnectivityBuilder.java b/src/main/java/org/apache/commons/graph/connectivity/ConnectivityBuilder.java
index 5c3ad3b..1c4af5f 100644
--- a/src/main/java/org/apache/commons/graph/connectivity/ConnectivityBuilder.java
+++ b/src/main/java/org/apache/commons/graph/connectivity/ConnectivityBuilder.java
@@ -29,18 +29,18 @@ public interface ConnectivityBuilder<V, E>
{
/**
- * Specifies the set of vertices included into a connected component.
+ * Find all the connected components included into the specified graph
*
- * @param vertices the set of vertices included into a connected component.
* @return the connectivity algorithm selector.
*/
- ConnectivityAlgorithmsSelector<V, E> includingVertices( V... vertices );
+ ConnectivityAlgorithmsSelector<V, E> includingAllVertices();
/**
- * Find all the connected components included into the specified graph
+ * Specifies the set of vertices included into a connected component.
*
+ * @param vertices the set of vertices included into a connected component.
* @return the connectivity algorithm selector.
*/
- ConnectivityAlgorithmsSelector<V, E> includingAllVertices();
+ ConnectivityAlgorithmsSelector<V, E> includingVertices( V... vertices );
}
diff --git a/src/main/java/org/apache/commons/graph/connectivity/DefaultConnectivityBuilder.java b/src/main/java/org/apache/commons/graph/connectivity/DefaultConnectivityBuilder.java
index 5992d09..73fc57d 100644
--- a/src/main/java/org/apache/commons/graph/connectivity/DefaultConnectivityBuilder.java
+++ b/src/main/java/org/apache/commons/graph/connectivity/DefaultConnectivityBuilder.java
@@ -48,19 +48,19 @@ public class DefaultConnectivityBuilder<V, E>
/**
* {@inheritDoc}
*/
- public ConnectivityAlgorithmsSelector<V, E> includingVertices( V... vertices )
+ public ConnectivityAlgorithmsSelector<V, E> includingAllVertices()
{
- vertices = checkNotNull( vertices,
- "Graph connectivity cannote be applied on null vertices array, use no-args if you intend specify no vertices" );
- return new DefaultConnectivityAlgorithmsSelector<V, E>( graph, asList( vertices ) );
+ return new DefaultConnectivityAlgorithmsSelector<V, E>( graph, graph.getVertices() );
}
/**
* {@inheritDoc}
*/
- public ConnectivityAlgorithmsSelector<V, E> includingAllVertices()
+ public ConnectivityAlgorithmsSelector<V, E> includingVertices( V... vertices )
{
- return new DefaultConnectivityAlgorithmsSelector<V, E>( graph, graph.getVertices() );
+ vertices = checkNotNull( vertices,
+ "Graph connectivity cannote be applied on null vertices array, use no-args if you intend specify no vertices" );
+ return new DefaultConnectivityAlgorithmsSelector<V, E>( graph, asList( vertices ) );
}
}
diff --git a/src/main/java/org/apache/commons/graph/elo/Category.java b/src/main/java/org/apache/commons/graph/elo/Category.java
index d677816..36f4673 100644
--- a/src/main/java/org/apache/commons/graph/elo/Category.java
+++ b/src/main/java/org/apache/commons/graph/elo/Category.java
@@ -26,17 +26,17 @@ public interface Category
{
/**
- * Returns the category minimum points.
+ * Returns the category maximum points.
*
- * @return the category minimum points.
+ * @return the category maximum points.
*/
- Double getMinimum();
+ Double getMaximum();
/**
- * Returns the category maximum points.
+ * Returns the category minimum points.
*
- * @return the category maximum points.
+ * @return the category minimum points.
*/
- Double getMaximum();
+ Double getMinimum();
}
diff --git a/src/main/java/org/apache/commons/graph/elo/DefaultKFactorBuilder.java b/src/main/java/org/apache/commons/graph/elo/DefaultKFactorBuilder.java
index 83e099c..1b12aed 100644
--- a/src/main/java/org/apache/commons/graph/elo/DefaultKFactorBuilder.java
+++ b/src/main/java/org/apache/commons/graph/elo/DefaultKFactorBuilder.java
@@ -33,6 +33,11 @@ final class DefaultKFactorBuilder<P>
private static final int DEFAULT_K_FACTOR = 32;
+ private static double calculateEFactor( double qA, double qB )
+ {
+ return qA / ( qA + qB );
+ }
+
private final DirectedGraph<P, GameResult> tournamentGraph;
private final PlayersRank<P> playerRanking;
@@ -44,22 +49,10 @@ final class DefaultKFactorBuilder<P>
this.playerRanking = playerRanking;
}
- public void withDefaultKFactor()
- {
- withKFactor( DEFAULT_K_FACTOR );
- }
-
- public void withKFactor( int kFactor )
+ private double calculateQFactor( P player )
{
- // TODO find a way to improve performances, this impl is just a spike
- for ( P player : tournamentGraph.getVertices() )
- {
- for ( P opponent : tournamentGraph.getOutbound( player ) )
- {
- GameResult gameResult = tournamentGraph.getEdge( player, opponent );
- evaluateMatch( player, gameResult, opponent, kFactor );
- }
- }
+ double ranking = playerRanking.getRanking( player );
+ return pow( DEFAULT_POW_BASE, ranking / DEFAULT_DIVISOR );
}
private boolean evaluateMatch( P playerA, GameResult gameResult, P playerB, int kFactor )
@@ -94,21 +87,28 @@ final class DefaultKFactorBuilder<P>
return true;
}
- private double calculateQFactor( P player )
+ private void updateRanking( P player, double kFactor, double sFactor, double eFactor )
{
- double ranking = playerRanking.getRanking( player );
- return pow( DEFAULT_POW_BASE, ranking / DEFAULT_DIVISOR );
+ double newRanking = playerRanking.getRanking( player ) + ( kFactor * ( sFactor - eFactor ) );
+ playerRanking.updateRanking( player, newRanking );
}
- private static double calculateEFactor( double qA, double qB )
+ public void withDefaultKFactor()
{
- return qA / ( qA + qB );
+ withKFactor( DEFAULT_K_FACTOR );
}
- private void updateRanking( P player, double kFactor, double sFactor, double eFactor )
+ public void withKFactor( int kFactor )
{
- double newRanking = playerRanking.getRanking( player ) + ( kFactor * ( sFactor - eFactor ) );
- playerRanking.updateRanking( player, newRanking );
+ // TODO find a way to improve performances, this impl is just a spike
+ for ( P player : tournamentGraph.getVertices() )
+ {
+ for ( P opponent : tournamentGraph.getOutbound( player ) )
+ {
+ GameResult gameResult = tournamentGraph.getEdge( player, opponent );
+ evaluateMatch( player, gameResult, opponent, kFactor );
+ }
+ }
}
}
diff --git a/src/main/java/org/apache/commons/graph/export/AbstractExporter.java b/src/main/java/org/apache/commons/graph/export/AbstractExporter.java
index 3558085..9a0ffc9 100644
--- a/src/main/java/org/apache/commons/graph/export/AbstractExporter.java
+++ b/src/main/java/org/apache/commons/graph/export/AbstractExporter.java
@@ -62,6 +62,50 @@ abstract class AbstractExporter<V, E, T extends AbstractExporter<V, E, T>>
this.name = name != null ? name : G;
}
+ protected void addEdgeProperty( String propertyName, Mapper<E, ?> edgeProperty )
+ {
+ this.edgeProperties.put( propertyName, edgeProperty );
+ }
+
+ protected void addVertexProperty( String propertyName, Mapper<V, ?> vertexProperty )
+ {
+ this.vertexProperties.put( propertyName, vertexProperty );
+ }
+
+ protected abstract void comment( String text )
+ throws Exception;
+
+ protected abstract void edge( E edge, V head, V tail, Map<String, Object> properties )
+ throws Exception;
+
+ protected abstract void endGraph()
+ throws Exception;
+
+ protected abstract void endSerialization()
+ throws Exception;
+
+ protected abstract void enlistEdgesProperty( String name, Class<?> type )
+ throws Exception;
+
+ protected abstract void enlistVerticesProperty( String name, Class<?> type )
+ throws Exception;
+
+ protected final Graph<V, E> getGraph()
+ {
+ return graph;
+ }
+
+ protected final Writer getWriter()
+ {
+ return writer;
+ }
+
+ protected abstract void startGraph( String name )
+ throws Exception;
+
+ protected abstract void startSerialization()
+ throws Exception;
+
public final void to( File outputFile )
throws GraphExportException
{
@@ -151,51 +195,7 @@ abstract class AbstractExporter<V, E, T extends AbstractExporter<V, E, T>>
}
}
- protected final Graph<V, E> getGraph()
- {
- return graph;
- }
-
- protected final Writer getWriter()
- {
- return writer;
- }
-
- protected void addVertexProperty( String propertyName, Mapper<V, ?> vertexProperty )
- {
- this.vertexProperties.put( propertyName, vertexProperty );
- }
-
- protected void addEdgeProperty( String propertyName, Mapper<E, ?> edgeProperty )
- {
- this.edgeProperties.put( propertyName, edgeProperty );
- }
-
- protected abstract void startSerialization()
- throws Exception;
-
- protected abstract void endSerialization()
- throws Exception;
-
- protected abstract void startGraph( String name )
- throws Exception;
-
- protected abstract void endGraph()
- throws Exception;
-
- protected abstract void comment( String text )
- throws Exception;
-
- protected abstract void enlistVerticesProperty( String name, Class<?> type )
- throws Exception;
-
- protected abstract void enlistEdgesProperty( String name, Class<?> type )
- throws Exception;
-
protected abstract void vertex( V vertex, Map<String, Object> properties )
throws Exception;
- protected abstract void edge( E edge, V head, V tail, Map<String, Object> properties )
- throws Exception;
-
}
diff --git a/src/main/java/org/apache/commons/graph/export/DotExporter.java b/src/main/java/org/apache/commons/graph/export/DotExporter.java
index 3106558..44bfbf9 100644
--- a/src/main/java/org/apache/commons/graph/export/DotExporter.java
+++ b/src/main/java/org/apache/commons/graph/export/DotExporter.java
@@ -62,51 +62,23 @@ final class DotExporter<V, E>
this.vertexIdentifiers = generateVertexIdentifiers( graph );
}
- private Map<V, Integer> generateVertexIdentifiers( Graph<V, E> graph )
- {
- Map<V, Integer> vertexIdentifiers = new HashMap<V, Integer>();
- int count = 1;
-
- for ( V vertex : graph.getVertices() )
- {
- vertexIdentifiers.put( vertex, count++ );
- }
-
- return vertexIdentifiers;
- }
-
- @Override
- protected void startSerialization()
- throws Exception
- {
- printWriter = new PrintWriter( getWriter() );
- }
-
@Override
- protected void endSerialization()
+ protected void comment( String text )
throws Exception
{
- // do nothing?
+ printWriter.write( text );
}
@Override
- protected void startGraph( String name )
+ protected void edge( E edge, V head, V tail, Map<String, Object> properties )
throws Exception
{
- String graphDeclaration;
-
- if ( getGraph() instanceof DirectedGraph )
- {
- graphDeclaration = DIGRAPH;
- connector = DICONNECTOR;
- }
- else
- {
- graphDeclaration = GRAPH;
- connector = CONNECTOR;
- }
+ printWriter.format( " %s %s %s",
+ vertexIdentifiers.get( head ),
+ connector,
+ vertexIdentifiers.get( tail ) );
- printWriter.format( "%s %s {%n", graphDeclaration, name );
+ printVertexOrEdgeProperties( properties );
}
@Override
@@ -117,17 +89,10 @@ final class DotExporter<V, E>
}
@Override
- protected void comment( String text )
- throws Exception
- {
- printWriter.write( text );
- }
-
- @Override
- protected void enlistVerticesProperty( String name, Class<?> type )
+ protected void endSerialization()
throws Exception
{
- // not needed in DOT
+ // do nothing?
}
@Override
@@ -139,24 +104,23 @@ final class DotExporter<V, E>
}
@Override
- protected void vertex( V vertex, Map<String, Object> properties )
+ protected void enlistVerticesProperty( String name, Class<?> type )
throws Exception
{
- printWriter.format( " %s", vertexIdentifiers.get( vertex ) );
-
- printVertexOrEdgeProperties( properties );
+ // not needed in DOT
}
- @Override
- protected void edge( E edge, V head, V tail, Map<String, Object> properties )
- throws Exception
+ private Map<V, Integer> generateVertexIdentifiers( Graph<V, E> graph )
{
- printWriter.format( " %s %s %s",
- vertexIdentifiers.get( head ),
- connector,
- vertexIdentifiers.get( tail ) );
+ Map<V, Integer> vertexIdentifiers = new HashMap<V, Integer>();
+ int count = 1;
- printVertexOrEdgeProperties( properties );
+ for ( V vertex : graph.getVertices() )
+ {
+ vertexIdentifiers.put( vertex, count++ );
+ }
+
+ return vertexIdentifiers;
}
private void printVertexOrEdgeProperties( Map<String, Object> properties )
@@ -177,10 +141,40 @@ final class DotExporter<V, E>
}
}
- public <N extends Number> DotExporter<V, E> withEdgeWeights( Mapper<E, N> edgeWeights )
+ @Override
+ protected void startGraph( String name )
+ throws Exception
{
- super.addEdgeProperty( WEIGHT, edgeWeights );
- return this;
+ String graphDeclaration;
+
+ if ( getGraph() instanceof DirectedGraph )
+ {
+ graphDeclaration = DIGRAPH;
+ connector = DICONNECTOR;
+ }
+ else
+ {
+ graphDeclaration = GRAPH;
+ connector = CONNECTOR;
+ }
+
+ printWriter.format( "%s %s {%n", graphDeclaration, name );
+ }
+
+ @Override
+ protected void startSerialization()
+ throws Exception
+ {
+ printWriter = new PrintWriter( getWriter() );
+ }
+
+ @Override
+ protected void vertex( V vertex, Map<String, Object> properties )
+ throws Exception
+ {
+ printWriter.format( " %s", vertexIdentifiers.get( vertex ) );
+
+ printVertexOrEdgeProperties( properties );
}
public DotExporter<V, E> withEdgeLabels( Mapper<E, String> edgeLabels )
@@ -189,6 +183,12 @@ final class DotExporter<V, E>
return this;
}
+ public <N extends Number> DotExporter<V, E> withEdgeWeights( Mapper<E, N> edgeWeights )
+ {
+ super.addEdgeProperty( WEIGHT, edgeWeights );
+ return this;
+ }
+
public DotExporter<V, E> withVertexLabels( Mapper<V, String> vertexLabels )
{
super.addVertexProperty( LABEL, vertexLabels );
diff --git a/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java b/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
index 1c2d7aa..2a59386 100644
--- a/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
+++ b/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
@@ -86,6 +86,31 @@ final class GraphMLExporter<V, E>
private static final String CDATA_TYPE = "CDATA";
+ private static <T> String getStringType( Class<T> type )
+ {
+ if ( Integer.class == type )
+ {
+ return INT;
+ }
+ else if ( Long.class == type )
+ {
+ return LONG;
+ }
+ else if ( Float.class == type )
+ {
+ return FLOAT;
+ }
+ else if ( Double.class == type )
+ {
+ return DOUBLE;
+ }
+ else if ( Boolean.class == type )
+ {
+ return BOOLEAN;
+ }
+ return STRING;
+ }
+
private TransformerHandler transformerHandler;
GraphMLExporter( Graph<V, E> graph, String name )
@@ -94,33 +119,25 @@ final class GraphMLExporter<V, E>
}
@Override
- protected void startSerialization()
+ protected void comment( String text )
throws Exception
{
- transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
- transformerHandler.setResult( new StreamResult( getWriter() ) );
- transformerHandler.startDocument();
+ transformerHandler.comment( text.toCharArray(), 0, text.length() );
}
@Override
- protected void endSerialization()
+ protected void edge( E edge, V head, V tail, Map<String, Object> properties )
throws Exception
{
- transformerHandler.endDocument();
- getWriter().flush();
- getWriter().close();
- }
+ AttributesImpl atts = new AttributesImpl();
+ atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, String.valueOf( edge.hashCode() ) );
+ atts.addAttribute( GRAPHML_XMLNS, SOURCE, SOURCE, CDATA_TYPE, String.valueOf( getGraph().getVertices( edge ).getHead().hashCode() ) );
+ atts.addAttribute( GRAPHML_XMLNS, TARGET, TARGET, CDATA_TYPE, String.valueOf( getGraph().getVertices( edge ).getTail().hashCode() ) );
+ transformerHandler.startElement( GRAPHML_XMLNS, EDGE, EDGE, atts );
- @Override
- protected void startGraph( String name )
- throws Exception
- {
- transformerHandler.startElement( GRAPHML_XMLNS, GRAPHML, GRAPHML, new AttributesImpl() );
+ // TODO print properties
- AttributesImpl atts = new AttributesImpl();
- atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, name );
- atts.addAttribute( GRAPHML_XMLNS, EDGEDEFAULT, EDGEDEFAULT, CDATA_TYPE, DIRECTED );
- transformerHandler.startElement( GRAPHML_XMLNS, GRAPH, GRAPH, atts );
+ transformerHandler.endElement( GRAPHML_XMLNS, NODE, NODE );
}
@Override
@@ -132,17 +149,12 @@ final class GraphMLExporter<V, E>
}
@Override
- protected void comment( String text )
- throws Exception
- {
- transformerHandler.comment( text.toCharArray(), 0, text.length() );
- }
-
- @Override
- protected void enlistVerticesProperty( String name, Class<?> type )
+ protected void endSerialization()
throws Exception
{
- enlistProperty( name, type, NODE );
+ transformerHandler.endDocument();
+ getWriter().flush();
+ getWriter().close();
}
@Override
@@ -165,56 +177,50 @@ final class GraphMLExporter<V, E>
}
@Override
- protected void vertex( V vertex, Map<String, Object> properties )
+ protected void enlistVerticesProperty( String name, Class<?> type )
throws Exception
{
- AttributesImpl atts = new AttributesImpl();
- atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, String.valueOf( vertex.hashCode() ) );
- transformerHandler.startElement( GRAPHML_XMLNS, NODE, NODE, atts );
+ enlistProperty( name, type, NODE );
+ }
- // TODO print properties
+ @Override
+ protected void startGraph( String name )
+ throws Exception
+ {
+ transformerHandler.startElement( GRAPHML_XMLNS, GRAPHML, GRAPHML, new AttributesImpl() );
- transformerHandler.endElement( GRAPHML_XMLNS, NODE, NODE );
+ AttributesImpl atts = new AttributesImpl();
+ atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, name );
+ atts.addAttribute( GRAPHML_XMLNS, EDGEDEFAULT, EDGEDEFAULT, CDATA_TYPE, DIRECTED );
+ transformerHandler.startElement( GRAPHML_XMLNS, GRAPH, GRAPH, atts );
}
@Override
- protected void edge( E edge, V head, V tail, Map<String, Object> properties )
+ protected void startSerialization()
+ throws Exception
+ {
+ transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
+ transformerHandler.setResult( new StreamResult( getWriter() ) );
+ transformerHandler.startDocument();
+ }
+
+ @Override
+ protected void vertex( V vertex, Map<String, Object> properties )
throws Exception
{
AttributesImpl atts = new AttributesImpl();
- atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, String.valueOf( edge.hashCode() ) );
- atts.addAttribute( GRAPHML_XMLNS, SOURCE, SOURCE, CDATA_TYPE, String.valueOf( getGraph().getVertices( edge ).getHead().hashCode() ) );
- atts.addAttribute( GRAPHML_XMLNS, TARGET, TARGET, CDATA_TYPE, String.valueOf( getGraph().getVertices( edge ).getTail().hashCode() ) );
- transformerHandler.startElement( GRAPHML_XMLNS, EDGE, EDGE, atts );
+ atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, String.valueOf( vertex.hashCode() ) );
+ transformerHandler.startElement( GRAPHML_XMLNS, NODE, NODE, atts );
// TODO print properties
transformerHandler.endElement( GRAPHML_XMLNS, NODE, NODE );
}
- private static <T> String getStringType( Class<T> type )
+ public GraphMLExporter<V, E> withEdgeLabels( Mapper<E, String> edgeLabels )
{
- if ( Integer.class == type )
- {
- return INT;
- }
- else if ( Long.class == type )
- {
- return LONG;
- }
- else if ( Float.class == type )
- {
- return FLOAT;
- }
- else if ( Double.class == type )
- {
- return DOUBLE;
- }
- else if ( Boolean.class == type )
- {
- return BOOLEAN;
- }
- return STRING;
+ super.addEdgeProperty( LABEL, edgeLabels );
+ return this;
}
public <N extends Number> GraphMLExporter<V, E> withEdgeWeights( Mapper<E, N> edgeWeights )
@@ -223,21 +229,15 @@ final class GraphMLExporter<V, E>
return this;
}
- public <N extends Number> GraphMLExporter<V, E> withVertexWeights( Mapper<V, N> vertexWeights )
- {
- super.addVertexProperty( WEIGHT, vertexWeights );
- return this;
- }
-
- public GraphMLExporter<V, E> withEdgeLabels( Mapper<E, String> edgeLabels )
+ public GraphMLExporter<V, E> withVertexLabels( Mapper<V, String> vertexLabels )
{
- super.addEdgeProperty( LABEL, edgeLabels );
+ super.addVertexProperty( LABEL, vertexLabels );
return this;
}
- public GraphMLExporter<V, E> withVertexLabels( Mapper<V, String> vertexLabels )
+ public <N extends Number> GraphMLExporter<V, E> withVertexWeights( Mapper<V, N> vertexWeights )
{
- super.addVertexProperty( LABEL, vertexLabels );
+ super.addVertexProperty( WEIGHT, vertexWeights );
return this;
}
diff --git a/src/main/java/org/apache/commons/graph/flow/DefaultMaxFlowAlgorithmSelector.java b/src/main/java/org/apache/commons/graph/flow/DefaultMaxFlowAlgorithmSelector.java
index 1bdd041..1babbf1 100644
--- a/src/main/java/org/apache/commons/graph/flow/DefaultMaxFlowAlgorithmSelector.java
+++ b/src/main/java/org/apache/commons/graph/flow/DefaultMaxFlowAlgorithmSelector.java
@@ -40,6 +40,54 @@ final class DefaultMaxFlowAlgorithmSelector<V, WE, W>
implements MaxFlowAlgorithmSelector<V, WE, W>
{
+ private static final class EdgeWrapper<WE>
+ {
+
+ private final WE wrapped;
+
+ public EdgeWrapper()
+ {
+ this( null );
+ }
+
+ public EdgeWrapper( WE wrapped )
+ {
+ this.wrapped = wrapped;
+ }
+
+ public WE getWrapped()
+ {
+ return wrapped;
+ }
+
+ }
+
+ @SuppressWarnings( "serial" ) //the algorithm isn't serializable.
+ private static final class MapperWrapper<WE, W, WO extends OrderedMonoid<W>>
+ implements Mapper<EdgeWrapper<WE>, W>
+ {
+
+ private final WO weightOperations;
+
+ private final Mapper<WE, W> weightedEdges;
+
+ public MapperWrapper( WO weightOperations, Mapper<WE, W> weightedEdges )
+ {
+ this.weightOperations = weightOperations;
+ this.weightedEdges = weightedEdges;
+ }
+
+ public W map( EdgeWrapper<WE> input )
+ {
+ if ( input.getWrapped() == null )
+ {
+ return weightOperations.identity();
+ }
+ return weightedEdges.map( input.getWrapped() );
+ }
+
+ }
+
private final DirectedGraph<V, WE> graph;
private final Mapper<WE, W> weightedEdges;
@@ -59,7 +107,7 @@ final class DefaultMaxFlowAlgorithmSelector<V, WE, W>
/**
* {@inheritDoc}
*/
- public <WO extends OrderedMonoid<W>> W applyingFordFulkerson( WO weightOperations )
+ public <WO extends OrderedMonoid<W>> W applyingEdmondsKarp( WO weightOperations )
{
final WO checkedWeightOperations = checkNotNull( weightOperations, "Weight operations can not be null to find the max flow in the graph" );
@@ -70,15 +118,15 @@ final class DefaultMaxFlowAlgorithmSelector<V, WE, W>
final FlowNetworkHandler<V, EdgeWrapper<WE>, W> flowNetworkHandler =
new FlowNetworkHandler<V, EdgeWrapper<WE>, W>( flowNetwork, source, target, checkedWeightOperations, new MapperWrapper<WE, W, WO>( checkedWeightOperations, weightedEdges ) );
- // perform depth first search
- visit( flowNetwork ).from( source ).applyingDepthFirstSearch( flowNetworkHandler );
+ // perform breadth first search
+ visit( flowNetwork ).from( source ).applyingBreadthFirstSearch( flowNetworkHandler );
while ( flowNetworkHandler.hasAugmentingPath() )
{
// update flow network
flowNetworkHandler.updateResidualNetworkWithCurrentAugmentingPath();
- // look for another augmenting path with depth first search
- visit( flowNetwork ).from( source ).applyingDepthFirstSearch( flowNetworkHandler );
+ // look for another augmenting path with breadth first search
+ visit( flowNetwork ).from( source ).applyingBreadthFirstSearch( flowNetworkHandler );
}
return flowNetworkHandler.onCompleted();
@@ -87,7 +135,7 @@ final class DefaultMaxFlowAlgorithmSelector<V, WE, W>
/**
* {@inheritDoc}
*/
- public <WO extends OrderedMonoid<W>> W applyingEdmondsKarp( WO weightOperations )
+ public <WO extends OrderedMonoid<W>> W applyingFordFulkerson( WO weightOperations )
{
final WO checkedWeightOperations = checkNotNull( weightOperations, "Weight operations can not be null to find the max flow in the graph" );
@@ -98,15 +146,15 @@ final class DefaultMaxFlowAlgorithmSelector<V, WE, W>
final FlowNetworkHandler<V, EdgeWrapper<WE>, W> flowNetworkHandler =
new FlowNetworkHandler<V, EdgeWrapper<WE>, W>( flowNetwork, source, target, checkedWeightOperations, new MapperWrapper<WE, W, WO>( checkedWeightOperations, weightedEdges ) );
- // perform breadth first search
- visit( flowNetwork ).from( source ).applyingBreadthFirstSearch( flowNetworkHandler );
+ // perform depth first search
+ visit( flowNetwork ).from( source ).applyingDepthFirstSearch( flowNetworkHandler );
while ( flowNetworkHandler.hasAugmentingPath() )
{
// update flow network
flowNetworkHandler.updateResidualNetworkWithCurrentAugmentingPath();
- // look for another augmenting path with breadth first search
- visit( flowNetwork ).from( source ).applyingBreadthFirstSearch( flowNetworkHandler );
+ // look for another augmenting path with depth first search
+ visit( flowNetwork ).from( source ).applyingDepthFirstSearch( flowNetworkHandler );
}
return flowNetworkHandler.onCompleted();
@@ -144,52 +192,4 @@ final class DefaultMaxFlowAlgorithmSelector<V, WE, W>
} );
}
- private static final class EdgeWrapper<WE>
- {
-
- private final WE wrapped;
-
- public EdgeWrapper()
- {
- this( null );
- }
-
- public EdgeWrapper( WE wrapped )
- {
- this.wrapped = wrapped;
- }
-
- public WE getWrapped()
- {
- return wrapped;
- }
-
- }
-
- @SuppressWarnings( "serial" ) //the algorithm isn't serializable.
- private static final class MapperWrapper<WE, W, WO extends OrderedMonoid<W>>
- implements Mapper<EdgeWrapper<WE>, W>
- {
-
- private final WO weightOperations;
-
- private final Mapper<WE, W> weightedEdges;
-
- public MapperWrapper( WO weightOperations, Mapper<WE, W> weightedEdges )
- {
- this.weightOperations = weightOperations;
- this.weightedEdges = weightedEdges;
- }
-
- public W map( EdgeWrapper<WE> input )
- {
- if ( input.getWrapped() == null )
- {
- return weightOperations.identity();
- }
- return weightedEdges.map( input.getWrapped() );
- }
-
- }
-
}
diff --git a/src/main/java/org/apache/commons/graph/flow/FlowNetworkHandler.java b/src/main/java/org/apache/commons/graph/flow/FlowNetworkHandler.java
index b101432..64e55d5 100644
--- a/src/main/java/org/apache/commons/graph/flow/FlowNetworkHandler.java
+++ b/src/main/java/org/apache/commons/graph/flow/FlowNetworkHandler.java
@@ -83,6 +83,55 @@ class FlowNetworkHandler<V, E, W>
predecessors = null;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public VisitState discoverEdge( V head, E edge, V tail )
+ {
+ W residualEdgeCapacity = residualEdgeCapacities.get( edge );
+ // avoid expanding the edge when it has no residual capacity
+ if ( weightOperations.compare( residualEdgeCapacity, weightOperations.identity() ) <= 0 )
+ {
+ return SKIP;
+ }
+ predecessors.addPredecessor( tail, head );
+ return CONTINUE;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void discoverGraph( DirectedGraph<V, E> graph )
+ {
+ // reset ausiliary structures for a new graph visit
+ predecessors = new PredecessorsList<V, E, W>( graph, weightOperations, weightedEdges );
+ foundAugmentingPath = false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public VisitState discoverVertex( V vertex )
+ {
+ return finishVertex( vertex );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public VisitState finishVertex( V vertex )
+ {
+ if ( vertex.equals( target ) )
+ {
+ // search ends when target vertex is reached
+ foundAugmentingPath = true;
+ return ABORT;
+ }
+ return CONTINUE;
+ }
+
/**
* Checks whether there is an augmenting path in the flow network,
* given the current residual capacities.
@@ -93,6 +142,12 @@ class FlowNetworkHandler<V, E, W>
return foundAugmentingPath;
}
+ @Override
+ public W onCompleted()
+ {
+ return maxFlow;
+ }
+
/**
* Updates the residual capacities in the flow network,
* based on the most recent augmenting path.
@@ -130,59 +185,4 @@ class FlowNetworkHandler<V, E, W>
}
}
- /**
- * {@inheritDoc}
- */
- @Override
- public void discoverGraph( DirectedGraph<V, E> graph )
- {
- // reset ausiliary structures for a new graph visit
- predecessors = new PredecessorsList<V, E, W>( graph, weightOperations, weightedEdges );
- foundAugmentingPath = false;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public VisitState discoverEdge( V head, E edge, V tail )
- {
- W residualEdgeCapacity = residualEdgeCapacities.get( edge );
- // avoid expanding the edge when it has no residual capacity
- if ( weightOperations.compare( residualEdgeCapacity, weightOperations.identity() ) <= 0 )
- {
- return SKIP;
- }
- predecessors.addPredecessor( tail, head );
- return CONTINUE;
- }
-
- /**
- * {@inheritDoc}
- */
- public VisitState discoverVertex( V vertex )
- {
- return finishVertex( vertex );
- }
-
- /**
- * {@inheritDoc}
- */
- public VisitState finishVertex( V vertex )
- {
- if ( vertex.equals( target ) )
- {
- // search ends when target vertex is reached
- foundAugmentingPath = true;
- return ABORT;
- }
- return CONTINUE;
- }
-
- @Override
- public W onCompleted()
- {
- return maxFlow;
- }
-
}
diff --git a/src/main/java/org/apache/commons/graph/flow/MaxFlowAlgorithmSelector.java b/src/main/java/org/apache/commons/graph/flow/MaxFlowAlgorithmSelector.java
index 14e0579..072416e 100644
--- a/src/main/java/org/apache/commons/graph/flow/MaxFlowAlgorithmSelector.java
+++ b/src/main/java/org/apache/commons/graph/flow/MaxFlowAlgorithmSelector.java
@@ -32,21 +32,21 @@ public interface MaxFlowAlgorithmSelector<V, WE, W>
{
/**
- * Calculates the maximum flow using the Ford-Fulkerson method.
+ * Calculates the maximum flow using the Edmonds-Karp algorithm.
*
* @param <WO> the type of weight operations
* @param weightOperations the class responsible for operations on weights
- * @return the max flow calculate with Ford and Furkenson algorithm
+ * @return the max flow calculate with Edmonds and Karp algorithm
*/
- <WO extends OrderedMonoid<W>> W applyingFordFulkerson( WO weightOperations );
+ <WO extends OrderedMonoid<W>> W applyingEdmondsKarp( WO weightOperations );
/**
- * Calculates the maximum flow using the Edmonds-Karp algorithm.
+ * Calculates the maximum flow using the Ford-Fulkerson method.
*
* @param <WO> the type of weight operations
* @param weightOperations the class responsible for operations on weights
- * @return the max flow calculate with Edmonds and Karp algorithm
+ * @return the max flow calculate with Ford and Furkenson algorithm
*/
- <WO extends OrderedMonoid<W>> W applyingEdmondsKarp( WO weightOperations );
+ <WO extends OrderedMonoid<W>> W applyingFordFulkerson( WO weightOperations );
}
diff --git a/src/main/java/org/apache/commons/graph/model/BaseGraph.java b/src/main/java/org/apache/commons/graph/model/BaseGraph.java
index 5e0dd48..706c3c6 100644
--- a/src/main/java/org/apache/commons/graph/model/BaseGraph.java
+++ b/src/main/java/org/apache/commons/graph/model/BaseGraph.java
@@ -49,6 +49,30 @@ public abstract class BaseGraph<V, E>
private static final long serialVersionUID = -8066786787634472712L;
+ /**
+ * Ensures the truth of an expression involving one or more parameters to the
+ * calling method.
+ *
+ * @param expression a boolean expression
+ * @param errorMessageTemplate a template for the exception message should the
+ * check fail. The message is formed by replacing each {@code %s}
+ * placeholder in the template with an argument. These are matched by
+ * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
+ * Unmatched arguments will be appended to the formatted message in square
+ * braces. Unmatched placeholders will be left as-is.
+ * @param errorMessageArgs the arguments to be substituted into the message
+ * template. Arguments are converted to strings using
+ * {@link String#valueOf(Object)}.
+ * @throws GraphException if {@code expression} is false
+ */
+ protected static void checkGraphCondition( boolean expression, String errorMessageTemplate, Object...errorMessageArgs )
+ {
+ if ( !expression )
+ {
+ throw new GraphException( format( errorMessageTemplate, errorMessageArgs ) );
+ }
+ }
+
private final Map<V, Set<V>> adjacencyList = new HashMap<V, Set<V>>();
private final Set<E> allEdges = new HashSet<E>();
@@ -60,33 +84,58 @@ public abstract class BaseGraph<V, E>
/**
* {@inheritDoc}
*/
- public final Iterable<V> getVertices()
+ public boolean containsEdge( E e )
{
- return unmodifiableSet( adjacencyList.keySet() );
+ return indexedVertices.containsKey( e );
}
/**
* {@inheritDoc}
*/
- public final int getOrder()
+ public boolean containsVertex( V v )
{
- return adjacencyList.size();
+ return adjacencyList.containsKey( v );
}
/**
* {@inheritDoc}
*/
- public final Iterable<E> getEdges()
+ @Override
+ public boolean equals( Object obj )
{
- return unmodifiableCollection( allEdges );
+ if ( this == obj )
+ {
+ return true;
+ }
+
+ if ( obj == null || getClass() != obj.getClass() )
+ {
+ return false;
+ }
+
+ @SuppressWarnings( "unchecked" )
+ // test against any Graph typed instance
+ BaseGraph<Object, Object> other = (BaseGraph<Object, Object>) obj;
+ return eq( adjacencyList, other.getAdjacencyList() );
}
/**
- * {@inheritDoc}
+ * Returns the adjacency list where stored vertex/edges.
+ *
+ * @return the adjacency list where stored vertex/edges.
*/
- public int getSize()
+ protected final Map<V, Set<V>> getAdjacencyList()
{
- return allEdges.size();
+ return adjacencyList;
+ }
+
+ /**
+ * Return the edge {@link Set}
+ * @return the edge {@link Set}
+ */
+ protected Set<E> getAllEdges()
+ {
+ return allEdges;
}
/**
@@ -113,128 +162,79 @@ public abstract class BaseGraph<V, E>
/**
* {@inheritDoc}
*/
- public final VertexPair<V> getVertices( E e )
- {
- return indexedVertices.get( e );
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean containsVertex( V v )
+ public final Iterable<E> getEdges()
{
- return adjacencyList.containsKey( v );
+ return unmodifiableCollection( allEdges );
}
/**
- * {@inheritDoc}
+ * Returns the {@code Map} of indexed edges.
+ *
+ * @return the {@link Map} of indexed edges
*/
- public boolean containsEdge( E e )
+ protected Map<VertexPair<V>, E> getIndexedEdges()
{
- return indexedVertices.containsKey( e );
+ return indexedEdges;
}
/**
- * Returns the adjacency list where stored vertex/edges.
- *
- * @return the adjacency list where stored vertex/edges.
+ * Returns the {@code Map} of indexed vertices.
+ *
+ * @return the indexed vertices {@link Map}
*/
- protected final Map<V, Set<V>> getAdjacencyList()
+ protected Map<E, VertexPair<V>> getIndexedVertices()
{
- return adjacencyList;
+ return indexedVertices;
}
/**
* {@inheritDoc}
*/
- @Override
- public int hashCode()
+ public final int getOrder()
{
- final int prime = 31;
- return hash( 1, prime, adjacencyList, allEdges, indexedEdges, indexedVertices );
+ return adjacencyList.size();
}
/**
* {@inheritDoc}
*/
- @Override
- public boolean equals( Object obj )
+ public int getSize()
{
- if ( this == obj )
- {
- return true;
- }
-
- if ( obj == null || getClass() != obj.getClass() )
- {
- return false;
- }
-
- @SuppressWarnings( "unchecked" )
- // test against any Graph typed instance
- BaseGraph<Object, Object> other = (BaseGraph<Object, Object>) obj;
- return eq( adjacencyList, other.getAdjacencyList() );
+ return allEdges.size();
}
/**
* {@inheritDoc}
*/
- @Override
- public String toString()
- {
- return String.valueOf( adjacencyList );
- }
-
- /**
- * Return the edge {@link Set}
- * @return the edge {@link Set}
- */
- protected Set<E> getAllEdges()
+ public final Iterable<V> getVertices()
{
- return allEdges;
+ return unmodifiableSet( adjacencyList.keySet() );
}
/**
- * Returns the {@code Map} of indexed edges.
- *
- * @return the {@link Map} of indexed edges
+ * {@inheritDoc}
*/
- protected Map<VertexPair<V>, E> getIndexedEdges()
+ public final VertexPair<V> getVertices( E e )
{
- return indexedEdges;
+ return indexedVertices.get( e );
}
/**
- * Returns the {@code Map} of indexed vertices.
- *
- * @return the indexed vertices {@link Map}
+ * {@inheritDoc}
*/
- protected Map<E, VertexPair<V>> getIndexedVertices()
+ @Override
+ public int hashCode()
{
- return indexedVertices;
+ final int prime = 31;
+ return hash( 1, prime, adjacencyList, allEdges, indexedEdges, indexedVertices );
}
/**
- * Ensures the truth of an expression involving one or more parameters to the
- * calling method.
- *
- * @param expression a boolean expression
- * @param errorMessageTemplate a template for the exception message should the
- * check fail. The message is formed by replacing each {@code %s}
- * placeholder in the template with an argument. These are matched by
- * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
- * Unmatched arguments will be appended to the formatted message in square
- * braces. Unmatched placeholders will be left as-is.
- * @param errorMessageArgs the arguments to be substituted into the message
- * template. Arguments are converted to strings using
- * {@link String#valueOf(Object)}.
- * @throws GraphException if {@code expression} is false
+ * {@inheritDoc}
*/
- protected static void checkGraphCondition( boolean expression, String errorMessageTemplate, Object...errorMessageArgs )
+ @Override
+ public String toString()
{
- if ( !expression )
- {
- throw new GraphException( format( errorMessageTemplate, errorMessageArgs ) );
- }
+ return String.valueOf( adjacencyList );
}
}
diff --git a/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java b/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
index 6d881e1..581781e 100644
--- a/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
+++ b/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
@@ -39,6 +39,25 @@ public abstract class BaseMutableGraph<V, E>
private static final long serialVersionUID = 1549113549446254183L;
+ /**
+ * {@inheritDoc}
+ */
+ public void addEdge( V head, E e, V tail )
+ {
+ checkGraphCondition( head != null, "Null head Vertex not admitted" );
+ checkGraphCondition( e != null, "Impossible to add a null Edge in the Graph" );
+ checkGraphCondition( tail != null, "Null tail Vertex not admitted" );
+ checkGraphCondition( containsVertex( head ), "Head Vertex '%s' not present in the Graph", head );
+ checkGraphCondition( containsVertex( tail ), "Head Vertex '%s' not present in the Graph", tail );
+ checkGraphCondition( getEdge( head, tail ) == null, "Edge %s is already present in the Graph", e );
+
+ getAllEdges().add( e );
+
+ internalAddEdge( head, e, tail );
+
+ decorateAddEdge( head, e, tail );
+ }
+
/**
* {@inheritDoc}
*/
@@ -52,6 +71,14 @@ public abstract class BaseMutableGraph<V, E>
decorateAddVertex( v );
}
+ /**
+ * Executes additional actions to edge that will be added
+ * @param head the head vertex
+ * @param e the edge
+ * @param tail the tail vertex
+ */
+ protected abstract void decorateAddEdge( V head, E e, V tail );
+
/**
* Executes additional actions to vertex that will be added
* @param v the vertex
@@ -59,21 +86,10 @@ public abstract class BaseMutableGraph<V, E>
protected abstract void decorateAddVertex( V v );
/**
- * {@inheritDoc}
+ * Executes additional actions to edge that will be removed
+ * @param e the edge
*/
- public final void removeVertex( V v )
- {
- checkGraphCondition( v != null, "Impossible to remove a null Vertex from the Graph" );
- checkGraphCondition( containsVertex( v ), "Vertex '%s' not present in the Graph", v );
-
- for ( V tail : getAdjacencyList().get( v ) )
- {
- getIndexedEdges().remove( new VertexPair<V>( v, tail ) );
- }
- getAdjacencyList().remove( v );
-
- decorateRemoveVertex( v );
- }
+ protected abstract void decorateRemoveEdge( E e );
/**
* Executes additional actions to vertex that will be removed
@@ -81,25 +97,6 @@ public abstract class BaseMutableGraph<V, E>
*/
protected abstract void decorateRemoveVertex( V v );
- /**
- * {@inheritDoc}
- */
- public void addEdge( V head, E e, V tail )
- {
- checkGraphCondition( head != null, "Null head Vertex not admitted" );
- checkGraphCondition( e != null, "Impossible to add a null Edge in the Graph" );
- checkGraphCondition( tail != null, "Null tail Vertex not admitted" );
- checkGraphCondition( containsVertex( head ), "Head Vertex '%s' not present in the Graph", head );
- checkGraphCondition( containsVertex( tail ), "Head Vertex '%s' not present in the Graph", tail );
- checkGraphCondition( getEdge( head, tail ) == null, "Edge %s is already present in the Graph", e );
-
- getAllEdges().add( e );
-
- internalAddEdge( head, e, tail );
-
- decorateAddEdge( head, e, tail );
- }
-
/**
* Performs the internal operations to add the edge
*
@@ -135,14 +132,6 @@ public abstract class BaseMutableGraph<V, E>
getAdjacencyList().get( vertexPair.getHead() ).remove( vertexPair.getTail() );
}
- /**
- * Executes additional actions to edge that will be added
- * @param head the head vertex
- * @param e the edge
- * @param tail the tail vertex
- */
- protected abstract void decorateAddEdge( V head, E e, V tail );
-
/**
* {@inheritDoc}
*/
@@ -158,9 +147,20 @@ public abstract class BaseMutableGraph<V, E>
}
/**
- * Executes additional actions to edge that will be removed
- * @param e the edge
+ * {@inheritDoc}
*/
- protected abstract void decorateRemoveEdge( E e );
+ public final void removeVertex( V v )
+ {
+ checkGraphCondition( v != null, "Impossible to remove a null Vertex from the Graph" );
+ checkGraphCondition( containsVertex( v ), "Vertex '%s' not present in the Graph", v );
+
+ for ( V tail : getAdjacencyList().get( v ) )
+ {
+ getIndexedEdges().remove( new VertexPair<V>( v, tail ) );
+ }
+ getAdjacencyList().remove( v );
+
+ decorateRemoveVertex( v );
+ }
}
diff --git a/src/main/java/org/apache/commons/graph/model/DirectedMutableGraph.java b/src/main/java/org/apache/commons/graph/model/DirectedMutableGraph.java
index 5e2f971..cb4c3ea 100644
--- a/src/main/java/org/apache/commons/graph/model/DirectedMutableGraph.java
+++ b/src/main/java/org/apache/commons/graph/model/DirectedMutableGraph.java
@@ -49,82 +49,82 @@ public class DirectedMutableGraph<V, E>
/**
* {@inheritDoc}
*/
- public final int getDegree( V v )
+ @Override
+ protected void decorateAddEdge( V head, E e, V tail )
{
- return getInDegree( v ) + getOutDegree( v );
+ inbound.get( tail ).add( head );
+ outbound.get( head ).add( tail );
}
/**
* {@inheritDoc}
*/
- public final int getInDegree( V v )
+ @Override
+ protected void decorateAddVertex( V v )
{
- return inbound.get( v ).size();
+ inbound.put( v, new LinkedHashSet<V>() );
+ outbound.put( v, new LinkedHashSet<V>() );
}
/**
* {@inheritDoc}
*/
- public final Iterable<V> getInbound( V v )
+ @Override
+ protected void decorateRemoveEdge( E e )
{
- return inbound.get( v );
+ final VertexPair<V> vertices = getVertices( e );
+ inbound.get( vertices.getTail() ).remove( vertices.getHead() );
+ outbound.get( vertices.getHead() ).remove( vertices.getTail() );
}
/**
* {@inheritDoc}
*/
- public final int getOutDegree( V v )
+ @Override
+ protected void decorateRemoveVertex( V v )
{
- return outbound.get( v ).size();
+ inbound.remove( v );
+ outbound.remove( v );
}
/**
* {@inheritDoc}
*/
- public final Iterable<V> getOutbound( V v )
+ public final int getDegree( V v )
{
- return outbound.get( v );
+ return getInDegree( v ) + getOutDegree( v );
}
/**
* {@inheritDoc}
*/
- @Override
- protected void decorateAddVertex( V v )
+ public final Iterable<V> getInbound( V v )
{
- inbound.put( v, new LinkedHashSet<V>() );
- outbound.put( v, new LinkedHashSet<V>() );
+ return inbound.get( v );
}
/**
* {@inheritDoc}
*/
- @Override
- protected void decorateRemoveVertex( V v )
+ public final int getInDegree( V v )
{
- inbound.remove( v );
- outbound.remove( v );
+ return inbound.get( v ).size();
}
/**
* {@inheritDoc}
*/
- @Override
- protected void decorateAddEdge( V head, E e, V tail )
+ public final Iterable<V> getOutbound( V v )
{
- inbound.get( tail ).add( head );
- outbound.get( head ).add( tail );
+ return outbound.get( v );
}
/**
* {@inheritDoc}
*/
- @Override
- protected void decorateRemoveEdge( E e )
+ public final int getOutDegree( V v )
{
- final VertexPair<V> vertices = getVertices( e );
- inbound.get( vertices.getTail() ).remove( vertices.getHead() );
- outbound.get( vertices.getHead() ).remove( vertices.getTail() );
+ return outbound.get( v ).size();
}
}
diff --git a/src/main/java/org/apache/commons/graph/model/InMemoryPath.java b/src/main/java/org/apache/commons/graph/model/InMemoryPath.java
index 68e6992..5a8982e 100644
--- a/src/main/java/org/apache/commons/graph/model/InMemoryPath.java
+++ b/src/main/java/org/apache/commons/graph/model/InMemoryPath.java
@@ -73,36 +73,13 @@ public class InMemoryPath<V, E>
this.target = checkNotNull( target, "Path target cannot be null" );
}
- /**
- * {@inheritDoc}
- */
- public V getSource()
- {
- return source;
- }
-
- /**
- * {@inheritDoc}
- */
- public V getTarget()
- {
- return target;
- }
-
- /**
- * {@inheritDoc}
- */
- public Iterable<V> getVertices()
+ private void addConnection( V head, E edge, V tail )
{
- return unmodifiableList( vertices );
- }
+ successors.put( head, tail );
- /**
- * {@inheritDoc}
- */
- public int getOrder()
- {
- return vertices.size();
+ VertexPair<V> vertexPair = new VertexPair<V>( head, tail );
+ indexedEdges.put( vertexPair, edge );
+ indexedVertices.put( edge, vertexPair );
}
/**
@@ -145,46 +122,44 @@ public class InMemoryPath<V, E>
addConnection( head, edge, tail );
}
- private void addConnection( V head, E edge, V tail )
- {
- successors.put( head, tail );
-
- VertexPair<V> vertexPair = new VertexPair<V>( head, tail );
- indexedEdges.put( vertexPair, edge );
- indexedVertices.put( edge, vertexPair );
- }
-
/**
* {@inheritDoc}
*/
- public Iterable<E> getEdges()
+ public boolean containsEdge( E e )
{
- return unmodifiableList( edges );
+ return edges.contains( e );
}
/**
* {@inheritDoc}
*/
- public int getSize()
+ public boolean containsVertex( V v )
{
- return edges.size();
+ return vertices.contains( v );
}
/**
* {@inheritDoc}
*/
- public int getDegree( V v )
+ @Override
+ public boolean equals( Object obj )
{
- v = checkNotNull( v, "Impossible to get the degree of a null vertex" );
- checkArgument( successors.containsKey( v ),
- "Impossible to get the degree of input vertex; %s not contained in this path", v );
+ if ( this == obj )
+ {
+ return true;
+ }
- if ( source.equals( v ) || target.equals( v ) )
+ if ( obj == null || getClass() != obj.getClass() )
{
- return 1;
+ return false;
}
- return 2;
+ @SuppressWarnings( "unchecked" ) // test against any Path typed instance
+ InMemoryPath<Object, Object> other = (InMemoryPath<Object, Object>) obj;
+ return eq( source, other.getSource() )
+ && eq( target, other.getTarget() )
+ && eq( vertices, other.getVertices() )
+ && eq( edges, other.getEdges() );
}
/**
@@ -207,6 +182,23 @@ public class InMemoryPath<V, E>
return connected;
}
+ /**
+ * {@inheritDoc}
+ */
+ public int getDegree( V v )
+ {
+ v = checkNotNull( v, "Impossible to get the degree of a null vertex" );
+ checkArgument( successors.containsKey( v ),
+ "Impossible to get the degree of input vertex; %s not contained in this path", v );
+
+ if ( source.equals( v ) || target.equals( v ) )
+ {
+ return 1;
+ }
+
+ return 2;
+ }
+
/**
* {@inheritDoc}
*/
@@ -218,59 +210,67 @@ public class InMemoryPath<V, E>
/**
* {@inheritDoc}
*/
- public VertexPair<V> getVertices( E e )
+ public Iterable<E> getEdges()
{
- return indexedVertices.get( e );
+ return unmodifiableList( edges );
}
/**
* {@inheritDoc}
*/
- public boolean containsVertex( V v )
+ public int getOrder()
{
- return vertices.contains( v );
+ return vertices.size();
}
/**
* {@inheritDoc}
*/
- public boolean containsEdge( E e )
+ public int getSize()
{
- return edges.contains( e );
+ return edges.size();
}
/**
* {@inheritDoc}
*/
- @Override
- public int hashCode()
+ public V getSource()
{
- final int prime = 31;
- return hash( 1, prime, edges, source, target, vertices );
+ return source;
}
/**
* {@inheritDoc}
*/
- @Override
- public boolean equals( Object obj )
+ public V getTarget()
{
- if ( this == obj )
- {
- return true;
- }
+ return target;
+ }
- if ( obj == null || getClass() != obj.getClass() )
- {
- return false;
- }
+ /**
+ * {@inheritDoc}
+ */
+ public Iterable<V> getVertices()
+ {
+ return unmodifiableList( vertices );
+ }
- @SuppressWarnings( "unchecked" ) // test against any Path typed instance
- InMemoryPath<Object, Object> other = (InMemoryPath<Object, Object>) obj;
- return eq( source, other.getSource() )
- && eq( target, other.getTarget() )
- && eq( vertices, other.getVertices() )
- && eq( edges, other.getEdges() );
+ /**
+ * {@inheritDoc}
+ */
+ public VertexPair<V> getVertices( E e )
+ {
+ return indexedVertices.get( e );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ return hash( 1, prime, edges, source, target, vertices );
}
/**
diff --git a/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java b/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java
index fcc8539..87d82f6 100644
--- a/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java
+++ b/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java
@@ -85,13 +85,29 @@ public final class InMemoryWeightedPath<V, WE, W>
}
/**
- * Increase the path weight with the weight of the input weighted edge.
- *
- * @param edge the edge whose weight is used to increase the path weight
+ * {@inheritDoc}
*/
- private void increaseWeight( WE edge )
+ @Override
+ public boolean equals( Object obj )
{
- weight = weightOperations.append( weightedEdges.map( edge ), weight );
+ if ( this == obj )
+ {
+ return true;
+ }
+
+ if ( !super.equals( obj ) )
+ {
+ return false;
+ }
+
+ if ( getClass() != obj.getClass() )
+ {
+ return false;
+ }
+
+ @SuppressWarnings( "unchecked" ) // test against any WeightedPath typed instance
+ InMemoryWeightedPath<Object, Object, W> other = (InMemoryWeightedPath<Object, Object, W>) obj;
+ return eq( weight, other.getWeight() );
}
/**
@@ -115,29 +131,13 @@ public final class InMemoryWeightedPath<V, WE, W>
}
/**
- * {@inheritDoc}
+ * Increase the path weight with the weight of the input weighted edge.
+ *
+ * @param edge the edge whose weight is used to increase the path weight
*/
- @Override
- public boolean equals( Object obj )
+ private void increaseWeight( WE edge )
{
- if ( this == obj )
- {
- return true;
- }
-
- if ( !super.equals( obj ) )
- {
- return false;
- }
-
- if ( getClass() != obj.getClass() )
- {
- return false;
- }
-
- @SuppressWarnings( "unchecked" ) // test against any WeightedPath typed instance
- InMemoryWeightedPath<Object, Object, W> other = (InMemoryWeightedPath<Object, Object, W>) obj;
- return eq( weight, other.getWeight() );
+ weight = weightOperations.append( weightedEdges.map( edge ), weight );
}
/**
diff --git a/src/main/java/org/apache/commons/graph/model/MutableSpanningTree.java b/src/main/java/org/apache/commons/graph/model/MutableSpanningTree.java
index 674affb..a34b7e8 100644
--- a/src/main/java/org/apache/commons/graph/model/MutableSpanningTree.java
+++ b/src/main/java/org/apache/commons/graph/model/MutableSpanningTree.java
@@ -59,14 +59,6 @@ public final class MutableSpanningTree<V, WE, W>
this.weight = weightOperations.identity();
}
- /**
- * {@inheritDoc}
- */
- public W getWeight()
- {
- return weight;
- }
-
/**
* {@inheritDoc}
*/
@@ -86,4 +78,12 @@ public final class MutableSpanningTree<V, WE, W>
weight = weightOperations.append( weight, weightOperations.inverse( weightedEdges.map( e ) ) );
}
+ /**
+ * {@inheritDoc}
+ */
+ public W getWeight()
+ {
+ return weight;
+ }
+
}
diff --git a/src/main/java/org/apache/commons/graph/model/RevertedGraph.java b/src/main/java/org/apache/commons/graph/model/RevertedGraph.java
index 112d27f..9f1960a 100644
--- a/src/main/java/org/apache/commons/graph/model/RevertedGraph.java
+++ b/src/main/java/org/apache/commons/graph/model/RevertedGraph.java
@@ -58,82 +58,81 @@ public final class RevertedGraph<V, E>
/**
* {@inheritDoc}
*/
- public Iterable<V> getVertices()
+ public boolean containsEdge( E e )
{
- return directedGraph.getVertices();
+ return directedGraph.containsEdge( e );
}
/**
* {@inheritDoc}
*/
- public int getOrder()
+ public boolean containsVertex( V v )
{
- return directedGraph.getOrder();
+ return directedGraph.containsVertex( v );
}
/**
* {@inheritDoc}
*/
- public Iterable<E> getEdges()
+ public Iterable<V> getConnectedVertices( V v )
{
- return directedGraph.getEdges();
+ return directedGraph.getConnectedVertices( v );
}
/**
* {@inheritDoc}
*/
- public int getSize()
+ public int getDegree( V v )
{
- return directedGraph.getSize();
+ return directedGraph.getDegree( v );
}
/**
* {@inheritDoc}
*/
- public int getDegree( V v )
+ public E getEdge( V source, V target )
{
- return directedGraph.getDegree( v );
+ return directedGraph.getEdge( target, source );
}
/**
* {@inheritDoc}
*/
- public Iterable<V> getConnectedVertices( V v )
+ public Iterable<E> getEdges()
{
- return directedGraph.getConnectedVertices( v );
+ return directedGraph.getEdges();
}
/**
* {@inheritDoc}
*/
- public E getEdge( V source, V target )
+ public Iterable<V> getInbound( V v )
{
- return directedGraph.getEdge( target, source );
+ return directedGraph.getOutbound( v );
}
/**
* {@inheritDoc}
*/
- public VertexPair<V> getVertices( E e )
+ public int getInDegree( V v )
{
- VertexPair<V> directedVertexPair = directedGraph.getVertices( e );
- return new VertexPair<V>( directedVertexPair.getTail(), directedVertexPair.getHead() );
+ return directedGraph.getOutDegree( v );
}
/**
* {@inheritDoc}
*/
- public int getInDegree( V v )
+ public int getOrder()
{
- return directedGraph.getOutDegree( v );
+ return directedGraph.getOrder();
}
/**
* {@inheritDoc}
*/
- public Iterable<V> getInbound( V v )
+ public Iterable<V> getOutbound( V v )
{
- return directedGraph.getOutbound( v );
+ return directedGraph.getInbound( v );
}
/**
@@ -147,25 +146,26 @@ public final class RevertedGraph<V, E>
/**
* {@inheritDoc}
*/
- public Iterable<V> getOutbound( V v )
+ public int getSize()
{
- return directedGraph.getInbound( v );
+ return directedGraph.getSize();
}
/**
* {@inheritDoc}
*/
- public boolean containsVertex( V v )
+ public Iterable<V> getVertices()
{
- return directedGraph.containsVertex( v );
+ return directedGraph.getVertices();
}
/**
* {@inheritDoc}
*/
- public boolean containsEdge( E e )
+ public VertexPair<V> getVertices( E e )
{
- return directedGraph.containsEdge( e );
+ VertexPair<V> directedVertexPair = directedGraph.getVertices( e );
+ return new VertexPair<V>( directedVertexPair.getTail(), directedVertexPair.getHead() );
}
}
\ No newline at end of file
diff --git a/src/main/java/org/apache/commons/graph/model/UndirectedMutableGraph.java b/src/main/java/org/apache/commons/graph/model/UndirectedMutableGraph.java
index 0c73e32..ec205db 100644
--- a/src/main/java/org/apache/commons/graph/model/UndirectedMutableGraph.java
+++ b/src/main/java/org/apache/commons/graph/model/UndirectedMutableGraph.java
@@ -39,9 +39,10 @@ public class UndirectedMutableGraph<V, E>
/**
* {@inheritDoc}
*/
- public final int getDegree( V v )
+ @Override
+ protected void decorateAddEdge( V head, E e, V tail )
{
- return getAdjacencyList().get( v ).size();
+ internalAddEdge( tail, e, head );
}
/**
@@ -57,27 +58,26 @@ public class UndirectedMutableGraph<V, E>
* {@inheritDoc}
*/
@Override
- protected void decorateRemoveVertex( V v )
+ protected void decorateRemoveEdge( E e )
{
- // do nothing
+ internalRemoveEdge( getVertices( e ).getTail(), e, getVertices( e ).getHead() );
}
/**
* {@inheritDoc}
*/
@Override
- protected void decorateAddEdge( V head, E e, V tail )
+ protected void decorateRemoveVertex( V v )
{
- internalAddEdge( tail, e, head );
+ // do nothing
}
/**
* {@inheritDoc}
*/
- @Override
- protected void decorateRemoveEdge( E e )
+ public final int getDegree( V v )
{
- internalRemoveEdge( getVertices( e ).getTail(), e, getVertices( e ).getHead() );
+ return getAdjacencyList().get( v ).size();
}
}
diff --git a/src/main/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowAlgorithm.java b/src/main/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowAlgorithm.java
index f829925..e054fb8 100644
--- a/src/main/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowAlgorithm.java
+++ b/src/main/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowAlgorithm.java
@@ -60,35 +60,6 @@ final class CheriyanMehlhornGabowAlgorithm<V, E>
this.graph = graph;
}
- /**
- */
- public Set<Set<V>> perform()
- {
- for ( V vertex : graph.getVertices() )
- {
- if ( !marked.contains( vertex ) )
- {
- dfs( vertex );
- }
- }
-
- final List<Set<V>> indexedSccComponents = new ArrayList<Set<V>>();
- for ( int i = 0; i < sscCounter; i++ )
- {
- indexedSccComponents.add( new HashSet<V>() );
- }
-
- for ( V w : graph.getVertices() )
- {
- Set<V> component = indexedSccComponents.get( sscId.get( w ) );
- component.add( w );
- }
-
- final Set<Set<V>> scc = new HashSet<Set<V>>();
- scc.addAll( indexedSccComponents );
- return scc;
- }
-
private void dfs( V vertex )
{
marked.add( vertex );
@@ -123,4 +94,33 @@ final class CheriyanMehlhornGabowAlgorithm<V, E>
sscCounter++;
}
}
+
+ /**
+ */
+ public Set<Set<V>> perform()
+ {
+ for ( V vertex : graph.getVertices() )
+ {
+ if ( !marked.contains( vertex ) )
+ {
+ dfs( vertex );
+ }
+ }
+
+ final List<Set<V>> indexedSccComponents = new ArrayList<Set<V>>();
+ for ( int i = 0; i < sscCounter; i++ )
+ {
+ indexedSccComponents.add( new HashSet<V>() );
+ }
+
+ for ( V w : graph.getVertices() )
+ {
+ Set<V> component = indexedSccComponents.get( sscId.get( w ) );
+ component.add( w );
+ }
+
+ final Set<Set<V>> scc = new HashSet<Set<V>>();
+ scc.addAll( indexedSccComponents );
+ return scc;
+ }
}
diff --git a/src/main/java/org/apache/commons/graph/scc/DefaultSccAlgorithmSelector.java b/src/main/java/org/apache/commons/graph/scc/DefaultSccAlgorithmSelector.java
index 7c9014a..5ea0957 100644
--- a/src/main/java/org/apache/commons/graph/scc/DefaultSccAlgorithmSelector.java
+++ b/src/main/java/org/apache/commons/graph/scc/DefaultSccAlgorithmSelector.java
@@ -46,46 +46,46 @@ public final class DefaultSccAlgorithmSelector<V, E>
}
/**
- * {@inheritDoc}
+ * Just calculates the SCC depending on the selected algorithm.
+ *
+ * @param algorithm
+ * @return
*/
- public Set<V> applyingKosarajuSharir( final V source )
+ private Set<Set<V>> applying( SccAlgorithm<V> algorithm )
{
- return new KosarajuSharirAlgorithm<V, E>( graph ).perform( source );
+ return algorithm.perform();
}
/**
* {@inheritDoc}
*/
- public Set<Set<V>> applyingKosarajuSharir()
+ public Set<Set<V>> applyingCheriyanMehlhornGabow()
{
- return applying( new KosarajuSharirAlgorithm<V, E>( graph ) );
+ return applying( new CheriyanMehlhornGabowAlgorithm<V, E>( graph ) );
}
/**
* {@inheritDoc}
*/
- public Set<Set<V>> applyingCheriyanMehlhornGabow()
+ public Set<Set<V>> applyingKosarajuSharir()
{
- return applying( new CheriyanMehlhornGabowAlgorithm<V, E>( graph ) );
+ return applying( new KosarajuSharirAlgorithm<V, E>( graph ) );
}
/**
* {@inheritDoc}
*/
- public Set<Set<V>> applyingTarjan()
+ public Set<V> applyingKosarajuSharir( final V source )
{
- return applying( new TarjanAlgorithm<V, E>( graph ) );
+ return new KosarajuSharirAlgorithm<V, E>( graph ).perform( source );
}
/**
- * Just calculates the SCC depending on the selected algorithm.
- *
- * @param algorithm
- * @return
+ * {@inheritDoc}
*/
- private Set<Set<V>> applying( SccAlgorithm<V> algorithm )
+ public Set<Set<V>> applyingTarjan()
{
- return algorithm.perform();
+ return applying( new TarjanAlgorithm<V, E>( graph ) );
}
}
diff --git a/src/main/java/org/apache/commons/graph/scc/KosarajuSharirAlgorithm.java b/src/main/java/org/apache/commons/graph/scc/KosarajuSharirAlgorithm.java
index e808d4a..ff97900 100644
--- a/src/main/java/org/apache/commons/graph/scc/KosarajuSharirAlgorithm.java
+++ b/src/main/java/org/apache/commons/graph/scc/KosarajuSharirAlgorithm.java
@@ -57,26 +57,46 @@ final class KosarajuSharirAlgorithm<V, E>
}
/**
- * Applies the classical Kosaraju's algorithm to find the strongly connected components of
- * a vertex <code>source</code>.
+ * Performs a depth-first search to create a recursive vertex list.
*
- * @param source the source vertex to start the search from
- * @return the input graph strongly connected component.
+ * @param source the starting vertex
+ * @param visitedVertices a {@link Set} containing all visited vertices
+ * @return the recursively expanded vertex list for Kosaraju's algorithm
*/
- public Set<V> perform( final V source )
+ private List<V> getExpandedVertexList( final V source, final Set<V> visitedVertices )
{
- checkNotNull( source, "Kosaraju Sharir algorithm cannot be calculated without expressing the source vertex" );
- checkState( graph.containsVertex( source ), "Vertex %s does not exist in the Graph", source );
+ final int size = ( source != null ) ? 13 : graph.getOrder();
+ final Set<V> vertices = new HashSet<V>( size );
- final Set<V> visitedVertices = new HashSet<V>();
- final List<V> expandedVertexList = getExpandedVertexList( source, visitedVertices );
- final DirectedGraph<V, E> reverted = new RevertedGraph<V, E>( graph );
+ if ( source != null )
+ {
+ vertices.add( source );
+ }
+ else
+ {
+ for ( V vertex : graph.getVertices() )
+ {
+ vertices.add( vertex );
+ }
+ }
- // remove the last element from the expanded vertices list
- final V v = expandedVertexList.remove( expandedVertexList.size() - 1 );
- final Set<V> sccSet = new HashSet<V>();
- searchRecursive( reverted, v, sccSet, visitedVertices, false );
- return sccSet;
+ // use an ArrayList so that subList is fast
+ final ArrayList<V> expandedVertexList = new ArrayList<V>();
+
+ int idx = 0;
+ while ( ! vertices.isEmpty() )
+ {
+ // get the next vertex that has not yet been added to the expanded list
+ final V v = vertices.iterator().next();
+ searchRecursive( graph, v, expandedVertexList, visitedVertices, true );
+ // remove all expanded vertices from the list of vertices that have to be
+ // still processed. To improve performance, only the items that have been
+ // added to the list since the last iteration are removed
+ vertices.removeAll( expandedVertexList.subList( idx, expandedVertexList.size() ) );
+ idx = expandedVertexList.size();
+ }
+
+ return expandedVertexList;
}
/**
@@ -116,46 +136,26 @@ final class KosarajuSharirAlgorithm<V, E>
}
/**
- * Performs a depth-first search to create a recursive vertex list.
+ * Applies the classical Kosaraju's algorithm to find the strongly connected components of
+ * a vertex <code>source</code>.
*
- * @param source the starting vertex
- * @param visitedVertices a {@link Set} containing all visited vertices
- * @return the recursively expanded vertex list for Kosaraju's algorithm
+ * @param source the source vertex to start the search from
+ * @return the input graph strongly connected component.
*/
- private List<V> getExpandedVertexList( final V source, final Set<V> visitedVertices )
+ public Set<V> perform( final V source )
{
- final int size = ( source != null ) ? 13 : graph.getOrder();
- final Set<V> vertices = new HashSet<V>( size );
-
- if ( source != null )
- {
- vertices.add( source );
- }
- else
- {
- for ( V vertex : graph.getVertices() )
- {
- vertices.add( vertex );
- }
- }
-
- // use an ArrayList so that subList is fast
- final ArrayList<V> expandedVertexList = new ArrayList<V>();
+ checkNotNull( source, "Kosaraju Sharir algorithm cannot be calculated without expressing the source vertex" );
+ checkState( graph.containsVertex( source ), "Vertex %s does not exist in the Graph", source );
- int idx = 0;
- while ( ! vertices.isEmpty() )
- {
- // get the next vertex that has not yet been added to the expanded list
- final V v = vertices.iterator().next();
- searchRecursive( graph, v, expandedVertexList, visitedVertices, true );
- // remove all expanded vertices from the list of vertices that have to be
- // still processed. To improve performance, only the items that have been
- // added to the list since the last iteration are removed
- vertices.removeAll( expandedVertexList.subList( idx, expandedVertexList.size() ) );
- idx = expandedVertexList.size();
- }
+ final Set<V> visitedVertices = new HashSet<V>();
+ final List<V> expandedVertexList = getExpandedVertexList( source, visitedVertices );
+ final DirectedGraph<V, E> reverted = new RevertedGraph<V, E>( graph );
- return expandedVertexList;
+ // remove the last element from the expanded vertices list
+ final V v = expandedVertexList.remove( expandedVertexList.size() - 1 );
+ final Set<V> sccSet = new HashSet<V>();
+ searchRecursive( reverted, v, sccSet, visitedVertices, false );
+ return sccSet;
}
/**
diff --git a/src/main/java/org/apache/commons/graph/scc/SccAlgorithmSelector.java b/src/main/java/org/apache/commons/graph/scc/SccAlgorithmSelector.java
index 0bb1dfa..dd1bf86 100644
--- a/src/main/java/org/apache/commons/graph/scc/SccAlgorithmSelector.java
+++ b/src/main/java/org/apache/commons/graph/scc/SccAlgorithmSelector.java
@@ -31,13 +31,11 @@ public interface SccAlgorithmSelector<V, E>
{
/**
- * Applies the classical Kosaraju's algorithm to find the strongly connected components of
- * a vertex <code>source</code>.
+ * Applies the classical Cheriyan/Mehlhorn/Gabow's algorithm to find the strongly connected components, if exist.
*
- * @param source the source vertex to start the search from
* @return the input graph strongly connected component.
*/
- Set<V> applyingKosarajuSharir( V source );
+ Set<Set<V>> applyingCheriyanMehlhornGabow();
/**
* Applies the classical Kosaraju's algorithm to find the strongly connected components.
@@ -50,11 +48,13 @@ public interface SccAlgorithmSelector<V, E>
Set<Set<V>> applyingKosarajuSharir();
/**
- * Applies the classical Cheriyan/Mehlhorn/Gabow's algorithm to find the strongly connected components, if exist.
+ * Applies the classical Kosaraju's algorithm to find the strongly connected components of
+ * a vertex <code>source</code>.
*
+ * @param source the source vertex to start the search from
* @return the input graph strongly connected component.
*/
- Set<Set<V>> applyingCheriyanMehlhornGabow();
+ Set<V> applyingKosarajuSharir( V source );
/**
* Tarjan's algorithm is a variation (slightly faster) on KosarajuSharir's algorithm for finding
diff --git a/src/main/java/org/apache/commons/graph/scc/TarjanAlgorithm.java b/src/main/java/org/apache/commons/graph/scc/TarjanAlgorithm.java
index b514314..874d916 100644
--- a/src/main/java/org/apache/commons/graph/scc/TarjanAlgorithm.java
+++ b/src/main/java/org/apache/commons/graph/scc/TarjanAlgorithm.java
@@ -41,43 +41,6 @@ final class TarjanAlgorithm<V, E>
implements SccAlgorithm<V>
{
- private final DirectedGraph<V, E> graph;
-
- /**
- */
- public TarjanAlgorithm( DirectedGraph<V, E> graph )
- {
- this.graph = graph;
- }
-
- /**
- * Tarjan's algorithm is a variation (slightly faster) on KosarajuSharir's algorithm for finding strongly-connected
- * components in a directed graph.
- *
- * @return the input graph strongly connected component.
- */
- public Set<Set<V>> perform()
- {
- final Map<V, TarjanVertexMetaInfo> verticesMetaInfo = new HashMap<V, TarjanVertexMetaInfo>();
- final Stack<V> s = new Stack<V>();
- final Set<Set<V>> stronglyConnectedComponents = new LinkedHashSet<Set<V>>();
- Integer index = 0;
-
- for ( V vertex : graph.getVertices() )
- {
- TarjanVertexMetaInfo vertexMetaInfo = getMetaInfo( vertex, verticesMetaInfo );
- final Set<V> stronglyConnectedComponent = new LinkedHashSet<V>();
-
- if ( vertexMetaInfo.hasUndefinedIndex() )
- {
- strongConnect( graph, vertex, verticesMetaInfo, s, stronglyConnectedComponent, index );
- stronglyConnectedComponents.add( stronglyConnectedComponent );
- }
- }
-
- return stronglyConnectedComponents;
- }
-
private static <V> TarjanVertexMetaInfo getMetaInfo( V vertex, Map<V, TarjanVertexMetaInfo> verticesMetaInfo )
{
TarjanVertexMetaInfo vertexMetaInfo = verticesMetaInfo.get( vertex );
@@ -127,4 +90,41 @@ final class TarjanAlgorithm<V, E>
while ( !vertex.equals( v ) );
}
}
+
+ private final DirectedGraph<V, E> graph;
+
+ /**
+ */
+ public TarjanAlgorithm( DirectedGraph<V, E> graph )
+ {
+ this.graph = graph;
+ }
+
+ /**
+ * Tarjan's algorithm is a variation (slightly faster) on KosarajuSharir's algorithm for finding strongly-connected
+ * components in a directed graph.
+ *
+ * @return the input graph strongly connected component.
+ */
+ public Set<Set<V>> perform()
+ {
+ final Map<V, TarjanVertexMetaInfo> verticesMetaInfo = new HashMap<V, TarjanVertexMetaInfo>();
+ final Stack<V> s = new Stack<V>();
+ final Set<Set<V>> stronglyConnectedComponents = new LinkedHashSet<Set<V>>();
+ Integer index = 0;
+
+ for ( V vertex : graph.getVertices() )
+ {
+ TarjanVertexMetaInfo vertexMetaInfo = getMetaInfo( vertex, verticesMetaInfo );
+ final Set<V> stronglyConnectedComponent = new LinkedHashSet<V>();
+
+ if ( vertexMetaInfo.hasUndefinedIndex() )
+ {
+ strongConnect( graph, vertex, verticesMetaInfo, s, stronglyConnectedComponent, index );
+ stronglyConnectedComponents.add( stronglyConnectedComponent );
+ }
+ }
+
+ return stronglyConnectedComponents;
+ }
}
diff --git a/src/main/java/org/apache/commons/graph/scc/TarjanVertexMetaInfo.java b/src/main/java/org/apache/commons/graph/scc/TarjanVertexMetaInfo.java
index bfb4514..7556c86 100644
--- a/src/main/java/org/apache/commons/graph/scc/TarjanVertexMetaInfo.java
+++ b/src/main/java/org/apache/commons/graph/scc/TarjanVertexMetaInfo.java
@@ -33,6 +33,11 @@ final class TarjanVertexMetaInfo
return index;
}
+ public int getLowLink()
+ {
+ return lowLink;
+ }
+
public boolean hasUndefinedIndex()
{
return UNDEFINED == index;
@@ -43,11 +48,6 @@ final class TarjanVertexMetaInfo
this.index = index;
}
- public int getLowLink()
- {
- return lowLink;
- }
-
public void setLowLink( int lowLink )
{
this.lowLink = lowLink;
diff --git a/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java b/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java
index 82c66c8..66e8285 100644
--- a/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java
+++ b/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java
@@ -52,6 +52,20 @@ public final class AllVertexPairsShortestPath<V, WE, W>
this.weightOperations = weightOperations;
}
+ /**
+ * @param source
+ * @param target
+ * @param distance
+ */
+ void addShortestDistance( V source, V target, W distance )
+ {
+ source = checkNotNull( source, "Impossible to add a shortest path from a null source" );
+ target = checkNotNull( target, "Impossible to add a shortest path to a null target" );
+ distance = checkNotNull( distance, "Impossible to add a shortest distance with a null distance" );
+
+ shortestDistances.put( new VertexPair<V>( source, target ), distance );
+ }
+
/**
* @param source
* @param target
@@ -88,20 +102,6 @@ public final class AllVertexPairsShortestPath<V, WE, W>
return path;
}
- /**
- * @param source
- * @param target
- * @param distance
- */
- void addShortestDistance( V source, V target, W distance )
- {
- source = checkNotNull( source, "Impossible to add a shortest path from a null source" );
- target = checkNotNull( target, "Impossible to add a shortest path to a null target" );
- distance = checkNotNull( distance, "Impossible to add a shortest distance with a null distance" );
-
- shortestDistances.put( new VertexPair<V>( source, target ), distance );
- }
-
/**
* Returns the shortest distance between source and target.
*
diff --git a/src/main/java/org/apache/commons/graph/shortestpath/DefaultPathSourceSelector.java b/src/main/java/org/apache/commons/graph/shortestpath/DefaultPathSourceSelector.java
index c7acc18..f9e3a86 100644
--- a/src/main/java/org/apache/commons/graph/shortestpath/DefaultPathSourceSelector.java
+++ b/src/main/java/org/apache/commons/graph/shortestpath/DefaultPathSourceSelector.java
@@ -116,6 +116,15 @@ final class DefaultPathSourceSelector<V, WE, W>
return shortestPaths;
}
+ /**
+ * {@inheritDoc}
+ */
+ public <H extends V> TargetSourceSelector<V, WE, W> from( H source )
+ {
+ source = checkNotNull( source, "Shortest path can not be calculated from a null source" );
+ return new DefaultTargetSourceSelector<V, WE, W>( graph, weightedEdges, source );
+ }
+
private void pathReconstruction( PredecessorsList<V, WE, W> path,
V source, V target,
Map<VertexPair<V>, V> next )
@@ -137,13 +146,4 @@ final class DefaultPathSourceSelector<V, WE, W>
}
}
- /**
- * {@inheritDoc}
- */
- public <H extends V> TargetSourceSelector<V, WE, W> from( H source )
- {
- source = checkNotNull( source, "Shortest path can not be calculated from a null source" );
- return new DefaultTargetSourceSelector<V, WE, W>( graph, weightedEdges, source );
- }
-
}
\ No newline at end of file
diff --git a/src/main/java/org/apache/commons/graph/shortestpath/DefaultShortestPathAlgorithmSelector.java b/src/main/java/org/apache/commons/graph/shortestpath/DefaultShortestPathAlgorithmSelector.java
index e21e4d9..2c14c55 100644
--- a/src/main/java/org/apache/commons/graph/shortestpath/DefaultShortestPathAlgorithmSelector.java
+++ b/src/main/java/org/apache/commons/graph/shortestpath/DefaultShortestPathAlgorithmSelector.java
@@ -61,65 +61,6 @@ final class DefaultShortestPathAlgorithmSelector<V, WE, W>
return new DefaultHeuristicBuilder<V, WE, W>( graph, weightedEdges, source, target, weightOperations );
}
- /**
- * {@inheritDoc}
- */
- public <WO extends OrderedMonoid<W>> WeightedPath<V, WE, W> applyingDijkstra( WO weightOperations )
- {
- weightOperations = checkNotNull( weightOperations, "Dijkstra algorithm can not be applied using null weight operations" );
-
- final ShortestDistances<V, W> shortestDistances = new ShortestDistances<V, W>( weightOperations );
- shortestDistances.setWeight( source, weightOperations.identity() );
-
- final Queue<V> unsettledNodes = new FibonacciHeap<V>( shortestDistances );
- unsettledNodes.add( source );
-
- final Set<V> settledNodes = new HashSet<V>();
-
- final PredecessorsList<V, WE, W> predecessors = new PredecessorsList<V, WE, W>( graph, weightOperations, weightedEdges );
-
- // extract the node with the shortest distance
- while ( !unsettledNodes.isEmpty() )
- {
- V vertex = unsettledNodes.remove();
-
- // destination reached, stop and build the path
- if ( target.equals( vertex ) )
- {
- return predecessors.buildPath( source, target );
- }
-
- settledNodes.add( vertex );
-
- for ( V v : graph.getConnectedVertices( vertex ) )
- {
- // skip node already settled
- if ( !settledNodes.contains( v ) )
- {
- WE edge = graph.getEdge( vertex, v );
- if ( shortestDistances.alreadyVisited( vertex ) )
- {
- W shortDist = weightOperations.append( shortestDistances.getWeight( vertex ), weightedEdges.map( edge ) );
-
- if ( !shortestDistances.alreadyVisited( v )
- || weightOperations.compare( shortDist, shortestDistances.getWeight( v ) ) < 0 )
- {
- // assign new shortest distance and mark unsettled
- shortestDistances.setWeight( v, shortDist );
- unsettledNodes.add( v );
-
- // assign predecessor in shortest path
- predecessors.addPredecessor( v, vertex );
- }
- }
-
- }
- }
- }
-
- throw new PathNotFoundException( "Path from '%s' to '%s' doesn't exist in Graph '%s'", source, target, graph );
- }
-
/**
* {@inheritDoc}
*/
@@ -243,4 +184,63 @@ final class DefaultShortestPathAlgorithmSelector<V, WE, W>
return predecessorsForward.buildPath( source, touch, target, predecessorsBackwards );
}
+
+ /**
+ * {@inheritDoc}
+ */
+ public <WO extends OrderedMonoid<W>> WeightedPath<V, WE, W> applyingDijkstra( WO weightOperations )
+ {
+ weightOperations = checkNotNull( weightOperations, "Dijkstra algorithm can not be applied using null weight operations" );
+
+ final ShortestDistances<V, W> shortestDistances = new ShortestDistances<V, W>( weightOperations );
+ shortestDistances.setWeight( source, weightOperations.identity() );
+
+ final Queue<V> unsettledNodes = new FibonacciHeap<V>( shortestDistances );
+ unsettledNodes.add( source );
+
+ final Set<V> settledNodes = new HashSet<V>();
+
+ final PredecessorsList<V, WE, W> predecessors = new PredecessorsList<V, WE, W>( graph, weightOperations, weightedEdges );
+
+ // extract the node with the shortest distance
+ while ( !unsettledNodes.isEmpty() )
+ {
+ V vertex = unsettledNodes.remove();
+
+ // destination reached, stop and build the path
+ if ( target.equals( vertex ) )
+ {
+ return predecessors.buildPath( source, target );
+ }
+
+ settledNodes.add( vertex );
+
+ for ( V v : graph.getConnectedVertices( vertex ) )
+ {
+ // skip node already settled
+ if ( !settledNodes.contains( v ) )
+ {
+ WE edge = graph.getEdge( vertex, v );
+ if ( shortestDistances.alreadyVisited( vertex ) )
+ {
+ W shortDist = weightOperations.append( shortestDistances.getWeight( vertex ), weightedEdges.map( edge ) );
+
+ if ( !shortestDistances.alreadyVisited( v )
+ || weightOperations.compare( shortDist, shortestDistances.getWeight( v ) ) < 0 )
+ {
+ // assign new shortest distance and mark unsettled
+ shortestDistances.setWeight( v, shortDist );
+ unsettledNodes.add( v );
+
+ // assign predecessor in shortest path
+ predecessors.addPredecessor( v, vertex );
+ }
+ }
+
+ }
+ }
+ }
+
+ throw new PathNotFoundException( "Path from '%s' to '%s' doesn't exist in Graph '%s'", source, target, graph );
+ }
}
diff --git a/src/main/java/org/apache/commons/graph/shortestpath/NegativeWeightedCycleException.java b/src/main/java/org/apache/commons/graph/shortestpath/NegativeWeightedCycleException.java
index ebcb0e1..9fa8bc8 100644
--- a/src/main/java/org/apache/commons/graph/shortestpath/NegativeWeightedCycleException.java
+++ b/src/main/java/org/apache/commons/graph/shortestpath/NegativeWeightedCycleException.java
@@ -35,14 +35,14 @@ public final class NegativeWeightedCycleException
super( messagePattern, arguments );
}
- public NegativeWeightedCycleException( Throwable cause )
+ public NegativeWeightedCycleException( String msg, Throwable cause )
{
- super( cause );
+ super( msg, cause );
}
- public NegativeWeightedCycleException( String msg, Throwable cause )
+ public NegativeWeightedCycleException( Throwable cause )
{
- super( msg, cause );
+ super( cause );
}
}
diff --git a/src/main/java/org/apache/commons/graph/shortestpath/ShortestDistances.java b/src/main/java/org/apache/commons/graph/shortestpath/ShortestDistances.java
index dfa4abb..51bc74e 100644
--- a/src/main/java/org/apache/commons/graph/shortestpath/ShortestDistances.java
+++ b/src/main/java/org/apache/commons/graph/shortestpath/ShortestDistances.java
@@ -44,20 +44,6 @@ final class ShortestDistances<V, W>
this.weightOperations = weightOperations;
}
- /**
- * Returns the distance related to input vertex, or null if it wasn't previously visited.
- *
- * <b>NOTE</b>: the method {@link alreadyVisited} should be used first to check if
- * the input vertex was already visited and a distance value is available for it.
- *
- * @param vertex the vertex whose distance has to be retrieved
- * @return the distance related to input vertex, or null if it wasn't previously visited.
- */
- public W getWeight( V vertex )
- {
- return distances.get( vertex );
- }
-
/**
* Checks if the input {@code Vertex} was already visited.
*
@@ -69,17 +55,6 @@ final class ShortestDistances<V, W>
return distances.containsKey( vertex );
}
- /**
- * Update the input vertex distance.
- *
- * @param vertex the vertex for which the distance has to be updated
- * @param distance the new input vertex distance
- */
- public void setWeight( V vertex, W distance )
- {
- distances.put( vertex, distance );
- }
-
/**
* {@inheritDoc}
*/
@@ -100,4 +75,29 @@ final class ShortestDistances<V, W>
return weightOperations.compare( getWeight( left ), getWeight( right ) );
}
+ /**
+ * Returns the distance related to input vertex, or null if it wasn't previously visited.
+ *
+ * <b>NOTE</b>: the method {@link alreadyVisited} should be used first to check if
+ * the input vertex was already visited and a distance value is available for it.
+ *
+ * @param vertex the vertex whose distance has to be retrieved
+ * @return the distance related to input vertex, or null if it wasn't previously visited.
+ */
+ public W getWeight( V vertex )
+ {
+ return distances.get( vertex );
+ }
+
+ /**
+ * Update the input vertex distance.
+ *
+ * @param vertex the vertex for which the distance has to be updated
+ * @param distance the new input vertex distance
+ */
+ public void setWeight( V vertex, W distance )
+ {
+ distances.put( vertex, distance );
+ }
+
}
diff --git a/src/main/java/org/apache/commons/graph/shortestpath/ShortestPathAlgorithmSelector.java b/src/main/java/org/apache/commons/graph/shortestpath/ShortestPathAlgorithmSelector.java
index c84ab90..a328c3e 100644
--- a/src/main/java/org/apache/commons/graph/shortestpath/ShortestPathAlgorithmSelector.java
+++ b/src/main/java/org/apache/commons/graph/shortestpath/ShortestPathAlgorithmSelector.java
@@ -42,21 +42,21 @@ public interface ShortestPathAlgorithmSelector<V, WE, W>
<WO extends OrderedMonoid<W>> HeuristicBuilder<V, WE, W> applyingAStar( WO weightOperations );
/**
- * Calculates the shortest path using Dijkstra's algorithm.
+ * Calculates the shortest path using bidirectional Dijkstra's algorithm.
*
* @param <WO> the type of weight operations
* @param weightOperations the class responsible for operations on weights
* @return a path which describes the shortest path, if any, otherwise a {@link PathNotFoundException} will be thrown
*/
- <WO extends OrderedMonoid<W>> WeightedPath<V, WE, W> applyingDijkstra( WO weightOperations );
+ <WO extends OrderedMonoid<W>> WeightedPath<V, WE, W> applyingBidirectionalDijkstra( WO weightOperations );
/**
- * Calculates the shortest path using bidirectional Dijkstra's algorithm.
+ * Calculates the shortest path using Dijkstra's algorithm.
*
* @param <WO> the type of weight operations
* @param weightOperations the class responsible for operations on weights
* @return a path which describes the shortest path, if any, otherwise a {@link PathNotFoundException} will be thrown
*/
- <WO extends OrderedMonoid<W>> WeightedPath<V, WE, W> applyingBidirectionalDijkstra( WO weightOperations );
+ <WO extends OrderedMonoid<W>> WeightedPath<V, WE, W> applyingDijkstra( WO weightOperations );
}
diff --git a/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java b/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java
index 82078c9..15fe227 100644
--- a/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java
+++ b/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java
@@ -58,25 +58,6 @@ final class DefaultSpanningTreeSourceSelector<V, W, WE>
this.weightedEdges = weightedEdges;
}
- /**
- * {@inheritDoc}
- */
- public SpanningTreeAlgorithmSelector<V, W, WE> fromArbitrarySource()
- {
- checkState( graph.getOrder() > 0, "Spanning tree cannot be calculated on an empty graph" );
- return fromSource( graph.getVertices().iterator().next() );
- }
-
- /**
- * {@inheritDoc}
- */
- public <S extends V> SpanningTreeAlgorithmSelector<V, W, WE> fromSource( S source )
- {
- source = checkNotNull( source, "Spanning tree cannot be calculated without expressing the source vertex" );
- checkState( graph.containsVertex( source ), "Vertex %s does not exist in the Graph", source );
- return new DefaultSpanningTreeAlgorithmSelector<V, W, WE>( graph, weightedEdges, source );
- }
-
/**
* {@inheritDoc}
*/
@@ -135,4 +116,23 @@ final class DefaultSpanningTreeSourceSelector<V, W, WE>
return res;
}
+ /**
+ * {@inheritDoc}
+ */
+ public SpanningTreeAlgorithmSelector<V, W, WE> fromArbitrarySource()
+ {
+ checkState( graph.getOrder() > 0, "Spanning tree cannot be calculated on an empty graph" );
+ return fromSource( graph.getVertices().iterator().next() );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public <S extends V> SpanningTreeAlgorithmSelector<V, W, WE> fromSource( S source )
+ {
+ source = checkNotNull( source, "Spanning tree cannot be calculated without expressing the source vertex" );
+ checkState( graph.containsVertex( source ), "Vertex %s does not exist in the Graph", source );
+ return new DefaultSpanningTreeAlgorithmSelector<V, W, WE>( graph, weightedEdges, source );
+ }
+
}
diff --git a/src/main/java/org/apache/commons/graph/spanning/ReverseDeleteGraph.java b/src/main/java/org/apache/commons/graph/spanning/ReverseDeleteGraph.java
index b2ee26f..65c23c1 100644
--- a/src/main/java/org/apache/commons/graph/spanning/ReverseDeleteGraph.java
+++ b/src/main/java/org/apache/commons/graph/spanning/ReverseDeleteGraph.java
@@ -50,32 +50,14 @@ final class ReverseDeleteGraph<V, WE>
this.visitedEdge = visitedEdge;
}
- public Iterable<V> getVertices()
- {
- return graph.getVertices();
- }
-
- public int getOrder()
- {
- return graph.getOrder();
- }
-
- public Iterable<WE> getEdges()
- {
- List<WE> e = new ArrayList<WE>();
- e.addAll( sortedEdge );
- e.addAll( visitedEdge );
- return e;
- }
-
- public int getSize()
+ public boolean containsEdge( WE e )
{
- return sortedEdge.size() + visitedEdge.size();
+ return graph.containsEdge( e );
}
- public int getDegree( V v )
+ public boolean containsVertex( V v )
{
- throw new GraphException( "Unused Method" );
+ return graph.containsVertex( v );
}
public Iterable<V> getConnectedVertices( V v )
@@ -93,6 +75,11 @@ final class ReverseDeleteGraph<V, WE>
return tmp;
}
+ public int getDegree( V v )
+ {
+ throw new GraphException( "Unused Method" );
+ }
+
public WE getEdge( V source, V target )
{
WE edge = graph.getEdge( source, target );
@@ -108,6 +95,29 @@ final class ReverseDeleteGraph<V, WE>
return null;
}
+ public Iterable<WE> getEdges()
+ {
+ List<WE> e = new ArrayList<WE>();
+ e.addAll( sortedEdge );
+ e.addAll( visitedEdge );
+ return e;
+ }
+
+ public int getOrder()
+ {
+ return graph.getOrder();
+ }
+
+ public int getSize()
+ {
+ return sortedEdge.size() + visitedEdge.size();
+ }
+
+ public Iterable<V> getVertices()
+ {
+ return graph.getVertices();
+ }
+
public VertexPair<V> getVertices( WE e )
{
for ( WE edge : sortedEdge )
@@ -131,14 +141,4 @@ final class ReverseDeleteGraph<V, WE>
return null;
}
- public boolean containsVertex( V v )
- {
- return graph.containsVertex( v );
- }
-
- public boolean containsEdge( WE e )
- {
- return graph.containsEdge( e );
- }
-
}
diff --git a/src/main/java/org/apache/commons/graph/spanning/ShortestEdges.java b/src/main/java/org/apache/commons/graph/spanning/ShortestEdges.java
index 3ef03a7..e730d1c 100644
--- a/src/main/java/org/apache/commons/graph/spanning/ShortestEdges.java
+++ b/src/main/java/org/apache/commons/graph/spanning/ShortestEdges.java
@@ -43,6 +43,18 @@ final class ShortestEdges<V, WE, W>
implements Comparator<V>
{
+ private static <V, WE, W> void addEdgeIgnoringExceptions( V vertex, MutableSpanningTree<V, WE, W> spanningTree )
+ {
+ try
+ {
+ spanningTree.addVertex( vertex );
+ }
+ catch ( GraphException e )
+ {
+ // just swallow it
+ }
+ }
+
private final Map<V, WE> predecessors = new HashMap<V, WE>();
private final OrderedMonoid<W> weightOperations;
@@ -72,6 +84,26 @@ final class ShortestEdges<V, WE, W>
predecessors.put( tail, head );
}
+ /**
+ * {@inheritDoc}
+ */
+ public int compare( V left, V right )
+ {
+ if ( !hasWeight( left ) && !hasWeight( right ) )
+ {
+ return 0;
+ }
+ else if ( !hasWeight( left ) )
+ {
+ return 1;
+ }
+ else if ( !hasWeight( right ) )
+ {
+ return -1;
+ }
+ return weightOperations.compare( getWeight( left ), getWeight( right ) );
+ }
+
/**
* Creates a spanning tree using the current data.
*
@@ -97,28 +129,6 @@ final class ShortestEdges<V, WE, W>
return spanningTree;
}
- private static <V, WE, W> void addEdgeIgnoringExceptions( V vertex, MutableSpanningTree<V, WE, W> spanningTree )
- {
- try
- {
- spanningTree.addVertex( vertex );
- }
- catch ( GraphException e )
- {
- // just swallow it
- }
- }
-
- /**
- * Checks the predecessor list has no elements.
- *
- * @return true, if the predecessor list has no elements, false otherwise.
- */
- public boolean isEmpty()
- {
- return predecessors.isEmpty();
- }
-
/**
* Returns the distance related to input vertex, or null if it does not exist.
*
@@ -157,23 +167,13 @@ final class ShortestEdges<V, WE, W>
}
/**
- * {@inheritDoc}
+ * Checks the predecessor list has no elements.
+ *
+ * @return true, if the predecessor list has no elements, false otherwise.
*/
- public int compare( V left, V right )
+ public boolean isEmpty()
{
- if ( !hasWeight( left ) && !hasWeight( right ) )
- {
- return 0;
- }
- else if ( !hasWeight( left ) )
- {
- return 1;
- }
- else if ( !hasWeight( right ) )
- {
- return -1;
- }
- return weightOperations.compare( getWeight( left ), getWeight( right ) );
+ return predecessors.isEmpty();
}
/**
diff --git a/src/main/java/org/apache/commons/graph/spanning/SpanningTreeSourceSelector.java b/src/main/java/org/apache/commons/graph/spanning/SpanningTreeSourceSelector.java
index 1cd5468..bd5f3c7 100644
--- a/src/main/java/org/apache/commons/graph/spanning/SpanningTreeSourceSelector.java
+++ b/src/main/java/org/apache/commons/graph/spanning/SpanningTreeSourceSelector.java
@@ -32,21 +32,6 @@ import org.apache.commons.graph.weight.OrderedMonoid;
public interface SpanningTreeSourceSelector<V, W, WE>
{
- /**
- * Selects an arbitrary source from the input Graph to calculate the spanning tree.
- *
- * @return the linked spanning tree algorithm builder
- */
- SpanningTreeAlgorithmSelector<V, W, WE> fromArbitrarySource();
-
- /**
- * Allows specify a source vertex to calculate the spanning tree.
- *
- * @param source the source vertex to calculate the spanning tree.
- * @return the linked spanning tree algorithm builder
- */
- <S extends V> SpanningTreeAlgorithmSelector<V, W, WE> fromSource( S source );
-
/**
* Applies the <a href="http://en.wikipedia.org/wiki/Reverse-Delete_algorithm">Reverse-Delete</a> algorithm.
*
@@ -69,4 +54,19 @@ public interface SpanningTreeSourceSelector<V, W, WE>
*/
<WO extends OrderedMonoid<W>> SpanningTree<V, WE, W> applyingReverseDeleteAlgorithm( WO weightOperations );
+ /**
+ * Selects an arbitrary source from the input Graph to calculate the spanning tree.
+ *
+ * @return the linked spanning tree algorithm builder
+ */
+ SpanningTreeAlgorithmSelector<V, W, WE> fromArbitrarySource();
+
+ /**
+ * Allows specify a source vertex to calculate the spanning tree.
+ *
+ * @param source the source vertex to calculate the spanning tree.
+ * @return the linked spanning tree algorithm builder
+ */
+ <S extends V> SpanningTreeAlgorithmSelector<V, W, WE> fromSource( S source );
+
}
diff --git a/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java b/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java
index 8d29f7f..84f4a03 100644
--- a/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java
+++ b/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java
@@ -75,6 +75,28 @@ class SuperVertex<V, W, WE>
}
}
+ /**
+ * Returns the edge with the minimum weight to other {@link SuperVertex}
+ * instances.
+ *
+ * @return the minimum weight edge or <code>null</code> if there is no edge
+ */
+ public WE getMinimumWeightEdge()
+ {
+ boolean found = false;
+ WE edge = null;
+ while ( !found && !orderedEdges.isEmpty() )
+ {
+ edge = orderedEdges.pollFirst();
+ VertexPair<V> pair = graph.getVertices( edge );
+ if ( !vertices.contains( pair.getHead() ) || !vertices.contains( pair.getTail() ) )
+ {
+ found = true;
+ }
+ }
+ return edge;
+ }
+
/** {@inheritDoc} */
public Iterator<V> iterator()
{
@@ -105,26 +127,4 @@ class SuperVertex<V, W, WE>
}
}
- /**
- * Returns the edge with the minimum weight to other {@link SuperVertex}
- * instances.
- *
- * @return the minimum weight edge or <code>null</code> if there is no edge
- */
- public WE getMinimumWeightEdge()
- {
- boolean found = false;
- WE edge = null;
- while ( !found && !orderedEdges.isEmpty() )
- {
- edge = orderedEdges.pollFirst();
- VertexPair<V> pair = graph.getVertices( edge );
- if ( !vertices.contains( pair.getHead() ) || !vertices.contains( pair.getTail() ) )
- {
- found = true;
- }
- }
- return edge;
- }
-
}
diff --git a/src/main/java/org/apache/commons/graph/utils/Assertions.java b/src/main/java/org/apache/commons/graph/utils/Assertions.java
index d7695a8..dbda86c 100644
--- a/src/main/java/org/apache/commons/graph/utils/Assertions.java
+++ b/src/main/java/org/apache/commons/graph/utils/Assertions.java
@@ -27,11 +27,6 @@ import static java.lang.String.format;
public final class Assertions
{
- private Assertions()
- {
- // do nothing
- }
-
/**
* Ensures the truth of an expression involving one or more parameters to the
* calling method.
@@ -59,31 +54,6 @@ public final class Assertions
}
}
- /**
- * Ensures the truth of an expression involving the state of the calling
- * instance, but not involving any parameters to the calling method.
- *
- * @param expression a boolean expression
- * @param errorMessageTemplate a template for the exception message should the
- * check fail. The message is formed by replacing each {@code %s}
- * placeholder in the template with an argument. These are matched by
- * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
- * Unmatched arguments will be appended to the formatted message in square
- * braces. Unmatched placeholders will be left as-is.
- * @param errorMessageArgs the arguments to be substituted into the message template.
- * @throws IllegalStateException if {@code expression} is false
- * @throws NullPointerException if the check fails and either {@code
- * errorMessageTemplate} or {@code errorMessageArgs} is null (don't let
- * this happen)
- */
- public static void checkState( boolean expression, String errorMessageTemplate, Object... errorMessageArgs )
- {
- if ( !expression )
- {
- throw new IllegalStateException( format( errorMessageTemplate, errorMessageArgs ) );
- }
- }
-
/**
* Ensures that an object reference passed as a parameter to the calling
* method is not null.
@@ -112,4 +82,34 @@ public final class Assertions
return reference;
}
+ /**
+ * Ensures the truth of an expression involving the state of the calling
+ * instance, but not involving any parameters to the calling method.
+ *
+ * @param expression a boolean expression
+ * @param errorMessageTemplate a template for the exception message should the
+ * check fail. The message is formed by replacing each {@code %s}
+ * placeholder in the template with an argument. These are matched by
+ * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
+ * Unmatched arguments will be appended to the formatted message in square
+ * braces. Unmatched placeholders will be left as-is.
+ * @param errorMessageArgs the arguments to be substituted into the message template.
+ * @throws IllegalStateException if {@code expression} is false
+ * @throws NullPointerException if the check fails and either {@code
+ * errorMessageTemplate} or {@code errorMessageArgs} is null (don't let
+ * this happen)
+ */
+ public static void checkState( boolean expression, String errorMessageTemplate, Object... errorMessageArgs )
+ {
+ if ( !expression )
+ {
+ throw new IllegalStateException( format( errorMessageTemplate, errorMessageArgs ) );
+ }
+ }
+
+ private Assertions()
+ {
+ // do nothing
+ }
+
}
diff --git a/src/main/java/org/apache/commons/graph/utils/Objects.java b/src/main/java/org/apache/commons/graph/utils/Objects.java
index 14ebb99..d47ff1a 100644
--- a/src/main/java/org/apache/commons/graph/utils/Objects.java
+++ b/src/main/java/org/apache/commons/graph/utils/Objects.java
@@ -26,11 +26,15 @@ public final class Objects
{
/**
- * Hidden constructor, this class must not be instantiated.
+ * Verifies input objects are equal.
+ *
+ * @param o1 the first argument to compare
+ * @param o2 the second argument to compare
+ * @return true, if the input arguments are equal, false otherwise.
*/
- private Objects()
+ public static <O> boolean eq( O o1, O o2 )
{
- // do nothing
+ return o1 != null ? o1.equals( o2 ) : o2 == null;
}
/**
@@ -52,15 +56,11 @@ public final class Objects
}
/**
- * Verifies input objects are equal.
- *
- * @param o1 the first argument to compare
- * @param o2 the second argument to compare
- * @return true, if the input arguments are equal, false otherwise.
+ * Hidden constructor, this class must not be instantiated.
*/
- public static <O> boolean eq( O o1, O o2 )
+ private Objects()
{
- return o1 != null ? o1.equals( o2 ) : o2 == null;
+ // do nothing
}
}
diff --git a/src/main/java/org/apache/commons/graph/visit/BaseGraphVisitHandler.java b/src/main/java/org/apache/commons/graph/visit/BaseGraphVisitHandler.java
index ca7f23c..2e6a9ad 100644
--- a/src/main/java/org/apache/commons/graph/visit/BaseGraphVisitHandler.java
+++ b/src/main/java/org/apache/commons/graph/visit/BaseGraphVisitHandler.java
@@ -36,24 +36,24 @@ public class BaseGraphVisitHandler<V, E, G extends Graph<V, E>, O>
/**
* {@inheritDoc}
*/
- public void discoverGraph( G graph )
+ public VisitState discoverEdge( V head, E edge, V tail )
{
// do nothing
+ return CONTINUE;
}
/**
* {@inheritDoc}
*/
- public VisitState discoverVertex( V vertex )
+ public void discoverGraph( G graph )
{
// do nothing
- return CONTINUE;
}
/**
* {@inheritDoc}
*/
- public VisitState discoverEdge( V head, E edge, V tail )
+ public VisitState discoverVertex( V vertex )
{
// do nothing
return CONTINUE;
@@ -71,18 +71,18 @@ public class BaseGraphVisitHandler<V, E, G extends Graph<V, E>, O>
/**
* {@inheritDoc}
*/
- public VisitState finishVertex( V vertex )
+ public void finishGraph( G graph )
{
// do nothing
- return CONTINUE;
}
/**
* {@inheritDoc}
*/
- public void finishGraph( G graph )
+ public VisitState finishVertex( V vertex )
{
// do nothing
+ return CONTINUE;
}
/**
diff --git a/src/main/java/org/apache/commons/graph/visit/DefaultVisitAlgorithmsSelector.java b/src/main/java/org/apache/commons/graph/visit/DefaultVisitAlgorithmsSelector.java
index bb343d0..8179a59 100644
--- a/src/main/java/org/apache/commons/graph/visit/DefaultVisitAlgorithmsSelector.java
+++ b/src/main/java/org/apache/commons/graph/visit/DefaultVisitAlgorithmsSelector.java
@@ -72,17 +72,17 @@ final class DefaultVisitAlgorithmsSelector<V, E, G extends Graph<V, E>>
/**
* {@inheritDoc}
*/
- public Graph<V, E> applyingDepthFirstSearch()
+ public <O> O applyingBreadthFirstSearch( GraphVisitHandler<V, E, G, O> handler )
{
- return applyingDepthFirstSearch( new VisitGraphBuilder<V, E, G>() );
+ return applyingSearch( handler, true );
}
/**
* {@inheritDoc}
*/
- public <O> O applyingBreadthFirstSearch( GraphVisitHandler<V, E, G, O> handler )
+ public Graph<V, E> applyingDepthFirstSearch()
{
- return applyingSearch( handler, true );
+ return applyingDepthFirstSearch( new VisitGraphBuilder<V, E, G>() );
}
/**
diff --git a/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java b/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java
index d609d69..399e448 100644
--- a/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java
+++ b/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java
@@ -28,6 +28,18 @@ import org.apache.commons.graph.Graph;
public interface GraphVisitHandler<V, E, G extends Graph<V, E>, O>
{
+ /**
+ * Performs operations on the input edge and determines the behavior of the visit algorithm
+ * based on the return value:
+ * <ul>
+ * <li>{@link VisitState.CONTINUE} continues the visit normally;</li>
+ * <li>{@link VisitState.SKIP} continues the visit skipping the input edge;</li>
+ * <li>{@link VisitState.ABORT} terminates the visit.</li>
+ * </ul>
+ * @return the state of the visit after operations on the edge
+ */
+ VisitState discoverEdge( V head, E edge, V tail );
+
/**
* Called at the beginning of breadth-first and depth-first search.
*/
@@ -45,18 +57,6 @@ public interface GraphVisitHandler<V, E, G extends Graph<V, E>, O>
*/
VisitState discoverVertex( V vertex );
- /**
- * Performs operations on the input edge and determines the behavior of the visit algorithm
- * based on the return value:
- * <ul>
- * <li>{@link VisitState.CONTINUE} continues the visit normally;</li>
- * <li>{@link VisitState.SKIP} continues the visit skipping the input edge;</li>
- * <li>{@link VisitState.ABORT} terminates the visit.</li>
- * </ul>
- * @return the state of the visit after operations on the edge
- */
- VisitState discoverEdge( V head, E edge, V tail );
-
/**
* Checks if the search algorithm should be terminated. Called after the search algorithm has finished
* visiting the input edge.
@@ -64,6 +64,11 @@ public interface GraphVisitHandler<V, E, G extends Graph<V, E>, O>
*/
VisitState finishEdge( V head, E edge, V tail );
+ /**
+ * Called upon termination of the search algorithm.
+ */
+ void finishGraph( G graph );
+
/**
* Checks if the search algorithm should be terminated. Called after the search algorithm has finished
* visiting the input vertex.
@@ -71,11 +76,6 @@ public interface GraphVisitHandler<V, E, G extends Graph<V, E>, O>
*/
VisitState finishVertex( V vertex );
- /**
- * Called upon termination of the search algorithm.
- */
- void finishGraph( G graph );
-
/**
* Invoked once the visit is finished.
*
diff --git a/src/main/java/org/apache/commons/graph/visit/VisitAlgorithmsSelector.java b/src/main/java/org/apache/commons/graph/visit/VisitAlgorithmsSelector.java
index 31affc2..894de41 100644
--- a/src/main/java/org/apache/commons/graph/visit/VisitAlgorithmsSelector.java
+++ b/src/main/java/org/apache/commons/graph/visit/VisitAlgorithmsSelector.java
@@ -39,18 +39,18 @@ public interface VisitAlgorithmsSelector<V, E, G extends Graph<V, E>>
Graph<V, E> applyingBreadthFirstSearch();
/**
- * Depth-first search algorithm implementation.
+ * Breadth-first search algorithm implementation.
*
- * @return the depth first search tree
+ * @param handler the handler intercepts visit actions
*/
- Graph<V, E> applyingDepthFirstSearch();
+ <O> O applyingBreadthFirstSearch( GraphVisitHandler<V, E, G, O> handler );
/**
- * Breadth-first search algorithm implementation.
+ * Depth-first search algorithm implementation.
*
- * @param handler the handler intercepts visit actions
+ * @return the depth first search tree
*/
- <O> O applyingBreadthFirstSearch( GraphVisitHandler<V, E, G, O> handler );
+ Graph<V, E> applyingDepthFirstSearch();
/**
* Depth-first search algorithm implementation.
diff --git a/src/main/java/org/apache/commons/graph/visit/VisitGraphBuilder.java b/src/main/java/org/apache/commons/graph/visit/VisitGraphBuilder.java
index 31a4181..a41a7a9 100644
--- a/src/main/java/org/apache/commons/graph/visit/VisitGraphBuilder.java
+++ b/src/main/java/org/apache/commons/graph/visit/VisitGraphBuilder.java
@@ -37,6 +37,16 @@ final class VisitGraphBuilder<V, E, G extends Graph<V, E>>
private BaseMutableGraph<V, E> visitGraph;
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public VisitState discoverEdge( V head, E edge, V tail )
+ {
+ visitGraph.addEdge( head, edge, tail );
+ return VisitState.CONTINUE;
+ }
+
/**
* {@inheritDoc}
*/
@@ -58,16 +68,6 @@ final class VisitGraphBuilder<V, E, G extends Graph<V, E>>
}
}
- /**
- * {@inheritDoc}
- */
- @Override
- public VisitState discoverEdge( V head, E edge, V tail )
- {
- visitGraph.addEdge( head, edge, tail );
- return VisitState.CONTINUE;
- }
-
/**
* {@inheritDoc}
*/
diff --git a/src/main/java/org/apache/commons/graph/weight/Monoid.java b/src/main/java/org/apache/commons/graph/weight/Monoid.java
index 4f99408..bc4cd3a 100644
--- a/src/main/java/org/apache/commons/graph/weight/Monoid.java
+++ b/src/main/java/org/apache/commons/graph/weight/Monoid.java
@@ -30,13 +30,6 @@ public interface Monoid<E>
extends Serializable
{
- /**
- * Returns the identity value.
- *
- * @return the identity value
- */
- E identity();
-
/**
* Returns the result of the associative binary operation defined by this
* {@link Semigroup} between two elements of appropriate type.
@@ -47,6 +40,13 @@ public interface Monoid<E>
*/
E append( E e1, E e2 );
+ /**
+ * Returns the identity value.
+ *
+ * @return the identity value
+ */
+ E identity();
+
/**
* Returns the inverse of the input element.
* @param element the input element
diff --git a/src/main/java/org/apache/commons/graph/weight/primitive/BigDecimalWeightBaseOperations.java b/src/main/java/org/apache/commons/graph/weight/primitive/BigDecimalWeightBaseOperations.java
index c3ac94d..1b91357 100644
--- a/src/main/java/org/apache/commons/graph/weight/primitive/BigDecimalWeightBaseOperations.java
+++ b/src/main/java/org/apache/commons/graph/weight/primitive/BigDecimalWeightBaseOperations.java
@@ -35,14 +35,6 @@ public class BigDecimalWeightBaseOperations
private static final long serialVersionUID = -317234195461348466L;
- /**
- * {@inheritDoc}
- */
- public BigDecimal identity()
- {
- return ZERO;
- }
-
/**
* {@inheritDoc}
*/
@@ -58,17 +50,25 @@ public class BigDecimalWeightBaseOperations
/**
* {@inheritDoc}
*/
- public BigDecimal inverse( BigDecimal element )
+ public int compare( BigDecimal o1, BigDecimal o2 )
{
- return element.negate();
+ return o1.compareTo( o2 );
}
/**
* {@inheritDoc}
*/
- public int compare( BigDecimal o1, BigDecimal o2 )
+ public BigDecimal identity()
{
- return o1.compareTo( o2 );
+ return ZERO;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public BigDecimal inverse( BigDecimal element )
+ {
+ return element.negate();
}
}
diff --git a/src/main/java/org/apache/commons/graph/weight/primitive/BigIntegerWeightBaseOperations.java b/src/main/java/org/apache/commons/graph/weight/primitive/BigIntegerWeightBaseOperations.java
index 2dc9128..b817ae5 100644
--- a/src/main/java/org/apache/commons/graph/weight/primitive/BigIntegerWeightBaseOperations.java
+++ b/src/main/java/org/apache/commons/graph/weight/primitive/BigIntegerWeightBaseOperations.java
@@ -35,14 +35,6 @@ public class BigIntegerWeightBaseOperations
private static final long serialVersionUID = 4118152508299694652L;
- /**
- * {@inheritDoc}
- */
- public BigInteger identity()
- {
- return ZERO;
- }
-
/**
* {@inheritDoc}
*/
@@ -58,17 +50,25 @@ public class BigIntegerWeightBaseOperations
/**
* {@inheritDoc}
*/
- public BigInteger inverse( BigInteger element )
+ public int compare( BigInteger o1, BigInteger o2 )
{
- return element.negate();
+ return o1.compareTo( o2 );
}
/**
* {@inheritDoc}
*/
- public int compare( BigInteger o1, BigInteger o2 )
+ public BigInteger identity()
{
- return o1.compareTo( o2 );
+ return ZERO;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public BigInteger inverse( BigInteger element )
+ {
+ return element.negate();
}
}
diff --git a/src/main/java/org/apache/commons/graph/weight/primitive/DoubleWeightBaseOperations.java b/src/main/java/org/apache/commons/graph/weight/primitive/DoubleWeightBaseOperations.java
index 26c2744..148e84f 100644
--- a/src/main/java/org/apache/commons/graph/weight/primitive/DoubleWeightBaseOperations.java
+++ b/src/main/java/org/apache/commons/graph/weight/primitive/DoubleWeightBaseOperations.java
@@ -30,14 +30,6 @@ public class DoubleWeightBaseOperations
{
private static final long serialVersionUID = 4440399710792243877L;
- /**
- * {@inheritDoc}
- */
- public Double identity()
- {
- return 0.0;
- }
-
/**
* {@inheritDoc}
*/
@@ -53,17 +45,25 @@ public class DoubleWeightBaseOperations
/**
* {@inheritDoc}
*/
- public Double inverse( Double element )
+ public int compare( Double s1, Double s2 )
{
- return -element;
+ return s1.compareTo( s2 );
}
/**
* {@inheritDoc}
*/
- public int compare( Double s1, Double s2 )
+ public Double identity()
{
- return s1.compareTo( s2 );
+ return 0.0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Double inverse( Double element )
+ {
+ return -element;
}
}
diff --git a/src/main/java/org/apache/commons/graph/weight/primitive/FloatWeightBaseOperations.java b/src/main/java/org/apache/commons/graph/weight/primitive/FloatWeightBaseOperations.java
index 3418fde..0ae8b54 100644
--- a/src/main/java/org/apache/commons/graph/weight/primitive/FloatWeightBaseOperations.java
+++ b/src/main/java/org/apache/commons/graph/weight/primitive/FloatWeightBaseOperations.java
@@ -31,14 +31,6 @@ public class FloatWeightBaseOperations
private static final long serialVersionUID = 8542498901286671169L;
- /**
- * {@inheritDoc}
- */
- public Float identity()
- {
- return 0.0F;
- }
-
/**
* {@inheritDoc}
*/
@@ -54,17 +46,25 @@ public class FloatWeightBaseOperations
/**
* {@inheritDoc}
*/
- public Float inverse( Float element )
+ public int compare( Float s1, Float s2 )
{
- return -element;
+ return s1.compareTo( s2 );
}
/**
* {@inheritDoc}
*/
- public int compare( Float s1, Float s2 )
+ public Float identity()
{
- return s1.compareTo( s2 );
+ return 0.0F;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Float inverse( Float element )
+ {
+ return -element;
}
}
diff --git a/src/main/java/org/apache/commons/graph/weight/primitive/IntegerWeightBaseOperations.java b/src/main/java/org/apache/commons/graph/weight/primitive/IntegerWeightBaseOperations.java
index b0406e8..91b6c9d 100644
--- a/src/main/java/org/apache/commons/graph/weight/primitive/IntegerWeightBaseOperations.java
+++ b/src/main/java/org/apache/commons/graph/weight/primitive/IntegerWeightBaseOperations.java
@@ -31,14 +31,6 @@ public class IntegerWeightBaseOperations
private static final long serialVersionUID = -8641477350652350485L;
- /**
- * {@inheritDoc}
- */
- public Integer identity()
- {
- return 0;
- }
-
/**
* {@inheritDoc}
*/
@@ -54,17 +46,25 @@ public class IntegerWeightBaseOperations
/**
* {@inheritDoc}
*/
- public Integer inverse( Integer element )
+ public int compare( Integer o1, Integer o2 )
{
- return -element;
+ return o1.compareTo( o2 );
}
/**
* {@inheritDoc}
*/
- public int compare( Integer o1, Integer o2 )
+ public Integer identity()
{
- return o1.compareTo( o2 );
+ return 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Integer inverse( Integer element )
+ {
+ return -element;
}
}
diff --git a/src/main/java/org/apache/commons/graph/weight/primitive/LongWeightBaseOperations.java b/src/main/java/org/apache/commons/graph/weight/primitive/LongWeightBaseOperations.java
index d730575..0bcb2a8 100644
--- a/src/main/java/org/apache/commons/graph/weight/primitive/LongWeightBaseOperations.java
+++ b/src/main/java/org/apache/commons/graph/weight/primitive/LongWeightBaseOperations.java
@@ -31,14 +31,6 @@ public class LongWeightBaseOperations
private static final long serialVersionUID = 3149327896191098756L;
- /**
- * {@inheritDoc}
- */
- public Long identity()
- {
- return 0L;
- }
-
/**
* {@inheritDoc}
*/
@@ -54,17 +46,25 @@ public class LongWeightBaseOperations
/**
* {@inheritDoc}
*/
- public Long inverse( Long element )
+ public int compare( Long s1, Long s2 )
{
- return -element;
+ return s1.compareTo( s2 );
}
/**
* {@inheritDoc}
*/
- public int compare( Long s1, Long s2 )
+ public Long identity()
{
- return s1.compareTo( s2 );
+ return 0L;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Long inverse( Long element )
+ {
+ return -element;
}
}
diff --git a/src/test/java/org/apache/commons/graph/collections/FibonacciHeapTestCase.java b/src/test/java/org/apache/commons/graph/collections/FibonacciHeapTestCase.java
index 3b0a928..58e1974 100644
--- a/src/test/java/org/apache/commons/graph/collections/FibonacciHeapTestCase.java
+++ b/src/test/java/org/apache/commons/graph/collections/FibonacciHeapTestCase.java
@@ -44,6 +44,12 @@ public final class FibonacciHeapTestCase
private Queue<Integer> queue;
+ @Test( expected = NoSuchElementException.class )
+ public void elementThrowsException()
+ {
+ queue.element();
+ }
+
@Before
public void setUp()
{
@@ -56,6 +62,42 @@ public final class FibonacciHeapTestCase
queue = null;
}
+ @Test
+ public void testAddAllAndContinsItem()
+ {
+ Collection<Integer> c = new ArrayList<Integer>();
+
+ c.add( 50 );
+ c.add( 100 );
+ c.add( 20 );
+ c.add( 21 );
+
+ queue.addAll( c );
+
+ assertThat( queue.isEmpty(), is( false ) );
+ assertThat( queue.containsAll( c ), is( true ) );
+
+ assertThat( queue.contains( 100 ), is( true ) );
+ assertThat( queue.contains( 21 ), is( true ) );
+ assertThat( queue.contains( 50 ), is( true ) );
+ assertThat( queue.contains( 20 ), is( true ) );
+ }
+
+ @Test
+ public void testClearQueue()
+ {
+ final Random r = new Random( System.currentTimeMillis() );
+ for ( int i = 0; i < 1000; i++ )
+ {
+ Integer number = new Integer( r.nextInt( 10000 ) );
+ queue.add( number );
+ }
+
+ assertThat( queue.isEmpty(), is( false ) );
+ queue.clear();
+ assertThat( queue.isEmpty(), is( true ) );
+ }
+
@Test
public void testEmptyWhenCreated()
{
@@ -64,19 +106,25 @@ public final class FibonacciHeapTestCase
}
@Test
- public void testNoLongerEmptyAfterAdd()
+ public void testInsertSameValuesAndReturnsOrderedItems()
{
queue.add( 50 );
+ queue.add( 100 );
+ queue.add( 50 );
- assertThat( queue.isEmpty(), is( false ) );
+ assertThat( queue.poll(), is( 50 ) );
+ assertThat( queue.poll(), is( 50 ) );
+ assertThat( queue.poll(), is( 100 ) );
+ assertThat( queue.isEmpty(), is( true ) );
}
@Test
- public void testSingletonQueueReturnsSoleItemOnPoll()
+ public void testInsertSingleItem()
{
queue.add( 50 );
assertThat( queue.poll(), is( 50 ) );
+ assertThat( queue.isEmpty(), is( true ) );
}
@Test
@@ -89,33 +137,33 @@ public final class FibonacciHeapTestCase
}
@Test
- public void testReturnsOrderedItems()
+ public void testNoLongerEmptyAfterAdd()
{
- queue.add( 100 );
queue.add( 50 );
- assertThat( queue.poll(), is( 50 ) );
- assertThat( queue.poll(), is( 100 ) );
- assertThat( queue.isEmpty(), is( true ) );
+ assertThat( queue.isEmpty(), is( false ) );
}
@Test
- public void testInsertSingleItem()
+ public void testOfferPeekAndElement()
{
- queue.add( 50 );
+ queue.offer( 50 );
+ queue.offer( 100 );
+ queue.offer( 20 );
+ queue.offer( 21 );
- assertThat( queue.poll(), is( 50 ) );
- assertThat( queue.isEmpty(), is( true ) );
+ assertThat( queue.isEmpty(), is( false ) );
+ assertThat( queue.peek(), is( 20 ) );
+ assertThat( queue.element(), is( 20 ) );
+ assertThat( queue.size(), is( 4 ) );
}
@Test
- public void testInsertSameValuesAndReturnsOrderedItems()
+ public void testReturnsOrderedItems()
{
- queue.add( 50 );
queue.add( 100 );
queue.add( 50 );
- assertThat( queue.poll(), is( 50 ) );
assertThat( queue.poll(), is( 50 ) );
assertThat( queue.poll(), is( 100 ) );
assertThat( queue.isEmpty(), is( true ) );
@@ -144,60 +192,12 @@ public final class FibonacciHeapTestCase
assertThat( queue.isEmpty(), is( true ) );
}
-
- @Test
- public void testAddAllAndContinsItem()
- {
- Collection<Integer> c = new ArrayList<Integer>();
-
- c.add( 50 );
- c.add( 100 );
- c.add( 20 );
- c.add( 21 );
-
- queue.addAll( c );
-
- assertThat( queue.isEmpty(), is( false ) );
- assertThat( queue.containsAll( c ), is( true ) );
-
- assertThat( queue.contains( 100 ), is( true ) );
- assertThat( queue.contains( 21 ), is( true ) );
- assertThat( queue.contains( 50 ), is( true ) );
- assertThat( queue.contains( 20 ), is( true ) );
- }
-
- @Test
- public void testClearQueue()
- {
- final Random r = new Random( System.currentTimeMillis() );
- for ( int i = 0; i < 1000; i++ )
- {
- Integer number = new Integer( r.nextInt( 10000 ) );
- queue.add( number );
- }
-
- assertThat( queue.isEmpty(), is( false ) );
- queue.clear();
- assertThat( queue.isEmpty(), is( true ) );
- }
-
+
@Test
- public void testOfferPeekAndElement()
+ public void testSingletonQueueReturnsSoleItemOnPoll()
{
- queue.offer( 50 );
- queue.offer( 100 );
- queue.offer( 20 );
- queue.offer( 21 );
+ queue.add( 50 );
- assertThat( queue.isEmpty(), is( false ) );
- assertThat( queue.peek(), is( 20 ) );
- assertThat( queue.element(), is( 20 ) );
- assertThat( queue.size(), is( 4 ) );
- }
-
- @Test( expected = NoSuchElementException.class )
- public void elementThrowsException()
- {
- queue.element();
+ assertThat( queue.poll(), is( 50 ) );
}
}
diff --git a/src/test/java/org/apache/commons/graph/coloring/AbstractColoringTest.java b/src/test/java/org/apache/commons/graph/coloring/AbstractColoringTest.java
index 7c642ae..c32bac1 100644
--- a/src/test/java/org/apache/commons/graph/coloring/AbstractColoringTest.java
+++ b/src/test/java/org/apache/commons/graph/coloring/AbstractColoringTest.java
@@ -41,6 +41,27 @@ abstract class AbstractColoringTest
{
}
+ /**
+ * This method checks if all connected vertices have different colors.
+ *
+ * @param g
+ * @param coloredVertices
+ */
+ protected <V, E, C> void checkColoring( UndirectedMutableGraph<V, E> g,
+ ColoredVertices<V, C> coloredVertices )
+ {
+ for ( E e : g.getEdges() )
+ {
+ VertexPair<V> vp = g.getVertices( e );
+ C h = coloredVertices.getColor( vp.getHead() );
+ C t = coloredVertices.getColor( vp.getTail() );
+
+ assertNotNull( h );
+ assertNotNull( t );
+ assertTrue( !h.equals( t ) );
+ }
+ }
+
/**
* Return a random association with index and a color string in RGB.
*/
@@ -71,25 +92,4 @@ abstract class AbstractColoringTest
return colors;
}
- /**
- * This method checks if all connected vertices have different colors.
- *
- * @param g
- * @param coloredVertices
- */
- protected <V, E, C> void checkColoring( UndirectedMutableGraph<V, E> g,
- ColoredVertices<V, C> coloredVertices )
- {
- for ( E e : g.getEdges() )
- {
- VertexPair<V> vp = g.getVertices( e );
- C h = coloredVertices.getColor( vp.getHead() );
- C t = coloredVertices.getColor( vp.getTail() );
-
- assertNotNull( h );
- assertNotNull( t );
- assertTrue( !h.equals( t ) );
- }
- }
-
}
diff --git a/src/test/java/org/apache/commons/graph/coloring/GraphColoringBackTrackingTestCase.java b/src/test/java/org/apache/commons/graph/coloring/GraphColoringBackTrackingTestCase.java
index 36b81ea..bcba8f3 100644
--- a/src/test/java/org/apache/commons/graph/coloring/GraphColoringBackTrackingTestCase.java
+++ b/src/test/java/org/apache/commons/graph/coloring/GraphColoringBackTrackingTestCase.java
@@ -47,56 +47,22 @@ public class GraphColoringBackTrackingTestCase
extends AbstractColoringTest
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- coloring( (UndirectedGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null ).withColors( null ).applyingBackTrackingAlgorithm();
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullColorGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- coloring( g ).withColors( null ).applyingBackTrackingAlgorithm();
- }
-
+ /**
+ * see <a href="http://en.wikipedia.org/wiki/Crown_graph">wiki</a> for more details
+ */
@Test
- public void testEmptyGraph()
+ public void testCrawnGraph()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+ buildCrownGraph( 6, g );
+
ColoredVertices<BaseLabeledVertex, Integer> coloredVertices =
- coloring( g ).withColors( createColorsList( 1 ) ).applyingBackTrackingAlgorithm();
+ coloring( g ).withColors( createColorsList( 2 ) ).applyingBackTrackingAlgorithm();
assertNotNull( coloredVertices );
- assertEquals( 0, coloredVertices.getRequiredColors() );
- }
-
- @Test(expected=NotEnoughColorsException.class)
- public void testNotEnoughtColorGraph()
- throws NotEnoughColorsException
- {
- final BaseLabeledVertex two = new BaseLabeledVertex( "2" );
-
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
- newUndirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
- {
-
- @Override
- public void connect()
- {
- BaseLabeledVertex one = addVertex( new BaseLabeledVertex( "1" ) );
- addVertex( two );
- BaseLabeledVertex three = addVertex( new BaseLabeledVertex( "3" ) );
-
- addEdge( new BaseLabeledEdge( "1 -> 2" ) ).from( one ).to( two );
- addEdge( new BaseLabeledEdge( "2 -> 3" ) ).from( two ).to( three );
- addEdge( new BaseLabeledEdge( "3 -> 1" ) ).from( three ).to( one );
- }
-
- } );
- coloring( g ).withColors( createColorsList( 1 ) ).applyingBackTrackingAlgorithm();
+ assertEquals( 2, coloredVertices.getRequiredColors() );
+ checkColoring( g, coloredVertices );
}
@Test
@@ -134,32 +100,32 @@ public class GraphColoringBackTrackingTestCase
}
@Test
- public void testCromaticNumberComplete()
+ public void testCromaticNumberBiparted()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g1 =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- buildCompleteGraph( 100, g1 );
+ buildBipartedGraph( 100, g1 );
ColoredVertices<BaseLabeledVertex, Integer> coloredVertices =
- coloring( g1 ).withColors( createColorsList( 100 ) ).applyingBackTrackingAlgorithm();
+ coloring( g1 ).withColors( createColorsList( 2 ) ).applyingBackTrackingAlgorithm();
assertNotNull( coloredVertices );
- assertEquals( 100, coloredVertices.getRequiredColors() );
+ assertEquals( 2, coloredVertices.getRequiredColors() );
checkColoring( g1, coloredVertices );
}
@Test
- public void testCromaticNumberBiparted()
+ public void testCromaticNumberComplete()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g1 =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- buildBipartedGraph( 100, g1 );
+ buildCompleteGraph( 100, g1 );
ColoredVertices<BaseLabeledVertex, Integer> coloredVertices =
- coloring( g1 ).withColors( createColorsList( 2 ) ).applyingBackTrackingAlgorithm();
+ coloring( g1 ).withColors( createColorsList( 100 ) ).applyingBackTrackingAlgorithm();
assertNotNull( coloredVertices );
- assertEquals( 2, coloredVertices.getRequiredColors() );
+ assertEquals( 100, coloredVertices.getRequiredColors() );
checkColoring( g1, coloredVertices );
}
@@ -180,22 +146,56 @@ public class GraphColoringBackTrackingTestCase
checkColoring( g1, coloredVertices );
}
- /**
- * see <a href="http://en.wikipedia.org/wiki/Crown_graph">wiki</a> for more details
- */
@Test
- public void testCrawnGraph()
+ public void testEmptyGraph()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- buildCrownGraph( 6, g );
-
ColoredVertices<BaseLabeledVertex, Integer> coloredVertices =
- coloring( g ).withColors( createColorsList( 2 ) ).applyingBackTrackingAlgorithm();
+ coloring( g ).withColors( createColorsList( 1 ) ).applyingBackTrackingAlgorithm();
assertNotNull( coloredVertices );
- assertEquals( 2, coloredVertices.getRequiredColors() );
- checkColoring( g, coloredVertices );
+ assertEquals( 0, coloredVertices.getRequiredColors() );
+ }
+
+ @Test(expected=NotEnoughColorsException.class)
+ public void testNotEnoughtColorGraph()
+ throws NotEnoughColorsException
+ {
+ final BaseLabeledVertex two = new BaseLabeledVertex( "2" );
+
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
+ newUndirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
+ {
+
+ @Override
+ public void connect()
+ {
+ BaseLabeledVertex one = addVertex( new BaseLabeledVertex( "1" ) );
+ addVertex( two );
+ BaseLabeledVertex three = addVertex( new BaseLabeledVertex( "3" ) );
+
+ addEdge( new BaseLabeledEdge( "1 -> 2" ) ).from( one ).to( two );
+ addEdge( new BaseLabeledEdge( "2 -> 3" ) ).from( two ).to( three );
+ addEdge( new BaseLabeledEdge( "3 -> 1" ) ).from( three ).to( one );
+ }
+
+ } );
+ coloring( g ).withColors( createColorsList( 1 ) ).applyingBackTrackingAlgorithm();
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullColorGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+ coloring( g ).withColors( null ).applyingBackTrackingAlgorithm();
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ coloring( (UndirectedGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null ).withColors( null ).applyingBackTrackingAlgorithm();
}
@Test
diff --git a/src/test/java/org/apache/commons/graph/coloring/GraphColoringTestCase.java b/src/test/java/org/apache/commons/graph/coloring/GraphColoringTestCase.java
index bb21be1..32a3d70 100644
--- a/src/test/java/org/apache/commons/graph/coloring/GraphColoringTestCase.java
+++ b/src/test/java/org/apache/commons/graph/coloring/GraphColoringTestCase.java
@@ -46,55 +46,19 @@ public class GraphColoringTestCase extends AbstractColoringTest
private Set<Integer> colors = createColorsList( 11 );
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- coloring( (UndirectedGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null ).withColors( null ).applyingGreedyAlgorithm();
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullColorGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
-
- coloring( g ).withColors( null ).applyingBackTrackingAlgorithm();
- }
-
+ /**
+ * see <a href="http://en.wikipedia.org/wiki/Crown_graph">wiki</a> for more details
+ */
@Test
- public void testEmptyGraph()
+ public void testCrawnGraph()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+ buildCrownGraph( 6, g );
- ColoredVertices<BaseLabeledVertex, Integer> coloredVertices = coloring( g ).withColors( createColorsList( 1 ) ).applyingGreedyAlgorithm();
- assertNotNull( coloredVertices );
- assertEquals( 0, coloredVertices.getRequiredColors() );
- }
-
- @Test( expected = NotEnoughColorsException.class )
- public void testNotEnoughtColorGreedyGraph()
- {
- final BaseLabeledVertex two = new BaseLabeledVertex( "2" );
-
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
- newUndirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
- {
-
- @Override
- public void connect()
- {
- BaseLabeledVertex one = addVertex( new BaseLabeledVertex( "1" ) );
- addVertex( two );
- BaseLabeledVertex three = addVertex( new BaseLabeledVertex( "3" ) );
-
- addEdge( new BaseLabeledEdge( "1 -> 2" ) ).from( one ).to( two );
- addEdge( new BaseLabeledEdge( "2 -> 3" ) ).from( two ).to( three );
- addEdge( new BaseLabeledEdge( "3 -> 1" ) ).from( three ).to( one );
- }
-
- } );
- coloring( g ).withColors( createColorsList( 1 ) ).applyingGreedyAlgorithm();
+ ColoredVertices<BaseLabeledVertex, Integer> coloredVertices =
+ coloring( g ).withColors( colors ).applyingGreedyAlgorithm();
+ checkColoring( g, coloredVertices );
}
@Test
@@ -125,27 +89,27 @@ public class GraphColoringTestCase extends AbstractColoringTest
}
@Test
- public void testCromaticNumberComplete()
+ public void testCromaticNumberBiparted()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g1 =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- buildCompleteGraph( 100, g1 );
+ buildBipartedGraph( 100, g1 );
ColoredVertices<BaseLabeledVertex, Integer> coloredVertices =
- coloring( g1 ).withColors( createColorsList( 100 ) ).applyingGreedyAlgorithm();
+ coloring( g1 ).withColors( colors ).applyingGreedyAlgorithm();
+
checkColoring( g1, coloredVertices );
}
@Test
- public void testCromaticNumberBiparted()
+ public void testCromaticNumberComplete()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g1 =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- buildBipartedGraph( 100, g1 );
+ buildCompleteGraph( 100, g1 );
ColoredVertices<BaseLabeledVertex, Integer> coloredVertices =
- coloring( g1 ).withColors( colors ).applyingGreedyAlgorithm();
-
+ coloring( g1 ).withColors( createColorsList( 100 ) ).applyingGreedyAlgorithm();
checkColoring( g1, coloredVertices );
}
@@ -166,19 +130,55 @@ public class GraphColoringTestCase extends AbstractColoringTest
checkColoring( g1, coloredVertices );
}
- /**
- * see <a href="http://en.wikipedia.org/wiki/Crown_graph">wiki</a> for more details
- */
@Test
- public void testCrawnGraph()
+ public void testEmptyGraph()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- buildCrownGraph( 6, g );
- ColoredVertices<BaseLabeledVertex, Integer> coloredVertices =
- coloring( g ).withColors( colors ).applyingGreedyAlgorithm();
- checkColoring( g, coloredVertices );
+ ColoredVertices<BaseLabeledVertex, Integer> coloredVertices = coloring( g ).withColors( createColorsList( 1 ) ).applyingGreedyAlgorithm();
+ assertNotNull( coloredVertices );
+ assertEquals( 0, coloredVertices.getRequiredColors() );
+ }
+
+ @Test( expected = NotEnoughColorsException.class )
+ public void testNotEnoughtColorGreedyGraph()
+ {
+ final BaseLabeledVertex two = new BaseLabeledVertex( "2" );
+
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
+ newUndirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
+ {
+
+ @Override
+ public void connect()
+ {
+ BaseLabeledVertex one = addVertex( new BaseLabeledVertex( "1" ) );
+ addVertex( two );
+ BaseLabeledVertex three = addVertex( new BaseLabeledVertex( "3" ) );
+
+ addEdge( new BaseLabeledEdge( "1 -> 2" ) ).from( one ).to( two );
+ addEdge( new BaseLabeledEdge( "2 -> 3" ) ).from( two ).to( three );
+ addEdge( new BaseLabeledEdge( "3 -> 1" ) ).from( three ).to( one );
+ }
+
+ } );
+ coloring( g ).withColors( createColorsList( 1 ) ).applyingGreedyAlgorithm();
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullColorGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+
+ coloring( g ).withColors( null ).applyingBackTrackingAlgorithm();
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ coloring( (UndirectedGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null ).withColors( null ).applyingGreedyAlgorithm();
}
@Test
diff --git a/src/test/java/org/apache/commons/graph/connectivity/FindConnectedComponetTestCase.java b/src/test/java/org/apache/commons/graph/connectivity/FindConnectedComponetTestCase.java
index afbd60c..be3647f 100644
--- a/src/test/java/org/apache/commons/graph/connectivity/FindConnectedComponetTestCase.java
+++ b/src/test/java/org/apache/commons/graph/connectivity/FindConnectedComponetTestCase.java
@@ -41,44 +41,6 @@ import org.junit.Test;
public final class FindConnectedComponetTestCase
{
- @Test(expected=NullPointerException.class)
- public void verifyNullGraph()
- {
- findConnectedComponent( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null ).includingAllVertices().applyingMinimumSpanningTreeAlgorithm();
- }
-
- @Test
- public void testVerifyEmptyGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
-
- Collection<List<BaseLabeledVertex>> c =
- findConnectedComponent( graph ).includingAllVertices().applyingMinimumSpanningTreeAlgorithm();
- assertNotNull( c );
- assertEquals( 0, c.size() );
- }
-
- @Test
- public void testVerifyNullVerticesGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> graph =
- newUndirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
- {
-
- public void connect()
- {
- addVertex( new BaseLabeledVertex( "B" ) );
- addVertex( new BaseLabeledVertex( "C" ) );
- }
-
- } );
- Collection<List<BaseLabeledVertex>> c =
- findConnectedComponent( graph ).includingVertices().applyingMinimumSpanningTreeAlgorithm();
- assertNotNull( c );
- assertEquals( 0, c.size() );
- }
-
@Test
public void testVerifyConnectedComponents()
{
@@ -246,4 +208,42 @@ public final class FindConnectedComponetTestCase
assertEquals( 2, coll.size() );
}
+ @Test
+ public void testVerifyEmptyGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+
+ Collection<List<BaseLabeledVertex>> c =
+ findConnectedComponent( graph ).includingAllVertices().applyingMinimumSpanningTreeAlgorithm();
+ assertNotNull( c );
+ assertEquals( 0, c.size() );
+ }
+
+ @Test
+ public void testVerifyNullVerticesGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> graph =
+ newUndirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
+ {
+
+ public void connect()
+ {
+ addVertex( new BaseLabeledVertex( "B" ) );
+ addVertex( new BaseLabeledVertex( "C" ) );
+ }
+
+ } );
+ Collection<List<BaseLabeledVertex>> c =
+ findConnectedComponent( graph ).includingVertices().applyingMinimumSpanningTreeAlgorithm();
+ assertNotNull( c );
+ assertEquals( 0, c.size() );
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void verifyNullGraph()
+ {
+ findConnectedComponent( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null ).includingAllVertices().applyingMinimumSpanningTreeAlgorithm();
+ }
+
}
diff --git a/src/test/java/org/apache/commons/graph/elo/SimplePlayersRank.java b/src/test/java/org/apache/commons/graph/elo/SimplePlayersRank.java
index 261564a..9acc6d0 100644
--- a/src/test/java/org/apache/commons/graph/elo/SimplePlayersRank.java
+++ b/src/test/java/org/apache/commons/graph/elo/SimplePlayersRank.java
@@ -37,15 +37,15 @@ final class SimplePlayersRank
return ranks.get( player );
}
- public void updateRanking( String player, Double ranking )
- {
- ranks.put( player, ranking );
- }
-
@Override
public String toString()
{
return ranks.toString();
}
+ public void updateRanking( String player, Double ranking )
+ {
+ ranks.put( player, ranking );
+ }
+
}
diff --git a/src/test/java/org/apache/commons/graph/flow/EdmondsKarpTestCase.java b/src/test/java/org/apache/commons/graph/flow/EdmondsKarpTestCase.java
index 8553780..07ce663 100644
--- a/src/test/java/org/apache/commons/graph/flow/EdmondsKarpTestCase.java
+++ b/src/test/java/org/apache/commons/graph/flow/EdmondsKarpTestCase.java
@@ -39,6 +39,55 @@ import org.junit.Test;
public class EdmondsKarpTestCase
{
+ @Test
+ public void testFindMaxFlowAndVerify()
+ {
+ final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
+ final BaseLabeledVertex g = new BaseLabeledVertex( "G" );
+
+ DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph =
+ newDirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>()
+ {
+
+ @Override
+ public void connect()
+ {
+ addVertex( a );
+ BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "B" ) );
+ BaseLabeledVertex c = addVertex( new BaseLabeledVertex( "C" ) );
+ BaseLabeledVertex d = addVertex( new BaseLabeledVertex( "D" ) );
+ BaseLabeledVertex e = addVertex( new BaseLabeledVertex( "E" ) );
+ BaseLabeledVertex f = addVertex( new BaseLabeledVertex( "F" ) );
+ addVertex( g );
+
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "A -> B", 3 ) ).from( a ).to( b );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "A -> D", 3 ) ).from( a ).to( d );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "B -> C", 4 ) ).from( b ).to( c );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "C -> A", 3 ) ).from( c ).to( a );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "C -> D", 1 ) ).from( c ).to( d );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "C -> E", 2 ) ).from( c ).to( e );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "D -> E", 2 ) ).from( d ).to( e );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "D -> F", 6 ) ).from( d ).to( f );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "E -> B", 1 ) ).from( e ).to( b );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "E -> G", 1 ) ).from( e ).to( g );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "F -> G", 9 ) ).from( f ).to( g );
+ }
+
+ } );
+
+ // expected max flow
+ final Integer expected = 5;
+
+ // actual max flow
+ Integer actual = findMaxFlow( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
+ .from( a )
+ .to( g )
+ .applyingEdmondsKarp( new IntegerWeightBaseOperations() );
+
+ assertEquals( expected, actual );
+ }
+
@Test( expected = NullPointerException.class )
public void testNullGraph()
{
@@ -109,53 +158,4 @@ public class EdmondsKarpTestCase
assertEquals( actual, expected );
}
- @Test
- public void testFindMaxFlowAndVerify()
- {
- final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
- final BaseLabeledVertex g = new BaseLabeledVertex( "G" );
-
- DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph =
- newDirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>()
- {
-
- @Override
- public void connect()
- {
- addVertex( a );
- BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "B" ) );
- BaseLabeledVertex c = addVertex( new BaseLabeledVertex( "C" ) );
- BaseLabeledVertex d = addVertex( new BaseLabeledVertex( "D" ) );
- BaseLabeledVertex e = addVertex( new BaseLabeledVertex( "E" ) );
- BaseLabeledVertex f = addVertex( new BaseLabeledVertex( "F" ) );
- addVertex( g );
-
- addEdge( new BaseLabeledWeightedEdge<Integer>( "A -> B", 3 ) ).from( a ).to( b );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "A -> D", 3 ) ).from( a ).to( d );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "B -> C", 4 ) ).from( b ).to( c );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "C -> A", 3 ) ).from( c ).to( a );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "C -> D", 1 ) ).from( c ).to( d );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "C -> E", 2 ) ).from( c ).to( e );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "D -> E", 2 ) ).from( d ).to( e );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "D -> F", 6 ) ).from( d ).to( f );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "E -> B", 1 ) ).from( e ).to( b );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "E -> G", 1 ) ).from( e ).to( g );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "F -> G", 9 ) ).from( f ).to( g );
- }
-
- } );
-
- // expected max flow
- final Integer expected = 5;
-
- // actual max flow
- Integer actual = findMaxFlow( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
- .from( a )
- .to( g )
- .applyingEdmondsKarp( new IntegerWeightBaseOperations() );
-
- assertEquals( expected, actual );
- }
-
}
diff --git a/src/test/java/org/apache/commons/graph/flow/FordFulkersonTestCase.java b/src/test/java/org/apache/commons/graph/flow/FordFulkersonTestCase.java
index c88ae17..bfdbe72 100644
--- a/src/test/java/org/apache/commons/graph/flow/FordFulkersonTestCase.java
+++ b/src/test/java/org/apache/commons/graph/flow/FordFulkersonTestCase.java
@@ -39,27 +39,44 @@ import org.junit.Test;
public final class FordFulkersonTestCase
{
- @Test(expected=NullPointerException.class)
- public void testNullGraph()
+ @Test
+ public void testFindMaxFlowAndVerify()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
final BaseLabeledVertex d = new BaseLabeledVertex( "D" );
- findMaxFlow( (DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>) null )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
- .from( a )
- .to( d )
- .applyingFordFulkerson( new IntegerWeightBaseOperations() );
- }
+ DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph =
+ newDirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>()
+ {
- @Test(expected=NullPointerException.class)
- public void testNullGraphAndVertices()
- {
- findMaxFlow( (DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>) null )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
- .from( null )
- .to( null )
- .applyingFordFulkerson( new IntegerWeightBaseOperations() );
+ @Override
+ public void connect()
+ {
+ addVertex( a );
+ BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "B" ) );
+ BaseLabeledVertex c = addVertex( new BaseLabeledVertex( "C" ) );
+ addVertex( d );
+
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "A -> B", 1000 ) ).from( a ).to( b );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "A -> C", 1000 ) ).from( a ).to( c );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "B -> C", 1 ) ).from( b ).to( c );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "B -> D", 1000 ) ).from( b ).to( d );
+ addEdge( new BaseLabeledWeightedEdge<Integer>( "C -> D", 1000 ) ).from( c ).to( d );
+ }
+
+ } );
+
+ // expected max flow
+ final Integer expected = 2000;
+
+ // actual max flow
+ Integer actual = findMaxFlow( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
+ .from( a )
+ .to( d )
+ .applyingFordFulkerson( new IntegerWeightBaseOperations() );
+
+ assertEquals( expected, actual );
}
@Test
@@ -134,57 +151,40 @@ public final class FordFulkersonTestCase
}
@Test(expected=NullPointerException.class)
- public void testNullVertices()
+ public void testNullGraph()
{
- DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph =
- new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>();
+ final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
+ final BaseLabeledVertex d = new BaseLabeledVertex( "D" );
- // actual max flow
- findMaxFlow( graph )
+ findMaxFlow( (DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
+ .from( a )
+ .to( d )
+ .applyingFordFulkerson( new IntegerWeightBaseOperations() );
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testNullGraphAndVertices()
+ {
+ findMaxFlow( (DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>) null )
.whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
.from( null )
.to( null )
.applyingFordFulkerson( new IntegerWeightBaseOperations() );
}
- @Test
- public void testFindMaxFlowAndVerify()
+ @Test(expected=NullPointerException.class)
+ public void testNullVertices()
{
- final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
- final BaseLabeledVertex d = new BaseLabeledVertex( "D" );
-
DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph =
- newDirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>()
- {
-
- @Override
- public void connect()
- {
- addVertex( a );
- BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "B" ) );
- BaseLabeledVertex c = addVertex( new BaseLabeledVertex( "C" ) );
- addVertex( d );
-
- addEdge( new BaseLabeledWeightedEdge<Integer>( "A -> B", 1000 ) ).from( a ).to( b );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "A -> C", 1000 ) ).from( a ).to( c );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "B -> C", 1 ) ).from( b ).to( c );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "B -> D", 1000 ) ).from( b ).to( d );
- addEdge( new BaseLabeledWeightedEdge<Integer>( "C -> D", 1000 ) ).from( c ).to( d );
- }
-
- } );
-
- // expected max flow
- final Integer expected = 2000;
+ new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>();
// actual max flow
- Integer actual = findMaxFlow( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
- .from( a )
- .to( d )
- .applyingFordFulkerson( new IntegerWeightBaseOperations() );
-
- assertEquals( expected, actual );
+ findMaxFlow( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Integer>() )
+ .from( null )
+ .to( null )
+ .applyingFordFulkerson( new IntegerWeightBaseOperations() );
}
}
diff --git a/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java b/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java
index d2af19d..47af995 100644
--- a/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java
+++ b/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java
@@ -38,23 +38,6 @@ public class BaseLabeledEdge
this.label = checkNotNull( label, "Argument 'label' must not be null" );
}
- /**
- * {@inheritDoc}
- */
- public String getLabel()
- {
- return label;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode()
- {
- return hash( 1, 31, label );
- }
-
/**
* {@inheritDoc}
*/
@@ -76,6 +59,23 @@ public class BaseLabeledEdge
return eq( label, other.label );
}
+ /**
+ * {@inheritDoc}
+ */
+ public String getLabel()
+ {
+ return label;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode()
+ {
+ return hash( 1, 31, label );
+ }
+
/**
* {@inheritDoc}
*/
diff --git a/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java b/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java
index ded8d3a..7a34d09 100644
--- a/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java
+++ b/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java
@@ -38,23 +38,6 @@ public class BaseLabeledVertex
this.label = checkNotNull( label, "Argument 'label' must not be null" );
}
- /**
- * {@inheritDoc}
- */
- public final String getLabel()
- {
- return label;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode()
- {
- return hash( 1, 31, label );
- }
-
/**
* {@inheritDoc}
*/
@@ -75,6 +58,23 @@ public class BaseLabeledVertex
return eq( label, other.getLabel() );
}
+ /**
+ * {@inheritDoc}
+ */
+ public final String getLabel()
+ {
+ return label;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode()
+ {
+ return hash( 1, 31, label );
+ }
+
/**
* {@inheritDoc}
*/
diff --git a/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.java b/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.java
index fbe8c3e..ee232cd 100644
--- a/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.java
+++ b/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.java
@@ -41,23 +41,6 @@ public class BaseLabeledWeightedEdge<W>
this.weight = checkNotNull( weight, "Argument 'weight' must not be null" );
}
- /**
- * {@inheritDoc}
- */
- public W getWeight()
- {
- return weight;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode()
- {
- return hash( super.hashCode(), 31, weight );
- }
-
/**
* {@inheritDoc}
*/
@@ -83,6 +66,23 @@ public class BaseLabeledWeightedEdge<W>
return eq( weight, other.getWeight() );
}
+ /**
+ * {@inheritDoc}
+ */
+ public W getWeight()
+ {
+ return weight;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode()
+ {
+ return hash( super.hashCode(), 31, weight );
+ }
+
/**
* {@inheritDoc}
*/
diff --git a/src/test/java/org/apache/commons/graph/model/BaseMutableGraphTestCase.java b/src/test/java/org/apache/commons/graph/model/BaseMutableGraphTestCase.java
index df9eff1..4bdb78c 100644
--- a/src/test/java/org/apache/commons/graph/model/BaseMutableGraphTestCase.java
+++ b/src/test/java/org/apache/commons/graph/model/BaseMutableGraphTestCase.java
@@ -43,6 +43,59 @@ import org.junit.Test;
public class BaseMutableGraphTestCase
{
+ // Utility class.
+ private class GraphInsert
+ extends TestRunner
+ {
+
+ private MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g;
+
+ private int start;
+
+ private int end;
+
+ private GraphInsert( MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g, int start, int end )
+ {
+ this.g = g;
+ this.start = start;
+ this.end = end;
+ }
+
+ @Override
+ public void runTest()
+ {
+
+ // building a complete Graph
+ for ( int i = start; i < end; i++ )
+ {
+ BaseLabeledVertex v = new BaseLabeledVertex( valueOf( i ) );
+ g.addVertex( v );
+ }
+ synchronized ( g )
+ {
+ for ( BaseLabeledVertex v1 : g.getVertices() )
+ {
+ for ( BaseLabeledVertex v2 : g.getVertices() )
+ {
+ if ( !v1.equals( v2 ) )
+ {
+ try
+ {
+ BaseLabeledEdge e = new BaseLabeledEdge( v1 + " -> " + v2 );
+ g.addEdge( v1, e, v2 );
+ }
+ catch ( GraphException e )
+ {
+ // ignore
+ }
+ }
+ }
+ }
+
+ }
+ }
+ }
+
/**
* Test method for
* {@link org.apache.commons.graph.model.BaseMutableGraph#addVertex(org.apache.commons.graph.Vertex)}.
@@ -99,28 +152,74 @@ public class BaseMutableGraphTestCase
/**
* Test method for
- * {@link org.apache.commons.graph.model.BaseGraph#getConnectedVertices(org.apache.commons.graph.Vertex)}
+ * {@link org.apache.commons.graph.model.BaseMutableGraph#removeEdge(org.apache.commons.graph.Edge)}
*/
@Test
- public final void testGetConnectedVertices()
+ public final void testDirectedGraphRemoveEdge()
{
// Test a complete undirect graph.
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+ final DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+
+ final BaseLabeledVertex source = new BaseLabeledVertex( valueOf( 1 ) );
+ final BaseLabeledVertex target = new BaseLabeledVertex( valueOf( 2 ) );
+
buildCompleteGraph( 10, g );
- final BaseLabeledVertex testVertex = new BaseLabeledVertex( valueOf( 1 ) );
- Iterable<BaseLabeledVertex> connectedVertices = g.getConnectedVertices( testVertex );
- assertNotNull( connectedVertices );
+ BaseLabeledEdge e = g.getEdge( source, target );
+ g.removeEdge( e );
- final List<BaseLabeledVertex> v = new ArrayList<BaseLabeledVertex>();
- for ( BaseLabeledVertex baseLabeledVertex : connectedVertices )
+ BaseLabeledEdge edge = g.getEdge( source, target );
+ assertNull( edge );
+ }
+
+ /**
+ * Test method for {@link org.apache.commons.graph.model.BaseMutableGraph#removeEdge(org.apache.commons.graph.Edge)}
+ */
+ @Test( expected = GraphException.class )
+ public final void testDirectedGraphRemoveEdgeNotExists()
+ {
+ // Test a complete undirect graph.
+ DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = null;
+ BaseLabeledEdge e = null;
+ try
{
- v.add( baseLabeledVertex );
+ g = new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+ buildCompleteGraph( 10, g );
+
+ e = new BaseLabeledEdge( "NOT EXIST" );
+ }
+ catch ( GraphException ex )
+ {
+ fail( ex.getMessage() );
}
- assertEquals( 9, v.size() );
- assertFalse( v.contains( testVertex ) );
+ g.removeEdge( e );
+
+ }
+
+ /**
+ * Test Graph model in a multi-thread enviroment.
+ */
+ @Test
+ public final void testDirectedMultiTh()
+ throws Throwable
+ {
+ final MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
+ (MutableGraph<BaseLabeledVertex, BaseLabeledEdge>) CommonsGraph.synchronize( (MutableGraph<BaseLabeledVertex, BaseLabeledEdge>) new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>() );
+
+ TestRunner tr1, tr2, tr3;
+ tr1 = new GraphInsert( g, 0, 10 );
+ tr2 = new GraphInsert( g, 10, 20 );
+ tr3 = new GraphInsert( g, 20, 30 );
+
+ TestRunner[] trs = { tr1, tr2, tr3 };
+ MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( trs );
+ mttr.runRunnables();
+
+ assertEquals( 30, g.getOrder() );
+
+ // test the # of edges = n (n-1)
+ assertEquals( ( 30 * ( 30 - 1 ) ), g.getSize() );
}
/**
@@ -128,18 +227,12 @@ public class BaseMutableGraphTestCase
* {@link org.apache.commons.graph.model.BaseGraph#getConnectedVertices(org.apache.commons.graph.Vertex)}
*/
@Test
- public final void testGetConnectedVerticesOnNotConnectedGraph()
+ public final void testGetConnectedVertices()
{
// Test a complete undirect graph.
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
-
- // building a not connected Graph
- for ( int i = 0; i < 4; i++ )
- {
- BaseLabeledVertex v = new BaseLabeledVertex( valueOf( i ) );
- g.addVertex( v );
- }
+ buildCompleteGraph( 10, g );
final BaseLabeledVertex testVertex = new BaseLabeledVertex( valueOf( 1 ) );
Iterable<BaseLabeledVertex> connectedVertices = g.getConnectedVertices( testVertex );
@@ -151,7 +244,8 @@ public class BaseMutableGraphTestCase
v.add( baseLabeledVertex );
}
- assertEquals( 0, v.size() );
+ assertEquals( 9, v.size() );
+ assertFalse( v.contains( testVertex ) );
}
/**
@@ -180,20 +274,33 @@ public class BaseMutableGraphTestCase
/**
* Test method for
- * {@link org.apache.commons.graph.model.BaseGraph#getEdge(org.apache.commons.graph.Vertex, org.apache.commons.graph.Vertex)}
+ * {@link org.apache.commons.graph.model.BaseGraph#getConnectedVertices(org.apache.commons.graph.Vertex)}
*/
@Test
- public final void testGetEdge()
+ public final void testGetConnectedVerticesOnNotConnectedGraph()
{
// Test a complete undirect graph.
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- buildCompleteGraph( 10, g );
- final BaseLabeledVertex source = new BaseLabeledVertex( valueOf( 1 ) );
- final BaseLabeledVertex target = new BaseLabeledVertex( valueOf( 2 ) );
- BaseLabeledEdge edge = g.getEdge( source, target );
- assertNotNull( edge );
+ // building a not connected Graph
+ for ( int i = 0; i < 4; i++ )
+ {
+ BaseLabeledVertex v = new BaseLabeledVertex( valueOf( i ) );
+ g.addVertex( v );
+ }
+
+ final BaseLabeledVertex testVertex = new BaseLabeledVertex( valueOf( 1 ) );
+ Iterable<BaseLabeledVertex> connectedVertices = g.getConnectedVertices( testVertex );
+ assertNotNull( connectedVertices );
+
+ final List<BaseLabeledVertex> v = new ArrayList<BaseLabeledVertex>();
+ for ( BaseLabeledVertex baseLabeledVertex : connectedVertices )
+ {
+ v.add( baseLabeledVertex );
+ }
+
+ assertEquals( 0, v.size() );
}
/**
@@ -201,22 +308,17 @@ public class BaseMutableGraphTestCase
* {@link org.apache.commons.graph.model.BaseGraph#getEdge(org.apache.commons.graph.Vertex, org.apache.commons.graph.Vertex)}
*/
@Test
- public final void testGetNotExistsEdge()
+ public final void testGetEdge()
{
// Test a complete undirect graph.
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- // building Graph
- for ( int i = 0; i < 4; i++ )
- {
- BaseLabeledVertex v = new BaseLabeledVertex( valueOf( i ) );
- g.addVertex( v );
- }
+ buildCompleteGraph( 10, g );
final BaseLabeledVertex source = new BaseLabeledVertex( valueOf( 1 ) );
final BaseLabeledVertex target = new BaseLabeledVertex( valueOf( 2 ) );
BaseLabeledEdge edge = g.getEdge( source, target );
- assertNull( edge );
+ assertNotNull( edge );
}
/**
@@ -276,24 +378,51 @@ public class BaseMutableGraphTestCase
/**
* Test method for
- * {@link org.apache.commons.graph.model.BaseMutableGraph#removeEdge(org.apache.commons.graph.Edge)}
+ * {@link org.apache.commons.graph.model.BaseGraph#getEdge(org.apache.commons.graph.Vertex, org.apache.commons.graph.Vertex)}
*/
@Test
- public final void testDirectedGraphRemoveEdge()
+ public final void testGetNotExistsEdge()
{
// Test a complete undirect graph.
- final DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
+ // building Graph
+ for ( int i = 0; i < 4; i++ )
+ {
+ BaseLabeledVertex v = new BaseLabeledVertex( valueOf( i ) );
+ g.addVertex( v );
+ }
final BaseLabeledVertex source = new BaseLabeledVertex( valueOf( 1 ) );
final BaseLabeledVertex target = new BaseLabeledVertex( valueOf( 2 ) );
+ BaseLabeledEdge edge = g.getEdge( source, target );
+ assertNull( edge );
+ }
- buildCompleteGraph( 10, g );
+ /**
+ * Test Graph model ina multi-thread enviroment.
+ */
+ @Test
+ public final void testMultiThreadUndirectGraph()
+ throws Throwable
+ {
+ final MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
+ (MutableGraph<BaseLabeledVertex, BaseLabeledEdge>) CommonsGraph.synchronize( (MutableGraph<BaseLabeledVertex, BaseLabeledEdge>) new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>() );
- BaseLabeledEdge e = g.getEdge( source, target );
- g.removeEdge( e );
+ TestRunner tr1, tr2, tr3;
+ tr1 = new GraphInsert( g, 0, 10 );
+ tr2 = new GraphInsert( g, 10, 20 );
+ tr3 = new GraphInsert( g, 20, 30 );
- BaseLabeledEdge edge = g.getEdge( source, target );
- assertNull( edge );
+ TestRunner[] trs = { tr1, tr2, tr3 };
+ MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( trs );
+
+ mttr.runRunnables();
+
+ assertEquals( 30, g.getOrder() );
+
+ // test the # of edges = n (n-1)/2
+ assertEquals( ( 30 * ( 30 - 1 ) / 2 ), g.getSize() );
}
/**
@@ -343,133 +472,4 @@ public class BaseMutableGraphTestCase
g.removeEdge( e );
}
-
- /**
- * Test method for {@link org.apache.commons.graph.model.BaseMutableGraph#removeEdge(org.apache.commons.graph.Edge)}
- */
- @Test( expected = GraphException.class )
- public final void testDirectedGraphRemoveEdgeNotExists()
- {
- // Test a complete undirect graph.
- DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = null;
- BaseLabeledEdge e = null;
- try
- {
- g = new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
- buildCompleteGraph( 10, g );
-
- e = new BaseLabeledEdge( "NOT EXIST" );
- }
- catch ( GraphException ex )
- {
- fail( ex.getMessage() );
- }
-
- g.removeEdge( e );
-
- }
-
- /**
- * Test Graph model ina multi-thread enviroment.
- */
- @Test
- public final void testMultiThreadUndirectGraph()
- throws Throwable
- {
- final MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
- (MutableGraph<BaseLabeledVertex, BaseLabeledEdge>) CommonsGraph.synchronize( (MutableGraph<BaseLabeledVertex, BaseLabeledEdge>) new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>() );
-
- TestRunner tr1, tr2, tr3;
- tr1 = new GraphInsert( g, 0, 10 );
- tr2 = new GraphInsert( g, 10, 20 );
- tr3 = new GraphInsert( g, 20, 30 );
-
- TestRunner[] trs = { tr1, tr2, tr3 };
- MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( trs );
-
- mttr.runRunnables();
-
- assertEquals( 30, g.getOrder() );
-
- // test the # of edges = n (n-1)/2
- assertEquals( ( 30 * ( 30 - 1 ) / 2 ), g.getSize() );
- }
-
- /**
- * Test Graph model in a multi-thread enviroment.
- */
- @Test
- public final void testDirectedMultiTh()
- throws Throwable
- {
- final MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g =
- (MutableGraph<BaseLabeledVertex, BaseLabeledEdge>) CommonsGraph.synchronize( (MutableGraph<BaseLabeledVertex, BaseLabeledEdge>) new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>() );
-
- TestRunner tr1, tr2, tr3;
- tr1 = new GraphInsert( g, 0, 10 );
- tr2 = new GraphInsert( g, 10, 20 );
- tr3 = new GraphInsert( g, 20, 30 );
-
- TestRunner[] trs = { tr1, tr2, tr3 };
- MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( trs );
- mttr.runRunnables();
-
- assertEquals( 30, g.getOrder() );
-
- // test the # of edges = n (n-1)
- assertEquals( ( 30 * ( 30 - 1 ) ), g.getSize() );
- }
-
- // Utility class.
- private class GraphInsert
- extends TestRunner
- {
-
- private MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g;
-
- private int start;
-
- private int end;
-
- private GraphInsert( MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g, int start, int end )
- {
- this.g = g;
- this.start = start;
- this.end = end;
- }
-
- @Override
- public void runTest()
- {
-
- // building a complete Graph
- for ( int i = start; i < end; i++ )
- {
- BaseLabeledVertex v = new BaseLabeledVertex( valueOf( i ) );
- g.addVertex( v );
- }
- synchronized ( g )
- {
- for ( BaseLabeledVertex v1 : g.getVertices() )
- {
- for ( BaseLabeledVertex v2 : g.getVertices() )
- {
- if ( !v1.equals( v2 ) )
- {
- try
- {
- BaseLabeledEdge e = new BaseLabeledEdge( v1 + " -> " + v2 );
- g.addEdge( v1, e, v2 );
- }
- catch ( GraphException e )
- {
- // ignore
- }
- }
- }
- }
-
- }
- }
- }
}
diff --git a/src/test/java/org/apache/commons/graph/model/GraphSerializationTestCase.java b/src/test/java/org/apache/commons/graph/model/GraphSerializationTestCase.java
index aaa0e04..02d0d3e 100644
--- a/src/test/java/org/apache/commons/graph/model/GraphSerializationTestCase.java
+++ b/src/test/java/org/apache/commons/graph/model/GraphSerializationTestCase.java
@@ -50,74 +50,95 @@ public class GraphSerializationTestCase
private final static String FILE_NAME = "target/serialiazedGraph.dat";
- @After
- public void cleanUp()
+ private static GraphConnection<BaseLabeledVertex, BaseLabeledEdge> buildGraphConnections()
{
- File f = new File( FILE_NAME );
- if ( f.exists() )
+ return new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
{
- f.delete();
- }
- }
- @Test
- public void testSerializeUndirectedGraph()
- throws Exception
- {
- MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = newUndirectedMutableGraph( buildGraphConnections() );
+ @Override
+ public void connect()
+ {
+ BaseLabeledVertex a = addVertex( new BaseLabeledVertex( "a" ) );
+ BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "b" ) );
+ BaseLabeledVertex c = addVertex( new BaseLabeledVertex( "c" ) );
+ BaseLabeledVertex d = addVertex( new BaseLabeledVertex( "d" ) );
- checkSerialization( g );
+ addEdge( new BaseLabeledEdge( "a -> c" ) ).from( a ).to( c );
+ addEdge( new BaseLabeledEdge( "c -> d" ) ).from( c ).to( d );
+ addEdge( new BaseLabeledEdge( "d -> b" ) ).from( d ).to( b );
+ }
+
+ };
}
- @Test
- public void testSerializeDirectedGraph()
- throws Exception
+ private static GraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> buildWeightedGraphConnections()
{
- MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = newDirectedMutableGraph( buildGraphConnections() );
+ return new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>()
+ {
- checkSerialization( g );
+ @Override
+ public void connect()
+ {
+ BaseLabeledVertex a = addVertex( new BaseLabeledVertex( "a" ) );
+ BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "b" ) );
+ BaseLabeledVertex c = addVertex( new BaseLabeledVertex( "c" ) );
+ BaseLabeledVertex d = addVertex( new BaseLabeledVertex( "d" ) );
+
+ addEdge( new BaseLabeledWeightedEdge<Double>( "a -> c", 1D ) ).from( a ).to( c );
+ addEdge( new BaseLabeledWeightedEdge<Double>( "c -> d", 1D ) ).from( c ).to( d );
+ addEdge( new BaseLabeledWeightedEdge<Double>( "d -> b", 1D ) ).from( d ).to( b );
+ }
+
+ };
}
- @Test
- public void testSerializeUndirectedWeightdGraph()
- throws Exception
+ /**
+ * @param g
+ * @throws FileNotFoundException
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ private static void checkSerialization( Graph<BaseLabeledVertex, ? extends Serializable> g )
+ throws FileNotFoundException, IOException, ClassNotFoundException
{
- MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
- newUndirectedMutableGraph( buildWeightedGraphConnections() );
+ FileOutputStream fout = new FileOutputStream( FILE_NAME );
+ ObjectOutputStream oos = new ObjectOutputStream( fout );
+ oos.writeObject( g );
+ oos.close();
- checkSerialization( g );
+ FileInputStream fin = new FileInputStream( FILE_NAME );
+ ObjectInputStream ois = new ObjectInputStream( fin );
+ Object cloned = ois.readObject();
+ ois.close();
+
+ assertEquals( g, cloned );
}
- @Test
- public void testSerializeDirectedWeightdGraph()
- throws Exception
+ @After
+ public void cleanUp()
{
- MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
- newDirectedMutableGraph( buildWeightedGraphConnections() );
-
- checkSerialization( g );
+ File f = new File( FILE_NAME );
+ if ( f.exists() )
+ {
+ f.delete();
+ }
}
@Test
- public void testSerializeSpanningTree()
+ public void testSerializeDirectedGraph()
throws Exception
{
- final MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> spanningTree =
- new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>(
- new DoubleWeightBaseOperations(),
- new BaseWeightedEdge<Double>() );
-
- populate( spanningTree ).withConnections( buildWeightedGraphConnections() );
+ MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = newDirectedMutableGraph( buildGraphConnections() );
- checkSerialization( spanningTree );
+ checkSerialization( g );
}
@Test
- public void testSerializeSyncronyzedDirectedWeightdGraph()
+ public void testSerializeDirectedWeightdGraph()
throws Exception
{
- Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
- synchronize( (MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) newDirectedMutableGraph( buildWeightedGraphConnections() ) );
+ MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
+ newDirectedMutableGraph( buildWeightedGraphConnections() );
checkSerialization( g );
}
@@ -147,68 +168,47 @@ public class GraphSerializationTestCase
checkSerialization( g );
}
- /**
- * @param g
- * @throws FileNotFoundException
- * @throws IOException
- * @throws ClassNotFoundException
- */
- private static void checkSerialization( Graph<BaseLabeledVertex, ? extends Serializable> g )
- throws FileNotFoundException, IOException, ClassNotFoundException
+ @Test
+ public void testSerializeSpanningTree()
+ throws Exception
{
- FileOutputStream fout = new FileOutputStream( FILE_NAME );
- ObjectOutputStream oos = new ObjectOutputStream( fout );
- oos.writeObject( g );
- oos.close();
+ final MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> spanningTree =
+ new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>(
+ new DoubleWeightBaseOperations(),
+ new BaseWeightedEdge<Double>() );
- FileInputStream fin = new FileInputStream( FILE_NAME );
- ObjectInputStream ois = new ObjectInputStream( fin );
- Object cloned = ois.readObject();
- ois.close();
+ populate( spanningTree ).withConnections( buildWeightedGraphConnections() );
- assertEquals( g, cloned );
+ checkSerialization( spanningTree );
}
- private static GraphConnection<BaseLabeledVertex, BaseLabeledEdge> buildGraphConnections()
+ @Test
+ public void testSerializeSyncronyzedDirectedWeightdGraph()
+ throws Exception
{
- return new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
- {
-
- @Override
- public void connect()
- {
- BaseLabeledVertex a = addVertex( new BaseLabeledVertex( "a" ) );
- BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "b" ) );
- BaseLabeledVertex c = addVertex( new BaseLabeledVertex( "c" ) );
- BaseLabeledVertex d = addVertex( new BaseLabeledVertex( "d" ) );
-
- addEdge( new BaseLabeledEdge( "a -> c" ) ).from( a ).to( c );
- addEdge( new BaseLabeledEdge( "c -> d" ) ).from( c ).to( d );
- addEdge( new BaseLabeledEdge( "d -> b" ) ).from( d ).to( b );
- }
+ Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
+ synchronize( (MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) newDirectedMutableGraph( buildWeightedGraphConnections() ) );
- };
+ checkSerialization( g );
}
- private static GraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> buildWeightedGraphConnections()
+ @Test
+ public void testSerializeUndirectedGraph()
+ throws Exception
{
- return new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>()
- {
+ MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = newUndirectedMutableGraph( buildGraphConnections() );
- @Override
- public void connect()
- {
- BaseLabeledVertex a = addVertex( new BaseLabeledVertex( "a" ) );
- BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "b" ) );
- BaseLabeledVertex c = addVertex( new BaseLabeledVertex( "c" ) );
- BaseLabeledVertex d = addVertex( new BaseLabeledVertex( "d" ) );
+ checkSerialization( g );
+ }
- addEdge( new BaseLabeledWeightedEdge<Double>( "a -> c", 1D ) ).from( a ).to( c );
- addEdge( new BaseLabeledWeightedEdge<Double>( "c -> d", 1D ) ).from( c ).to( d );
- addEdge( new BaseLabeledWeightedEdge<Double>( "d -> b", 1D ) ).from( d ).to( b );
- }
+ @Test
+ public void testSerializeUndirectedWeightdGraph()
+ throws Exception
+ {
+ MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
+ newUndirectedMutableGraph( buildWeightedGraphConnections() );
- };
+ checkSerialization( g );
}
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowTestCase.java b/src/test/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowTestCase.java
index c20afc2..d0d0f28 100644
--- a/src/test/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowTestCase.java
+++ b/src/test/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowTestCase.java
@@ -42,13 +42,6 @@ import org.junit.Test;
public final class CheriyanMehlhornGabowTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph = null;
- findStronglyConnectedComponent( graph ).applyingCheriyanMehlhornGabow();
- }
-
@Test
public void testEmptyGraph()
{
@@ -58,6 +51,13 @@ public final class CheriyanMehlhornGabowTestCase
findStronglyConnectedComponent( graph ).applyingCheriyanMehlhornGabow();
}
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph = null;
+ findStronglyConnectedComponent( graph ).applyingCheriyanMehlhornGabow();
+ }
+
@Test
public void testSparse()
{
diff --git a/src/test/java/org/apache/commons/graph/scc/KosarajuSharirTestCase.java b/src/test/java/org/apache/commons/graph/scc/KosarajuSharirTestCase.java
index b06049b..c653ce9 100644
--- a/src/test/java/org/apache/commons/graph/scc/KosarajuSharirTestCase.java
+++ b/src/test/java/org/apache/commons/graph/scc/KosarajuSharirTestCase.java
@@ -42,6 +42,15 @@ import org.junit.Test;
public final class KosarajuSharirTestCase
{
+ @Test( expected = IllegalStateException.class )
+ public void testNotExistVertex()
+ {
+ DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph =
+ new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>();
+
+ findStronglyConnectedComponent( graph ).applyingKosarajuSharir( new BaseLabeledVertex( "NOT EXISTS" ) );
+ }
+
@Test( expected = NullPointerException.class )
public void testNullGraph()
{
@@ -60,17 +69,8 @@ public final class KosarajuSharirTestCase
findStronglyConnectedComponent( graph ).applyingKosarajuSharir( a );
}
- @Test( expected = IllegalStateException.class )
- public void testNotExistVertex()
- {
- DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph =
- new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>>();
-
- findStronglyConnectedComponent( graph ).applyingKosarajuSharir( new BaseLabeledVertex( "NOT EXISTS" ) );
- }
-
@Test
- public void testVerifyHasStronglyConnectedComponents()
+ public void testUnconnectedGraph()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
final BaseLabeledVertex b = new BaseLabeledVertex( "B" );
@@ -96,12 +96,12 @@ public final class KosarajuSharirTestCase
addVertex( g );
addVertex( h );
- addEdge( new BaseLabeledEdge( "A -> F" ) ).from( a ).to( f );
+ //addEdge( new BaseLabeledEdge( "A -> F" ) ).from( a ).to( f );
addEdge( new BaseLabeledEdge( "A -> B" ) ).from( a ).to( b );
addEdge( new BaseLabeledEdge( "B -> D" ) ).from( b ).to( d );
addEdge( new BaseLabeledEdge( "C -> G" ) ).from( c ).to( g );
addEdge( new BaseLabeledEdge( "D -> A" ) ).from( d ).to( a );
- addEdge( new BaseLabeledEdge( "D -> G" ) ).from( d ).to( g );
+ //addEdge( new BaseLabeledEdge( "D -> G" ) ).from( d ).to( g );
addEdge( new BaseLabeledEdge( "E -> C" ) ).from( e ).to( c );
addEdge( new BaseLabeledEdge( "E -> F" ) ).from( e ).to( f );
addEdge( new BaseLabeledEdge( "F -> E" ) ).from( f ).to( e );
@@ -144,7 +144,7 @@ public final class KosarajuSharirTestCase
}
@Test
- public void testUnconnectedGraph()
+ public void testVerifyHasStronglyConnectedComponents()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
final BaseLabeledVertex b = new BaseLabeledVertex( "B" );
@@ -170,12 +170,12 @@ public final class KosarajuSharirTestCase
addVertex( g );
addVertex( h );
- //addEdge( new BaseLabeledEdge( "A -> F" ) ).from( a ).to( f );
+ addEdge( new BaseLabeledEdge( "A -> F" ) ).from( a ).to( f );
addEdge( new BaseLabeledEdge( "A -> B" ) ).from( a ).to( b );
addEdge( new BaseLabeledEdge( "B -> D" ) ).from( b ).to( d );
addEdge( new BaseLabeledEdge( "C -> G" ) ).from( c ).to( g );
addEdge( new BaseLabeledEdge( "D -> A" ) ).from( d ).to( a );
- //addEdge( new BaseLabeledEdge( "D -> G" ) ).from( d ).to( g );
+ addEdge( new BaseLabeledEdge( "D -> G" ) ).from( d ).to( g );
addEdge( new BaseLabeledEdge( "E -> C" ) ).from( e ).to( c );
addEdge( new BaseLabeledEdge( "E -> F" ) ).from( e ).to( f );
addEdge( new BaseLabeledEdge( "F -> E" ) ).from( f ).to( e );
diff --git a/src/test/java/org/apache/commons/graph/scc/TarjanTestCase.java b/src/test/java/org/apache/commons/graph/scc/TarjanTestCase.java
index 7f1de35..37bb8fb 100644
--- a/src/test/java/org/apache/commons/graph/scc/TarjanTestCase.java
+++ b/src/test/java/org/apache/commons/graph/scc/TarjanTestCase.java
@@ -43,13 +43,6 @@ import org.junit.Test;
public final class TarjanTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph = null;
- findStronglyConnectedComponent( graph ).applyingTarjan();
- }
-
@Test
public void testEmptyGraph()
{
@@ -59,6 +52,13 @@ public final class TarjanTestCase
findStronglyConnectedComponent( graph ).applyingTarjan();
}
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Integer>> graph = null;
+ findStronglyConnectedComponent( graph ).applyingTarjan();
+ }
+
@Test
public void testSparse()
{
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/AStarTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/AStarTestCase.java
index bd1e143..b9a0267 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/AStarTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/AStarTestCase.java
@@ -39,115 +39,6 @@ import org.junit.Test;
public final class AStarTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .to( null )
- .applyingAStar( new DoubleWeightBaseOperations() )
- .withHeuristic( null );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullVertices()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .to( null )
- .applyingAStar( new DoubleWeightBaseOperations() )
- .withHeuristic( null );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullHeuristic()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( new BaseLabeledVertex( "a" ) )
- .to( new BaseLabeledVertex( "b" ) )
- .applyingAStar( new DoubleWeightBaseOperations() )
- .withHeuristic( null );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullMonoid()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
- final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
- final Map<BaseLabeledVertex, Double> heuristics = new HashMap<BaseLabeledVertex, Double>();
- Heuristic<BaseLabeledVertex, Double> heuristic = null;
-
- try
- {
- graph.addVertex( a );
- graph.addVertex( b );
-
- heuristic = new Heuristic<BaseLabeledVertex, Double>()
- {
-
- public Double applyHeuristic( BaseLabeledVertex current, BaseLabeledVertex goal )
- {
- return heuristics.get( current );
- }
-
- };
- }
- catch ( NullPointerException e )
- {
- fail( e.getMessage() );
- }
-
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( a )
- .to( b )
- .applyingAStar( null )
- .withHeuristic( heuristic );
- }
-
- @Test( expected = PathNotFoundException.class )
- public void testNotConnectGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
- final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
- graph.addVertex( a );
- graph.addVertex( b );
-
- final Map<BaseLabeledVertex, Double> heuristics = new HashMap<BaseLabeledVertex, Double>();
-
- Heuristic<BaseLabeledVertex, Double> heuristic = new Heuristic<BaseLabeledVertex, Double>()
- {
-
- public Double applyHeuristic( BaseLabeledVertex current, BaseLabeledVertex goal )
- {
- return heuristics.get( current );
- }
-
- };
-
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( a )
- .to( b )
- .applyingAStar( new DoubleWeightBaseOperations() )
- .withHeuristic( heuristic );
- }
-
/**
* Test Graph and Dijkstra's solution can be seen on
* <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">Wikipedia</a>
@@ -230,4 +121,113 @@ public final class AStarTestCase
assertEquals( expected, actual );
}
+
+ @Test( expected = PathNotFoundException.class )
+ public void testNotConnectGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
+ final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
+ graph.addVertex( a );
+ graph.addVertex( b );
+
+ final Map<BaseLabeledVertex, Double> heuristics = new HashMap<BaseLabeledVertex, Double>();
+
+ Heuristic<BaseLabeledVertex, Double> heuristic = new Heuristic<BaseLabeledVertex, Double>()
+ {
+
+ public Double applyHeuristic( BaseLabeledVertex current, BaseLabeledVertex goal )
+ {
+ return heuristics.get( current );
+ }
+
+ };
+
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( a )
+ .to( b )
+ .applyingAStar( new DoubleWeightBaseOperations() )
+ .withHeuristic( heuristic );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .to( null )
+ .applyingAStar( new DoubleWeightBaseOperations() )
+ .withHeuristic( null );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullHeuristic()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( new BaseLabeledVertex( "a" ) )
+ .to( new BaseLabeledVertex( "b" ) )
+ .applyingAStar( new DoubleWeightBaseOperations() )
+ .withHeuristic( null );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullMonoid()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
+ final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
+ final Map<BaseLabeledVertex, Double> heuristics = new HashMap<BaseLabeledVertex, Double>();
+ Heuristic<BaseLabeledVertex, Double> heuristic = null;
+
+ try
+ {
+ graph.addVertex( a );
+ graph.addVertex( b );
+
+ heuristic = new Heuristic<BaseLabeledVertex, Double>()
+ {
+
+ public Double applyHeuristic( BaseLabeledVertex current, BaseLabeledVertex goal )
+ {
+ return heuristics.get( current );
+ }
+
+ };
+ }
+ catch ( NullPointerException e )
+ {
+ fail( e.getMessage() );
+ }
+
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( a )
+ .to( b )
+ .applyingAStar( null )
+ .withHeuristic( heuristic );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullVertices()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .to( null )
+ .applyingAStar( new DoubleWeightBaseOperations() )
+ .withHeuristic( null );
+ }
}
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
index bcdd2cb..f6d0864 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
@@ -37,84 +37,6 @@ import org.junit.Test;
public final class BellmannFordTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- // the actual weighted path
- findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .applyingBelmannFord( new DoubleWeightBaseOperations() );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullVertices()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .applyingBelmannFord( new DoubleWeightBaseOperations() );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullMonoid()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph = null;
- BaseLabeledVertex a = null;
- try
- {
- graph = new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- a = new BaseLabeledVertex( "a" );
- final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
- graph.addVertex( a );
- graph.addVertex( b );
- }
- catch ( NullPointerException e )
- {
- fail( e.getMessage() );
- }
-
- // the actual weighted path
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( a )
- .applyingBelmannFord( null );
- }
-
- @Test( expected = PathNotFoundException.class )
- public void testNotConnectGraph()
- {
- BaseLabeledVertex a = null;
- BaseLabeledVertex b = null;
- // the actual weighted path
- AllVertexPairsShortestPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> allVertexPairsShortestPath =
- null;
- try
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- a = new BaseLabeledVertex( "a" );
- b = new BaseLabeledVertex( "b" );
- graph.addVertex( a );
- graph.addVertex( b );
-
- allVertexPairsShortestPath = findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( a )
- .applyingBelmannFord( new DoubleWeightBaseOperations() );
- }
- catch ( PathNotFoundException e )
- {
- fail( e.getMessage() );
- }
- allVertexPairsShortestPath.findShortestPath( a, b );
- }
-
/**
* Test Graph and Dijkstra's solution can be seen on
* <a href="http://compprog.wordpress.com/2007/11/29/one-source-shortest-path-the-bellman-ford-algorithm/">Wikipedia</a>
@@ -171,4 +93,82 @@ public final class BellmannFordTestCase
assertEquals( expected, actual );
}
+ @Test( expected = PathNotFoundException.class )
+ public void testNotConnectGraph()
+ {
+ BaseLabeledVertex a = null;
+ BaseLabeledVertex b = null;
+ // the actual weighted path
+ AllVertexPairsShortestPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> allVertexPairsShortestPath =
+ null;
+ try
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ a = new BaseLabeledVertex( "a" );
+ b = new BaseLabeledVertex( "b" );
+ graph.addVertex( a );
+ graph.addVertex( b );
+
+ allVertexPairsShortestPath = findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( a )
+ .applyingBelmannFord( new DoubleWeightBaseOperations() );
+ }
+ catch ( PathNotFoundException e )
+ {
+ fail( e.getMessage() );
+ }
+ allVertexPairsShortestPath.findShortestPath( a, b );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ // the actual weighted path
+ findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .applyingBelmannFord( new DoubleWeightBaseOperations() );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullMonoid()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph = null;
+ BaseLabeledVertex a = null;
+ try
+ {
+ graph = new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ a = new BaseLabeledVertex( "a" );
+ final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
+ graph.addVertex( a );
+ graph.addVertex( b );
+ }
+ catch ( NullPointerException e )
+ {
+ fail( e.getMessage() );
+ }
+
+ // the actual weighted path
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( a )
+ .applyingBelmannFord( null );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullVertices()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .applyingBelmannFord( new DoubleWeightBaseOperations() );
+ }
+
}
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/BidirDijkstraTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/BidirDijkstraTestCase.java
index 107e491..258a481 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/BidirDijkstraTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/BidirDijkstraTestCase.java
@@ -70,6 +70,18 @@ public final class BidirDijkstraTestCase
{
Random r = new Random();
+ private boolean addEdge( BaseLabeledVertex src, BaseLabeledVertex dst )
+ {
+ try {
+ addEdge( new BaseLabeledWeightedEdge<Double>( format( "%s -> %s", src, dst ),
+ 10.0 * r.nextDouble() + 1.0 ) ).from( src ).to( dst );
+ return true;
+ } catch (GraphException e) {
+ // ignore duplicate edge exceptions
+ return false;
+ }
+ }
+
public void connect()
{
vertices = new ArrayList<BaseLabeledVertex>();
@@ -97,82 +109,43 @@ public final class BidirDijkstraTestCase
}
}
}
-
- private boolean addEdge( BaseLabeledVertex src, BaseLabeledVertex dst )
- {
- try {
- addEdge( new BaseLabeledWeightedEdge<Double>( format( "%s -> %s", src, dst ),
- 10.0 * r.nextDouble() + 1.0 ) ).from( src ).to( dst );
- return true;
- } catch (GraphException e) {
- // ignore duplicate edge exceptions
- return false;
- }
- }
} );
}
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- // the actual weighted path
- findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .to( null )
- .applyingBidirectionalDijkstra( new DoubleWeightBaseOperations() );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullVertices()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- // the actual weighted path
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .to( null )
- .applyingBidirectionalDijkstra( new DoubleWeightBaseOperations() );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullMonoid()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+ @Test
+ public void testCompareToUnidirectional() {
+ // It is hard to get unidirectional Dijkstra's algorithm wrong;
+ // therefore compare a sequence of outputs.
+ Random r = new Random();
- final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
- final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
- graph.addVertex( a );
- graph.addVertex( b );
+ for ( int ii = 0; ii < TIMES; ii++ )
+ {
+ BaseLabeledVertex s = vertices.get( r.nextInt( vertices.size() ) );
+ BaseLabeledVertex t;
- // the actual weighted path
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( a )
- .to( b )
- .applyingBidirectionalDijkstra( null );
- }
+ do
+ {
+ t = vertices.get( r.nextInt( vertices.size() ) );
+ }
+ while ( s.equals( t ) );
- @Test( expected = PathNotFoundException.class )
- public void testNotConnectGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+ WeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> pathUni =
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( s )
+ .to( t )
+ .applyingDijkstra( weightOperations );
- final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
- final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
- graph.addVertex( a );
- graph.addVertex( b );
+ WeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> pathBi =
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( s )
+ .to( t )
+ .applyingBidirectionalDijkstra( weightOperations );
- // the actual weighted path
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( a )
- .to( b )
- .applyingBidirectionalDijkstra( new DoubleWeightBaseOperations() );
+ assertEquals( pathUni.getSize(), pathBi.getSize() );
+ assertEquals( pathUni.getWeight(), pathBi.getWeight(), EPSILON );
+ }
}
/**
@@ -237,37 +210,67 @@ public final class BidirDijkstraTestCase
assertEquals( expected, actual );
}
- @Test
- public void testVerifyTwoNodePath() {
- DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+ @Test( expected = PathNotFoundException.class )
+ public void testNotConnectGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
- // building Graph
+ final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
+ final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
+ graph.addVertex( a );
+ graph.addVertex( b );
- BaseLabeledVertex one = new BaseLabeledVertex( "1" );
- BaseLabeledVertex two = new BaseLabeledVertex( "2" );
+ // the actual weighted path
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( a )
+ .to( b )
+ .applyingBidirectionalDijkstra( new DoubleWeightBaseOperations() );
+ }
- graph.addVertex( one );
- graph.addVertex( two );
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ // the actual weighted path
+ findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .to( null )
+ .applyingBidirectionalDijkstra( new DoubleWeightBaseOperations() );
+ }
- graph.addEdge( one, new BaseLabeledWeightedEdge<Double>( "1 -> 2", 14D ), two );
+ @Test( expected = NullPointerException.class )
+ public void testNullMonoid()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
- // expected path
- InMemoryWeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
- new InMemoryWeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( one, two, new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );
+ final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
+ final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
+ graph.addVertex( a );
+ graph.addVertex( b );
- expected.addConnectionInTail( one, new BaseLabeledWeightedEdge<Double>( "1 -> 2", 14D ), two );
+ // the actual weighted path
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( a )
+ .to( b )
+ .applyingBidirectionalDijkstra( null );
+ }
- // actual path
- Path<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> actual =
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( one )
- .to( two )
- .applyingBidirectionalDijkstra( new DoubleWeightBaseOperations() );
+ @Test( expected = NullPointerException.class )
+ public void testNullVertices()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
- // assert!
- assertEquals( expected, actual );
+ // the actual weighted path
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .to( null )
+ .applyingBidirectionalDijkstra( new DoubleWeightBaseOperations() );
}
@Test
@@ -308,38 +311,35 @@ public final class BidirDijkstraTestCase
}
@Test
- public void testCompareToUnidirectional() {
- // It is hard to get unidirectional Dijkstra's algorithm wrong;
- // therefore compare a sequence of outputs.
- Random r = new Random();
+ public void testVerifyTwoNodePath() {
+ DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
- for ( int ii = 0; ii < TIMES; ii++ )
- {
- BaseLabeledVertex s = vertices.get( r.nextInt( vertices.size() ) );
- BaseLabeledVertex t;
+ // building Graph
- do
- {
- t = vertices.get( r.nextInt( vertices.size() ) );
- }
- while ( s.equals( t ) );
+ BaseLabeledVertex one = new BaseLabeledVertex( "1" );
+ BaseLabeledVertex two = new BaseLabeledVertex( "2" );
- WeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> pathUni =
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( s )
- .to( t )
- .applyingDijkstra( weightOperations );
+ graph.addVertex( one );
+ graph.addVertex( two );
- WeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> pathBi =
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( s )
- .to( t )
- .applyingBidirectionalDijkstra( weightOperations );
+ graph.addEdge( one, new BaseLabeledWeightedEdge<Double>( "1 -> 2", 14D ), two );
- assertEquals( pathUni.getSize(), pathBi.getSize() );
- assertEquals( pathUni.getWeight(), pathBi.getWeight(), EPSILON );
- }
+ // expected path
+ InMemoryWeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
+ new InMemoryWeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( one, two, new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );
+
+ expected.addConnectionInTail( one, new BaseLabeledWeightedEdge<Double>( "1 -> 2", 14D ), two );
+
+ // actual path
+ Path<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> actual =
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( one )
+ .to( two )
+ .applyingBidirectionalDijkstra( new DoubleWeightBaseOperations() );
+
+ // assert!
+ assertEquals( expected, actual );
}
}
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
index 8c3af71..0f1a8a7 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
@@ -36,69 +36,6 @@ import org.junit.Test;
public final class DijkstraTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- // the actual weighted path
- findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .to( null )
- .applyingDijkstra( new DoubleWeightBaseOperations() );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullVertices()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- // the actual weighted path
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .to( null )
- .applyingDijkstra( new DoubleWeightBaseOperations() );
- }
-
- @Test( expected = NullPointerException.class )
- public void testNullMonoid()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
- final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
- graph.addVertex( a );
- graph.addVertex( b );
-
- // the actual weighted path
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( a )
- .to( b )
- .applyingDijkstra( null );
- }
-
- @Test( expected = PathNotFoundException.class )
- public void testNotConnectGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
- final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
- graph.addVertex( a );
- graph.addVertex( b );
-
- // the actual weighted path
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( a )
- .to( b )
- .applyingDijkstra( new DoubleWeightBaseOperations() );
- }
-
/**
* Test Graph and Dijkstra's solution can be seen on
* <a href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm>Wikipedia</a>
@@ -161,4 +98,67 @@ public final class DijkstraTestCase
assertEquals( expected, actual );
}
+ @Test( expected = PathNotFoundException.class )
+ public void testNotConnectGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
+ final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
+ graph.addVertex( a );
+ graph.addVertex( b );
+
+ // the actual weighted path
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( a )
+ .to( b )
+ .applyingDijkstra( new DoubleWeightBaseOperations() );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ // the actual weighted path
+ findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .to( null )
+ .applyingDijkstra( new DoubleWeightBaseOperations() );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullMonoid()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
+ final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
+ graph.addVertex( a );
+ graph.addVertex( b );
+
+ // the actual weighted path
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( a )
+ .to( b )
+ .applyingDijkstra( null );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullVertices()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ // the actual weighted path
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .to( null )
+ .applyingDijkstra( new DoubleWeightBaseOperations() );
+ }
+
}
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/FloydWarshallTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/FloydWarshallTestCase.java
index 195bd4d..d7ec5b2 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/FloydWarshallTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/FloydWarshallTestCase.java
@@ -42,49 +42,6 @@ import org.junit.Test;
public class FloydWarshallTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
- {
- // the actual weighted path
- findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .from( null )
- .to( null )
- .applyingDijkstra( new DoubleWeightBaseOperations() );
- }
-
- @Test( expected = PathNotFoundException.class )
- public void testNotConnectGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
- final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
- graph.addVertex( a );
- graph.addVertex( b );
-
- // the actual weighted path
- AllVertexPairsShortestPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> p =
- findShortestPath( graph )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .applyingFloydWarshall( new DoubleWeightBaseOperations() );
-
- p.findShortestPath( a, b );
- }
-
- @Test
- public void testUndirectedShortestPath()
- {
- findShortestPathAndVerify( new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>() );
- }
-
- @Test
- public void testDirectedShortestPath()
- {
- findShortestPathAndVerify( new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>() );
- }
-
private void findShortestPathAndVerify( Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> weighted )
{
MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> mutable =
@@ -181,4 +138,47 @@ public class FloydWarshallTestCase
}
}
+ @Test
+ public void testDirectedShortestPath()
+ {
+ findShortestPathAndVerify( new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>() );
+ }
+
+ @Test( expected = PathNotFoundException.class )
+ public void testNotConnectGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ final BaseLabeledVertex a = new BaseLabeledVertex( "a" );
+ final BaseLabeledVertex b = new BaseLabeledVertex( "b" );
+ graph.addVertex( a );
+ graph.addVertex( b );
+
+ // the actual weighted path
+ AllVertexPairsShortestPath<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> p =
+ findShortestPath( graph )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .applyingFloydWarshall( new DoubleWeightBaseOperations() );
+
+ p.findShortestPath( a, b );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ // the actual weighted path
+ findShortestPath( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .from( null )
+ .to( null )
+ .applyingDijkstra( new DoubleWeightBaseOperations() );
+ }
+
+ @Test
+ public void testUndirectedShortestPath()
+ {
+ findShortestPathAndVerify( new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>() );
+ }
+
}
diff --git a/src/test/java/org/apache/commons/graph/spanning/BoruvkaTestCase.java b/src/test/java/org/apache/commons/graph/spanning/BoruvkaTestCase.java
index 7a1c5f1..f3317ef 100644
--- a/src/test/java/org/apache/commons/graph/spanning/BoruvkaTestCase.java
+++ b/src/test/java/org/apache/commons/graph/spanning/BoruvkaTestCase.java
@@ -36,23 +36,35 @@ import org.junit.Test;
public final class BoruvkaTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
+ @Test( expected = IllegalStateException.class )
+ public void testEmptyGraph()
{
- minimumSpanningTree( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
.fromArbitrarySource()
.applyingBoruvkaAlgorithm( new DoubleWeightBaseOperations() );
}
- @Test( expected = NullPointerException.class )
- public void testNullVertex()
+ @Test( expected = IllegalStateException.class )
+ public void testNotExistVertex()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromSource( null )
+ .fromSource( new BaseLabeledVertex( "NOT EXIST" ) );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ minimumSpanningTree( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .fromArbitrarySource()
.applyingBoruvkaAlgorithm( new DoubleWeightBaseOperations() );
}
@@ -79,26 +91,14 @@ public final class BoruvkaTestCase
.applyingBoruvkaAlgorithm( null );
}
- @Test( expected = IllegalStateException.class )
- public void testNotExistVertex()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- minimumSpanningTree( input )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromSource( new BaseLabeledVertex( "NOT EXIST" ) );
- }
-
- @Test( expected = IllegalStateException.class )
- public void testEmptyGraph()
+ @Test( expected = NullPointerException.class )
+ public void testNullVertex()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromArbitrarySource()
+ .fromSource( null )
.applyingBoruvkaAlgorithm( new DoubleWeightBaseOperations() );
}
diff --git a/src/test/java/org/apache/commons/graph/spanning/KruskalTestCase.java b/src/test/java/org/apache/commons/graph/spanning/KruskalTestCase.java
index 9b6d82c..0f3b9f5 100644
--- a/src/test/java/org/apache/commons/graph/spanning/KruskalTestCase.java
+++ b/src/test/java/org/apache/commons/graph/spanning/KruskalTestCase.java
@@ -37,23 +37,84 @@ import org.junit.Test;
public final class KruskalTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
+ /**
+ * Test the algorithm with a disconnected graph on 4 vertices. In this
+ * case, we expect the Minimum spanning "tree" to actually be a minimum
+ * spanning forest with 2 components.
+ */
+ @Test
+ public void testDisconnectedMinimumSpanningTree()
{
- minimumSpanningTree( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
+ = new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ BaseLabeledVertex a = new BaseLabeledVertex( "A" );
+ BaseLabeledVertex b = new BaseLabeledVertex( "B" );
+ BaseLabeledVertex c = new BaseLabeledVertex( "C" );
+ BaseLabeledVertex d = new BaseLabeledVertex( "D" );
+
+
+ input.addVertex( a );
+ input.addVertex( b );
+ input.addVertex( c );
+ input.addVertex( d );
+
+ input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
+ input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );
+
+
+ // Expected
+ MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
+ new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );
+
+ for ( BaseLabeledVertex vertex : input.getVertices() )
+ {
+ expected.addVertex( vertex );
+ }
+ expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
+ expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );
+
+
+ // Actual
+ SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
+ minimumSpanningTree( input )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .fromArbitrarySource()
+ .applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );
+
+ // assert!
+ assertEquals( expected, actual );
+ }
+
+ @Test( expected = IllegalStateException.class )
+ public void testEmptyGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
.fromArbitrarySource()
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );
}
- @Test( expected = NullPointerException.class )
- public void testNullVertex()
+ @Test( expected = IllegalStateException.class )
+ public void testNotExistVertex()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromSource( null )
+ .fromSource( new BaseLabeledVertex( "NOT EXIST" ) );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ minimumSpanningTree( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .fromArbitrarySource()
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );
}
@@ -80,111 +141,24 @@ public final class KruskalTestCase
.applyingKruskalAlgorithm( null );
}
- @Test( expected = IllegalStateException.class )
- public void testNotExistVertex()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- minimumSpanningTree( input )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromSource( new BaseLabeledVertex( "NOT EXIST" ) );
- }
-
- @Test( expected = IllegalStateException.class )
- public void testEmptyGraph()
+ @Test( expected = NullPointerException.class )
+ public void testNullVertex()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromArbitrarySource()
+ .fromSource( null )
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );
}
- /**
- * Test Graph and Prim's solution can be seen on
- * <a href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">Wikipedia</a>
- */
- @Test
- public void testVerifyWikipediaMinimumSpanningTree()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
- = new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- BaseLabeledVertex a = new BaseLabeledVertex( "A" );
- BaseLabeledVertex b = new BaseLabeledVertex( "B" );
- BaseLabeledVertex c = new BaseLabeledVertex( "C" );
- BaseLabeledVertex d = new BaseLabeledVertex( "D" );
- BaseLabeledVertex e = new BaseLabeledVertex( "E" );
- BaseLabeledVertex f = new BaseLabeledVertex( "F" );
- BaseLabeledVertex g = new BaseLabeledVertex( "G" );
-
-
- input.addVertex( a );
- input.addVertex( b );
- input.addVertex( c );
- input.addVertex( d );
- input.addVertex( e );
- input.addVertex( f );
- input.addVertex( g );
-
-
- input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 7D ), b );
- input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> d", 5D ), d );
-
- input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 8D ), c );
- input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> d", 9D ), d );
- input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> e", 7D ), e );
-
- input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> e", 5D ), e );
-
- input.addEdge( d, new BaseLabeledWeightedEdge<Double>( "d <-> e", 15D ), e );
- input.addEdge( d, new BaseLabeledWeightedEdge<Double>( "d <-> f", 6D ), f );
-
- input.addEdge( e, new BaseLabeledWeightedEdge<Double>( "e <-> f", 8D ), f );
- input.addEdge( e, new BaseLabeledWeightedEdge<Double>( "e <-> g", 9D ), g );
-
- input.addEdge( f, new BaseLabeledWeightedEdge<Double>( "f <-> g", 11D ), g );
-
- // expected
-
- MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
- new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );
-
- for ( BaseLabeledVertex vertex : input.getVertices() )
- {
- expected.addVertex( vertex );
- }
-
- expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 7D ), b );
- expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> d", 5D ), d );
- expected.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> e", 9D ), e );
- expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> e", 5D ), e );
- expected.addEdge( d, new BaseLabeledWeightedEdge<Double>( "d <-> f", 6D ), f );
- expected.addEdge( e, new BaseLabeledWeightedEdge<Double>( "e <-> g", 9D ), g );
-
- // Actual
-
- SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
- minimumSpanningTree( input )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromArbitrarySource()
- .applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );
-
- // assert!
-
- assertEquals( expected, actual );
- }
-
/**
- * Test Graph and Prim's solution can be seen on
- * <a href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">Wikipedia</a>
+ * Test the minimum spanning tree on a path graph with 4 vertices
+ * and non-uniform weights.
*/
@Test
- public void testVerifyNotConnectedMinimumSpanningTree()
+ public void testP4NonUniformWeightsMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -200,11 +174,12 @@ public final class KruskalTestCase
input.addVertex( c );
input.addVertex( d );
- input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 7D ), b );
-
+ input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
+ input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 3D ), c );
+ input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );
- // expected
+ // Expected
MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );
@@ -212,10 +187,12 @@ public final class KruskalTestCase
{
expected.addVertex( vertex );
}
- expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 7D ), b );
+ expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
+ expected.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 3D ), c );
+ expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );
- // Actual
+ // Actual
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
@@ -223,7 +200,6 @@ public final class KruskalTestCase
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );
// assert!
-
assertEquals( expected, actual );
}
@@ -279,12 +255,11 @@ public final class KruskalTestCase
}
/**
- * Test the algorithm with a disconnected graph on 4 vertices. In this
- * case, we expect the Minimum spanning "tree" to actually be a minimum
- * spanning forest with 2 components.
+ * Test Graph and Prim's solution can be seen on
+ * <a href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">Wikipedia</a>
*/
@Test
- public void testDisconnectedMinimumSpanningTree()
+ public void testVerifyNotConnectedMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -300,11 +275,11 @@ public final class KruskalTestCase
input.addVertex( c );
input.addVertex( d );
- input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
- input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );
+ input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 7D ), b );
- // Expected
+ // expected
+
MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );
@@ -312,11 +287,10 @@ public final class KruskalTestCase
{
expected.addVertex( vertex );
}
- expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
- expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );
-
+ expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 7D ), b );
// Actual
+
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
@@ -324,15 +298,16 @@ public final class KruskalTestCase
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );
// assert!
+
assertEquals( expected, actual );
}
/**
- * Test the minimum spanning tree on a path graph with 4 vertices
- * and non-uniform weights.
+ * Test Graph and Prim's solution can be seen on
+ * <a href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">Wikipedia</a>
*/
@Test
- public void testP4NonUniformWeightsMinimumSpanningTree()
+ public void testVerifyWikipediaMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -341,19 +316,39 @@ public final class KruskalTestCase
BaseLabeledVertex b = new BaseLabeledVertex( "B" );
BaseLabeledVertex c = new BaseLabeledVertex( "C" );
BaseLabeledVertex d = new BaseLabeledVertex( "D" );
+ BaseLabeledVertex e = new BaseLabeledVertex( "E" );
+ BaseLabeledVertex f = new BaseLabeledVertex( "F" );
+ BaseLabeledVertex g = new BaseLabeledVertex( "G" );
input.addVertex( a );
input.addVertex( b );
input.addVertex( c );
input.addVertex( d );
+ input.addVertex( e );
+ input.addVertex( f );
+ input.addVertex( g );
- input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
- input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 3D ), c );
- input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );
+ input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 7D ), b );
+ input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> d", 5D ), d );
+
+ input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 8D ), c );
+ input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> d", 9D ), d );
+ input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> e", 7D ), e );
+
+ input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> e", 5D ), e );
+
+ input.addEdge( d, new BaseLabeledWeightedEdge<Double>( "d <-> e", 15D ), e );
+ input.addEdge( d, new BaseLabeledWeightedEdge<Double>( "d <-> f", 6D ), f );
+
+ input.addEdge( e, new BaseLabeledWeightedEdge<Double>( "e <-> f", 8D ), f );
+ input.addEdge( e, new BaseLabeledWeightedEdge<Double>( "e <-> g", 9D ), g );
+
+ input.addEdge( f, new BaseLabeledWeightedEdge<Double>( "f <-> g", 11D ), g );
+
+ // expected
- // Expected
MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );
@@ -361,12 +356,16 @@ public final class KruskalTestCase
{
expected.addVertex( vertex );
}
- expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
- expected.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 3D ), c );
- expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );
+ expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 7D ), b );
+ expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> d", 5D ), d );
+ expected.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> e", 9D ), e );
+ expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> e", 5D ), e );
+ expected.addEdge( d, new BaseLabeledWeightedEdge<Double>( "d <-> f", 6D ), f );
+ expected.addEdge( e, new BaseLabeledWeightedEdge<Double>( "e <-> g", 9D ), g );
// Actual
+
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
@@ -374,6 +373,7 @@ public final class KruskalTestCase
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );
// assert!
+
assertEquals( expected, actual );
}
diff --git a/src/test/java/org/apache/commons/graph/spanning/PrimTestCase.java b/src/test/java/org/apache/commons/graph/spanning/PrimTestCase.java
index 8f82f6d..8043508 100644
--- a/src/test/java/org/apache/commons/graph/spanning/PrimTestCase.java
+++ b/src/test/java/org/apache/commons/graph/spanning/PrimTestCase.java
@@ -35,24 +35,52 @@ import org.junit.Test;
public final class PrimTestCase
{
- @Test( expected = NullPointerException.class )
- public void testNullGraph()
+ private static void internalPrimAssertion( UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input,
+ BaseLabeledVertex source,
+ MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected )
{
- minimumSpanningTree( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ // actual
+
+ SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
+ minimumSpanningTree( input )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .fromSource( source )
+ .applyingPrimAlgorithm( new DoubleWeightBaseOperations() );
+
+ // assert!
+
+ assertEquals( expected, actual );
+ }
+
+ @Test( expected = IllegalStateException.class )
+ public void testEmptyGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
.fromArbitrarySource()
.applyingPrimAlgorithm( new DoubleWeightBaseOperations() );
}
- @Test( expected = NullPointerException.class )
- public void testNullVertex()
+ @Test( expected = IllegalStateException.class )
+ public void testNotExistVertex()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromSource( null )
+ .fromSource( new BaseLabeledVertex( "NOT EXIST" ) );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void testNullGraph()
+ {
+ minimumSpanningTree( (Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>) null )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .fromArbitrarySource()
.applyingPrimAlgorithm( new DoubleWeightBaseOperations() );
}
@@ -79,26 +107,15 @@ public final class PrimTestCase
.applyingBoruvkaAlgorithm( null );
}
- @Test( expected = IllegalStateException.class )
- public void testNotExistVertex()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- minimumSpanningTree( input )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromSource( new BaseLabeledVertex( "NOT EXIST" ) );
- }
-
- @Test( expected = IllegalStateException.class )
- public void testEmptyGraph()
+ @Test( expected = NullPointerException.class )
+ public void testNullVertex()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromArbitrarySource()
+ .fromSource( null )
.applyingPrimAlgorithm( new DoubleWeightBaseOperations() );
}
@@ -228,21 +245,4 @@ public final class PrimTestCase
internalPrimAssertion( input, d, expected );
}
- private static void internalPrimAssertion( UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input,
- BaseLabeledVertex source,
- MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected )
- {
- // actual
-
- SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
- minimumSpanningTree( input )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .fromSource( source )
- .applyingPrimAlgorithm( new DoubleWeightBaseOperations() );
-
- // assert!
-
- assertEquals( expected, actual );
- }
-
}
diff --git a/src/test/java/org/apache/commons/graph/spanning/ReverseDeleteTestCase.java b/src/test/java/org/apache/commons/graph/spanning/ReverseDeleteTestCase.java
index e3fb7bc..4d063d0 100644
--- a/src/test/java/org/apache/commons/graph/spanning/ReverseDeleteTestCase.java
+++ b/src/test/java/org/apache/commons/graph/spanning/ReverseDeleteTestCase.java
@@ -38,6 +38,22 @@ import org.junit.Test;
public class ReverseDeleteTestCase
{
+ @Test
+ public void testEmptyGraph()
+ {
+ UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
+ new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
+
+ SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> tree =
+ minimumSpanningTree( input )
+ .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
+ .applyingReverseDeleteAlgorithm( new DoubleWeightBaseOperations() );
+
+ assertEquals( 0, tree.getOrder() );
+ assertEquals( 0, tree.getSize() );
+
+ }
+
@Test( expected = NullPointerException.class )
public void testNullGraph()
{
@@ -56,22 +72,6 @@ public class ReverseDeleteTestCase
.applyingReverseDeleteAlgorithm( null );
}
- @Test
- public void testEmptyGraph()
- {
- UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
- new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
-
- SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> tree =
- minimumSpanningTree( input )
- .whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
- .applyingReverseDeleteAlgorithm( new DoubleWeightBaseOperations() );
-
- assertEquals( 0, tree.getOrder() );
- assertEquals( 0, tree.getSize() );
-
- }
-
/**
* Test Graph and Reverse-Delete Algorithm
*/
diff --git a/src/test/java/org/apache/commons/graph/utils/GraphUtils.java b/src/test/java/org/apache/commons/graph/utils/GraphUtils.java
index b344ea5..5e54202 100644
--- a/src/test/java/org/apache/commons/graph/utils/GraphUtils.java
+++ b/src/test/java/org/apache/commons/graph/utils/GraphUtils.java
@@ -38,12 +38,12 @@ public class GraphUtils
{
/**
- * Creates a complete graph with nVertices
+ * Create a Biparted graph
*
* @param nVertices number of vertices
* @param g graph
*/
- public static void buildCompleteGraph( int nVertices, BaseMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g )
+ public static void buildBipartedGraph( int nVertices, UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> g )
{
// building Graph
for ( int i = 0; i < nVertices; i++ )
@@ -52,9 +52,32 @@ public class GraphUtils
g.addVertex( v );
}
+ List<BaseLabeledVertex> fistPartition = new ArrayList<BaseLabeledVertex>();
+ List<BaseLabeledVertex> secondPartition = new ArrayList<BaseLabeledVertex>();
+
+ int count = 0;
for ( BaseLabeledVertex v1 : g.getVertices() )
{
- for ( BaseLabeledVertex v2 : g.getVertices() )
+ if ( count++ == nVertices / 2 )
+ {
+ break;
+ }
+ fistPartition.add( v1 );
+ }
+
+ count = 0;
+ for ( BaseLabeledVertex v2 : g.getVertices() )
+ {
+ if ( count++ < nVertices / 2 )
+ {
+ continue;
+ }
+ secondPartition.add( v2 );
+ }
+
+ for ( BaseLabeledVertex v1 : fistPartition )
+ {
+ for ( BaseLabeledVertex v2 : secondPartition )
{
if ( !v1.equals( v2 ) )
{
@@ -72,12 +95,12 @@ public class GraphUtils
}
/**
- * Create a Biparted graph
+ * Creates a complete graph with nVertices
*
* @param nVertices number of vertices
* @param g graph
... 102 lines suppressed ...
(commons-graph) 01/02: Normalize test method names
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-graph.git
commit 783a9c5c2832ed29181da22e17eeb50d48ffb241
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Nov 3 10:15:27 2023 -0400
Normalize test method names
---
.../graph/scc/SCCAlgorithmBenchmarkTestCase.java | 6 +++---
.../UniVsBiDijkstraBenchmarkTestCase.java | 4 ++--
.../MinimumSpanningTreeBenchmarkTestCase.java | 6 +++---
.../graph/builder/GraphBuilderTestCase.java | 2 +-
.../graph/collections/FibonacciHeapTestCase.java | 22 +++++++++++-----------
.../FindConnectedComponetTestCase.java | 14 +++++++-------
.../org/apache/commons/graph/elo/EloTestCase.java | 2 +-
.../commons/graph/export/ExportTestCase.java | 6 +++---
.../commons/graph/flow/EdmondsKarpTestCase.java | 2 +-
.../commons/graph/flow/FordFulkersonTestCase.java | 2 +-
.../graph/model/GraphSerializationTestCase.java | 14 +++++++-------
.../graph/scc/CheriyanMehlhornGabowTestCase.java | 2 +-
.../commons/graph/scc/KosarajuSharirTestCase.java | 2 +-
.../commons/graph/shortestpath/AStarTestCase.java | 2 +-
.../graph/shortestpath/BellmannFordTestCase.java | 2 +-
.../graph/shortestpath/BidirDijkstraTestCase.java | 8 ++++----
.../graph/shortestpath/DijkstraTestCase.java | 2 +-
.../graph/shortestpath/FloydWarshallTestCase.java | 4 ++--
.../commons/graph/spanning/BoruvkaTestCase.java | 2 +-
.../commons/graph/spanning/KruskalTestCase.java | 4 ++--
.../commons/graph/spanning/PrimTestCase.java | 4 ++--
.../graph/spanning/ReverseDeleteTestCase.java | 8 ++++----
.../apache/commons/graph/visit/VisitTestCase.java | 4 ++--
23 files changed, 62 insertions(+), 62 deletions(-)
diff --git a/src/benchmarks/java/org/apache/commons/graph/scc/SCCAlgorithmBenchmarkTestCase.java b/src/benchmarks/java/org/apache/commons/graph/scc/SCCAlgorithmBenchmarkTestCase.java
index d8aef0b..24c5c0f 100644
--- a/src/benchmarks/java/org/apache/commons/graph/scc/SCCAlgorithmBenchmarkTestCase.java
+++ b/src/benchmarks/java/org/apache/commons/graph/scc/SCCAlgorithmBenchmarkTestCase.java
@@ -86,21 +86,21 @@ public final class SCCAlgorithmBenchmarkTestCase
}
@Test
- public void performKosaraju()
+ public void testPerformKosaraju()
{
Set<Set<BaseLabeledVertex>> actual = findStronglyConnectedComponent( graph ).applyingKosarajuSharir();
assertTrue( actual.size() > 0 );
}
@Test
- public void performCheriyanMehlhornGabow()
+ public void testPerformCheriyanMehlhornGabow()
{
Set<Set<BaseLabeledVertex>> actual = findStronglyConnectedComponent( graph ).applyingCheriyanMehlhornGabow();
assertTrue( actual.size() > 0 );
}
@Test
- public void performTarjan()
+ public void testPerformTarjan()
{
Set<Set<BaseLabeledVertex>> actual = findStronglyConnectedComponent( graph ).applyingTarjan();
assertTrue( actual.size() > 0 );
diff --git a/src/benchmarks/java/org/apache/commons/graph/shortestpath/UniVsBiDijkstraBenchmarkTestCase.java b/src/benchmarks/java/org/apache/commons/graph/shortestpath/UniVsBiDijkstraBenchmarkTestCase.java
index f1f5ea1..914ce52 100644
--- a/src/benchmarks/java/org/apache/commons/graph/shortestpath/UniVsBiDijkstraBenchmarkTestCase.java
+++ b/src/benchmarks/java/org/apache/commons/graph/shortestpath/UniVsBiDijkstraBenchmarkTestCase.java
@@ -158,7 +158,7 @@ public final class UniVsBiDijkstraBenchmarkTestCase
}
@Test
- public void performUnidirectionalDijkstra() {
+ public void testPerformUnidirectionalDijkstra() {
BaseLabeledVertex source = sourceListUni.removeFirst();
BaseLabeledVertex target = targetListUni.removeFirst();
@@ -180,7 +180,7 @@ public final class UniVsBiDijkstraBenchmarkTestCase
}
@Test
- public void performBidirectionalDijkstra() {
+ public void testPerformBidirectionalDijkstra() {
BaseLabeledVertex source = sourceListBi.removeFirst();
BaseLabeledVertex target = targetListBi.removeFirst();
diff --git a/src/benchmarks/java/org/apache/commons/graph/spanning/MinimumSpanningTreeBenchmarkTestCase.java b/src/benchmarks/java/org/apache/commons/graph/spanning/MinimumSpanningTreeBenchmarkTestCase.java
index 23e824e..f42135c 100644
--- a/src/benchmarks/java/org/apache/commons/graph/spanning/MinimumSpanningTreeBenchmarkTestCase.java
+++ b/src/benchmarks/java/org/apache/commons/graph/spanning/MinimumSpanningTreeBenchmarkTestCase.java
@@ -118,7 +118,7 @@ public final class MinimumSpanningTreeBenchmarkTestCase
}
@Test
- public void performBoruvka()
+ public void testPerformBoruvka()
{
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( graph )
@@ -130,7 +130,7 @@ public final class MinimumSpanningTreeBenchmarkTestCase
}
@Test
- public void performKruskal()
+ public void testPerformKruskal()
{
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( graph )
@@ -142,7 +142,7 @@ public final class MinimumSpanningTreeBenchmarkTestCase
}
@Test
- public void performPrim()
+ public void testPerformPrim()
{
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( graph )
diff --git a/src/test/java/org/apache/commons/graph/builder/GraphBuilderTestCase.java b/src/test/java/org/apache/commons/graph/builder/GraphBuilderTestCase.java
index cfe9cd0..277c340 100644
--- a/src/test/java/org/apache/commons/graph/builder/GraphBuilderTestCase.java
+++ b/src/test/java/org/apache/commons/graph/builder/GraphBuilderTestCase.java
@@ -31,7 +31,7 @@ public final class GraphBuilderTestCase
{
@Test
- public void verifyProducedGraphesAreEquals()
+ public void testVerifyProducedGraphesAreEquals()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> expected =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
diff --git a/src/test/java/org/apache/commons/graph/collections/FibonacciHeapTestCase.java b/src/test/java/org/apache/commons/graph/collections/FibonacciHeapTestCase.java
index fe75c3f..3b0a928 100644
--- a/src/test/java/org/apache/commons/graph/collections/FibonacciHeapTestCase.java
+++ b/src/test/java/org/apache/commons/graph/collections/FibonacciHeapTestCase.java
@@ -57,14 +57,14 @@ public final class FibonacciHeapTestCase
}
@Test
- public void emptyWhenCreated()
+ public void testEmptyWhenCreated()
{
assertThat( queue.isEmpty(), is( true ) );
assertThat( queue.poll(), nullValue() );
}
@Test
- public void noLongerEmptyAfterAdd()
+ public void testNoLongerEmptyAfterAdd()
{
queue.add( 50 );
@@ -72,7 +72,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void singletonQueueReturnsSoleItemOnPoll()
+ public void testSingletonQueueReturnsSoleItemOnPoll()
{
queue.add( 50 );
@@ -80,7 +80,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void isEmptyAfterSoleElementRemoved()
+ public void testIsEmptyAfterSoleElementRemoved()
{
queue.add( 50 );
queue.poll();
@@ -89,7 +89,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void returnsOrderedItems()
+ public void testReturnsOrderedItems()
{
queue.add( 100 );
queue.add( 50 );
@@ -100,7 +100,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void insertSingleItem()
+ public void testInsertSingleItem()
{
queue.add( 50 );
@@ -109,7 +109,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void insertSameValuesAndReturnsOrderedItems()
+ public void testInsertSameValuesAndReturnsOrderedItems()
{
queue.add( 50 );
queue.add( 100 );
@@ -122,7 +122,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void returnsOrderedItemsFromRandomInsert()
+ public void testReturnsOrderedItemsFromRandomInsert()
{
final Random r = new Random( System.currentTimeMillis() );
final List<Integer> expected = new ArrayList<Integer>();
@@ -146,7 +146,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void addAllAndContinsItem()
+ public void testAddAllAndContinsItem()
{
Collection<Integer> c = new ArrayList<Integer>();
@@ -167,7 +167,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void clearQueue()
+ public void testClearQueue()
{
final Random r = new Random( System.currentTimeMillis() );
for ( int i = 0; i < 1000; i++ )
@@ -182,7 +182,7 @@ public final class FibonacciHeapTestCase
}
@Test
- public void offerPeekAndElement()
+ public void testOfferPeekAndElement()
{
queue.offer( 50 );
queue.offer( 100 );
diff --git a/src/test/java/org/apache/commons/graph/connectivity/FindConnectedComponetTestCase.java b/src/test/java/org/apache/commons/graph/connectivity/FindConnectedComponetTestCase.java
index cad923f..afbd60c 100644
--- a/src/test/java/org/apache/commons/graph/connectivity/FindConnectedComponetTestCase.java
+++ b/src/test/java/org/apache/commons/graph/connectivity/FindConnectedComponetTestCase.java
@@ -48,7 +48,7 @@ public final class FindConnectedComponetTestCase
}
@Test
- public void verifyEmptyGraph()
+ public void testVerifyEmptyGraph()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> graph =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge>();
@@ -60,7 +60,7 @@ public final class FindConnectedComponetTestCase
}
@Test
- public void verifyNullVerticesGraph()
+ public void testVerifyNullVerticesGraph()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledEdge> graph =
newUndirectedMutableGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledEdge>()
@@ -80,7 +80,7 @@ public final class FindConnectedComponetTestCase
}
@Test
- public void verifyConnectedComponents()
+ public void testVerifyConnectedComponents()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
@@ -110,7 +110,7 @@ public final class FindConnectedComponetTestCase
}
@Test
- public void verifyConnectedComponents2()
+ public void testVerifyConnectedComponents2()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
@@ -148,7 +148,7 @@ public final class FindConnectedComponetTestCase
}
@Test
- public void verifyConnectedComponents3()
+ public void testVerifyConnectedComponents3()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
@@ -177,7 +177,7 @@ public final class FindConnectedComponetTestCase
}
@Test
- public void verifyConnectedComponentsIncludingVertices()
+ public void testVerifyConnectedComponentsIncludingVertices()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
@@ -215,7 +215,7 @@ public final class FindConnectedComponetTestCase
}
@Test
- public void verifyConnectedComponentsIncludingVertices2()
+ public void testVerifyConnectedComponentsIncludingVertices2()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
final BaseLabeledVertex e = new BaseLabeledVertex( "E" );
diff --git a/src/test/java/org/apache/commons/graph/elo/EloTestCase.java b/src/test/java/org/apache/commons/graph/elo/EloTestCase.java
index ff8deda..083ffb7 100644
--- a/src/test/java/org/apache/commons/graph/elo/EloTestCase.java
+++ b/src/test/java/org/apache/commons/graph/elo/EloTestCase.java
@@ -35,7 +35,7 @@ public final class EloTestCase
{
@Test
- public void performElo()
+ public void testPerformElo()
{
DirectedGraph<String, GameResult> tournament =
newDirectedMutableGraph( new AbstractGraphConnection<String, GameResult>()
diff --git a/src/test/java/org/apache/commons/graph/export/ExportTestCase.java b/src/test/java/org/apache/commons/graph/export/ExportTestCase.java
index dc0fc82..476434c 100644
--- a/src/test/java/org/apache/commons/graph/export/ExportTestCase.java
+++ b/src/test/java/org/apache/commons/graph/export/ExportTestCase.java
@@ -65,7 +65,7 @@ public class ExportTestCase {
}
@Test
- public void shouldPrintDotFormat()
+ public void testShouldPrintDotFormat()
throws Exception
{
export( actual ).withName( "DotFormatGraph" )
@@ -77,7 +77,7 @@ public class ExportTestCase {
}
@Test
- public void shouldPrintGraphML()
+ public void testShouldPrintGraphML()
throws Exception
{
export( actual ).withName( "GraphMLGraph" )
@@ -89,7 +89,7 @@ public class ExportTestCase {
}
@Test
- public void shouldPrintGraphMLFormat()
+ public void testShouldPrintGraphMLFormat()
throws Exception
{
export( actual ).usingGraphMLFormat().to( System.out );
diff --git a/src/test/java/org/apache/commons/graph/flow/EdmondsKarpTestCase.java b/src/test/java/org/apache/commons/graph/flow/EdmondsKarpTestCase.java
index 53b6a0a..8553780 100644
--- a/src/test/java/org/apache/commons/graph/flow/EdmondsKarpTestCase.java
+++ b/src/test/java/org/apache/commons/graph/flow/EdmondsKarpTestCase.java
@@ -110,7 +110,7 @@ public class EdmondsKarpTestCase
}
@Test
- public void findMaxFlowAndVerify()
+ public void testFindMaxFlowAndVerify()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
final BaseLabeledVertex g = new BaseLabeledVertex( "G" );
diff --git a/src/test/java/org/apache/commons/graph/flow/FordFulkersonTestCase.java b/src/test/java/org/apache/commons/graph/flow/FordFulkersonTestCase.java
index d4d2a56..c88ae17 100644
--- a/src/test/java/org/apache/commons/graph/flow/FordFulkersonTestCase.java
+++ b/src/test/java/org/apache/commons/graph/flow/FordFulkersonTestCase.java
@@ -148,7 +148,7 @@ public final class FordFulkersonTestCase
}
@Test
- public void findMaxFlowAndVerify()
+ public void testFindMaxFlowAndVerify()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
final BaseLabeledVertex d = new BaseLabeledVertex( "D" );
diff --git a/src/test/java/org/apache/commons/graph/model/GraphSerializationTestCase.java b/src/test/java/org/apache/commons/graph/model/GraphSerializationTestCase.java
index 76e4e60..aaa0e04 100644
--- a/src/test/java/org/apache/commons/graph/model/GraphSerializationTestCase.java
+++ b/src/test/java/org/apache/commons/graph/model/GraphSerializationTestCase.java
@@ -61,7 +61,7 @@ public class GraphSerializationTestCase
}
@Test
- public void serializeUndirectedGraph()
+ public void testSerializeUndirectedGraph()
throws Exception
{
MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = newUndirectedMutableGraph( buildGraphConnections() );
@@ -70,7 +70,7 @@ public class GraphSerializationTestCase
}
@Test
- public void serializeDirectedGraph()
+ public void testSerializeDirectedGraph()
throws Exception
{
MutableGraph<BaseLabeledVertex, BaseLabeledEdge> g = newDirectedMutableGraph( buildGraphConnections() );
@@ -79,7 +79,7 @@ public class GraphSerializationTestCase
}
@Test
- public void serializeUndirectedWeightdGraph()
+ public void testSerializeUndirectedWeightdGraph()
throws Exception
{
MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
@@ -89,7 +89,7 @@ public class GraphSerializationTestCase
}
@Test
- public void serializeDirectedWeightdGraph()
+ public void testSerializeDirectedWeightdGraph()
throws Exception
{
MutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
@@ -99,7 +99,7 @@ public class GraphSerializationTestCase
}
@Test
- public void serializeSpanningTree()
+ public void testSerializeSpanningTree()
throws Exception
{
final MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> spanningTree =
@@ -113,7 +113,7 @@ public class GraphSerializationTestCase
}
@Test
- public void serializeSyncronyzedDirectedWeightdGraph()
+ public void testSerializeSyncronyzedDirectedWeightdGraph()
throws Exception
{
Graph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> g =
@@ -123,7 +123,7 @@ public class GraphSerializationTestCase
}
@Test
- public void serializePath()
+ public void testSerializePath()
throws Exception
{
BaseLabeledVertex start = new BaseLabeledVertex( "start" );
diff --git a/src/test/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowTestCase.java b/src/test/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowTestCase.java
index fd5d75f..c20afc2 100644
--- a/src/test/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowTestCase.java
+++ b/src/test/java/org/apache/commons/graph/scc/CheriyanMehlhornGabowTestCase.java
@@ -87,7 +87,7 @@ public final class CheriyanMehlhornGabowTestCase
}
@Test
- public void verifyHasStronglyConnectedComponents()
+ public void testVerifyHasStronglyConnectedComponents()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
final BaseLabeledVertex b = new BaseLabeledVertex( "B" );
diff --git a/src/test/java/org/apache/commons/graph/scc/KosarajuSharirTestCase.java b/src/test/java/org/apache/commons/graph/scc/KosarajuSharirTestCase.java
index 783b96a..b06049b 100644
--- a/src/test/java/org/apache/commons/graph/scc/KosarajuSharirTestCase.java
+++ b/src/test/java/org/apache/commons/graph/scc/KosarajuSharirTestCase.java
@@ -70,7 +70,7 @@ public final class KosarajuSharirTestCase
}
@Test
- public void verifyHasStronglyConnectedComponents()
+ public void testVerifyHasStronglyConnectedComponents()
{
final BaseLabeledVertex a = new BaseLabeledVertex( "A" );
final BaseLabeledVertex b = new BaseLabeledVertex( "B" );
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/AStarTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/AStarTestCase.java
index ed996da..bd1e143 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/AStarTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/AStarTestCase.java
@@ -153,7 +153,7 @@ public final class AStarTestCase
* <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">Wikipedia</a>
*/
@Test
- public void findShortestPathAndVerify()
+ public void testFindShortestPathAndVerify()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
index f2d4797..bcdd2cb 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
@@ -120,7 +120,7 @@ public final class BellmannFordTestCase
* <a href="http://compprog.wordpress.com/2007/11/29/one-source-shortest-path-the-bellman-ford-algorithm/">Wikipedia</a>
*/
@Test
- public void findShortestPathAndVerify()
+ public void testFindShortestPathAndVerify()
{
// the input graph
DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/BidirDijkstraTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/BidirDijkstraTestCase.java
index 662cef4..107e491 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/BidirDijkstraTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/BidirDijkstraTestCase.java
@@ -180,7 +180,7 @@ public final class BidirDijkstraTestCase
* <a href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm>Wikipedia</a>
*/
@Test
- public void findShortestPathAndVerify()
+ public void testFindShortestPathAndVerify()
{
DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -238,7 +238,7 @@ public final class BidirDijkstraTestCase
}
@Test
- public void verifyTwoNodePath() {
+ public void testVerifyTwoNodePath() {
DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -271,7 +271,7 @@ public final class BidirDijkstraTestCase
}
@Test
- public void verifyThreeNodePath() {
+ public void testVerifyThreeNodePath() {
DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -308,7 +308,7 @@ public final class BidirDijkstraTestCase
}
@Test
- public void compareToUnidirectional() {
+ public void testCompareToUnidirectional() {
// It is hard to get unidirectional Dijkstra's algorithm wrong;
// therefore compare a sequence of outputs.
Random r = new Random();
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
index e0553fe..8c3af71 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
@@ -104,7 +104,7 @@ public final class DijkstraTestCase
* <a href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm>Wikipedia</a>
*/
@Test
- public void findShortestPathAndVerify()
+ public void testFindShortestPathAndVerify()
{
DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> graph =
new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
diff --git a/src/test/java/org/apache/commons/graph/shortestpath/FloydWarshallTestCase.java b/src/test/java/org/apache/commons/graph/shortestpath/FloydWarshallTestCase.java
index 5d697ef..195bd4d 100644
--- a/src/test/java/org/apache/commons/graph/shortestpath/FloydWarshallTestCase.java
+++ b/src/test/java/org/apache/commons/graph/shortestpath/FloydWarshallTestCase.java
@@ -74,13 +74,13 @@ public class FloydWarshallTestCase
}
@Test
- public void undirectedShortestPath()
+ public void testUndirectedShortestPath()
{
findShortestPathAndVerify( new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>() );
}
@Test
- public void directedShortestPath()
+ public void testDirectedShortestPath()
{
findShortestPathAndVerify( new DirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>() );
}
diff --git a/src/test/java/org/apache/commons/graph/spanning/BoruvkaTestCase.java b/src/test/java/org/apache/commons/graph/spanning/BoruvkaTestCase.java
index 433fe1f..7a1c5f1 100644
--- a/src/test/java/org/apache/commons/graph/spanning/BoruvkaTestCase.java
+++ b/src/test/java/org/apache/commons/graph/spanning/BoruvkaTestCase.java
@@ -106,7 +106,7 @@ public final class BoruvkaTestCase
* Test Graph and boruvka's solution can be seen on
*/
@Test
- public void verifyWikipediaMinimumSpanningTree()
+ public void testVerifyWikipediaMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
diff --git a/src/test/java/org/apache/commons/graph/spanning/KruskalTestCase.java b/src/test/java/org/apache/commons/graph/spanning/KruskalTestCase.java
index bbfd676..9b6d82c 100644
--- a/src/test/java/org/apache/commons/graph/spanning/KruskalTestCase.java
+++ b/src/test/java/org/apache/commons/graph/spanning/KruskalTestCase.java
@@ -108,7 +108,7 @@ public final class KruskalTestCase
* <a href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">Wikipedia</a>
*/
@Test
- public void verifyWikipediaMinimumSpanningTree()
+ public void testVerifyWikipediaMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -184,7 +184,7 @@ public final class KruskalTestCase
* <a href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">Wikipedia</a>
*/
@Test
- public void verifyNotConnectedMinimumSpanningTree()
+ public void testVerifyNotConnectedMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
diff --git a/src/test/java/org/apache/commons/graph/spanning/PrimTestCase.java b/src/test/java/org/apache/commons/graph/spanning/PrimTestCase.java
index 1cdb53f..8f82f6d 100644
--- a/src/test/java/org/apache/commons/graph/spanning/PrimTestCase.java
+++ b/src/test/java/org/apache/commons/graph/spanning/PrimTestCase.java
@@ -107,7 +107,7 @@ public final class PrimTestCase
* <a href="http://gauguin.info.uniroma2.it/~italiano/Teaching/Algoritmi/Lezioni/cap12.pdf">slides</a>
*/
@Test
- public void verifyMinimumSpanningTree2()
+ public void testVerifyMinimumSpanningTree2()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -170,7 +170,7 @@ public final class PrimTestCase
* <a href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">Wikipedia</a>
*/
@Test
- public void verifyWikipediaMinimumSpanningTree()
+ public void testVerifyWikipediaMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
diff --git a/src/test/java/org/apache/commons/graph/spanning/ReverseDeleteTestCase.java b/src/test/java/org/apache/commons/graph/spanning/ReverseDeleteTestCase.java
index 5f6c9b1..e3fb7bc 100644
--- a/src/test/java/org/apache/commons/graph/spanning/ReverseDeleteTestCase.java
+++ b/src/test/java/org/apache/commons/graph/spanning/ReverseDeleteTestCase.java
@@ -76,7 +76,7 @@ public class ReverseDeleteTestCase
* Test Graph and Reverse-Delete Algorithm
*/
@Test
- public void verifyMinimumSpanningTree()
+ public void testVerifyMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -117,7 +117,7 @@ public class ReverseDeleteTestCase
* Test Graph and Reverse-Delete Algorithm
*/
@Test
- public void verifyNotConnectGraph()
+ public void testVerifyNotConnectGraph()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -151,7 +151,7 @@ public class ReverseDeleteTestCase
* Test Graph and Reverse-Delete Algorithm
*/
@Test
- public void verifyNotConnectGraph2()
+ public void testVerifyNotConnectGraph2()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
@@ -202,7 +202,7 @@ public class ReverseDeleteTestCase
* Test Graph and Reverse-Delete Algorithm
*/
@Test
- public void verifyNotConnectGraph3()
+ public void testVerifyNotConnectGraph3()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input =
new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();
diff --git a/src/test/java/org/apache/commons/graph/visit/VisitTestCase.java b/src/test/java/org/apache/commons/graph/visit/VisitTestCase.java
index ddbda74..212ff0a 100644
--- a/src/test/java/org/apache/commons/graph/visit/VisitTestCase.java
+++ b/src/test/java/org/apache/commons/graph/visit/VisitTestCase.java
@@ -58,7 +58,7 @@ public final class VisitTestCase
* <a href="http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/breadthSearch.htm">here</a>
*/
@Test
- public void verifyBreadthFirstSearch()
+ public void testVerifyBreadthFirstSearch()
{
// input graph
@@ -142,7 +142,7 @@ public final class VisitTestCase
* <a href="http://aiukswkelasgkelompok7.wordpress.com/metode-pencarian-dan-pelacakan/">here</a>
*/
@Test
- public void verifyDepthFirstSearch()
+ public void testVerifyDepthFirstSearch()
{
// expected node set
final List<BaseLabeledVertex> expected = new ArrayList<BaseLabeledVertex>();