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

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Graphs problems