Find all the paths forming simple cycles on 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.
Introduction
In graph theory, a simple cycle is defined as a path in an undirected graph that starts and ends at the same vertex, with all other vertices and edges being distinct along the path. Identifying simple cycles in a graph is a classic problem that has numerous applications in fields such as network analysis, circuit design, and computational biology. This article delves into the methods for finding all simple cycles in an undirected graph, including technical explanations and examples.
Definitions and Basic Concepts
Graph Definitions
• Vertex (Node): A fundamental part of a graph, represented as a dot. • Edge: A line connecting two vertices in the graph. • Undirected Graph: A graph in which edges do not have directions. This means the connection between two vertices is bidirectional.
Simple Cycle
A simple cycle is a closed path that doesn't repeat any vertices or edges, except for the starting and ending vertex which are the same. For example, in a triangle graph consisting of edges A-B, B-C, and C-A, all paths from any vertex back to itself are simple cycles.
Algorithms for Finding Simple Cycles
Overview
Various algorithms can be employed to detect simple cycles in an undirected graph, including depth-first search (DFS), backtracking, and Johnson's algorithm. Below, we discuss some of these methods with examples.
Depth-First Search (DFS)
The depth-first search is a foundational algorithm to traverse graphs. It can be adapted to find simple cycles by keeping track of the visited nodes and backtracking when necessary.
Steps
- Start with any vertex
v. - Perform a DFS from
v, maintaining a path list. - If a DFS reaches a vertex
uthat is already in the path list, a cycle is detected. - Save the current path as a cycle.
- Backtrack to explore other paths.
- Continue the process until all nodes have been visited.
Example
Consider a graph with vertices {A, B, C, D} and edges {AB, AC, BD, CD}. Using DFS:
• Start at A: • Path: A → B → D → C → A (Cycle found) • Path: A → C → D → B → A (Cycle found)
This finds all simple cycles involving node A.
Johnson's Algorithm
Johnson's algorithm is a more advanced technique that uses Tarjan's algorithm. It's efficient for finding all elementary circuits in a directed graph but can be adapted for undirected graphs.
Key Steps
- Transform the undirected graph into a directed format.
- Apply Tarjan's algorithm to find strongly connected components.
- Use a backtracking approach to detect cycles.
This method is more suitable for dense graphs and has a time complexity of , where n
is the number of vertices, e
is the number of edges, and c
is the number of cycles.
Challenges and Considerations
Avoiding Repetition
One of the main challenges is avoiding repetitions in cycle detection. This can be managed by carefully managing the visited nodes list and ensuring each vertex is revisited only when verifying its presence in a new cycle.
Computational Complexity
The task of finding all cycles is inherently complex, especially for large graphs. Optimizations, such as pruning of paths and early exits from DFS, can mitigate this.
Example: Algorithm Application
Suppose we have the following undirected graph represented as an adjacency list:
• DFS Start at A: • A → B → C → A • A → C → B → A • A → B → D → C → A
Related reading
- Find an algorithm to balance this game
- Find an algorithm to win this battle against crime!
- Find an element in an array recursively
- Find common elements from two very large Arrays
- Find all triplets in array with sum less than or equal to given sum
- Find an item and change value in custom object array - Swift
- Find common elements in N sorted arrays with no extra space
- Find common substring between two strings

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.