Author: simonetripodi
Date: Tue Jun 21 15:34:38 2011
New Revision: 1138053
URL: http://svn.apache.org/viewvc?rev=1138053&view=rev
Log:
made the InMemoryWeightedPath more flexible and adaptable for algorithms which
don't know the path weight at the end of execution
Modified:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/Dijkstra.java
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/PredecessorsList.java
commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
Modified:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java?rev=1138053&r1=1138052&r2=1138053&view=diff
==============================================================================
---
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java
(original)
+++
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java
Tue Jun 21 15:34:38 2011
@@ -37,17 +37,41 @@ public final class InMemoryWeightedPath<
implements WeightedPath<V, WE>
{
- private final Double weigth;
+ private Double weigth = 0D;
- public InMemoryWeightedPath( V start, V target, Double weigth )
+ public InMemoryWeightedPath( V start, V target )
{
super( start, target );
+ }
- if ( weigth == null )
- {
- throw new IllegalArgumentException( "Path weigth cannot be null" );
- }
- this.weigth = weigth;
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void addEdgeInHead( WE edge )
+ {
+ super.addEdgeInHead( edge );
+ increaseWeight( edge );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void addEdgeInTail( WE edge )
+ {
+ super.addEdgeInTail( edge );
+ increaseWeight( edge );
+ }
+
+ /**
+ * Increase the path weight
+ *
+ * @param edge the edge which weigth increase the path weigth
+ */
+ private void increaseWeight( WE edge )
+ {
+ weigth = edge.getWeight().doubleValue() + weigth.doubleValue();
}
/**
Modified:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/Dijkstra.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/Dijkstra.java?rev=1138053&r1=1138052&r2=1138053&view=diff
==============================================================================
---
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/Dijkstra.java
(original)
+++
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/Dijkstra.java
Tue Jun 21 15:34:38 2011
@@ -81,7 +81,7 @@ public final class Dijkstra
// destination reached, stop and build the path
if ( target.equals( vertex ) )
{
- return predecessors.buildPath( source, target,
shortestDistances.getWeight( target ) );
+ return predecessors.buildPath( source, target );
}
settledNodes.add( vertex );
Modified:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/PredecessorsList.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/PredecessorsList.java?rev=1138053&r1=1138052&r2=1138053&view=diff
==============================================================================
---
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/PredecessorsList.java
(original)
+++
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/PredecessorsList.java
Tue Jun 21 15:34:38 2011
@@ -61,9 +61,9 @@ final class PredecessorsList<V extends V
* @param cost the path cost
* @return the weighted path related to source to target
*/
- public WeightedPath<V, WE> buildPath( V source, V target, Double cost )
+ public WeightedPath<V, WE> buildPath( V source, V target )
{
- InMemoryWeightedPath<V, WE> path = new InMemoryWeightedPath<V, WE>(
source, target, cost );
+ InMemoryWeightedPath<V, WE> path = new InMemoryWeightedPath<V, WE>(
source, target );
V vertex = target;
while ( !source.equals( vertex ) )
Modified:
commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java?rev=1138053&r1=1138052&r2=1138053&view=diff
==============================================================================
---
commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
(original)
+++
commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/DijkstraTestCase.java
Tue Jun 21 15:34:38 2011
@@ -69,7 +69,7 @@ public final class DijkstraTestCase
// expected path
InMemoryWeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge>
expected =
- new InMemoryWeightedPath<BaseLabeledVertex,
BaseLabeledWeightedEdge>( one, five, 20D );
+ new InMemoryWeightedPath<BaseLabeledVertex,
BaseLabeledWeightedEdge>( one, five );
expected.addVertexInTail( three );
expected.addVertexInTail( six );