0

I'm doing a graph question and here I'm using BFS. I know bfs gives us the shortest path from a given node to all the other node in an array but I also want to store the nodes included in those shortest path. How can I achieve this-

Here's a piece of my code-

void bfsUtil(ll src, ll *pred, ll *dist, bool *visited){

        queue<ll> q;
        visited[src]=true;
        dist[src]=0;
        q.push(src);

        // an array of vector to store the path between a given node to all the other node.
        vector<ll> path[V+1];
        for(int i=0; i<=V; i++){
            // pushed back source first in every path as source node will definitely going to be there. 
            path[i].push_back(src);
        }

        while (!q.empty())
        {
            ll u = q.front();
            q.pop();

            for(ll i=0; i<adj[u].size(); i++){
                if(!visited[adj[u][i]]){
                    visited[adj[u][i]]=true;
                    dist[adj[u][i]]=dist[u]+1;
                    pred[adj[u][i]]=u;
                    q.push(adj[u][i]);

                    // pushing the node and its parent-
                    path[adj[u][i]].push_back(u);
                    path[adj[u][i]].push_back(adj[u][i]);
                }
            }
        }

        for(int i=0; i<=V; i++){
            for(int j=0; j<path[i].size(); j++) cout << path[i][j] << " ";
            cout << "\n";
        }

    }

The output is not accurate, i know I'm not pushing nodes correctly, but how can I do this? Any help would be appreciated.

  • It's easier to recover the paths from the `pred` array rather than keeping track of multiple paths. (See [here](https://stackoverflow.com/questions/28998597/how-to-save-shortest-path-in-dijkstra-algorithm/28999743#28999743). This answer is for Dijkstra, but the recovery is the same.) If you *do* want to store each path as it is generated, you'll need to push the *entire* path of the parent plus the parent itself. – beaker Apr 09 '20 at 16:37
  • I was thinking to do this way and I think I'll do this, thanks for the help. But do you know about al an algorithm to find shortest path among every pair of nodes in a unweighted and undirected simple graph? I can do this by running BFS for each node and storing the pred array, but suggest me if there exist a better way to do this. – Aaditya rathore Apr 09 '20 at 16:59
  • That's the best way I can think of. You could give all edges a weight of 1 and use Floyd-Warshall, but that's going to have a higher time complexity and only makes sense if you have a ready-made method available. – beaker Apr 09 '20 at 17:10
  • There seem to be some algorithms that give incremental improvements over BFS (see https://math.stackexchange.com/questions/58198/all-pairs-shortest-path-in-undirected-and-unweighted-graphs), but unless you're dealing with huge graphs, the algorithmic complexity may not be worth it. – beaker Apr 09 '20 at 17:14
  • @beaker what should I choose between dijkstra and bfs to find shortest path among every pair of vertices in a undirected and unweighted graph? I think n*bfs would have better time complexity than n*dijkstra. – Aaditya rathore Apr 10 '20 at 14:33
  • BFS is absolutely the better option on an unweighted graph. – beaker Apr 10 '20 at 14:45

0 Answers0