簡體   English   中英

Floyd-Warshall算法實現不起作用

[英]Floyd-Warshall algorithm implementation not working

我正在為分配執行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

就您的代碼而言,從算法角度來看似乎還可以。 有些事情可以用較短的代碼來完成(例如向量的初始化),但除此之外,如果矩陣沒有負距離/周期,則代碼應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM