Graph Theory
Shortest Cycle
Undirected Graph
Algorithms
Computational Techniques

Finding length of shortest cycle in 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.

Practice algorithms

In graph theory, finding the length of the shortest cycle in an undirected graph is a well-studied problem with numerous applications in computer science, network analysis, and operational research. A cycle in a graph is a path that starts and ends at the same vertex without traversing any edge more than once. Identifying the shortest cycle can be crucial for understanding the connectivity and efficiency of networks.

Technical Explanation

An undirected graph G=(V,E)G = (V, E) consists of a set of vertices VV and a set of edges EE. A cycle is a path such that the first and last vertices are the same, and no edge is repeated. The length of a cycle is defined by the number of edges it contains. The problem is to find the cycle in the graph with the smallest length.

Key Concepts

  • Graph Representation: Graphs can be represented using adjacency lists or adjacency matrices. An adjacency list is more space-efficient for sparse graphs, while an adjacency matrix allows for simpler implementations of certain algorithms.
  • Breadth-First Search (BFS): BFS can be used effectively to find the shortest path in unweighted graphs. To find the shortest cycle, a BFS rooted at each vertex can be used to detect cycles by marking nodes and revisiting them through different paths.

Algorithm for Finding the Shortest Cycle

  1. Initialize: Set the length of the shortest cycle to infinity. This represents the absence of cycles until one is found.
  2. Perform BFS from Each Vertex:
    • For each vertex `v` in the graph, perform BFS.
    • Track the paths taken using predecessor arrays.
    • As you visit each vertex `u`, if a neighbor `w` that is not the immediate predecessor, but is already visited, a cycle is detected.
  3. Update Shortest Cycle:
    • Calculate the length of the cycle as the sum of the path lengths from `v` to `w` and back to `v`.
    • Compare it with the current known length of the shortest cycle, and update if it's smaller.
  4. Result: If the shortest cycle length remains infinity, the graph contains no cycles. Otherwise, the length of the shortest cycle is returned.

Complexity Analysis

  • Time Complexity: The BFS algorithm runs in O(V+E)O(V + E) time for a graph represented as adjacency lists, and this must be performed for each vertex, resulting in a total complexity of O(V(V+E))O(V \cdot (V + E)). This complexity can be reduced in dense graphs using optimized data structures.
  • Space Complexity: The space complexity is O(V)O(V) due to the storage of BFS queues and visited flags.

Examples

Consider a simple triangle graph with three vertices and three edges. Each vertex is connected to the two others, creating three cycles, each of length 3. For this graph, BFS initiated from any vertex will quickly reveal the shortest cycle as 3.

Edge Cases

  • Disconnected Graphs: These might have parts that are internally cyclic and parts that are not. If disconnected, any isolated components cannot contribute to the cycle.
  • Acyclic Graphs: These are graphs with no cycles (trees), where the shortest cycle length remains infinity.

Optimization and Advanced Topics

  • Minimum Cycle Basis (MCB): This extends the problem to finding all the shortest unique cycles that combine to form all cycles in the graph, typically used in network topology and electrical circuit design.
  • Heuristic and Approximation Algorithms: For very large graphs, exact algorithms may be infeasible; hence, heuristic or approximation algorithms are employed to get near-optimal cycles.

Summary Table

Key ConceptExplanation
Graph RepresentationAdjacency list for sparse graphs; adjacency matrix benefits dense graphs
BFS for Cycle DetectionDetect cycles by checking re-visiting vertices with different paths
Time ComplexityO(V(V+E))O(V \cdot (V + E))
Space ComplexityO(V)O(V)
Special CasesDisconnected or acyclic graphs influence cycle detection outcomes
Advanced AlgorithmsMCB and heuristic approaches for complex networks

Finding the shortest cycle in an undirected graph not only aids in understanding the graph structure but also serves as a cornerstone for more complex tasks in network design and analysis. Whether using basic techniques or advanced methods, this problem remains a testament to the depth and complexity of graph theory.


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.