0

I've written a C++ program to find out the shortest path using BFS algorithm. However, I can't find a way to print out the path, as in, the nodes which make the shortest path. What should I add so that it's possible for me to print out that path?

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using st = set<ll>;
using kiwi = queue<ll>;
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll mx=1e5+123;
bool visited[mx];
ll dist[mx];
int main(){
    fastio;
    ll node, edge;
    cin>>node>>edge;
    st adj[node+1];
    for(ll i=1;i<=edge;++i){
        ll node1, node2;
        cin>>node1>>node2;
        adj[node1].emplace(node2);
        adj[node2].emplace(node1);
    }
    ///BFS
    ll src, dest; cin>>src>>dest;
    kiwi q;
    visited[src]=true; dist[src]=0;
    q.push(src);
    while(!q.empty()){
        ll s=q.front(); q.pop();
        for(auto& x:adj[s]){
            if(visited[x]) continue;
            visited[x] = true;
            dist[x] = dist[s]+1;
            q.push(x);
        }
    }
    cout<<dist[dest]<<endl;
return 0;
}
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56

1 Answers1

-1

The shortest path from source to destination can be found by keeping track of parent for each node traversed. Create an array parent[node+1] and assign parent of source as zero. While doing bfs from source update parent array. Now you can get the path from destination to source by accessing parent of destination and parent of that node and so on untill source is reached(parent of src is 0).

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using st = set<ll>;
using kiwi = queue<ll>;
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll mx=1e5+123;
bool visited[mx];
ll dist[mx];
int main(){
    fastio;
    ll node, edge;
    cin>>node>>edge;
    st adj[node+1];
    for(ll i=1;i<=edge;++i){
        ll node1, node2;
        cin>>node1>>node2;
        adj[node1].emplace(node2);
        adj[node2].emplace(node1);
    }
    ///BFS
    ll src, dest; cin>>src>>dest;
    kiwi q;
    visited[src]=true; dist[src]=0;
    // Create parent array to keep track of parents
    int parent[node+1]={-1};
    q.push(src);
    // Assign parent of source as 0 since we dont need source's parent.
    parent[src]=0;
    while(!q.empty()){
        ll s=q.front(); q.pop();
        for(auto& x:adj[s]){
            if(visited[x]) continue;
            visited[x] = true;
            // The parent of x will be s
            parent[x]=s;
            dist[x] = dist[s]+1;
            q.push(x);
        }
    }
    cout<<"Shortest path distance : "<<dist[dest]<<endl;

    // Use stack to store path from dest to src
    stack<int> s;
    s.push(dest);
    cout<<"The Shortest path is : ";
    // Travers untill parent is 0, since we fixed parent  of src as 0.
    while(parent[dest]!=0)
    {
        s.push(parent[dest]);
        // Traverse from dest to its parent.
        dest=parent[dest];
    }
    while(!s.empty())
    {
        cout<<s.top()<<" ";
        s.pop();
    }
return 0;
}