Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    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 <code>edgesi = u<sub>i</sub>, v<sub>i</sub></code> denotes an edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. 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 &lt;= n &lt;= 1000

    • 1 &lt;= edges.length &lt;= 1000

    • edges[i].length == 2

    • <code>0 <= u<sub>i</sub>, v<sub>i</sub>< n</code>

    • <code>u<sub>i</sub> != v<sub>i</sub></code>

    • There are no repeated edges.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer findShortestCycle(Integer n, Array<IntArray> edges)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait