实现FordAlgorithm计算机的最短路径

时间:2017-11-13 19:37:45

标签: algorithm bellman-ford

以下代码来自FordAlgorithm,它将找到从源到另一个节点(或目标)的最短路径。

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <list>
#include <stack>
using namespace std;


struct wde{ // a weighted directed edge
    char s, d;
    int w;
};

const int INF = 9999;
char noPred = ' ';
struct label{
    char vertex;
    int currDist;
    char predecessor;
    label(){}
    label(const label& other){
        cout << "Copy constructor called\n";
        vertex = other.vertex;
        currDist = other.currDist;
        predecessor = other.predecessor;
    }
    label(char v, int d, char p) :vertex(v), currDist(d), predecessor(p){}
    bool operator==(const label other){
        return vertex == other.vertex;
    }
};

class compareLabels{
public:
    bool operator() (label* lhs, const label* rhs) const{
        return (lhs->currDist < rhs->currDist);
    }
};
list<label*> myqp;

wde EL[] = { // 12 edges (we were originally missing a->h
    { 'c', 'd', 1 }, { 'c', 'g', 1 }, { 'c', 'h', 1 },
    { 'h', 'g', -1 }, { 'g', 'd', -1 }, { 'd', 'a', 2 },
    { 'd', 'e', 4 }, { 'd', 'i', 1 }, { 'a', 'b', 1 },
    { 'b', 'e', -5 }, { 'e', 'f', 4 }, { 'i', 'f', 1 }};

int infAdd(int a, int b){
    if (a == INF || b == INF)
        return INF;
    else
        return a + b;
}
void ShortestPath(int ne, wde elist[], char first){
    int i, ui, nv, wt, dist1, dist2, dist3;
    // Determine the set of vertices from the edge list
    set<char> vertices;
    for (i = 0; i < ne; i++){ //look at each edge
        vertices.insert(elist[i].s);
        vertices.insert(elist[i].d);
    }
    cout << "We found " << (nv = vertices.size()) << " vertices\n";

    // Set up the adjacency matrix
    vector<vector<int>> adjmat(nv, vector<int>(nv, 0));
    const int asa = int('a'); // char 'a' as an ascii value

    // Set up the labels and store in a 'priority queue'
    vector<label> labels(nv);
    for each (char ch in vertices){
        labels[int(ch) - asa] = label(ch, INF, noPred);
    }


    labels[int(first) - asa].currDist = 0;
    list<label*> toBeChecked; // pointers to labels
    //toBeChecked.assign = first;

    for (i = 0; i < ne; i++){
        wde &e = elist[i]; // refer to elist[i]
        adjmat[int(e.s) - asa][int(e.d) - asa] = e.w;
    }

我认为问题从这里开始:

    // FordAlgorithm*******************************************
    for (i = 1; i <= nv - 1; i++){
        dist1 = labels[first].currDist;
        for (ui = 0; ui < nv; ui++){

            dist2 = labels[ui].currDist;
            if ((wt = adjmat[first][ui]) != 0)
            {
                dist3 = infAdd(dist1, wt);
                if (dist2 > dist3){
                    labels[ui].currDist = dist3;
                }
            }
        }
    }
cout << "The shortest distances from " << first << " to each vertex:\n";
    stack<char> path;
    char v;
    for (i = 0; i < nv; i++){  // each of the destinations
        cout << labels[i].vertex << " " << labels[i].currDist << endl;
        v = labels[i].vertex;
        // put the code you're given
        // in the first while loop, we follow the predecssors back
        // from the "sink" to the "source"
    }
}


int main(void){
    int nedges = sizeof(EL) / sizeof(wde);
    cout << "We have " << nedges << " edges\n";
    ShortestPath(nedges, EL, 'c');
    cout << "done\n";
    return 0;
}

该程序已成功构建。但是,它有错误:调试时矢量下标超出范围。你们有什么想法为什么会这样吗?感谢。

0 个答案:

没有答案