q.push(strt); bfs(strt); void bfs(int node) { ///This segment works to return if goal acheived. (Main modification of Standerd algo). vis[node]=1; if(get_goal) return; if(node==goal) { path.push_back(node); get_goal=true; return; } path.push_back(node); if(q.empty()) return; int x=q.front(); q.pop(); for(int i=1;i<=n;i++) { if(graph[x][i] && !vis[i]) { q.push(i); } } bfs(q.front()); }