#8714: add Bellman-Ford algorithm for shortest paths
--------------------------------+-------------------------------------------
       Reporter:  mvngu         |         Owner:  jason, ncohen, rlm
           Type:  enhancement   |        Status:  new               
       Priority:  major         |     Milestone:  sage-5.1          
      Component:  graph theory  |    Resolution:                    
       Keywords:                |   Work issues:                    
Report Upstream:  N/A           |     Reviewers:                    
        Authors:                |     Merged in:                    
   Dependencies:  #12806        |      Stopgaps:                    
--------------------------------+-------------------------------------------
Changes (by dcoudert):

 * cc: dcoudert (added)


Comment:

 Hello,

 I have checked the networkx implementation of the Bellman-Ford algorithm
 
([https://networkx.lanl.gov/trac/browser/networkx/networkx/algorithms/shortest_paths/weighted.py
 See here]) and we can propose a better implementation.

 This is a first implementation that can certainly be improved. Its
 advantage is that in the best case the time complexity is in O(|V|+|E|)
 and in the worst case, it is O(|V|.|E|). It uses a set to maintain the set
 of active vertices, that is vertices for which a change has been performed
 during previous round.
 {{{
 def bellman_ford(G, s):
     """
     some documentation
     """
     from sage.rings.infinity import Infinity
     P = []
     dist = {}
     predecessor = {}
     V = G.vertices()
     N = G.num_verts()
     E = G.edges()
     for v in V:
         if v == s:
             dist[v] = 0
         else:
             dist[v] = Infinity
         predecessor[v] = 0
     W = {}
     for e in E:
         W[(e[0],e[1])] = e[2]
         W[(e[1],e[0])] = e[2]
     A = set([s])
     B = set()
     cpt = 0
     while len(A) > 0 and cpt < N:
         while len(A) > 0:
             u = A.pop()
             for v in G.neighbor_iterator(u):
                 if dist[u] + W[(u,v)] < dist[v]:
                     dist[v] = dist[u] + W[u,v]
                     predecessor[v] = u
                     B.add(v)
         A = B.copy()
         B.clear()
         cpt += 1
     # 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
 }}}

 The implementation can be adapted to graphs and digraphs.

 Let me know if you think it is a good idea to write this patch.

 Best,
 D.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/8714#comment:4>
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