2608. Shortest Cycle in a Graph LeetCode Solution
In this guide, you will get 2608. Shortest Cycle in a Graph LeetCode Solution with the best time and space complexity. The solution to Shortest Cycle in a Graph problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Shortest Cycle in a Graph solution in C++
- Shortest Cycle in a Graph solution in Java
- Shortest Cycle in a Graph solution in Python
- Additional Resources

Problem Statement of Shortest Cycle in a Graph
There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n – 1. The edges in the graph are represented by a given 2D integer array edges, where edges[i] = [ui, vi] denotes an edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
Return the length of the shortest cycle in the graph. If no cycle exists, return -1.
A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.
Example 1:
Input: n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
Output: 3
Explanation: The cycle with the smallest length is : 0 -> 1 -> 2 -> 0
Example 2:
Input: n = 4, edges = [[0,1],[0,2]]
Output: -1
Explanation: There are no cycles in this graph.
Constraints:
2 <= n <= 1000
1 <= edges.length <= 1000
edges[i].length == 2
0 <= ui, vi < n
ui != vi
There are no repeated edges.
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
2608. Shortest Cycle in a Graph LeetCode Solution in C++
class Solution {
public:
int findShortestCycle(int n, vector<vector<int>>& edges) {
int ans = kInf;
vector<vector<int>> graph(n);
for (const vector<int>& edge : edges) {
const int u = edge[0];
const int v = edge[1];
graph[u].push_back(v);
graph[v].push_back(u);
}
for (int i = 0; i < n; ++i)
ans = min(ans, bfs(graph, i));
return ans == kInf ? -1 : ans;
}
private:
static constexpr int kInf = 1001;
// Returns the length of the minimum cycle by starting BFS from node `i`.
// Returns `kInf` if there's no cycle.
int bfs(const vector<vector<int>>& graph, int i) {
vector<int> dist(graph.size(), kInf);
queue<int> q{{i}};
dist[i] = 0;
while (!q.empty()) {
const int u = q.front();
q.pop();
for (const int v : graph[u]) {
if (dist[v] == kInf) {
dist[v] = dist[u] + 1;
q.push(v);
} else if (dist[v] + 1 != dist[u]) { // v is not a parent u.
return dist[v] + dist[u] + 1;
}
}
}
return kInf;
}
};
/* code provided by PROGIEZ */
2608. Shortest Cycle in a Graph LeetCode Solution in Java
class Solution {
public int findShortestCycle(int n, int[][] edges) {
int ans = kInf;
List<Integer>[] graph = new List[n];
for (int i = 0; i < n; ++i)
graph[i] = new ArrayList<>();
for (int[] edge : edges) {
final int u = edge[0];
final int v = edge[1];
graph[u].add(v);
graph[v].add(u);
}
for (int i = 0; i < n; ++i)
ans = Math.min(ans, bfs(graph, i));
return ans == kInf ? -1 : ans;
}
private static final int kInf = 1001;
// Returns the length of the minimum cycle by starting BFS from node `i`.
// Returns `kInf` if there's no cycle.
private int bfs(List<Integer>[] graph, int i) {
int[] dist = new int[graph.length];
Arrays.fill(dist, kInf);
Queue<Integer> q = new ArrayDeque<>(List.of(i));
dist[i] = 0;
while (!q.isEmpty()) {
final int u = q.poll();
for (final int v : graph[u]) {
if (dist[v] == kInf) {
dist[v] = dist[u] + 1;
q.offer(v);
} else if (dist[v] + 1 != dist[u]) { // v is not a parent u.
return dist[v] + dist[u] + 1;
}
}
}
return kInf;
}
}
// code provided by PROGIEZ
2608. Shortest Cycle in a Graph LeetCode Solution in Python
class Solution:
def findShortestCycle(self, n: int, edges: list[list[int]]) -> int:
kInf = 1001
ans = kInf
graph = [[] for _ in range(n)]
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
def bfs(i: int) -> int:
"""Returns the length of the minimum cycle by starting BFS from node `i`.
Returns `kInf` if there's no cycle.
"""
dist = [kInf] * n
q = collections.deque([i])
dist[i] = 0
while q:
u = q.popleft()
for v in graph[u]:
if dist[v] == kInf:
dist[v] = dist[u] + 1
q.append(v)
elif dist[v] + 1 != dist[u]: # v is not a parent u.
return dist[v] + dist[u] + 1
return kInf
ans = min(map(bfs, range(n)))
return -1 if ans == kInf else ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.