Sir question mein aapne bataya ki hume har node ko check krna hai kyunki jab cycle detect hogi to usme wo node hona chahiye jis node se BFS start kiya tha tabhi wo correct length dega cycle prr humne koi condition kyun ni likhi code mein jab hum cycle detect kr rhe hai ki wo cycle usi node se aarhi hai jahan se bfs start kr rhe hai usme to direct bs ye check kiya ki neighbour hai wo parent ke equal to nhin hai agar nhin hai to uski cycle length include kr di
Shortest Cycle in the Graph using BFS
hey it may be case that graph is not connected, so if two separate components of graphs are there so there may be case that shortest cycle may be in first component or second component so start bfs from each node so that all cases can be consider.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.