All Simple Paths in a Cyclic Directed Graph
Problem statement
Build a directed graph with nodes 0..n-1 from edges. Return every simple path from source to target.
A simple path repeats no node. Sort adjacency lists and return paths in depth-first lexicographic order.
Function
allSimpleDirectedPaths(n: int, edges: int[][], source: int, target: int) → int[][]Examples
Example 1
n = 4edges = [[0,1],[1,2],[2,0],[1,3],[2,3],[0,3]]source = 0target = 3return = [[0,1,2,3],[0,1,3],[0,3]]The visited-path set prevents cycling through 0,1,2.
Constraints
1 <= n <= 15.- Directed edges are unique.