LeetCode-in-Java

2608. Shortest Cycle in a Graph

Hard

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:

Solution

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class Solution {
    private int output = 1001;

    public int findShortestCycle(int n, int[][] edges) {
        List<Integer>[] nexts = new ArrayList[n];
        int[] ranks = new int[n];
        for (int i = 0; i < n; i++) {
            nexts[i] = new ArrayList<>();
        }
        for (int[] edge : edges) {
            for (int i = 0; i < 2; i++) {
                nexts[edge[i]].add(edge[1 - i]);
            }
        }
        for (int i = 0; i < n; i++) {
            if (ranks[i] == 0) {
                findShortestCycle(nexts, i, -1, -1001, ranks);
            }
        }
        return output == 1001 ? -1 : output;
    }

    private void findShortestCycle(List<Integer>[] nexts, int c, int p, int r, int[] ranks) {
        ranks[c] = r;
        for (int n : nexts[c]) {
            if (n != p) {
                if (ranks[n] > r + 1) {
                    findShortestCycle(nexts, n, c, r + 1, ranks);
                } else if (ranks[c] > ranks[n]) {
                    output = Math.min(output, ranks[c] - ranks[n] + 1);
                }
            }
        }
    }
}