LeetCode 2608: Shortest Cycle in a Graph Solution

Master LeetCode problem 2608 (Shortest Cycle in a Graph), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2608. Shortest Cycle in a Graph

Problem Explanation

Explanation

To find the shortest cycle in a graph, we can use Breadth-First Search (BFS) starting from each vertex. For each vertex, we perform a BFS traversal to find the shortest cycle that starts and ends at that vertex. If a cycle is found, we update the minimum cycle length encountered so far. We repeat this process for all vertices and return the minimum cycle length.

The key idea is to start BFS from each vertex and keep track of visited nodes and the parent of each node to avoid revisiting the same node.

  • Time complexity: O(V*(V+E)), where V is the number of vertices and E is the number of edges.
  • Space complexity: O(V^2), where V is the number of vertices.

Solution Code

class Solution {
    public int shortestCycle(int n, int[][] edges) {
        List<List<Integer>> adjList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            adjList.add(new ArrayList<>());
        }
        for (int[] edge : edges) {
            adjList.get(edge[0]).add(edge[1]);
            adjList.get(edge[1]).add(edge[0]);
        }
        
        int minCycle = Integer.MAX_VALUE;
        for (int start = 0; start < n; start++) {
            Queue<Integer> queue = new LinkedList<>();
            queue.offer(start);
            int[] dist = new int[n];
            Arrays.fill(dist, Integer.MAX_VALUE);
            dist[start] = 0;
            
            while (!queue.isEmpty()) {
                int curr = queue.poll();
                for (int neighbor : adjList.get(curr)) {
                    if (neighbor == start) {
                        minCycle = Math.min(minCycle, dist[curr] + 1);
                    }
                    else if (dist[neighbor] == Integer.MAX_VALUE) {
                        dist[neighbor] = dist[curr] + 1;
                        queue.offer(neighbor);
                    }
                }
            }
        }
        
        return minCycle == Integer.MAX_VALUE ? -1 : minCycle;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2608 (Shortest Cycle in a Graph)?

This page provides optimized solutions for LeetCode problem 2608 (Shortest Cycle in a Graph) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2608 (Shortest Cycle in a Graph)?

The time complexity for LeetCode 2608 (Shortest Cycle in a Graph) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2608 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2608 in Java, C++, or Python.

Back to LeetCode Solutions