31. Shortest Cycle
β GFG solution to the Shortest Cycle in Undirected Graph problem: find minimum length cycle in an undirected graph using multi-source BFS technique. π
π§© Problem Description
π Examples
Example 1
Input: V = 7, E = 8, edges[][] = [[0, 5], [0, 6], [5, 1], [6, 1], [6, 2], [2, 3], [3, 4], [1, 4]]
Output: 4
Explanation: Possible cycles are:
0 β 5 β 1 β 6 β 0 (length = 4)
1 β 4 β 3 β 2 β 6 β 1 (length = 5)
The smallest one is 0 β 5 β 1 β 6 β 0, with length 4.Example 2
Input: V = 7, E = 9, edges[][] = [[0, 5], [0, 6], [1, 2], [1, 4], [1, 5], [1, 6], [2, 6], [2, 3], [3, 4]]
Output: 3
Explanation: Possible cycles include:
1 β 2 β 6 β 1 (length = 3)
1 β 2 β 3 β 4 β 1 (length = 4)
0 β 5 β 1 β 6 β 0 (length = 4)
The smallest one is 1 β 2 β 6 β 1, with length 3.π Constraints
β
My Approach
Multi-Source BFS Approach
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated