<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>bellman_ford — NetworkX 1.8.1 documentation</title> <link rel="stylesheet" href="../../_static/networkx.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.8.1', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: false }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within NetworkX 1.8.1 documentation" href="../../_static/opensearch.xml"/> <link rel="top" title="NetworkX 1.8.1 documentation" href="../../index.html" /> <link rel="up" title="Shortest Paths" href="../algorithms.shortest_paths.html" /> <link rel="next" title="negative_edge_cycle" href="networkx.algorithms.shortest_paths.weighted.negative_edge_cycle.html" /> <link rel="prev" title="dijkstra_predecessor_and_distance" href="networkx.algorithms.shortest_paths.weighted.dijkstra_predecessor_and_distance.html" /> </head> <body> <div style="color: black;background-color: white; font-size: 3.2em; text-align: left; padding: 15px 10px 10px 15px"> NetworkX </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="networkx.algorithms.shortest_paths.weighted.negative_edge_cycle.html" title="negative_edge_cycle" accesskey="N">next</a> |</li> <li class="right" > <a href="networkx.algorithms.shortest_paths.weighted.dijkstra_predecessor_and_distance.html" title="dijkstra_predecessor_and_distance" accesskey="P">previous</a> |</li> <li><a href="http://networkx.github.com/">NetworkX Home </a> | </li> <li><a href="http://networkx.github.com/documentation.html">Documentation </a>| </li> <li><a href="http://networkx.github.com/download.html">Download </a> | </li> <li><a href="http://github.com/networkx">Developer (Github)</a></li> <li><a href="../index.html" >Reference</a> »</li> <li><a href="../pdf_reference.html" >Reference</a> »</li> <li><a href="../algorithms.html" >Algorithms</a> »</li> <li><a href="../algorithms.shortest_paths.html" accesskey="U">Shortest Paths</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="networkx.algorithms.shortest_paths.weighted.dijkstra_predecessor_and_distance.html" title="previous chapter">dijkstra_predecessor_and_distance</a></p> <h4>Next topic</h4> <p class="topless"><a href="networkx.algorithms.shortest_paths.weighted.negative_edge_cycle.html" title="next chapter">negative_edge_cycle</a></p> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="bellman-ford"> <h1>bellman_ford<a class="headerlink" href="#bellman-ford" title="Permalink to this headline">¶</a></h1> <dl class="function"> <dt id="networkx.algorithms.shortest_paths.weighted.bellman_ford"> <tt class="descname">bellman_ford</tt><big>(</big><em>G</em>, <em>source</em>, <em>weight='weight'</em><big>)</big><a class="reference internal" href="../../_modules/networkx/algorithms/shortest_paths/weighted.html#bellman_ford"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#networkx.algorithms.shortest_paths.weighted.bellman_ford" title="Permalink to this definition">¶</a></dt> <dd><p>Compute shortest path lengths and predecessors on shortest paths in weighted graphs.</p> <p>The algorithm has a running time of O(mn) where n is the number of nodes and m is the number of edges. It is slower than Dijkstra but can handle negative edge weights.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>G</strong> : NetworkX graph</p> <blockquote> <div><p>The algorithm works for all types of graphs, including directed graphs and multigraphs.</p> </div></blockquote> <p><strong>source: node label</strong> :</p> <blockquote> <div><p>Starting node for path</p> </div></blockquote> <p><strong>weight: string, optional (default=’weight’)</strong> :</p> <blockquote> <div><p>Edge data key corresponding to the edge weight</p> </div></blockquote> </td> </tr> <tr class="field-even field"><th class="field-name">Returns :</th><td class="field-body"><p class="first"><strong>pred, dist</strong> : dictionaries</p> <blockquote> <div><p>Returns two dictionaries keyed by node to predecessor in the path and to the distance from the source respectively.</p> </div></blockquote> </td> </tr> <tr class="field-odd field"><th class="field-name">Raises :</th><td class="field-body"><p class="first"><strong>NetworkXUnbounded</strong> :</p> <blockquote class="last"> <div><p>If the (di)graph contains a negative cost (di)cycle, the algorithm raises an exception to indicate the presence of the negative cost (di)cycle. Note: any negative weight edge in an undirected graph is a negative cost cycle.</p> </div></blockquote> </td> </tr> </tbody> </table> <p class="rubric">Notes</p> <p>Edge weight attributes must be numerical. Distances are calculated as sums of weighted edges traversed.</p> <p>The dictionaries returned only have keys for nodes reachable from the source.</p> <p>In the case where the (di)graph is not connected, if a component not containing the source contains a negative cost (di)cycle, it will not be detected.</p> <p class="rubric">Examples</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">networkx</span> <span class="kn">as</span> <span class="nn">nx</span> <span class="gp">>>> </span><span class="n">G</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">path_graph</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">create_using</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">pred</span><span class="p">,</span> <span class="n">dist</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">bellman_ford</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">pred</span> <span class="go">{0: None, 1: 0, 2: 1, 3: 2, 4: 3}</span> <span class="gp">>>> </span><span class="n">dist</span> <span class="go">{0: 0, 1: 1, 2: 2, 3: 3, 4: 4}</span> </pre></div> </div> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">nose.tools</span> <span class="kn">import</span> <span class="n">assert_raises</span> <span class="gp">>>> </span><span class="n">G</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">cycle_graph</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">create_using</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">G</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">2</span><span class="p">][</span><span class="s">'weight'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">7</span> <span class="gp">>>> </span><span class="n">assert_raises</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">NetworkXUnbounded</span><span class="p">,</span> <span class="n">nx</span><span class="o">.</span><span class="n">bellman_ford</span><span class="p">,</span> <span class="n">G</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> </pre></div> </div> </dd></dl> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="networkx.algorithms.shortest_paths.weighted.negative_edge_cycle.html" title="negative_edge_cycle" >next</a> |</li> <li class="right" > <a href="networkx.algorithms.shortest_paths.weighted.dijkstra_predecessor_and_distance.html" title="dijkstra_predecessor_and_distance" >previous</a> |</li> <li><a href="http://networkx.github.com/">NetworkX Home </a> | </li> <li><a href="http://networkx.github.com/documentation.html">Documentation </a>| </li> <li><a href="http://networkx.github.com/download.html">Download </a> | </li> <li><a href="http://github.com/networkx">Developer (Github)</a></li> <li><a href="../index.html" >Reference</a> »</li> <li><a href="../pdf_reference.html" >Reference</a> »</li> <li><a href="../algorithms.html" >Algorithms</a> »</li> <li><a href="../algorithms.shortest_paths.html" >Shortest Paths</a> »</li> </ul> </div> <div class="footer"> © Copyright 2013, NetworkX Developers. Last updated on Oct 23, 2013. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>