如何使用BFS和DFS遍历加权无向图中的指定节点?

问题描述

我已经实现了加权图以及 BFS 和 DFS。但是当到达目标节点(由用户指定)时,我无法找出如何停止遍历的方法。就像用户应该输入 src 和 dest,BFS 和 DFS 算法应该打印树,直到到达指定的节点。我尝试了一些事情,但我无法理解如何做到这一点。我附上了代码,任何帮助将不胜感激。

#include "iostream"
#include "vector"
#include "queue"
#include "stack"

using namespace std;

typedef pair<int,int> Pair;

struct Edge{
    int src,dest,weight;
};

class Graph{
public:

    vector<vector<Pair>> adjacencyList;

    Graph(vector<Edge> const &edges,int N)
    {
       adjacencyList.resize(N);

       for(auto &edge: edges)
       {
           int src = edge.src;
           int dest = edge.dest;
           int weight = edge.weight;

           adjacencyList[src].push_back(make_pair(dest,weight));
           adjacencyList[dest].push_back(make_pair(src,weight));
       }
    }

};

void BFS(Graph const &graph,int src,vector<bool> &discovered)
{
    queue<int> q;
    discovered[src] = true;

    q.push(src);

    while(!q.empty())
    {
        src = q.front();
        q.pop();
        cout<<src<<" ";

        for(int i = 0; i != graph.adjacencyList[src].size() ;i++)

        {
            if(!discovered[i])
            {
                discovered[i] = true;
                q.push(i);
            }
        }

    }

}

void DFS(Graph const &graph,vector<bool> &discovered)
{
    stack<int> stack;
    stack.push(src);

    while(!stack.empty()){
        src = stack.top();
        stack.pop();

        if(discovered[src])
        {
            continue;
        }
        discovered[src] = true;
        cout<<src<< " ";

        for(int i = 0 ; i < graph.adjacencyList[src].size() ; i++)
        {
            if(!discovered[i])
            {
                stack.push(i);
            }
        }
    }
}

void printGraph(Graph const &graph,int N)
{
    for (int i = 0; i < N; ++i) {
        for(Pair v: graph.adjacencyList[i])
        {
            cout<<"("<<i<<","<<v.first<<","<<v.second<<")";
        }
        cout<<endl;
    }
}








int main()
{
    vector<Edge> edges =
            {
                    // `(x,y,w)` —> edge from `x` to `y` having weight `w`
                    {0,1},{0,2},3},{1,{2,4},{3,{4,4}


            };

    int N = 5;

    Graph graph(edges,N);
   // printGraph(graph,N);

    vector<bool> discovered(N,false);


    for(int i = 0; i<N; ++i)
    {
        if(!discovered[i])
        {
            BFS(graph,i,discovered);
        }
    }

    cout<<endl;
    vector<bool> discovered2(N,false);

    for(int i = 0; i<N; i++)
    {
        if(!discovered2[i])
        {
            DFS(graph,discovered2);
        }
    }

    cout<<endl;
    printGraph(graph,N);


}

解决方法

递归设计使这更简单。这是深度优先版本

// set stopNode global
......


bool cPathFinder::depthRecurse(int v)
{

    // remember this node has been visted
    visted[v] = true;

    // is this the sop npde
    if ( v == stopNode ) {
        return true;
    }

    // look for new adjacent nodes
    for (int w : myGraph.all_neighbors(v)) {
        if (!visited[w])
        {
            // search from new node
            if( depthRecurse(w) )
                return true;
        }
     }
    }