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
4mo
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...
Amazon Software Developer interview questions & answers
A Software Developer was asked 1mo agoQ. Could you describe the process for designing a data structure that allows for al...read more
A Software Developer was asked 1mo agoQ. What is MySQL?
A Software Developer was asked 1mo agoQ. What is Java?
Popular interview questions of Software Developer
A Software Developer was asked 1mo agoQ1. Could you describe the process for designing a data structure that allows for al...read more
A Software Developer was asked 1mo agoQ2. What is MySQL?
A Software Developer was asked 1mo agoQ3. What is Java?
Top HR questions asked in Amazon Software Developer
A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

