Spot the bug: every edge is a cycle
Standard · Find the bug · about 8 min
This function should report whether an undirected graph has a cycle. It returns true even for the path 0 - 1 - 2. Click the line with the bug, then fix it.
Example 1
Input:
[{"n":3,"edges":[[0,1],[1,2]],"directed":false}]
Output:
false
The buggy code returns true here.
Constraints
- 1 ≤ n ≤ 100
- No edge appears twice and there are no self-loops.
Learn the idea first: Cycle detection in undirected and directed graphs, a visual lesson.
More Cycle detection in undirected and directed graphs problems
- The edge that closes the cycle Warm-up
- Detect a cycle Standard
- Course schedule Standard
- Find the cycle itself Challenge