Cycle Detection in Undirected Graph Problem Statement

You are provided with an undirected graph containing 'N' vertices and 'M' edges. The vertices are numbered from 1 to 'N'. Your objective is to determine whether the graph contains a cycle.

A cycle is defined as a path that starts and ends at the same vertex, traversing each edge exactly once.

Example:

Input: N = 3, Edges = [[1, 2], [2, 3], [1, 3]].
Output: Yes

Explanation: There are 3 vertices in this graph and edges between vertex pairs (1, 2), (2, 3), and (1, 3). A cycle exists involving all these vertices.

Input:

The first line of input contains an integer 'T' representing the number of test cases. For each test case: The first line contains two space-separated integers 'N' and 'M', denoting the number of vertices and edges. The next 'M' lines each contain two space-separated integers representing an edge between two vertices in the graph.

Output:

For each test case, print "Yes" if a cycle is present in the graph; otherwise, print "No".

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 5000
  • 0 <= M <= min(5000, (N * (N - 1)) / 2)
  • 1 <= edges[i][0] <= N
  • 1 <= edges[i][1] <= N
  • Time Limit: 1 sec

Note:

Please note that: 1. There are no parallel edges between two vertices. 2. There are no self-loops, i.e., an edge that connects a vertex to itself. 3. The graph can be disconnected.
AnswerBot
4mo

Detect cycles in an undirected graph with given vertices and edges.

  • Use Depth First Search (DFS) to traverse the graph and detect cycles.

  • Maintain a visited array to keep track of visited vertices and a...read more

Help your peers!
Select
Add answer anonymously...

PayPal Software Engineer interview questions & answers

A Software Engineer was asked 2mo agoQ. Similar to lc921
A Software Engineer was asked 12mo agoQ. Write code to encrypt data.
A Software Engineer was asked 12mo agoQ. Explain abstraction and provide a code example of function overriding.

Popular interview questions of Software Engineer

A Software Engineer was asked 2mo agoQ1. Similar to lc921
A Software Engineer was asked 12mo agoQ2. Write code to encrypt data.
A Software Engineer was asked 12mo agoQ3. Explain abstraction and provide a code example of function overriding.
PayPal Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits