You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/06/30 21:47:47 UTC

svn commit: r1141686 - in /commons/sandbox/graph/trunk/src: main/java/org/apache/commons/graph/shortestpath/BellmannFord.java test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java

Author: simonetripodi
Date: Thu Jun 30 19:47:47 2011
New Revision: 1141686

URL: http://svn.apache.org/viewvc?rev=1141686&view=rev
Log:
first checkin of BellmannFordTestCase
fixed BellmannFord algorithm (changed also the signature, it is the single source algorithm)

Added:
    commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java   (with props)
Modified:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/BellmannFord.java

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/BellmannFord.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/BellmannFord.java?rev=1141686&r1=1141685&r2=1141686&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/BellmannFord.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/BellmannFord.java Thu Jun 30 19:47:47 2011
@@ -50,30 +50,32 @@ public final class BellmannFord
      * @param target the shortest path target Vertex
      * @return a path which describes the shortest path, if any, otherwise a {@link PathNotFoundException} will be thrown
      */
-    public static <V extends Vertex, WE extends WeightedEdge, G extends WeightedGraph<V, WE> & DirectedGraph<V, WE>> WeightedPath<V, WE> findShortestPath( G graph,
-                                                                                                                                                              V source,
-                                                                                                                                                              V target )
+    public static <V extends Vertex, WE extends WeightedEdge, G extends WeightedGraph<V, WE> & DirectedGraph<V, WE>> AllVertexPairsShortestPath<V, WE> findShortestPath( G graph,
+                                                                                                                                                                         V source)
     {
         final ShortestDistances<V> shortestDistances = new ShortestDistances<V>();
         shortestDistances.setWeight( source, 0D );
 
         final PredecessorsList<V, WE> predecessors = new PredecessorsList<V, WE>( graph );
 
-        for ( WE edge : graph.getEdges() )
+        for ( int i = 0; i < graph.getOrder(); i++ )
         {
-            VertexPair<V> vertexPair = graph.getVertices( edge );
-            V u = vertexPair.getHead();
-            V v = vertexPair.getTail();
-
-            Double shortDist = shortestDistances.getWeight( u ) + edge.getWeight();
-
-            if ( shortDist.compareTo( shortestDistances.getWeight( v ) ) < 0 )
+            for ( WE edge : graph.getEdges() )
             {
-                // assign new shortest distance and mark unsettled
-                shortestDistances.setWeight( v, shortDist );
-
-                // assign predecessor in shortest path
-                predecessors.addPredecessor( v, u );
+                VertexPair<V> vertexPair = graph.getVertices( edge );
+                V u = vertexPair.getHead();
+                V v = vertexPair.getTail();
+
+                Double shortDist = shortestDistances.getWeight( u ) + edge.getWeight();
+
+                if ( shortDist.compareTo( shortestDistances.getWeight( v ) ) < 0 )
+                {
+                    // assign new shortest distance and mark unsettled
+                    shortestDistances.setWeight( v, shortDist );
+
+                    // assign predecessor in shortest path
+                    predecessors.addPredecessor( v, u );
+                }
             }
         }
 
@@ -93,7 +95,18 @@ public final class BellmannFord
             }
         }
 
-        return null;
+        AllVertexPairsShortestPath<V, WE> allVertexPairsShortestPath = new AllVertexPairsShortestPath<V, WE>();
+
+        for ( V target : graph.getVertices() )
+        {
+            if ( !source.equals( target ) )
+            {
+                WeightedPath<V, WE> weightedPath = predecessors.buildPath( source, target );
+                allVertexPairsShortestPath.addShortestPath( source, target, weightedPath );
+            }
+        }
+
+        return allVertexPairsShortestPath;
     }
 
 }

Added: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java?rev=1141686&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java (added)
+++ commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java Thu Jun 30 19:47:47 2011
@@ -0,0 +1,92 @@
+package org.apache.commons.graph.shortestpath;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static junit.framework.Assert.assertEquals;
+import static org.apache.commons.graph.shortestpath.BellmannFord.findShortestPath;
+
+import org.apache.commons.graph.WeightedPath;
+import org.apache.commons.graph.model.BaseLabeledVertex;
+import org.apache.commons.graph.model.BaseLabeledWeightedEdge;
+import org.apache.commons.graph.model.DirectedMutableWeightedGraph;
+import org.apache.commons.graph.model.InMemoryWeightedPath;
+import org.junit.Test;
+
+public final class BellmannFordTestCase
+{
+
+    /**
+     * 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>
+     */
+    @Test
+    public void findShortestPathAndVerify()
+    {
+        // the input graph
+        DirectedMutableWeightedGraph<BaseLabeledVertex, BaseLabeledWeightedEdge> graph =
+            new DirectedMutableWeightedGraph<BaseLabeledVertex, BaseLabeledWeightedEdge>();
+
+        BaseLabeledVertex one = new BaseLabeledVertex( "1" );
+        BaseLabeledVertex two = new BaseLabeledVertex( "2" );
+        BaseLabeledVertex three = new BaseLabeledVertex( "3" );
+        BaseLabeledVertex four = new BaseLabeledVertex( "4" );
+        BaseLabeledVertex five = new BaseLabeledVertex( "5" );
+
+        graph.addVertex( one );
+        graph.addVertex( two );
+        graph.addVertex( three );
+        graph.addVertex( four );
+        graph.addVertex( five );
+
+        graph.addEdge( one, new BaseLabeledWeightedEdge( "1 -> 2", 6D ), two );
+        graph.addEdge( one, new BaseLabeledWeightedEdge( "1 -> 4", 7D ), four );
+
+        graph.addEdge( two, new BaseLabeledWeightedEdge( "2 -> 3", 5D ), three );
+        graph.addEdge( two, new BaseLabeledWeightedEdge( "2 -> 5", -4D ), five );
+        graph.addEdge( two, new BaseLabeledWeightedEdge( "2 -> 4", 8D ), four );
+
+        graph.addEdge( three, new BaseLabeledWeightedEdge( "3 -> 2", -2D ), two );
+
+        graph.addEdge( four, new BaseLabeledWeightedEdge( "4 -> 3", -3D ), three );
+        graph.addEdge( four, new BaseLabeledWeightedEdge( "4 -> 5", 9D ), five );
+
+        graph.addEdge( five, new BaseLabeledWeightedEdge( "5 -> 3", 7D ), three );
+        graph.addEdge( five, new BaseLabeledWeightedEdge( "5 -> 1", 2D ), one );
+
+        // the expected weighted path
+        InMemoryWeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge> expected =
+            new InMemoryWeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge>( one, three );
+        expected.addVertexInTail( one );
+        expected.addVertexInTail( four );
+        expected.addVertexInTail( three );
+
+        expected.addEdgeInTail( new BaseLabeledWeightedEdge( "1 -> 4", 7D ) );
+        expected.addEdgeInTail( new BaseLabeledWeightedEdge( "4 -> 3", -3D ) );
+
+        // the actual weighted path
+        AllVertexPairsShortestPath<BaseLabeledVertex, BaseLabeledWeightedEdge> allVertexPairsShortestPath =
+            findShortestPath( graph, one );
+
+        WeightedPath<BaseLabeledVertex, BaseLabeledWeightedEdge> actual =
+            allVertexPairsShortestPath.findShortestPath( one, three );
+        assertEquals( expected, actual );
+    }
+
+}

Propchange: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/shortestpath/BellmannFordTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain