Hard · Graphs

Print All Paths From Source to Destination

Given a directed graph as adjacency lists adj and two nodes src and dest, return every path from src to dest that visits no node twice, each as the list of its nodes, in any order ([[src]] when src equals dest).

Examples

Example 1

{
  "adj": [
    [1, 2],
    [3],
    [3, 4],
    [5],
    [5],
    []
  ],
  "pos": [
    [176, 44],
    [262, 94],
    [262, 193],
    [176, 242],
    [90, 193],
    [90, 93]
  ],
  "src": 0,
  "dest": 5
}

Output: 3 paths

Example 2

{
  "adj": [
    [1, 2],
    [3],
    [3],
    []
  ],
  "pos": [
    [176, 44],
    [275, 143],
    [176, 242],
    [77, 143]
  ],
  "src": 0,
  "dest": 3
}

Output: 2 paths

Rebuild it in the studio

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Graphs problems