c++ - Floyd-Warshall 算法实现不起作用

标签 c++ algorithm

我正在为一项作业实现 Floyd-Warshall 算法,但输出矩阵不正确。我已经在网上与其他人仔细检查了我的算法,它看起来和其他人一样。我只是错过了什么吗?感谢您的帮助。

我的输入文件是:

4

 0  2  2 -1

-1  0  2  3

-1 -1  0  2

-1 -1 -1  0

结果矩阵应该是:

 0  2  2  4

-1  0  2  3

-1 -1  0  2

-1 -1 -1  0

路径矩阵应该是:

0 0 0 3

0 0 0 0

0 0 0 0 

0 0 0 0 

我收到了我的结果矩阵:

-4 -3 -2 -5

-5 -4 -3 -6

-6 -5 -4 -7

-7 -6 -5 -8

对于路径矩阵:

4 4 4 4

4 4 4 4

4 4 4 4

4 4 4 4

这是我程序的代码:

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

using namespace std;

// Forward Declarations
void readFile();
void fillPathWithZeros();
void findFloydsAlgorithmMatrix();
void printGraph(vector< vector<int> >& inVector, string heading);

int size;
vector< vector<int> > M; // Matrix with weights
vector< vector<int> > P; // Path Matrix

int main()
{
    readFile();
    fillPathWithZeros();
    findFloydsAlgorithmMatrix();

    cin.get();
    return 0;
}

void readFile()
{
    int z = 0;

    ifstream file("example.txt", fstream::in); // File name  is example.txt
    if (file.is_open())
    {
        // read characters by using "file >>" 
        file >> z;
        size = z;
        vector< vector<int> > fileGraph(size, vector<int>(size)); // creates a local 2D vector to size stated in file
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                file >> fileGraph[i][j];
            }
        } 

        M = fileGraph; // Copies the local vector to a global vector to be used in other functions
        printGraph(fileGraph, "Input matrix M: "); // Print out the input Matrix
    }


}

// Initialize the Path Vector with all 0's
void fillPathWithZeros()
{
    vector< vector<int> > localPathGraph(size, vector<int>(size));

    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++) {
            localPathGraph[i][j] = 0;
        }
    }

    P = localPathGraph; // set the local Path Vector to the Global Path Vector
}

void findFloydsAlgorithmMatrix()
{

    for (int k = 0; k < size; k++) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++){
                    if ( ( M[i][k] + M[k][j] ) < M[i][j] ) {
                        M[i][j] = M[i][k] + M[k][j];
                        P[i][j] = k + 1;
                    }
            }
        }
    }

    printGraph(M, "Resultant matrix M: ");
    printGraph(P, "Path matrix P: ");

}

void printGraph(vector< vector<int> >& inVector, string heading){

    cout << heading << endl;

    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++) {
            cout << setw(2) << inVector[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

最佳答案

矩阵中有负距离。 Floyd-Warshall 不适用于负距离或周期。

参见此处:http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm

就您的代码而言,从算法的角度来看它看起来不错。有些事情可以用更短的代码完成(例如 vector 的初始化),但除此之外,如果矩阵没有负距离/周期,代码应该可以工作。

关于c++ - Floyd-Warshall 算法实现不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22165460/

相关文章:

c++ - 如何获取 Ascii 格式 llvm 的指针类型?

c++ - 为什么 C++ 不允许函数参数用于后一个参数的默认值?

c++ - priority_queue、迭代器和排序

java - 使用 ArrayList 在 Java 中递归生成二进制字符串

algorithm - 找到给定 K 个最佳候选者的时间戳

c++ - 查找两个字符串是否在O(n)中是字谜-使用XOR的解决方案

C++ [错误] 没有匹配的调用函数

c++ - 为什么有些共享库以 d.so 而不是 .so 结尾?

c++ - 如何实现运算符>>(std::istream&, std::array<char, N>&)?

algorithm - 算法的最坏情况时间复杂度与其上限之间的关系/区别是什么?