Skip to content

Commit 87cd7f9

Browse files
committed
cycle
1 parent db461b7 commit 87cd7f9

1 file changed

Lines changed: 4 additions & 11 deletions

File tree

Graph/Cycle Detection/1059. All Paths from Source Lead to Destination.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,26 @@
22

33
'''
44
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:
5-
65
At least one path exists from the source node to the destination node
76
If a path exists from the source node to a node with no outgoing edges, then that node is equal to destination.
87
The number of possible paths from source to destination is a finite number.
98
Return true if and only if all roads from source lead to destination.
109
11-
12-
1310
Example 1:
14-
15-
1611
Input: n = 3, edges = [[0,1],[0,2]], source = 0, destination = 2
1712
Output: false
1813
Explanation: It is possible to reach and get stuck on both node 1 and node 2.
19-
Example 2:
20-
2114
15+
Example 2:
2216
Input: n = 4, edges = [[0,1],[0,3],[1,2],[2,1]], source = 0, destination = 3
2317
Output: false
2418
Explanation: We have two possibilities: to end at node 3, or to loop over node 1 and node 2 indefinitely.
25-
Example 3:
26-
2719
20+
Example 3:
2821
Input: n = 4, edges = [[0,1],[0,2],[1,3],[2,3]], source = 0, destination = 3
2922
Output: true
3023
31-
3224
Constraints:
33-
3425
1 <= n <= 104
3526
0 <= edges.length <= 104
3627
edges.length == 2
@@ -40,6 +31,8 @@
4031
The given graph may have self-loops and parallel edges.
4132
'''
4233

34+
from collections import defaultdict,List
35+
4336
class Solution:
4437
BLACK=2
4538
GRAY=1

0 commit comments

Comments
 (0)