0

I have implemented the Weighted graph along with BFS and DFS. But I cannot figure a way out how to stop the traversal when a destination node (specified by user) is reached. Like user should enter the src and dest, and the BFS and DFS algorithm should print the tree until that specified node is reached. I have tried some things but I just cannot understand how to do this. I am attaching the code, any help would be appreciated.

#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);


}

1 Answers1

0

A recursive design makes this much simpler. here is the depth first version

// 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;
        }
     }
    }
ravenspoint
  • 19,093
  • 6
  • 57
  • 103