AlgoFrames

Back edge

Warm-up · Trace the algorithm · about 4 min

This directed graph is searched by DFS from node 0. A back edge points from a node to an ancestor still on the call stack. Click every back edge.

Example 1

Input:

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

Output:

["3-1","5-0"]

Edge 3-1 points back to 1, which is still on the stack.

Constraints

  • Neighbors are tried in the order edges are listed.

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

More Depth-first search problems