DSA Section簡単

Cycle Detection

Detailed guide and Python implementation for the 'Cycle Detection' problem.

問題提起

簡単

Write a function has_cycle(graph) that takes a directed graph represented as an adjacency list of neighbor lists and returns True if it contains at least one cycle, or False otherwise.

制約
  • 1 <= V <= 500
  • 0 <= E <= 1000

Example 1
Input
graph = {0: [1], 1: [2], 2: [0]}
Output
True
Explanation

The path 0 -> 1 -> 2 -> 0 forms a cycle.

Example 2
Input
graph = {0: [1], 1: [2], 2: []}
Output
False
Explanation

The graph is acyclic.

Need a Hint?
Consider using Graphs-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。