Coverage Report - net.sourceforge.combean.graph.alg.spath.BellmanFordAlg
 
Classes in this File Line Coverage Branch Coverage Complexity
BellmanFordAlg
84%
21/25
75%
9/12
0
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Combean; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 /*
 19  
  * Created on 10.04.2005
 20  
  *
 21  
  */
 22  
 package net.sourceforge.combean.graph.alg.spath;
 23  
 
 24  
 import net.sourceforge.combean.graph.containers.ListAsNodeQueue;
 25  
 import net.sourceforge.combean.interfaces.graph.Edge;
 26  
 import net.sourceforge.combean.interfaces.graph.EdgeIterator;
 27  
 import net.sourceforge.combean.interfaces.graph.Node;
 28  
 import net.sourceforge.combean.interfaces.graph.containers.NodeQueue;
 29  
 
 30  
 
 31  
 /**
 32  
  * Implements the classic Bellman-Ford algorithm
 33  
  * (search shortest paths by a label correcting algorithm which visits the
 34  
  * nodes in a FIFO order)
 35  
  * 
 36  
  * Requires OutgoingEdgeNeighborhoodGraphProp.
 37  
  * 
 38  
  * @author schickin
 39  
  *
 40  
  */
 41  
 public class BellmanFordAlg
 42  
 <NumType extends Comparable<NumType>>
 43  
 extends AbstractLabellingAlg<NumType> {
 44  
     
 45  21
     private NodeQueue queue = null;
 46  
 
 47  
     /**
 48  
      * constructor
 49  
      */
 50  
     public BellmanFordAlg() {
 51  21
         super();
 52  21
     }
 53  
 
 54  
     /* (non-Javadoc)
 55  
      * @see net.sourceforge.combean.graph.alg.spath.AbstractLabellingAlg#calcShortestPaths()
 56  
      */
 57  
     protected void calcShortestPaths() {
 58  21
         this.queue.enqueue(this.startNode);
 59  
         
 60  243
         while (!this.queue.isEmpty()) {
 61  225
             Node v = this.queue.dequeue();
 62  225
             if (checkForNegativeCycleAtDequeue(v)) {
 63  3
                 break;
 64  
             }
 65  222
             EdgeIterator itOutgoing = this.neigh.getOutgoingEdges(v);
 66  513
             while (itOutgoing.hasNext()) {
 67  291
                 Edge e = itOutgoing.next();
 68  291
                 boolean labelChanged = correctLabel(v, e);
 69  291
                 if (labelChanged) {
 70  207
                     this.queue.enqueue(this.neigh.getOtherNode(e, v));
 71  
                 }
 72  291
             }
 73  222
         }
 74  21
     }
 75  
 
 76  
     /**
 77  
      * Optional. Set a queue which shall be used by the algorithm to queue
 78  
      * the nodes for which the labels shall be corrected.
 79  
      * 
 80  
      * @param queue the queue to be used
 81  
      */
 82  
     public void setNodeQueue(NodeQueue queue) {
 83  0
         this.queue = queue;
 84  0
     }
 85  
     
 86  
     /* (non-Javadoc)
 87  
      * @see net.sourceforge.combean.graph.alg.spath.AbstractLabellingAlg#init()
 88  
      */
 89  
     protected void init() {
 90  21
          super.init();
 91  
          
 92  21
          if (this.queue == null) {
 93  21
              this.queue = new ListAsNodeQueue();
 94  
          }
 95  21
     }
 96  
     
 97  
     /**
 98  
      * Check whether a negative cycle has been identified.
 99  
      * Default implementation does nothing, i.e., only more specialized
 100  
      * subclasses will be able to find negative cycles (this is not done
 101  
      * here because a direct implementation requires more specialised
 102  
      * graph properties).
 103  
      * The method is called immediately after a node has been dequeued.
 104  
      * 
 105  
      * @param v the node which has just been dequeued.
 106  
      * @return true if a negative cycle has been identified
 107  
      */
 108  
     protected boolean checkForNegativeCycleAtDequeue(Node v) {
 109  
         /* nothing to do here. intented to be overridden for detection of
 110  
          * negative cycles
 111  
          */
 112  0
         if (v == null) {
 113  
             /* do nothing ... just to sooth the compiler because v is used */
 114  
         }
 115  
         
 116  0
         return false;
 117  
     }
 118  
 }