Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
All Paths From Source to Target (medium)
On this page

Problem Statement

Given a directed acyclic graph (DAG) with nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.

The graph is given as follows: graph[i] is a list of all nodes you can visit from node i.

Examples

Example 1

  • Input: graph = [[2,3],[3,4],[3,4],[4],[]]
  • Expected Output: [[0,2,3,4],[0,2,4],[0,3,4]]
  • Justification: You can go from node 0 to 4 via 2 and 3 or only 2 or only 3.

Example 2

  • Input: graph = [[1,3,4],[2,4],[3],[],[2,3]]
  • Expected Output:

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page