Dsa
Graphs Dfs
Medium
Detect Cycle in Network
A network is represented by directed edges between nodes. You want to find out if there is a cycle in the network. If a cycle is detected, return 'Yes'; otherwise, return 'No'. The input format is as follows:
Input:
- First line: an integer n (1 ≤ n ≤ 100) - the number of nodes.
- Second line: an integer m (1 ≤ m ≤ 200) - the number of edges.
- Next m lines: two integers u and v (1 ≤ u, v ≤ n) representing a directed edge from u to v.
Output:
- 'Yes' if there's a cycle; 'No' otherwise.
Example:
Input:
3
3
1 2
2 3
3 1
Output:
Yes
Key concepts
graphsdfscycle_detection
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now