{"id":97,"date":"2011-05-15T03:31:22","date_gmt":"2011-05-14T21:31:22","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=97"},"modified":"2011-05-15T03:31:22","modified_gmt":"2011-05-14T21:31:22","slug":"bellman-ford-algorithm","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=97","title":{"rendered":"Bellman-Ford Algorithm"},"content":{"rendered":"<p>The Bellman-Ford algorithm solves the single-source shortest-paths problem in the general case in which edge weights may be negative. Given a weighted, directed graph G = (V, E) with source s and weight function w : E \u2192 R, the Bellman-Ford algorithm returns a Boolean value indicating whether or not there is a negative-weight cycle that is reachable from the source. If there is such a cycle, the algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortest paths and their weights. The algorithm uses relaxation, progressively decreasing an estimate d[v] on the weight of a shortest path from the source s to each vertex v \u2208 V until it achieves the actual shortest-path weight \u03b4(s, v). The algorithm returns TRUE if and only if the graph contains no negative-weight cycles that are reachable from the source.<\/p>\n<p>To understand this algorithm, we must know about the storing technique of graph.<\/p>\n<p><strong>The Algorithm<\/strong><br \/>\nThe Bellman-Ford algorithm is one of the classic solutions to this problem. It calculates the shortest path to all nodes in the graph from a single source.<\/p>\n<p>The basic idea is simple:<br \/>\nStep 1: Start by considering that the shortest path to all nodes, less the source, is infinity. Mark the length of the path to the source as 0:<\/p>\n<p>Step 2: Take every edge and try to relax it:<\/p>\n<p><strong>Relaxing <\/strong> an edge means checking to see if the path to the node the edge is pointing to can\u2019t be shortened, and if so, doing it.<br \/>\nNow, we apply the previous step n \u2013 1 times, where n is the number of nodes in the graph. <\/p>\n<p>For example <a href=\"http:\/\/compprog.wordpress.com\/2007\/11\/29\/one-source-shortest-path-the-bellman-ford-algorithm\/\" title=\"Click here \" target=\"_blank\">Click here<\/a> <\/p>\n<p>Here is the condensed form of the algorithm :<br \/>\n[code]<br \/>\nvoid bellman_ford(int s) {<br \/>\n        int i, j;<\/p>\n<p>        for (i = 0; i &lt; n; ++i)<br \/>\n                d[i] = INFINITY;<\/p>\n<p>        d[s] = 0;<\/p>\n<p>        for (i = 0; i &lt; n &#8211; 1; ++i)<br \/>\n                for (j = 0; j &lt; e; ++j)<br \/>\n                        if (d[edges[j].u] + edges[j].w &lt; d[edges[j].v])<br \/>\n                                d[edges[j].v] = d[edges[j].u] + edges[j].w;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>***the structure is<br \/>\n[code]<br \/>\ntypedef struct{<br \/>\nint u,v,w;<br \/>\n}Edge;<br \/>\nEdge edges[1000];<br \/>\n[\/code]<\/p>\n<p>It is seen that the working principle of Bellman-Ford algorithm is exactly same as another single source shortest path Dijkstra&#8217;s algorithm. Then what is the difference between them?<\/p>\n<p>Yes, their working principle is same but Dijkstra&#8217;s algorithm fails if there exists any negative valued weight or negative cycle. But Bellman-Ford algorithm is capable to determine the negative cycle in the graph and finds the shortest path even with negative valued weights.  <\/p>\n<p>Here is an real life example of bellman-Ford example . <a href=\"http:\/\/uva.onlinejudge.org\/external\/5\/558.html\" title=\"UVA 558\" target=\"_blank\">UVA 558<\/a><\/p>\n<p>[code]<br \/>\n\/\/warmholes 558<br \/>\n#include &lt;stdio.h&gt;<\/p>\n<p>typedef struct {<br \/>\n\tint u, v, w;<br \/>\n} Edge;<\/p>\n<p>int n; \/* the number of nodes *\/<br \/>\nint e; \/* the number of edges *\/<br \/>\nEdge edges[2500]; \/* large enough for n &lt;= 2^5=32 *\/<br \/>\nint d[1500]; \/* d[i] is the minimum distance from node s to node i *\/<\/p>\n<p>#define INFINITY 1000000<\/p>\n<p>void printDist() {<br \/>\n\tint i;<br \/>\n\tprintf(&quot;Distances:n&quot;);<br \/>\nfor (i = 0; i &lt; n; ++i)<br \/>\n\t\tprintf(&quot;to %dt&quot;, i );<br \/>\n\tprintf(&quot;n&quot;);<\/p>\n<p>for (i = 0; i &lt; n; ++i)<br \/>\n\t\tprintf(&quot;%dt&quot;, d[i]);<\/p>\n<p>\tprintf(&quot;nn&quot;);<br \/>\n}<\/p>\n<p>void bellman_ford(int s){<br \/>\n\tint i, j;<\/p>\n<p>for (i = 0; i &lt; n; ++i)<br \/>\n\t\td[i] = INFINITY;<\/p>\n<p>\td[s] = 0;<\/p>\n<p> for (i = 0; i &lt; n-1; ++i)<br \/>\n\tfor (j = 0; j &lt;e; ++j){<br \/>\n\t\tif (d[edges[j].u] + edges[j].w &lt; d[edges[j].v])<br \/>\n\t\t\td[edges[j].v] = d[edges[j].u] + edges[j].w;<br \/>\n}<\/p>\n<p>\/\/\/\/these 3 lines for the 558\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\nint f=0;<br \/>\n for (int j = 0; j &lt; e; j++)<br \/>\n        if (d[edges[j].v] &gt; d[edges[j].u] + edges[j].w) {<br \/>\n                            f=1;break;<br \/>\n                             }<\/p>\n<p>if(f==1)<br \/>\nprintf(&quot;possiblen&quot;);<br \/>\nelse<br \/>\nprintf(&quot;not possiblen&quot;);<\/p>\n<p>}<\/p>\n<p>int main() {<br \/>\n\tint i,p,j,k,c,l,m,kase;<br \/>\n\tint w;<\/p>\n<p>scanf(&quot;%d&quot;,&amp;kase);<br \/>\nfor(c=0;c&lt;kase;c++){<\/p>\n<p>scanf(&quot;%d %d&quot;,&amp;n,&amp;e);<br \/>\np=0;<\/p>\n<p>for(i=0;i&lt;e;i++){<br \/>\n    scanf(&quot;%d %d %d&quot;,&amp;j,&amp;k,&amp;l);<br \/>\n   edges[i].u=j;<br \/>\n   edges[i].v=k;<br \/>\n   edges[i].w=l;<br \/>\n}<br \/>\nbellman_ford(0);<br \/>\n\/\/printDist();<br \/>\n}<br \/>\n\treturn 0;<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p>References:<br \/>\n&#8220;Introduction to algorithms &#8221; by Coreman<br \/>\n<a href=\"http:\/\/compprog.wordpress.com\/2007\/11\/29\/one-source-shortest-path-the-bellman-ford-algorithm\/\" title=\"http:\/\/compprog.wordpress.com\/2007\/11\/29\/one-source-shortest-path-the-bellman-ford-algorithm\/\" target=\"_blank\">http:\/\/compprog.wordpress.com\/2007\/11\/29\/one-source-shortest-path-the-bellman-ford-algorithm\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Bellman-Ford algorithm solves the single-source shortest-paths problem in the general case in which edge weights may be negative. Given a weighted, directed graph G = (V, E) with source s and weight function w : E \u2192 R, the Bellman-Ford algorithm returns a Boolean value indicating whether or not there is a negative-weight cycle &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.ujjal.net\/?p=97\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bellman-Ford Algorithm&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-graph"],"_links":{"self":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97"}],"version-history":[{"count":0,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}