简体   繁体   English

如何在最小生成树上找到两个节点之间的路径

[英]How to find path between two nodes on a minimum spanning tree

I have minimum spanning tree and I have created list of adjacency.我有最小生成树,我已经创建了邻接列表。 With help of this list of adjacency I run DFS algorithm and it works correctly.在这个邻接表的帮助下,我运行了 DFS 算法并且它工作正常。 Problem is that I want to get path between two nodes.问题是我想获得两个节点之间的路径。 Example Tree:示例树: 在此处输入图像描述

For example I want to get path from 4 to 6例如我想获得从 4 到 6 的路径
Current output : 4-3-1-7-2-5-6当前 output : 4-3-1-7-2-5-6
Wanted output : 4-3-5-6通缉 output : 4-3-5-6

Code:代码:

void Graph::DFS(int source, int destination)
{
    Visited[source - 1] = true;

    vector<pair<int,int>> adjList = MSTAdjacencyLists[source - 1]; //first is vertice, second is weight

    cout << source << "-";
   
    for (int i = 0; i < adjList.size(); i++)
    {
        if (adjList[i].first == destination)
        {
            cout << adjList[i].first <<"\nFound!!" << endl;
            break;
        }

        if (Visited[adjList[i].first-1] == false)
        {
            DFS(adjList[i].first, destination);
        }
    }
}

I have read that DFS can be helpful but maybe there are better methods?我读过 DFS 可能会有帮助,但也许有更好的方法?

Not tested cos no small test availble未测试,因为没有可用的小测试

bool Graph::DFS(int source, int destination) <<<<====== bool return
{
    Visited[source - 1] = true;

    vector<pair<int,int>> adjList = MSTAdjacencyLists[source - 1]; //first is vertice, second is weight

    
   
    for (int i = 0; i < adjList.size(); i++)
    {
        if (adjList[i].first == destination)
        {
            cout << adjList[i].first <<"\nFound!!" << endl;
            return true;
        }

        if (Visited[adjList[i].first-1] == false)
        {
            if(DFS(adjList[i].first, destination))
            {
                cout << source << "-";
                return true;
            }
        }
    }
    return false;
}

ie DFS indicates whether it found the destination or not, when unwinding the recursion print the path that returned true即 DFS 指示它是否找到了目的地,当展开递归时打印返回 true 的路径

Not sure if we need the first 'cout << source' that I moved.不确定我们是否需要我移动的第一个“cout << source”。

After many hours I managed to find solution.几个小时后,我设法找到了解决方案。 This article was very helpful for me.这篇文章对我很有帮助。 https://www.geeksforgeeks.org/print-the-path-between-any-two-nodes-of-a-tree-dfs/ https://www.geeksforgeeks.org/print-the-path-between-any-two-nodes-of-a-tree-dfs/
Idea of using stack and backtracking is really smart.使用堆栈和回溯的想法非常聪明。 Code after solution:解决后的代码:

void Graph::DFS(int source, int destination, vector<int> stack)
{
    Visited[source - 1] = true;
    stack.push_back(source);
    vector<pair<int,int>> adjList = MSTAdjacencyLists[source - 1];
   
    for (int i = 0; i < adjList.size(); i++)
    {
        if (adjList[i].first == destination)
        {
            stack.push_back(adjList[i].first);
            printPath(stack);
            return;
        }

        if (Visited[adjList[i].first-1] == false)
        {
            DFS(adjList[i].first, destination,stack);
        }
    }

    stack.pop_back();
}

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

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