Computer scienceAlgorithms and Data StructuresAlgorithmsGraph algorithmsGraph search algorithms

Depth-first search

Finding a cycle

Report a typo

Here is a directed graph in the format of an adjacency list:

0: [4]
1: [0, 2, 4, 5]
2: [4]
3: [0, 1, 4, 6]
4: []
5: [2, 4, 7, 8]
6: [4]
7: [3, 4, 6, 8]
8: [4]

The graph contains exactly one cycle. Find this cycle by applying DFS to this list. Print all nodes of the cycle in the field below. Start the cycle with the node with the smallest label. For example, your output may look like this:

2 -> 3 -> 4 -> 5 -> 2
Enter a short text
___

Create a free account to access the full topic