简体   繁体   中英

Dijkstra's algorithm - adjacency matrix and list

I've got strange problem with my Dijkstra's implementation... I have 2 algorithms, one for adjacency matrix and the second one for adjacency list. They are almost identical and only difference is with passing numbers from these structures.

I keep numbers from matrix in simple two-dimensional matrix called weightmat. Numbers from list are kept in array of lists called nbhlist. Lists are composed from structs called ListNode.

struct ListNode{    
        int number;             
        int weight;         
        ListNode* next;             
        ListNode(){                         
            number = weight = 0;
            next = 0;
        }
    };

And few general variables: vertex (number of vertices), edge (number of edges), vstart (number of start vertex).

Now code of Dijkstra's algorithm for matrix:

typedef vector<vector<pair<int, float> > > Graph;
struct Compare{
    int operator() (const pair<int,float>& p1, const pair<int,float>& p2)
    {
        return p1.second > p2.second;
    }
};

vector<float> d(vertex);
vector<int> parent(vertex);

for (int i = 0; i < vertex; i++){
    d[i] = numeric_limits<float>::max();
    parent[i] = -1;
}

priority_queue<pair<int, float>, vector<pair<int, float> >, Compare> MatQueue;

d[vstart] = 0;
MatQueue.push(make_pair(vstart, d[vstart]));
while (!MatQueue.empty()){
    int u = MatQueue.top().first;
    if (u == vertex - 1) break;
    MatQueue.pop();
    for (int i = 0; i < vertex; i++){
        if (weightmat[u][i] != 0){
            int v = i;
            float w = weightmat[u][i];
            //cout << "U " << u << "Number " << i << " Weight " << weightmat[u][i] << endl;
            if (d[v]> d[u] + w){
                d[v] = d[u] + w;
                parent[v] = u;
                MatQueue.push(make_pair(v, d[v]));
            }
        }
    }
}

vector<int> path;
path.clear();
int p = vertex - 1;
path.push_back(vertex - 1);
while (p != vstart)
{
    p = parent[p];
    path.push_back(p);
}
for (int i = path.size()-1; i >=0; i--){
    cout << path[i] << "->";
}

And this is code of Dijkstra's algorithm for my lists:

typedef vector<vector<pair<int, float> > > Graph;

    struct Compare{
        int operator() (const pair<int, float>& p1, const pair<int, float>& p2)
        {
            return p1.second > p2.second;
        }
    };

    vector<float> d(vertex);
    vector<int> parent(vertex);

    for (int i = 0; i < vertex; i++){
        d[i] = numeric_limits<float>::max();
        parent[i] = -1;
    }

    priority_queue<pair<int, float>, vector<pair<int, float> >, Compare> MatQueue;

    d[vstart] = 0;
    MatQueue.push(make_pair(vstart, d[vstart]));

    ListNode* hand = new ListNode;

    while (!MatQueue.empty()){
        int u = MatQueue.top().first;
        if (u == vertex - 1) break;
        MatQueue.pop();
        hand = NbhList[u];
        while (hand){
            int v = hand->number;
            float w = hand->weight;
            //cout << "U " << u << "Number " << v << " Weight " << w << endl;
            hand = hand->next;
            if (d[v] > d[u] + w){
                d[v] = d[u] + w;
                parent[v] = u;
                MatQueue.push(make_pair(v, d[v]));
            }
        }
    }


    vector<int> path;
    path.clear();
    int p = (vertex-1);
    path.push_back(p);
    while (p != vstart)
    {
        p = parent[p];
        path.push_back(p);
    }
    cout << endl << endl;
    for (int i = path.size() - 1; i >= 0; i--){
        cout << path[i] << "->";
    }

As I said, they are almost identical. Only difference:

MatQueue.pop();
        hand = NbhList[u];
        while (hand){
            int v = hand->number;
            float w = hand->weight;
            //cout << "U " << u << "Number " << v << " Weight " << w << endl;
            hand = hand->next;
            if (d[v] > d[u] + w){
                d[v] = d[u] + w;
                parent[v] = u;
                MatQueue.push(make_pair(v, d[v]));
            }
        }

And:

MatQueue.pop();
        for (int i = 0; i < vertex; i++){
            if (weightmat[u][i] != 0){
                int v = i;
                float w = weightmat[u][i];
                //cout << "U " << u << "Number " << i << " Weight " << weightmat[u][i] << endl;
                if (d[v]> d[u] + w){
                    d[v] = d[u] + w;
                    parent[v] = u;
                    MatQueue.push(make_pair(v, d[v]));
                }
            }
        }

My problem is - they give me sometimes different outputs and I have no idea why. Could you help me find my problem?

One possible bug is that in your ListNode struct:

struct ListNode{    
        int number;             
        int weight;         
        ListNode* next;             
        ListNode(){                         
            number = weight = 0;
            next = 0;
        }
    };

weight is an int , but your weights are float s according to the rest of your code, which might result in unwanted truncation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM