【问题标题】:How to traverse till a specified node in Weighted Undirected Graphs using BFS and DFS?如何使用 BFS 和 DFS 遍历加权无向图中的指定节点?
【发布时间】:2021-08-23 17:40:42
【问题描述】:

我已经实现了加权图以及 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, int src, 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}, {0,3},
                    {1, 2}, {2,4}, {3,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, i , discovered2);
        }
    }

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


}

【问题讨论】:

    标签: c++ graph depth-first-search breadth-first-search


    【解决方案1】:

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

    // 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;
            }
         }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多