1059. All Paths from Source Lead to Destination π
Description
Given the edges of a directed graph where edges[i] = [ai, bi] indicates there is an edge between nodes ai and bi, and two nodes source and destination of this graph, determine whether or not all paths starting from source eventually, end at destination, that is:
- At least one path exists from the
sourcenode to thedestinationnode - If a path exists from the
sourcenode to a node with no outgoing edges, then that node is equal todestination. - The number of possible paths from
sourcetodestinationis a finite number.
Return true if and only if all roads from source lead to destination.
Example 1:
Input: n = 3, edges = [[0,1],[0,2]], source = 0, destination = 2 Output: false Explanation: It is possible to reach and get stuck on both node 1 and node 2.
Example 2:
Input: n = 4, edges = [[0,1],[0,3],[1,2],[2,1]], source = 0, destination = 3 Output: false Explanation: We have two possibilities: to end at node 3, or to loop over node 1 and node 2 indefinitely.
Example 3:
Input: n = 4, edges = [[0,1],[0,2],[1,3],[2,3]], source = 0, destination = 3 Output: true
Constraints:
1 <= n <= 1040 <= edges.length <= 104edges.length == 20 <= ai, bi <= n - 10 <= source <= n - 10 <= destination <= n - 1- The given graph may have self-loops and parallel edges.
Solutions
Solution 1: DFS
We use a state array \(\textit{state}\) to record the status of each node, where:
- State 0 indicates the node has not been visited;
- State 1 indicates the node is currently being visited;
- State 2 indicates the node has been visited and can lead to the destination.
First, we build the graph as an adjacency list, then perform a depth-first search (DFS) starting from the source node. During the DFS process:
- If the current node's state is 1, it means we have encountered a cycle, and we return \(\text{false}\) directly;
- If the current node's state is 2, it means the node has been visited and can lead to the destination, and we return \(\text{true}\) directly;
- If the current node has no outgoing edges, we check whether it is the destination node. If so, return \(\text{true}\); otherwise, return \(\text{false}\);
- Otherwise, set the current node's state to 1 and recursively visit all adjacent nodes;
- If all adjacent nodes can lead to the destination, set the current node's state to 2 and return \(\text{true}\); otherwise, return \(\text{false}\).
The answer is the result of \(\text{dfs}(\text{source})\).
The time complexity is \(O(n + m)\), where \(n\) and \(m\) are the number of nodes and edges, respectively. The space complexity is \(O(n + m)\), used to store the graph's adjacency list and state array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |


