Shortest Cycle in a Graph - Complete Solution Guide
Shortest Cycle in a Graph is LeetCode problem 2608, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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] = [u i , v i ] denotes an edge between vertex u i and vertex v i . 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
Detailed Explanation
The problem asks us to find the length of the shortest cycle in a given undirected graph. A cycle is a path that starts and ends at the same node, and each edge in the path is used only once. The graph is represented by a list of edges, where each edge connects two vertices. The goal is to return the minimum length of any cycle present in the graph. If the graph doesn't contain any cycles, we should return -1. The graph has 'n' vertices labeled from 0 to n-1. There are no self-loops (edge from a vertex to itself) and no repeated edges.
Solution Approach
The solution uses Breadth-First Search (BFS) to find the shortest cycle in the graph. For each node in the graph, BFS is performed to explore its neighbors. During the BFS traversal, we keep track of the distance from the starting node and the parent of each visited node. If we encounter a visited neighbor that is not the parent of the current node, we've found a cycle. The length of this cycle is the sum of the distances from the starting node to the two nodes plus 1 (for the edge connecting the two nodes). The algorithm iterates through all nodes, performing BFS from each and updating the minimum cycle length found so far.
Step-by-Step Algorithm
- Step 1: Create an adjacency list representation of the graph from the given edges. This allows efficient access to a node's neighbors.
- Step 2: Initialize the minimum cycle length to infinity (or a very large number) to ensure that any cycle found will be shorter.
- Step 3: Iterate through each node 'i' from 0 to n-1. This node will be the starting point for a BFS traversal.
- Step 4: For each node 'i', perform BFS: Initialize distance and parent arrays. Distance array stores the distance from node 'i' to other nodes. Parent array stores the parent of each node in the BFS tree.
- Step 5: Enqueue the starting node 'i' into the BFS queue and set its distance to 0.
- Step 6: While the queue is not empty, dequeue a node 'u'.
- Step 7: Iterate through each neighbor 'v' of node 'u'.
- Step 8: If 'v' has not been visited (distance[v] == -1), update its distance, set its parent to 'u', and enqueue 'v'.
- Step 9: If 'v' has been visited and 'v' is not the parent of 'u', a cycle is detected. The length of the cycle is distance[u] + distance[v] + 1. Update the minimum cycle length if the detected cycle is shorter.
- Step 10: After BFS from node 'i' completes, proceed to the next node and repeat steps 4-9.
- Step 11: After performing BFS from all nodes, if the minimum cycle length is still infinity, there are no cycles in the graph. Return -1. Otherwise, return the minimum cycle length found.
Key Insights
- Insight 1: BFS is a suitable algorithm to determine the distance between nodes. We can use BFS from each node to explore the graph and find potential cycles.
- Insight 2: The key to identifying a cycle during BFS is to detect an adjacent node that has already been visited (i.e., has a distance value other than -1) and is not the parent node of the current node in the BFS traversal. This indicates a back edge, closing a cycle.
- Insight 3: Since we need to find the *shortest* cycle, we need to perform BFS from every node and track the minimum cycle length found so far. This ensures that we explore all possible cycles in the graph.
Complexity Analysis
Time Complexity: O(n*(n+m))
Space Complexity: O(n+m)
Topics
This problem involves: Breadth-First Search, Graph.
Companies
Asked at: Zomato.