
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
All Paths From Source to Target in C++
Suppose we have a directed, acyclic graph with N nodes. We have to find all possible paths from node 0 to node N-1, and return them in any order. The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.
So if the input is like [[1,2], [3], [3], []], then the output will be [[0,1,3], [0,2,3]].
To solve this, we will follow these steps −
Make one 2d array called res
Define a method called solve, this will take graph, node, target and temp array
insert node into temp
if node is target, then insert temp into res and return
for i in range 0 to size of graph[node] – 1
call solve(graph, graph[node, i], target, temp)
From the main method create array temp, call solve(graph, 0, size of graph - 1, temp)
return res
Example(C++)
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<vector<auto> > v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << "["; for(int j = 0; j <v[i].size(); j++){ cout << v[i][j] << ", "; } cout << "],"; } cout << "]"<<endl; } class Solution { public: vector < vector <int> > res; void solve(vector < vector <int> >& graph, int node, int target, vector <int>temp){ temp.push_back(node); if(node == target){ res.push_back(temp); return; } for(int i = 0; i < graph[node].size(); i++){ solve(graph, graph[node][i], target, temp); } } vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) { vector <int> temp; solve(graph, 0, graph.size() - 1, temp); return res; } }; main(){ vector<vector<int>> v = {{1,2},{3},{3},{}}; Solution ob; print_vector(ob.allPathsSourceTarget(v)); }
Input
[[1,2],[3],[3],[]]
Output
[[0, 1, 3, ],[0, 2, 3, ],]
- Related Articles
- Print all paths from a given source to a destination in C++
- Print all paths from a given source to a destination using BFS in C++
- Can form target array from source array JavaScript
- Single-Source Shortest Paths, Nonnegative Weights
- Single-Source Shortest Paths, Arbitrary Weights
- Program to find the maximum score from all possible valid paths in Python
- All-Pairs Shortest Paths
- Print all palindromic paths from top left to bottom right in a matrix in C++
- Finding all the unique paths in JavaScript
- Count all possible paths from top left to bottom right of a mXn matrix in C++
- Print all possible paths from top left to bottom right of a mXn matrix in C++
- Print all the paths from root, with a specified sum in Binary tree in C++
- Print all paths from top left to bottom right in a matrix with four moves allowed in C++
- Count all possible paths between two vertices in C++
- Count all possible walks from a source to a destination with exactly k edges in C++
