Given a directed graph, a source vertex ‘src’ and a destination vertex ‘dst’, print all paths from given ‘src’ to ‘dst’.

Consider the following directed graph. Let the src be 2 and dst be 3. There are 3 different paths from 2 to 3.

We have already discussed Print all paths from a given source to a destination using DFS.

Below is BFS based solution.

**Algorithm :**

create a queue which will store path(s) of type vector initialise the queue with first path starting from src Now run a loop till queue is not empty get the frontmost path from queue check if the lastnode of this path is destination if true then print the path run a loop for all the vertices connected to the current vertex i.e. lastnode extracted from path if the vertex is not visited in current path a) create a new path from earlier path and append this vertex b) insert this new path to queue

`// CPP program to print all paths of source to ` `// destination in given graph ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// utility function for printing ` `// the found path in graph ` `void` `printpath(vector<` `int` `>& path) ` `{ ` ` ` `int` `size = path.size(); ` ` ` `for` `(` `int` `i = 0; i < size; i++) ` ` ` `cout << path[i] << ` `" "` `; ` ` ` `cout << endl; ` `} ` ` ` `// utility function to check if current ` `// vertex is already present in path ` `int` `isNotVisited(` `int` `x, vector<` `int` `>& path) ` `{ ` ` ` `int` `size = path.size(); ` ` ` `for` `(` `int` `i = 0; i < size; i++) ` ` ` `if` `(path[i] == x) ` ` ` `return` `0; ` ` ` `return` `1; ` `} ` ` ` `// utility function for finding paths in graph ` `// from source to destination ` `void` `findpaths(vector<vector<` `int` `> >&g, ` `int` `src, ` ` ` `int` `dst, ` `int` `v) ` `{ ` ` ` `// create a queue which stores ` ` ` `// the paths ` ` ` `queue<vector<` `int` `> > q; ` ` ` ` ` `// path vector to store the current path ` ` ` `vector<` `int` `> path; ` ` ` `path.push_back(src); ` ` ` `q.push(path); ` ` ` `while` `(!q.empty()) { ` ` ` `path = q.front(); ` ` ` `q.pop(); ` ` ` `int` `last = path[path.size() - 1]; ` ` ` ` ` `// if last vertex is the desired destination ` ` ` `// then print the path ` ` ` `if` `(last == dst) ` ` ` `printpath(path); ` ` ` ` ` `// traverse to all the nodes connected to ` ` ` `// current vertex and push new path to queue ` ` ` `for` `(` `int` `i = 0; i < g[last].size(); i++) { ` ` ` `if` `(isNotVisited(g[last][i], path)) { ` ` ` `vector<` `int` `> newpath(path); ` ` ` `newpath.push_back(g[last][i]); ` ` ` `q.push(newpath); ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `vector<vector<` `int` `> > g; ` ` ` `// number of vertices ` ` ` `int` `v = 4; ` ` ` `g.resize(4); ` ` ` ` ` `// construct a graph ` ` ` `g[0].push_back(3); ` ` ` `g[0].push_back(1); ` ` ` `g[0].push_back(2); ` ` ` `g[1].push_back(3); ` ` ` `g[2].push_back(0); ` ` ` `g[2].push_back(1); ` ` ` ` ` `int` `src = 2, dst = 3; ` ` ` `cout << ` `"path from src "` `<< src ` ` ` `<< ` `" to dst "` `<< dst << ` `" are \n"` `; ` ` ` ` ` `// function for finding the paths ` ` ` `findpaths(g, src, dst, v); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

path from src 2 to dst 3 are 2 0 3 2 1 3 2 0 1 3

This article is contributed by **Mandeep Singh**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print all paths from a given source to a destination
- Print all shortest paths between given source and destination in an undirected graph
- Count number of ways to reach destination in a Maze using BFS
- Level of Each node in a Tree from source node (using BFS)
- Count all possible walks from a source to a destination with exactly k edges
- Shortest paths from all vertices to a destination
- Minimum edges to reverse to make path from a source to a destination
- Minimum cost path from source node to destination node via an intermediate node
- Number of Walks from source to destination
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Sum of shortest distance on source to destination and back having at least a common vertex
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Count total ways to reach destination from source in an undirected Graph
- Shortest Path with even number of Edges from Source to Destination
- Program to print all the non-reachable nodes | Using BFS
- Print the lexicographically smallest BFS of the graph starting from 1
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Count the number of nodes at given level in a tree using BFS.
- Find integral points with minimum distance from given set of integers using BFS
- Check if the given permutation is a valid BFS of a given Tree