Path Count in Directed Graph
Submissions: 1623   Accuracy:


  Difficulty: Medium   Marks: 4

Given a directed graph and two vertices source and destination, your task is to complete the function countPaths(), whose function is to count the total number of ways or paths that exist between two vertices in a directed graph.
These paths don’t contain a cycle.

Input : Count paths between A and E

Output : Total paths between A and E are 4
         Explanation: The 4 paths between A and E are:
                      A -> E
                      A -> B -> E
                      A -> C -> E
                      A -> B -> D -> C -> E 

You will be provided graph as an adjacency list.
You should not read any input from stdin/console.
There are multiple test cases. For each test case, this method will be called individually.

Input (only to be used for Expected Output):
The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. 
Description of test cases is as follows:
The First line of each test case contains two integers 'N'
and 'M which denotes the no of vertices and no of edges respectively.
The Second line of each test case

'M space separated pairs u and v denoting that there is an edge from u to v.
The Third line of each test case contains two space-separated integers source and destination, for which the count of the path is needed to be calculated.

The method should return the count of all possible paths from source to destination.

1 <=T<= 100
0 <=u,v<= N-1


4 6
0 1 0 2 0 3 2 0 2 1 1 3
2 3
5 6
0 1 0 2 0 3 0 4 4 0 1 0
1 4



** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: harshitsidhwa

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.