![]() |
VOOZH | about |
Given a Directed Graph with n vertices represented as a list of directed edges represented by a 2D array edgeList[][], where each edge is defined as (u, v) meaning there is a directed edge from u to v. Additionally, you are given two vertices: source and destination.
The task is to determine the total number of distinct simple paths (i.e., paths that do not contain any cycles) from the source vertex to the destination vertex.
Note: Only acyclic (simple) paths are considered. Paths containing cycles are excluded, as they can lead to an infinite number of paths.
Examples:
Input: n = 5, edgeList[][] = [[A, B], [A, C], [A, E], [B, E], [B, D], [C, E], [D, C]], source = A, destination = E
Output: 4
Explanation: The 4 paths between A and E are:A -> E
A -> B -> E
A -> C -> E
A -> B -> D -> C -> EInput: n = 5, edgeList[][] = [[A, B], [A, C], [A, E], [B, E], [B, D], [C, E], [D, C]], source = A, destination = C
Output: 2
Explanation: The 2 paths between A and C are:A -> C
A -> B -> D -> C
Table of Content
The idea is to count all unique paths from a given source to a destination in a directed graph using Depth First Search (DFS). The thought process is to recursively explore all possible paths by visiting unvisited neighbors and backtrack to try alternative routes. We observe that since the graph is directed, we only follow edges in their specified direction, and we use a visited array to avoid revisiting nodes in the current path. The approach ensures that each valid path to the destination is counted exactly once by incrementing the count only when the base case node == dest is met.
Depth-First Search for the above graph can be shown like this:
Note: The red color vertex is the source vertex and the light-blue color vertex is destination, rest are either intermediate or discarded paths.
This give four paths between source(A) and destination(E) vertex
The Problem Associated with this is that now if one more edge is added between C and B, it would make a cycle (B -> D -> C -> B). And hence after every cycle through the loop, the length path will increase and that will be considered a different path, and there would be infinitely many paths because of the cycle
Steps to implement the above idea:
4
Time Complexity: O(2^n), In the worst case, every node branches to all others, exploring all simple paths.
Space Complexity: O(n), Stack space for recursion and visited array proportional to number of nodes.
The idea is to count all paths from a source to destination in a directed graph using topological sorting. The thought process is that by processing nodes in topological order, we ensure we always compute paths after all its predecessors are processed. We maintain a ways[] array where ways[i] stores the number of paths to reach node i from the source. An important observation is that once we know the number of ways to reach a node, we can propagate that to its outgoing neighbors.
Steps to implement the above idea:
4
Time Complexity: O(n + e), Each node and edge is processed once for topological sort and path update, where e is the total number of edges.
Space Complexity: O(n + e), Extra space is used for the adjacency list, indegree array, and ways array.