Graph Path Finder - Problem
Given an undirected graph represented as an adjacency list and two nodes start and end, find all possible paths between them.
Return a list containing:
- All paths from start to end (as arrays of node values)
- The shortest path (fewest nodes)
- The longest path (most nodes)
Note: If multiple paths have the same length, return any one of them for shortest/longest.
Input & Output
Example 1 — Simple Path
$
Input:
graph = {"1":[2,3], "2":[1,4], "3":[1,4], "4":[2,3]}, start = 1, end = 4
›
Output:
[[[1,2,4],[1,3,4]], [1,2,4], [1,2,4]]
💡 Note:
Two paths exist from node 1 to 4: [1,2,4] and [1,3,4]. Both have length 3, so either can be shortest/longest.
Example 2 — Multiple Lengths
$
Input:
graph = {"1":[2], "2":[1,3,4], "3":[2,4], "4":[2,3]}, start = 1, end = 4
›
Output:
[[[1,2,4],[1,2,3,4]], [1,2,4], [1,2,3,4]]
💡 Note:
Path [1,2,4] has length 3, path [1,2,3,4] has length 4. Shortest is [1,2,4], longest is [1,2,3,4].
Example 3 — Same Start and End
$
Input:
graph = {"1":[2], "2":[1]}, start = 1, end = 1
›
Output:
[[[1]], [1], [1]]
💡 Note:
When start equals end, return single-node path [1] for all three results.
Constraints
- 1 ≤ number of nodes ≤ 20
- Graph is undirected
- Node values are positive integers
- Graph may be disconnected
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code