
Path Counting in Directed Graph
Given a directed graph with a specified number of vertices V
and edges E
, your task is to calculate the total number of distinct paths from a given source node S
to all other nodes. A path is considered distinct if either the destination node or the set of edges used differs from others. The result should be provided modulo 109 + 7, as the number of paths can be very large.
Explanation:
Include the source node as a valid destination itself, and note that the graph contains no cycles.
Input:
The first input line will contain three integers V
, E
, and S
, separated by spaces.
The following E
lines will describe the directed edges in the graph.
Each edge is defined with two space-separated integers A
and B
, indicating a directed edge going from vertex A
to vertex B
.
Output:
Output a single integer representing the total number of distinct paths modulo 109 + 7.
Example:
Input:
4 5 1
1 2
2 3
3 4
1 3
2 4
Output:
5
Constraints:
1 <= S, V <= 10^5
0 <= E <= 2 * 10^5
- Time limit: 1 second

AnswerBot
7d

Calculate total number of distinct paths from a given source node to all other nodes in a directed graph.
Use dynamic programming to keep track of the number of paths from the source node to each node....read more

Help your peers!
Add answer anonymously...
Top Amazon Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Amazon Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app