BFS Approach to Find the Shortest Cycle in a Graph
Create an adjacency list to represent the graph.
Initialize a variable to store the minimum cycle length, setting it to infinity.
Iterate over each vertex in the graph.
For each vertex, perform BFS:
a. Initialize a visited array with values set to infinity, indicating unvisited nodes.
b. Set the distance to the starting vertex to 0.
c. Use a queue to perform BFS, starting with the current vertex.
d. For each vertex dequeued, visit its neighbors:
i. If the neighbor is unvisited, set its distance and enqueue it.
ii. If the neighbor is visited and not the parent, calculate the cycle length.
iii. Update the minimum cycle length if a shorter cycle is found.
After BFS for all vertices, check if the minimum cycle length is less than infinity.
If a cycle is found, return the minimum cycle length; otherwise, return -1.
DFS Approach to Find the Shortest Cycle in a Graph