Bellman_Ford implementation

Bellman Ford implementation


#include <iostream>
#include <limits.h>
#include <list>
#include <vector>

using namespace std;

void Bellman_Ford(vector<pair<pair<int, int>, int> > &edges, int numVertex, vector<vector<int> > &shortest/*result*/)
{
    int none = -1;
    if (numVertex == 0) {
        return;
    }
    if (shortest.size() != numVertex) {
        return;
    }
    for (int i = 0; i < numVertex; i++) {
        if ((shortest[i].size()) != numVertex)
            return;
    }

    // initialize shortest
    for (int i = 0; i < numVertex; i++) {
        for (int j = 0; j < numVertex; j++) {
            if (i == j) {
                shortest[i][j] = 0;
            } else {
                shortest[i][j] = INT_MAX;
            }
        }
    }

    bool globalChanged = true;
    for (int round = 0; round < numVertex; round++) {
        globalChanged = false;
        printf("round = %d\n", round);
        for (int vertex = 0; vertex < numVertex; vertex++) {
            for (int i = 0; i <= edges.size(); i++) {
                int start = edges[i].first.first;
                int end   = edges[i].first.second;
                if (shortest[vertex][start] != INT_MAX) {
                    if ((shortest[vertex][start] + edges[i].second) < shortest[vertex][end]) {
                        shortest[vertex][end] = (shortest[vertex][start] + edges[i].second);
                        globalChanged = true;
                    }
                }
            }
        }
        if (!globalChanged) {
            printf("!globalChanged\n");
            break;
        }
    }


    for (int i = 0; i < numVertex; i++) {
        for (int j = 0; j < numVertex; j++) {
            if (shortest[i][j] != INT_MAX)
                printf("%2d  ", shortest[i][j]);
            else
                printf("%2d  ", none);
        }
        printf("\n");
    }

    return;
}

// driver program to test above function
int main()
{
    /* Let us create the example graph discussed above */
    int input[9][9] = {{ 0,  4, -1, -1, -1, -1, -1,  8, -1},
                      { 4,  0,  8, -1, -1, -1, -1, 11, -1},
                      {-1,  8,  0,  7, -1,  4, -1, -1,  2},
                      {-1, -1,  7,  0,  9, 14, -1, -1, -1},
                      {-1, -1, -1,  9,  0, 10, -1, -1, -1},
                      {-1, -1,  4, 14, 10,  0,  2, -1, -1},
                      {-1, -1, -1, -1, -1,  2,  0,  1,  6},
                      { 8, 11, -1, -1, -1, -1,  1, -1,  7},
                      {-1, -1,  2, -1, -1, -1,  6,  7,  0}
                     };

    vector<pair<pair<int, int>, int> > edges;
    vector<vector<int> > result;
    for (int i = 0; i < 9; i++) {
        vector<int> temp(9, 0);
        result.push_back(temp);
        for (int j = 0; j < 9; j++) {
            if (input[i][j] > 0) {
                edges.push_back(make_pair(make_pair(i, j), input[i][j]));
            }
        }
    }

    Bellman_Ford(edges, result.size(), result);
    cout << "\n";
    return 0;
}

留言

這個網誌中的熱門文章

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/longest-univalue-path/

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/maximum-sum-of-3-non-overlapping-subarrays/