Given a Directed Acyclic Graph (DAG), having N vertices and M edges, The task is to print all path starting from vertex whose indegree is zero.
Indegree of a vertex is the total number of incoming edges to a vertex.
Example:
Input: N = 6, edges[] = {{5, 0}, {5, 2}, {4, 0}, {4, 1}, {2, 3}, {3, 1}}
Output: All possible paths:
4 0
4 1
5 0
5 2 3 1
Explaination:
The given graph can be represented as:
There are two vertices whose indegree are zero i.e vertex 5 and 4, after exploring these vertices we got the fallowing path:
4 > 0
4 > 1
5 > 0
5 > 2 > 3 > 1
Input: N = 6, edges[] = {{0, 5}}
Output: All possible paths:
0 5
Explaination:
There will be only one possible path in the graph.
Approach:

The above problem can be solve by exploring all indegree 0 vertices and printing all vertices in path until a vertex having outdegree 0 is encountered. Follow these steps to print all possible paths:
 Create a boolean array indeg0 which store a true value for all those vertex whose indegree is zero.
 Apply DFS on all those vertex whose indegree is 0.
 Print all path starting from a vertex whose indegree is 0 to a vertex whose outdegrees are zero.
Illustration:
For the above graph:
indeg0[] = {False, False, False, False, True, True}
Since indeg[4] = True, so appling DFS on vertex 4 and printing all path terminating to 0 outdegree vertex are as follow:
4 > 0
4 > 1
Also indeg[5] = True, so appling DFS on vertex 5 and printing all path terminating to 0 outdegree vertex are as follow:
5 > 0
5 > 2 > 3 > 1
Here is the implementation of the above approach:
Python3
# Python program to print all # possible paths in a DAG # Recursive function to print all paths def dfs(s): # Append the node in path # and set visited path.append(s) visited[s] = True # Path started with a node # having indegree 0 and # current node has outdegree 0, # print current path if outdeg0[s] and indeg0[path[ 0 ]]: print ( * path) # Recursive call to print all paths for node in adj[s]: if not visited[node]: dfs(node) # Remove node from path # and set unvisited path.pop() visited[s] = False def print_all_paths(n): for i in range (n): # for each node with indegree 0 # print all possible paths if indeg0[i] and adj[i]: path = [] visited = [ False ] * (n + 1 ) dfs(i) # Driver code from collections import defaultdict n = 6 # set all nodes unvisited visited = [ False ] * (n + 1 ) path = [] # edges = (a, b): a > b edges = [( 5 , 0 ), ( 5 , 2 ), ( 2 , 3 ), ( 4 , 0 ), ( 4 , 1 ), ( 3 , 1 )] # adjacency list for nodes adj = defaultdict( list ) # indeg0 and outdeg0 arrays indeg0 = [ True ] * n outdeg0 = [ True ] * n for edge in edges: u, v = edge[ 0 ], edge[ 1 ] # u > v adj[u].append(v) # set indeg0[v] < false indeg0[v] = False # set outdeg0[u] < false outdeg0[u] = False print ( 'All possible paths:' ) print_all_paths(n) 
All possible paths: 4 0 4 1 5 0 5 2 3 1
Time Complexity: O (N + E)^{2}
Auxiliary Space: O (N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a studentfriendly price and become industry ready.
Recommended Posts:
 Maximum edges that can be added to DAG so that is remains DAG
 Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
 Check if vertex X lies in subgraph of vertex Y for the given Graph
 Check if every vertex triplet in graph contains two vertices connected to third vertex
 Print all possible paths from the first row to the last row in a 2D array
 Count all possible paths between two vertices
 Check whether the cost of going from any node to any other node via all possible paths is same
 Count of all possible Paths in a Tree such that Node X does not appear before Node Y
 Count number of times each Edge appears in all possible paths of a given Tree
 Print all paths from a given source to a destination
 Print all paths from a given source to a destination using BFS
 Print all paths from top left to bottom right in a matrix with four moves allowed
 Print all shortest paths between given source and destination in an undirected graph
 Count of root to leaf paths whose permutation is palindrome in a Binary Tree
 All vertex pairs connected with exactly k edges in a graph
 Print node whose each neighboring Tree has all nodes of same color
 Vertex Cover Problem  Set 1 (Introduction and Approximate Algorithm)
 Find a Mother Vertex in a Graph
 Finding minimum vertex cover size of a graph using binary search
 Topological Sort of a graph using departure time of vertex
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.