Find a Directed Resource Access Path
Learn this problemProblem statement
Each row [a, b] in access means resource a can directly access resource b. Access is directed.
Return one shortest directed path from source to target, including both endpoints. If several shortest paths exist, process each resource’s outgoing neighbors in the order their pairs appear in access; return the path first discovered by breadth-first search. Duplicate pairs do not change the result.
If target is unreachable, return an empty array. If source == target, return [source].
Function
findAccessPath(access: int[][], source: int, target: int) → int[]Examples
Example 1
access = [[1,2],[2,3]]source = 1target = 3return = [1,2,3]Resource 1 reaches 3 through resource 2.
Example 2
access = [[1,2],[1,3],[2,4],[3,4]]source = 1target = 4return = [1,2,4]Two paths have two edges. Because pair [1, 2] appears before [1, 3], breadth-first search discovers the path through resource 2 first.
Example 3
access = [[5,6],[6,7],[8,5]]source = 7target = 5return = []The graph contains a path from 5 to 7, but directed access does not imply a path in the reverse direction.
Constraints
0 <= access.length <= 200000.- Every row of
accesscontains exactly two resource IDs. - Resource IDs,
source, andtargetare integers in[0, 10^9]. - Self-loops and duplicate directed pairs may appear.
- The graph may contain cycles.