Graph - 12: Check if Undirected Graph has Cycle

preview_player
Показать описание
Solution:
- We'll achieve this via dfs (depth first search)
- Whenever we visit the array, we mark the value True in visited Array
- If neighbour is already visited & if index is not parent, then we can say there exists a Cycle
- But if it's parant then it won't be considered in Cycle as it's parent
- Whenever we find cycle, we return true.
- We call the dfs only if element is not visited

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

Great work...made me fully Understand it . Thank you very much.

Non-residential_villager
Автор

Please upload more videos on hashmaps, dynamic programming, binary search trees and backtracking too.

abhinavminocha
Автор

Sir I am having a doubt.
I think that your program is not satisfying for 5 nodes..and 5 edges..
suppose n = 5..
and edges are as follows:
(0, 4), (1, 2), (1, 4), (2, 3), (3, 4)
for this input I am getting that no cycle is there..But cycle is forming in the form of (4->1->2->3->4) .. Please look into it sir.

jasjeetsingh
Автор

The solution seems incorrect, giving true on any value, hey bro, please give reply??

patrakarpopatlaltoofanexpr
Автор

For 3rd Graph, Its returns false.
0 1
1 2
2 3
1 3
what's the problem I can't understand.

foysalmahmud