C++计算有向图中的最短路径

标签 c++ constants nodes shortest-path directed-graph

我的任务是编写一个程序来维护简单网络(加权有向图)的表示,并根据请求计算两个给定节点之间的最佳路径。

目前,我正在尝试编写一个函数来计算两个节点之间最简单的函数,但是,在尝试运行我的程序时,我遇到了两个特定的错误

Severity    Code    Description Project File    Line    Suppression State
Error   C3863   array type 'bool [openNode]' is not assignable  P   127 

Severity    Code    Description Project File    Line    Suppression State
Error   C3863   array type 'int [openNode]' is not assignable   

我无法调试,因为这两个主要错误不允许我的程序运行。这些错误有什么特别的原因吗?

提前致谢!

这是Graph.h中定义的节点结构

struct GraphNode
{
    char ID;
    std::string name;
    int inNodes = 0;
    int outNodes = 0;
    std::vector<std::pair<GraphNode*, int>> connection;
    int  connections = 0;
};

这是导致错误的特定代码。

#include "Graph.h"

std::vector<GraphNode*> _graph;
int openNode = 0;

//Obligatory constructor
void Graph()
{

}

void shortestPath(char fromNode, char toNode)
{
    bool known[openNode];
    int distance[openNode];
    GraphNode*  previous[openNode];
    int numbChecked = 0;


    for (int i = 0; i < openNode; i++)
    {
        known[i] = false;
        distance[i] = 999999;
        previous[i] = nullptr;
    }

    distance[findNode(fromNode)] = 0;

    while (numbChecked < openNode)
    {
        int smallestUnknown = 9999999;
        int locationOfSmall = 0;
        for (int i = 0; i < openNode; i++)
        {
            if (known[i] == false && distance[i] < smallestUnknown)
            {
                smallestUnknown = distance[i];
                locationOfSmall = i;
            }
        }

        if (distance[locationOfSmall] == 0)
        {
            previous[locationOfSmall] = nullptr;
        }

        known[locationOfSmall] = true;
        numbChecked++;

        if (_graph[locationOfSmall]->outNodes > 0)
        {
            for (int i = 0; i < _graph[locationOfSmall]->outNodes; i++)
            {
                int newDistanceLocation = findNode(_graph[locationOfSmall]->connection[i].first->ID);
                if (known[newDistanceLocation] == false && (distance[locationOfSmall] + _graph[locationOfSmall]->connection[i].second) < distance[newDistanceLocation])
                {
                    distance[newDistanceLocation] = distance[locationOfSmall] + _graph[locationOfSmall]->connection[i].second;
                    previous[newDistanceLocation] = _graph[locationOfSmall];
                }
            }
        }
    }

    int destination = findNode(toNode);
    std::string output;
    std::string charTransfer;
    charTransfer = toNode;
    output = charTransfer;

    while (previous[destination] != nullptr)
    {
        destination = findNode(previous[destination]->ID);
        charTransfer = _graph[destination]->ID;
        output = charTransfer + "->" + output;
    }

    if (_graph[destination]->ID != fromNode)
    {
        std::cout << "The nodes are not connected." << std::endl;
    }
    else
    {
        std::cout << "The path is: " << output << std::endl;
        std::cout << "The distance is: " << distance[findNode(toNode)] << std::endl;
    }

}

如有任何更改建议,我们将不胜感激!

最佳答案

shortestPath 函数开头的代码无效:

bool known[openNode];
int distance[openNode];
GraphNode*  previous[openNode];

你不能使用变量在堆栈上创建数组(这是你试图在那里做的),因为编译器在编译时不知道 openNode 的值(这是需要的以确定堆栈大小)。

为什么不使用 vector ,例如:

std::vector<bool> known(openNode, false);
std::vector<int> distance(openNode, 999999);
std::vector<GraphNode*>  previous(openNode, nullptr);

使用此方法也会使下面的 for 循环过时。

关于C++计算有向图中的最短路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202326/

相关文章:

c++ - 为什么在将数据从文本文件移动到数组时无法获得正确的输出

C++ 二维 vector 按列搜索

c++ - 返回一个 const 引用和非 const 成员函数调用

php - 在字符串中包含常量

swift - 返回 GameScene 后 SpriteNodes 搞砸了 (Swift)

python - 使用多处理过滤 Pandas 中的大型数据框

c++ - 在 Visual Studio C++ 上使用 CUDA 编译错误 CMAKE

c++ - 具有多个实例的自动保存文件

c++ - 我可以在指针类型的声明中省略 const 限定符吗?

python - 标记外部节点,与networkx中的其他节点/边缘重叠最小