Code Examples
  Home arrow Code Examples arrow Page 3 - 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-Roy Floyd Technique


    (Page 3 of 4 )

     

    The previous algorithm's goal was to determine the shortest path from a source vertex to all the other vertexes. Now we are going to calculate the shortest paths between all the nodes inside a graph. The technique, also variously known as Floyd's algorithm, the Roy-Warshall algorithm, the Roy-Warshall algorithm, or the WFI algorithm is an extension of the Bellman Ford algorithm to the whole graph and all the vertexes.

    The only prerequisite of this algorithm remains that the graph should not contain any negative circuits. You can perceive the algorithm as similar to the earlier approach, but now we are going to build n trees simultaneously, and each one of them will contain in the end the shortest path from a vertex to all the others.

    We will switch our array of distances to two-dimensional arrays that at the start will be the same as the weight matrix. Therefore, we will guesstimate that the shortest road between two vertexes is the edge connecting them if it exists, infinity if there are no edges connecting and zero between the same vertexes.

    This is the initial start of the matrix. Now on this matrix we are going to make the approach n times, which is similar to how we made it with of Bellman-Ford. At step k, we are going to build the distance matrix from the k-1 state. Starting from the simple, trivial solution, we are going to create a more complex solution.

    This method is well known under the name of the dynamic programming technique. At each step, we will check if the "k" vertex is on the shortest road between "i" and "j". If the answer is yes, we do not need to make any further approximations; otherwise, we will make this part of the road by adding the current known shortest road between "i"-"k" and "k"-"j".

    This is true because the shortest road is built from the sub-shortest roads. In addition, if we know the shortest road between edges "i"-"k" and "k"-"j" then we can conclude that "i"-"j" is the shortest road between node "i" and "j".

    We need to repeat this n times, and the solution is complete. At each step, at least one shortest road will be determined, so at the end of n steps, all of the graph's shortest roads should be calculated.

    The complexity of all this, due to the fact that we make n iterations on the distance matrix, which is also of a size n X n, will be O (n^3). This is quite bad, so unless you need all of the roads and shortest paths, avoid using it. We can determine whether we have negative circles in the graph in two ways.

    We can observe that if, on the main diagonal after the n iteration, we can find a negative number (meaning that there is a road between "i" and "i" less than zero), we have a negative circle. Or, as in the case of the Bellman-Ford algorithm, we can run the approximation approach again and observe whether or not another change is made. The code snippet should look like this:

    void RoyFloyd ()

    {

    int i =0;

    int j = 0;

    int k = 0;

     

    // initialize the priorAncient array

    // Assign the default distances we estimate

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

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

    {

    if (weightMatrix[i] [j] == INFINITY || i == j)

    priorAncientV[i] [j] = 0;

    else

    priorAncientV[i] [j] = i;

    distance[i] [j] = weightMatrix[i] [j];

    }

     

     

     

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

    {

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

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

    if (i!=k && j!=k)

    if (distance[i] [k] + distance[k][j] < distance[i][j] && distance[i] [k] != INFINITY

    && distance[k] [j]!= INFINITY)

    // INFINITY plus/minus something is still infinity

    {

    // make the approximation

    distance[i] [j] = distance[i][k] + distance[k][j];

    priorAncientV[i] [j] = priorAncientV[k] [j];

    }

    }

     

    }

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek