如何打印从节点到另一个节点的所有可能路径的成本?

发布于 2025-02-09 23:23:46 字数 1959 浏览 2 评论 0原文

我想打印从源节点到目标节点的所有路径以及这些路径的成本。

到目前为止,我有以下代码:

// Find all paths from source to destination.
void search(Graph* graph, int src, int dst)
{
    // Mark the current node and store it in path.
    graph->visited[src] = true;
    graph->path[graph->index] = src;
    graph->index++;

    // If current vertex is same as destination.
    if (src == dst)
    {
        for (int i = 0; i < graph->index; i++)
        {
            if (i != graph->index - 1)
            {
                printf("%d -> ", graph->path[i] + 1);
            }
            else
            {
                printf("%d = %.3f\n\t", graph->path[i] + 1, graph->distance);
            }
        }
    }
    else
    {
        // For all unvisited vertices adjacent to current vertex.
        for (Node* adj = graph->array[src]; adj; adj = adj->next)
        {
            if (!graph->visited[adj->vertex])
            {
                graph->distance += adj->weight;
                search(graph,adj->vertex, dst);
            }
        }
    }

    // Remove current vertex from path and mark it as unvisited.
    graph->index--;
    graph->distance -= graph->array[graph->index]->weight;
    graph->visited[src] = false;
}

它可以部分工作,正确打印路径和成本,直到需要返回到先前的节点为止。此时,程序以错误代码结束。

这是一个如何发生的示例:

Paths from 1 to 5:
        1 -> 3 -> 5 = 39.576
        1 -> 3 -> 2 -> 5 = 46.172
        1 -> 3 -> 2 -> 4 -> 5 = 52.768

Process finished with exit code -1073741819 (0xC0000005)

预期的结果将是算法返回节点2,并从那里搜索其他路径。然后打印这样的结果:

Paths from 1 to 5:
        1 -> 3 -> 5 = 39.576
        1 -> 3 -> 2 -> 5 = 46.172
        1 -> 3 -> 2 -> 4 -> 5 = 52.768
        1 -> 2 -> 5 = 39.576
        1 -> 2 -> 4 -> 5 = 46.172

Process finished with exit code 0

有人可以帮助我解决这个问题吗?

I want to print all paths from source node to destination node, and the costs of these paths.

So far, I have the following code:

// Find all paths from source to destination.
void search(Graph* graph, int src, int dst)
{
    // Mark the current node and store it in path.
    graph->visited[src] = true;
    graph->path[graph->index] = src;
    graph->index++;

    // If current vertex is same as destination.
    if (src == dst)
    {
        for (int i = 0; i < graph->index; i++)
        {
            if (i != graph->index - 1)
            {
                printf("%d -> ", graph->path[i] + 1);
            }
            else
            {
                printf("%d = %.3f\n\t", graph->path[i] + 1, graph->distance);
            }
        }
    }
    else
    {
        // For all unvisited vertices adjacent to current vertex.
        for (Node* adj = graph->array[src]; adj; adj = adj->next)
        {
            if (!graph->visited[adj->vertex])
            {
                graph->distance += adj->weight;
                search(graph,adj->vertex, dst);
            }
        }
    }

    // Remove current vertex from path and mark it as unvisited.
    graph->index--;
    graph->distance -= graph->array[graph->index]->weight;
    graph->visited[src] = false;
}

It works partially, correctly prints the paths and costs, until it needs to go back to the previous nodes. At this point, the program ends with an error code.

Here's an example of how it happens:

Paths from 1 to 5:
        1 -> 3 -> 5 = 39.576
        1 -> 3 -> 2 -> 5 = 46.172
        1 -> 3 -> 2 -> 4 -> 5 = 52.768

Process finished with exit code -1073741819 (0xC0000005)

The expected result would be for the algorithm to return to node 2, and search for the other paths from there. And then print the results like this:

Paths from 1 to 5:
        1 -> 3 -> 5 = 39.576
        1 -> 3 -> 2 -> 5 = 46.172
        1 -> 3 -> 2 -> 4 -> 5 = 52.768
        1 -> 2 -> 5 = 39.576
        1 -> 2 -> 4 -> 5 = 46.172

Process finished with exit code 0

Could anyone help me to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2025-02-16 23:23:46

它没有回到节点2的原因是节点2被设置为“访问”,并且没有机会回到“未访问”。

基于您的预期输出,您的图应该是这样的:


   3 
 / | \
1  |  5
 \ |/  \
   2----4

您要查找的路径是从节点1到节点5。

现在,想象您的代码当前正在检查节点3,它获得了两个相邻的节点:节点5和节点2,都“尚未访问”。在第二个“ for循环”中,它首先递归检查节点5,很棒,它是目标节点,因此代码不在递归中,并将节点5设置为“未访问”(仅访问”(仅节点5,而不是节点3)。但是,当涉及节点2时,它也将其设置为“访问”,但它不是目的地,它在节点5和节点4上再次递归调用该功能。
现在您会看到,Node 2从未通过您的代码回到“未访问”,与节点3和节点4相同。基本上,您的代码仅将节点5设置为“未访问”。

因此,您需要做的是,在递归呼叫“ search()”之后,您必须立即将其放回原处。

  void search(Graph* graph, int src, int dst){
    // Mark the current node and store it in path.
    graph->visited[src] = true;
    graph->path[graph->index] = src;
    graph->index++;

    // If current vertex is same as destination.
    if (src == dst)
    {
        for (int i = 0; i < graph->index; i++)
        {
            if (i != graph->index - 1)
            {
                printf("%d -> ", graph->path[i] + 1);
            }
            else
            {
                printf("%d = %.3f\n\t", graph->path[i] + 1, graph->distance);
            }
        }
    }
    else
    {
        // For all unvisited vertices adjacent to current vertex.
        for (Node* adj = graph->array[src]; adj; adj = adj->next)
        {
            if (!graph->visited[adj->vertex])
            {
                graph->distance += adj->weight;
                search(graph,adj->vertex, dst);
                graph->index--;
                graph->distance -= adj->weight;
                graph->visited[adj->vertex] = false;
            }
        }
    }
}

the reason that it did not go back to node 2 was that the node 2 was set to "visited", and did not get the chance to set back to "not visited".

Based on your expected output, your graph should be something like this:


   3 
 / | \
1  |  5
 \ |/  \
   2----4

and that path you're looking for is from node 1 to node 5.

Now imagine, your code is currently checking node 3, it gets two adjacent nodes: node 5 and node 2, both are"not visited" yet. In the second "for loop", it recursively checks node 5 first, and great it is the destination node, so the code is out of the recursion and set node 5 to "not visited" (only node 5, not node 3). But when it comes to node 2, it sets it to "visited" as well, but it was not the destination, it calls the function again recursively on node 5 and node 4.
Now you see, node 2 was never set back to "not visited" by your code, same as node 3 and node 4. Basically your code only set the node 5 back to "not visited".

so what you need to do is, after your recursive call of "search()", you have to set it back immediately.

  void search(Graph* graph, int src, int dst){
    // Mark the current node and store it in path.
    graph->visited[src] = true;
    graph->path[graph->index] = src;
    graph->index++;

    // If current vertex is same as destination.
    if (src == dst)
    {
        for (int i = 0; i < graph->index; i++)
        {
            if (i != graph->index - 1)
            {
                printf("%d -> ", graph->path[i] + 1);
            }
            else
            {
                printf("%d = %.3f\n\t", graph->path[i] + 1, graph->distance);
            }
        }
    }
    else
    {
        // For all unvisited vertices adjacent to current vertex.
        for (Node* adj = graph->array[src]; adj; adj = adj->next)
        {
            if (!graph->visited[adj->vertex])
            {
                graph->distance += adj->weight;
                search(graph,adj->vertex, dst);
                graph->index--;
                graph->distance -= adj->weight;
                graph->visited[adj->vertex] = false;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文