我目前正在做一个家庭作业来实现贝尔曼福特算法。到目前为止,我已经设法读取提供的图表,将其放入一个向量中(使用 1d 向量来表示具有行主要顺序的 2d 向量)以用作矩阵。我正在使用一个结构来跟踪边缘的权重,一个布尔值是否为无穷大(不存在边)和一个变量来跟踪前面的节点。
我被难住的实际上是实现 dang 算法。我已经阅读了来自http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm的伪代码,但我很难掌握如何使用该算法。前 80 行正在读取文件,初始化向量,将值扔到正确位置的向量中。下面是我开始为算法实现的内容。我确实有几个具体的问题。
1)在我找到的算法的所有解释中,您使用算法# of nodes - 1 次。在我看过的一些实现中,我倾向于从 1 开始。这是为什么呢?此外,通过我的实施,这仍然有意义吗?
2)在维基百科的伪代码中,它说要检查每条边 u,v,其中 u 是起始顶点,v 是结束顶点。在我的矩阵中,据我所知,这意味着我需要检查每行、列对的权重/值,看看是否有更好的路径。我......不确定我是否正确地解释了这一点,甚至是否理解这一点。任何建议/指导/提示/示范将不胜感激。下面是源代码和教师演示输入的粘贴。
#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])
}
演示输入:
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