#8714: add Bellman-Ford algorithm for shortest paths
----------------------------+-----------------------------------------------
   Reporter:  mvngu         |       Owner:  jason, ncohen, rlm
       Type:  enhancement   |      Status:  new               
   Priority:  major         |   Milestone:  sage-4.4          
  Component:  graph theory  |    Keywords:                    
     Author:                |    Upstream:  N/A               
   Reviewer:                |      Merged:                    
Work_issues:                |  
----------------------------+-----------------------------------------------
Changes (by mvngu):

 * cc: wdj (added)


Old description:

> I'm using #698 as a wish list of items to add to the graph theory module
> of Sage. The purpose of this ticket is to implement the
> [http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm Bellman-
> Ford] algorithm for finding shortest paths in a weighted graph `G` that
> may have negative weights. If `G` doesn't have negative weights,
> Dijkstra's algorithm can be used. However, if `G` has negative weights,
> we fall back on the Bellman-Ford algorithm. The Bellman-Ford algorithm is
> able to handle graphs with negative weights, but not graphs that have
> negative-weight cycles. See also the function
> [http://reference.wolfram.com/mathematica/Combinatorica/ref/BellmanFord.html
> BellmanFord] in Mathematica's
> [http://reference.wolfram.com/mathematica/Combinatorica/guide/CombinatoricaPackage.html
> Combinatorica] package. See this [http://code.google.com/p/graph-theory-
> algorithms-book/ graph theory book] for an algorithmic presentation of
> the Bellman-Ford algorithm.

New description:

 I'm using #698 as a wish list of items to add to the graph theory module
 of Sage. The purpose of this ticket is to implement the
 [http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm Bellman-Ford]
 algorithm for finding shortest paths in a weighted graph `G` that may have
 negative weights. If `G` doesn't have negative weights, Dijkstra's
 algorithm can be used. However, if `G` has negative weights, we fall back
 on the Bellman-Ford algorithm. The Bellman-Ford algorithm is able to
 handle graphs with negative weights, but not graphs that have negative-
 weight cycles. See also the function
 [http://reference.wolfram.com/mathematica/Combinatorica/ref/BellmanFord.html
 BellmanFord] in Mathematica's
 
[http://reference.wolfram.com/mathematica/Combinatorica/guide/CombinatoricaPackage.html
 Combinatorica] package. See this [http://code.google.com/p/graph-theory-
 algorithms-book/ graph theory book] for an algorithmic presentation of the
 Bellman-Ford algorithm.

 See also the [/wiki/GraphTheoryRoadmap graph theory roadmap].

--

Comment:

 Here is an implementation by David Joyner:
 {{{
 def bellman_ford(Gamma, s):
     """
     Computes the shortest distance from s to all other vertices in Gamma.
     If Gamma has a negative weight cycle, then return an error.

     INPUT:

     - Gamma -- a graph.
     - s -- the source vertex.

     OUTPUT:

     - (d,p) -- pair of dictionaries keyed on the list of vertices,
       which store the distance and shortest paths.

     REFERENCE:

     http://en.wikipedia.org/wiki/Bellman-Ford_algorithm
     """
     P = []
     dist = {}
     predecessor = {}
     V = Gamma.vertices()
     E = Gamma.edges()
     for v in V:
         if v == s:
             dist[v] = 0
         else:
             dist[v] = infinity
         predecessor[v] = 0
     for i in range(1, len(V)):
         for e in E:
             u = e[0]
             v = e[1]
             wt = e[2]
             if dist[u] + wt < dist[v]:
                 dist[v] = dist[u] + wt
                 predecessor[v] = u
     # check for negative-weight cycles
     for e in E:
         u = e[0]
         v = e[1]
         wt = e[2]
         if dist[u] + wt < dist[v]:
             raise ValueError("Graph contains a negative-weight cycle")
     return dist, predecessor
 }}}

 And some examples:

 {{{
 sage: M = matrix([[0,1,4,0], [0,0,1,5], [0,0,0,3], [0,0,0,0]])
 sage: G = Graph(M, format="weighted_adjacency_matrix")
 sage: bellman_ford(G, G.vertices()[0])
   {0: 0, 1: 1, 2: 2, 3: 5}
 }}}

 and

 {{{
 sage: M = matrix([[0,1,0,0],[1,0,-4,1],[1,1,0,0],[0,0,1,0]])
 sage: G = DiGraph(M, format = "weighted_adjacency_matrix")
 sage: bellman_ford(G, G.vertices()[0])
 ---------------------------------------------------------------------------
 ...
 ValueError: Graph contains a negative-weight cycle
 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/8714#comment:1>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to