Bellman-Ford C ++ Algorithm Implementation

I am currently working on homework to implement the Bellman-Ford algorithm. So far, I have managed to read the provided graph, put it in a vector (using a 1d vector to represent 2d with string order) for use as a matrix. I use a structure that tracks the weight of the edge, a boolean for infinity (no edge), and a variable to track the previous node.

Being at a dead end actually implements the dang algorithm. I read the pseudocode from http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm , but it’s hard for me to learn how to use the algorithm. The first 80 lines are read in the file, initializing the vector, tossing the values ​​into the vector in the right place. Below I started to implement the algorithm. I have a few specific questions.

1) In all the explanations of the algorithm that I found, you work the algorithm No Nodes - 1 time. In some of the implementations of this, I looked, I usually start with 1. Why is this? Also, with my implementation, does this still make sense?

2) Further, in the wikipedia pseudo-code, it is said to check every edge u, v, where u is the initial vertex and v is the final vertex. In my matrix, as I understand it, this means that I need to check the weight / value of each row, a pair of columns and see if there is a better way. I'm ... not sure if I am explaining this correctly or even understanding it as this moment. Any tips / tricks / hints / demos would be highly appreciated. The source code, followed by the groove of the demo input of the instructor, is below.

#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

struct graphNode
{
    int value; //Weight of the edge
    bool isInfinity; //Boolean to flag an edge as infinity
    int pred; //predecessor node
};

// Code for reading inputfile cribbed and modified from http://stackoverflow.com/questions/7651243/c-read-a-file-name-from-the-command-line-and-utilize-it-in-my-file
int main(int argc, char** argv) 
{
    ifstream input; // ifstream for the input
    string inFile = ""; //name of the input file
    int row; //Variable to keep track of what row we're inputting data for
    int col; //Variable to keep track of a column thingie, expand on this later
    int infinity = 99999999;
    int nodeCount; //Number of nodes from input file
    int edgeCount = 0; //Number of edges from the input file

    vector<graphNode> edgeList; //2D list of edges, order is a->b
    edgeList.push_back(graphNode());
    edgeList[0].value = 0;
    edgeList[0].isInfinity = false;
    edgeList[0].pred = -1;

    if( argc == 2 ) 
    {
        inFile = argv[1];
    }
    else 
    {
        cout << "Usage: ./a.out inputFile\n";
        return 1;
    }

    input.open(inFile.c_str()); // opening the provided file

    if(input.is_open()) // making sure the input is open
    {
        input >> nodeCount; //Grabbing the number of nodes from the first value of the file

        for(int i = 1; i < nodeCount*nodeCount; i++)
        {
            edgeList.push_back(graphNode());
            edgeList[i].value = infinity;
            edgeList[i].isInfinity = true;
            edgeList[i].pred = -1;
        }

        //Putting data from the file into the vector array
        while(!input.eof())
        {
            input >> row; //For each cycle through the list, we grab the first number on the line to get which x value (start vertex) we're working with
            while(input.peek() != '\n' && input.peek() != '\r' && !input.eof())
            {
                input >> col;
                input >> edgeList[((row-1)*nodeCount)+(col-1)].value;
                edgeList[((row-1)*nodeCount)+(col-1)].isInfinity = false;
                edgeList[((row-1)*nodeCount)+(col-1)].pred = row;
                edgeCount++;
            }

        }
        input.close(); //Closing our input file since we don't need it anymore
    }
    else
    {
        cout << "Error, something happened with the input." << endl;
        return 1;
    }

    //for(int i = 0; i < nodeCount - 1; i++)
    //{
        //for(int r = 0; r < nodeCount - 1; r++)
        //{
            //for(int c = 0; r < nodeCount - 1; c++)
            //{
                //if(r == c) continue;
                //if(edgeList[r][c].isInfinity) continue;
                //if(edgeList[i][r] + edgeList[r][c] < edgeList[c][i])
}

Demo Input:

10
3 6 4 9 0 7 8
8 5 3 7 3 4 -2
5 10 2 8 1 4 1
2 6 -3 1 3 7 1
1 10 -1 2 2 4 -2
10 9 -3 1 3 7 2 5 1
7 3 0 10 1 2 1 8 2
9 6 6 3 4 10 7
4 8 5 1 9 5 6
6 2 4 3 0 9 0
+5
source share
2 answers

For # 1, you study edges between nodes, so the longest path cannot be greater than edgeCount-1. For example, if nodeCount == 1, then there is no need to examine any edges.

# 2 . , . "graphNode" "graphEdge", "pred". :

vector<int> predecessor;
vector<int> distance;

nodeCount. "W", , .

, , nodeCount . nodeCount-1, . , , . edgeList [(r-1) * nodeCount + c].

, , .

+2

-. , : https://class.coursera.org/algo2-2012-001/lecture/preview

Bellman-Ford :

, , s t, s t, 1 , .

k, k , s t

  • k
  • ,    , usea (k - 1) .

, k:

d [k] [t] - s a node t, k . :

d[ k ][ t ] = min{ 
d[k - 1][ t ], # Case 2
for all edges (u, t) in graph min d[k - 1][ u ] + c[ u ][ t ] # Case 1
}

min {.,.} s u t c [u] [t], u t, k .

, :

d[s][0] = 0 # The cost from s to itself requiring zero edges is 0.
d[u][0] = INFINITY for all other nodes other than s (i.e. you cannot reach them with no edges).

Let n = number of nodes in graph
for k = 1 to n - 1: # Allow one more edge in path in each iteration
  for every edge (u, v) in edges of graph: # Check whether can get a better cost to v using k edges by going through node u or k - 1 edges is enough.
    d[ v ][ k ] = min{ d[k - 1][ v ], d[k - 1][ u ] + c[ u ][ v ] }

1 , , . 1 n - 1, n - . , n - 1, , , , (n - 1) (.. n , s t, n - 1 ). ​​

2 , node , k - 1 PLUS node node , , .

, ( wiki), . , n - 1 . . , , 1 , , . , .

. , , .

+2

All Articles