05. Count the paths
β GFG solution to Count Paths in DAG problem: count total distinct paths from source to destination in a directed acyclic graph using topological sorting and DP. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given a Directed Acyclic Graph (DAG) with V nodes labeled from 0 to V-1, and a list of directed edges edges[i] = [u, v] representing a directed edge from node u to node v, find the total number of distinct paths from a given source node S to a destination node D.
π Examples
Example 1
Input: edges = [[0,1], [0,3], [2,0], [2,1], [1,3]], V = 4, S = 2, D = 3
Output: 3
Explanation: There are 3 ways to reach 3 from 2:
2 -> 1 -> 3,
2 -> 0 -> 3,
2 -> 0 -> 1 -> 3.Example 2
Input: edges = [[0,1], [1,2], [1,3], [2,3]], V = 4, S = 0, D = 3
Output: 2
Explanation: There are 2 ways to reach 3 from 0:
0 -> 1 -> 2 -> 3,
0 -> 1 -> 3.π Constraints
$2 \le V \le 10^3$
$1 \le E = \text{edges.size()} \le \frac{V \times (V - 1)}{2}$
β
My Approach
Topological Sort + Dynamic Programming (DP)
Idea:
Since the graph is a DAG (Directed Acyclic Graph), we can perform a topological sort to linearize the nodes in an order that respects dependencies (edges).
Build the adjacency list and an in-degree array (counts of incoming edges per node).
Use Kahnβs algorithm for topological sorting:
Initialize a queue with all nodes having zero in-degree.
Iteratively remove nodes from the queue, adding them to the topological order, and decrease in-degree of their neighbors.
Initialize a
dparray wheredp[i]= number of ways to reach the destinationDfrom nodei.Set
dp[D] = 1(base case).
Iterate nodes in reverse topological order:
For each node
u, sum the paths of all its neighborsv:dp[u] += dp[v].
The answer is
dp[S]β the number of ways from sourceSto destinationD.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(V + E), since each node and edge is processed exactly once during topological sort and DP calculation.
Expected Auxiliary Space Complexity: O(V + E), required for adjacency list, in-degree array, DP array, and queue.
π§βπ» Code (C++)
π§βπ» Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated