java.lang.Object
g2601_2700.s2608_shortest_cycle_in_a_graph.Solution

public class Solution extends Object
2608 - Shortest Cycle in a Graph.<p>Hard</p> <p>There is a <strong>bi-directional</strong> graph with <code>n</code> vertices, where each vertex is labeled from <code>0</code> to <code>n - 1</code>. The edges in the graph are represented by a given 2D integer array <code>edges</code>, where <code>edges[i] = [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.</p> <p>Return <em>the length of the <strong>shortest</strong> cycle in the graph</em>. If no cycle exists, return <code>-1</code>.</p> <p>A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/01/04/cropped.png" alt="" /></p> <p><strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The cycle with the smallest length is : 0 -> 1 -> 2 -> 0</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/01/04/croppedagin.png" alt="" /></p> <p><strong>Input:</strong> n = 4, edges = [[0,1],[0,2]]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> There are no cycles in this graph.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 1000</code></li> <li><code>1 <= edges.length <= 1000</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no repeated edges.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findShortestCycle

      public int findShortestCycle(int n, int[][] edges)