How do you detect cycles in an undirected graph?

Quick Overview

Describe how to tell whether an undirected graph contains a cycle, and give the time and space cost of your method. Tests graph traversal, handling of disconnected components, self-loops and parallel edges, union-find, and how undirected cycle detection differs from the directed case.

How do you detect cycles in an undirected graph?

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

How do you detect cycles in an undirected graph? Assume the graph has $V$ vertices and $E$ edges, is stored as an adjacency list or an edge list, and may have more than one connected component. ```hint What a revisit means During a traversal, ask what it means to reach a vertex you have already visited. In an undirected graph every edge can be seen from both of its ends, so one visited neighbor is always expected. ``` ### What a Strong Answer Covers - A traversal method (DFS or BFS) that tracks visited vertices and how each vertex was reached, and why the edge back to the parent must be ignored. - An alternative such as union-find over the edge list, and when it fits better. - Handling of disconnected graphs, self-loops and parallel edges. - Time and space complexity of each method. - Why the undirected method does not carry over to directed graphs. ### Follow-up Questions - How would you return the vertices of one cycle, not just whether a cycle exists? - How does cycle detection change for a directed graph? - Can you tell from the counts of vertices, edges and connected components alone whether the graph has a cycle? - How would you detect the first edge that creates a cycle when edges arrive one at a time?

Overview: Describe how to tell whether an undirected graph contains a cycle, and give the time and space cost of your method. Tests graph traversal, handling of disconnected components, self-loops and parallel edges, union-find, and how undirected cycle detection differs from the directed case.

General logo
General
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

How do you detect cycles in an undirected graph? Assume the graph has VV vertices and EE edges, is stored as an adjacency list or an edge list, and may have more than one connected component.

What a Strong Answer Covers Guidance

  • A traversal method (DFS or BFS) that tracks visited vertices and how each vertex was reached, and why the edge back to the parent must be ignored.
  • An alternative such as union-find over the edge list, and when it fits better.
  • Handling of disconnected graphs, self-loops and parallel edges.
  • Time and space complexity of each method.
  • Why the undirected method does not carry over to directed graphs.

Follow-up Questions Guidance

  • How would you return the vertices of one cycle, not just whether a cycle exists?
  • How does cycle detection change for a directed graph?
  • Can you tell from the counts of vertices, edges and connected components alone whether the graph has a cycle?
  • How would you detect the first edge that creates a cycle when edges arrive one at a time?