- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print all paths from a given source to a destination in C++
In this problem we are given a directed graph and we have to print all paths from the source to the destination of the graph.
Directed graph is a graph in with edges that are directed from vertex a to b.
Let’s take an example to understand the problem
Source = K destination = P
Output:
K -> T -> Y -> A -> P K -> T -> Y -> P K -> A -> P
Here, we have found paths from K to P. We have traversed paths and printed all paths from K that direct us to P.
To solve this problem, we will traverse the graph using depth-first search traversal technique. Starting from source, we will traverse each vertex store in our path array and mark it as visited (to avoid multiple visiting of the same vertex). And print this path, when the destination vertex is reached.
Let’s see the program implementing the logic -
Example
#include<iostream> #include <list> using namespace std; class Graph { int V; list<int> *adj; void findNewPath(int , int , bool [], int [], int &); public: Graph(int V); void addEdge(int u, int v); void printPaths(int s, int d); }; Graph::Graph(int V) { this->V = V; adj = new list<int>[V]; } void Graph::addEdge(int u, int v) { adj[u].push_back(v); } void Graph::printPaths(int s, int d) { bool *visited = new bool[V]; int *path = new int[V]; int path_index = 0; for (int i = 0; i < V; i++) visited[i] = false; findNewPath(s, d, visited, path, path_index); } void Graph::findNewPath(int u, int d, bool visited[], int path[], int &path_index) { visited[u] = true; path[path_index] = u; path_index++; if (u == d) { for (int i = 0; i<path_index; i++) cout<<path[i]<<" "; cout << endl; } else { list<int>::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) if (!visited[*i]) findNewPath(*i, d, visited, path, path_index); } path_index--; visited[u] = false; } int main() { Graph g(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(0, 3); g.addEdge(2, 0); g.addEdge(2, 1); g.addEdge(1, 3); int s = 2, d = 3; cout<<"Following are all different paths from source to destination : \n"; g.printPaths(s, d); return 0; }
Output
Following are all different paths from source to destination : 2 0 1 3 2 0 3 2 1 3
- Related Articles
- Print all paths from a given source to a destination using BFS in C++
- All Paths From Source to Target in C++
- Count all possible walks from a source to a destination with exactly k edges in C++
- Print all palindromic paths from top left to bottom right in a matrix in C++
- Concatenating n characters from source string to destination string in C
- Print all possible paths from top left to bottom right of a mXn matrix in C++
- Print all k-sum paths in a binary tree in C++
- Print all the paths from root, with a specified sum in Binary tree in C++
- Possible walks from a source to a destination with exactly k edges\n
- Print all paths from top left to bottom right in a matrix with four moves allowed in C++
- How OSPF routes the packets from source to destination?
- 8085 program to move blocks of bits from source location to a destination location
- Print all nodes at distance k from a given node in C++
- Print all root to leaf paths with there relative positions in C++
- Program to print all palindromes in a given range in C++

Advertisements