Graph traversal dfs

How to print a path from the given source node to the given destination node??

you can go through this or you can use help of lca

  1. You need to first run a dfs
void dfs(int current, int parent = 0){
    parentArray[current] = parent
    visited[current] = true;
    for(int &i: adj[current]){
       if(i!=parent)dfs(i,current);
    }
}
  1. Then check if you even went to destination
if(visited[destination] == false)return -1;
  1. Yay, we visited if not exited yet, just go reverse path from destination via parentArray
int current = destination
while(current != -1){
    printf("%d ",current);
    if(current == source)break;
    printf(" -> ");
    current = parentArray[current];
}