AlgoFrames

DFS order

Warm-up · Trace the algorithm · about 3 min

Depth-first search starts at node 0 and tries neighbors in the order the edges are listed. Drag the nodes into the order DFS visits them.

Example 1

Input:

{"edges":[[0,1],[0,2],[1,3],[1,4],[4,5],[2,5]],"source":0}

Output:

[0,1,3,4,5,2]

Edges are listed 0-1, 0-2, 1-3, 1-4, 4-5, 2-5.

Constraints

  • Neighbors are tried in the order their edges are listed.

Learn the idea first: Depth-first search, a visual lesson.

More Depth-first search problems