Finding all cycles in an undirected graph
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Finding cycles in an undirected graph is an important problem in computer science, with applications spanning network analysis, electrical circuits, and molecular chemistry. This article delves into various methods and the underlying principles for detecting and listing all possible cycles in an undirected graph.
Introduction
A cycle in a graph is a path that starts and ends at the same vertex with no other repeated vertices and edges. Identifying cycles is crucial in understanding the structure and properties of a graph. This task becomes more challenging for larger graphs, where efficient algorithms are necessary to manage complexity and computational resources.
Basic Definitions
• Graph: A collection of vertices (nodes) and edges (connections) between them. • Undirected Graph: A graph where the edges have no direction. That is, if there is an edge between vertices `A` and `B`, one can travel from `A` to `B` and vice versa. • Cycle: A path that starts and ends at the same vertex with no other repeated vertices or edges.
Approaches to Find All Cycles
Depth-First Search (DFS)
DFS is a fundamental graph traversal technique, and it can be adapted to detect cycles in an undirected graph. The process involves visiting vertices recursively and keeping track of the visited path. By detecting backtracking to an already visited vertex that is not the immediate predecessor, a cycle is recognized.
Algorithm Steps:
- Initialize an empty set to store cycles.
- For each unvisited vertex, start a DFS.
- Use a recursive helper function that: • Marks the current vertex as visited and adds it to the current path. • For each adjacent vertex: • If it is not visited, recursively perform DFS. • If it is visited and is not the immediate parent, a cycle is detected. • Backtrack by removing the current vertex from the path.
- Store unique cycles in the set.
Johnson's Algorithm
Johnson's algorithm is an advanced method that finds all elementary cycles in a directed graph using backtracking and a reduced graph concept. It can be adapted for undirected graphs by considering their bidirectional nature.
Algorithm Steps:
- Use Tarjan's algorithm to find Strongly Connected Components (SCCs) in the graph.
- For each SCC, attempt to find and output all cycles using a backtracking approach.
- Mark each vertex in the current cycle and restart from the next vertex.
Hierholzer's Algorithm
This algorithm efficiently constructs an Eulerian cycle (a cycle that visits every edge exactly once) and can be modified to help detect all simple cycles in an undirected graph.
Key Points:
• Typically used for Eulerian path and cycle problems, but variations can apply to simpler cycle detection. • Constructing Eulerian cycles can indicate the presence of cycles.
Challenges and Considerations
• Time Complexity: Finding all cycles can be computationally expensive since the problem is generally exponential with respect to the number of edges and vertices. • Graph Sparse vs. Dense: Sparse graphs (fewer edges) often require different strategies compared to dense graphs (many edges). • Cycle Storage and Uniqueness: Care must be taken to ensure cycles are stored uniquely and without duplicates due to different traversal orders.
Example
Consider the graph with vertices and edges listed as follows:
Vertices: {1, 2, 3, 4, 5}
Edges: {(1, 2), (2, 3), (3, 1), (3, 4), (4, 5), (5, 3)}
In this graph, the cycles can be identified as: • Cycle 1: 1 → 2 → 3 → 1 • Cycle 2: 3 → 4 → 5 → 3
Table: Key Points on Cycle Detection
| Aspect | Description |
| Graph Type | Undirected |
| Common Algorithms | Depth-First Search (DFS), Johnson's Algorithm, Hierholzer's |
| Complexity | Generally NP-hard for arbitrary graphs |
| Applications | Network analysis, Circuit design, Chemistry |
| Cycle Uniqueness | Ensure unique cycles, commonly using sets or checks within algorithms |
Conclusion
Identifying all cycles in an undirected graph is a complex task with significant implications across various scientific and engineering fields. While basic techniques like DFS provide an intuitive approach, more sophisticated algorithms like Johnson’s are often required for comprehensive solutions, particularly in the context of complex graphs. Understanding these algorithms and their applications is essential for leveraging graph theory in practical problem-solving scenarios.
Related reading
- Finding all disconnected subgraphs in a graph
- Finding all empty triangles
- Finding all permutations that match a set of rules
- Finding all permutations to get the given sum Coin change problem
- Finding all the shortest paths between two nodes in unweighted undirected graph
- Finding all the subsets of a set
- Finding all possible combinations of numbers to reach a given sum
- Finding all possible combined plus and minus sums of n arguments?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.