Code Examples
  Home arrow Code Examples arrow Page 2 - The Bellman-Ford and Roy-Floyd Algorithms
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

The Bellman-Ford and Roy-Floyd Algorithms
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-05-28

    Table of Contents:
  • The Bellman-Ford and Roy-Floyd Algorithms
  • The Bellman-Ford Algorithm
  • The-Roy Floyd Technique
  • Printing the road

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Bellman-Ford and Roy-Floyd Algorithms - The Bellman-Ford Algorithm


    (Page 2 of 4 )

    The Bellman-Ford technique continues the gradual approximation approach. If you missed my last article, I invite you to search it up and read it, as I will not explain again what is this all about and how it will help us in understanding and solving the problem.

    While using the topological order-based approach and Dijkstra's Algorithm we found some ways to determine the right arrangement of the edges so that we may make the approximation when we need it. When a negative edge is also present in our graph, with circles whose sums remain positive, we can no longer find an order like this.

    Therefore, we can no longer guarantee a single approximation with the edges. We will take the list of the edges and call the approximation algorithm as many times as an approximation is made. When we try to improve our distance array with each of the edges, and none can be made, we have finished the shortest path algorithm.

    In the best-case scenario, this will lead to a single call. This is the case when all the edges are in the order they would be in the tree that builds the solution. This time, the solution is a tree whose root vertex is the source node from where we want to calculate the roads.

    However, what is the limit of the maximal call, so that we do not enter into an endless loop? At the first call, the algorithm will solve the shortest path with the length of one edge. Further on, this will find the shortest roads containing two edges, and so on.

    In the end, our tree must have n-1 edges, therefore executing the approximation through all edges n-1 times guarantees that we will get a good solution. If we do not know whether or not our input has  negative circles, we can perform the approximation attempt once again on the graph (n-th time), and if it makes a single approximation, this will reveal a negative circuit.

    The complexity is O (n*m), including the negative circuit fail-proof detection system. This will offer a result to any graph; however, this can turn out to be painfully slow.

    The complexity of the topological order-based approach is O (n+m), and Dijkstra's Algorithm is O (m*lg (n)), so there is little competition if you want to solve a problem as fast as possible, and you know extra information, like the graph's contents include no circle or no negative edges. The parent-saving mechanism remains, so with a simple recursion we can print the roads as well. Here is the code snippet:

    void BellmanFordAlgorithm (int root)

    {

    int i = 0;

    int l;

    int itwasMod = false;

     

    for (i = 1; i <=n; ++i)

    {

    distance[i] = INT_MAX/2;

    ancient[i] = 0;

    }

     

    distance [root] = 0;

    ancient [root] = 0;

    int k = 1;

    do

    {

    itwasMod = false;

     

    for (i = 1; i <= n; ++i)

    {

    for (l = 1; l <= n; ++l)

    {

    if (distance[i] + adiacentMatrix[i] [l] < distance[l]

    && distance[i] != INT_MAX/2

    && adiacentMatrix[i] [l] != INT_MAX/2) // Infinity plus/minus something is still infinity

    {

    ancient[l] = i;

    itwasMod = true;

    distance[l] = distance[i] + adiacentMatrix[i] [l];

    }

    }

    }

     

    } while (itwasMod && k<=n);

    if (itwasMod)

    printf ("Negative edge detected");

    }

     

    More Code Examples Articles
    More By Gabor Bernat


     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek