简体   繁体   English

使用邻接矩阵的Dijkstra算法无法找到从每个节点到每个其他节点的正确距离/路径

[英]Dijkstra's Algorithm using Adjacency Matrix not finding correct distance/path from each node to every other node

I'm trying to write a program that builds a graph using an Adjacency Matrix and then finds the shortest path from each node to every other node using Dijkstra's Algorithm. 我正在尝试编写一个程序,该程序使用邻接矩阵构建图,然后使用Dijkstra的算法找到从每个节点到每个其他节点的最短路径。 My program is current not capable of finding the correct shortest paths every time. 我的程序当前无法每次都能找到正确的最短路径。 I also need to track the path, but I am unsure where to even start. 我还需要跟踪路径,但是我不确定从哪里开始。

class GraphD
{

public:
    GraphD();
    void buildGraph(ifstream &infile);

    void insertEdge(int from, int to, int distance);

    void findShortestPath();

private:
    static const int MAXNODES = 101;
    static const int infinity = 2147483647;
    struct TableType
    {
         bool visited;
         int dist;
         int path;
    };
    int C[MAXNODES][MAXNODES]; // holds adjacency matrix
    int size;
    TableType T[MAXNODES][MAXNODES]; // for dijkstra's algorithm
};


#include "GraphD.h"

GraphD::GraphD()
{
    size = 0;
    for(int i = 1; i < MAXNODES; i++)
    {
        for(int j = 1; j < MAXNODES; j++)
        {
            C[i][j] = infinity;
            T[i][j].dist = infinity;
            T[i][j].visited = false;
            T[i][j].path = 0;
        }
    }
}

void GraphD::buildGraph(ifstream &infile)
{
    string line;
    if(getline(infile, line))
    {
        size = atoi(line.c_str());
        for(int i = 1; i <= size; i++)
        {
            getline(infile, line);
            data[i] = line;
        }

        int vertex1, vertex2, distance;
        while(getline(infile, line))
        {
            stringstream edge(line);
            edge >> vertex1 >> vertex2 >> distance;
            if(vertex1 == 0)
                break;
            insertEdge(vertex1, vertex2, distance);
        }
        for(int i = 1; i <= size; i++)
        {
            C[i][i] = 0;
        }
    }
}

void GraphD::insertEdge(int from, int to, int distance)
{
    C[from][to] = distance;
} 

void GraphM::findShortestPath()
{
    for(int source = 1; source <= size; source++)
    {
        T[source][source].dist = 0;
        for(int i = 1; i <= size; i++)
        {
            int v = 0;
            int shortestDistance = infinity;
            for(int j = 1; j <= size; j++)
            {
                if((C[source][j] < shortestDistance) && !T[source][j].visited)
                {
                    shortestDistance = C[source][j];
                    v = j;
                }
            }
            T[source][v].visited = true;
            for(int w = 1; w <= size; w++)
            {
                if(!T[v][w].visited)
                {
                    T[v][w].dist = min(T[v][w].dist, T[source][v].dist + C[v][w]);
                }
            }
        }
    }
}

Set infinity value equal to 1'000'000'000 (or smth like this), because when you use MAX_INT value you get integer overflow here 将无穷大值设置为等于1'000'000'000(或类似的值),因为当您使用MAX_INT值时,此处会出现整数溢出

T[v][w].dist = min(T[v][w].dist, T[source][v].dist + C[v][w]);

Also, I think you should replace following part of code 另外,我认为您应该替换以下代码部分

if(!T[v][w].visited)
{
    T[v][w].dist = min(T[v][w].dist, T[source][v].dist + C[v][w]);
}

to the next one 到下一个

if(!T[source][w].visited)
{
    T[source][w].dist = min(T[source][w].dist, T[source][v].dist + C[v][w]);
}

because you need to find distance to vertex W from vertex Source, not V. 因为您需要找到从顶点源到顶点W的距离,而不是V。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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