Detect cycle in an undirected graph | GeeksforGeeks

  Рет қаралды 119,605

GeeksforGeeks

GeeksforGeeks

Күн бұрын

Explanation for the article: www.geeksforgeeks.org/detect-c...
This video is contributed by Illuminati.

Пікірлер: 42
@BrianKeeganMusic
@BrianKeeganMusic 6 жыл бұрын
Honestly the best video I have seen on this so far. Other videos did not explain the parent node.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 жыл бұрын
Thanks Brian Keegan :)
@asadsbhatti
@asadsbhatti 5 жыл бұрын
Sir , aap nain kamaaal kr dyaa........... What a Explanation.....!
@ionuttrif8378
@ionuttrif8378 3 жыл бұрын
I made a function that detect if there is a cycle in a graph using bfs, but i don't know how to make it print the members of the cycle, any tips?
@ishitamittal1546
@ishitamittal1546 3 жыл бұрын
Does this code work for non continuous graph?
@nitishgupta6802
@nitishgupta6802 5 жыл бұрын
Isn't it sufficient to check that if the number of edges in the connected graph is more than n-1 (where n is the number of vertices), then it must have a cycle.
@evapine2795
@evapine2795 4 жыл бұрын
no
@brkekr
@brkekr 3 жыл бұрын
@Ash Win Yes I totally agree with you, but in the case that E>n-1, that would be a smart move to place an if to check whether it has more than n-1 edges at the very beginning of the function isCyclicUtil.
@tarekadel7631
@tarekadel7631 2 жыл бұрын
This approach makes assumptions that may not hold true: 1) The graph is connected --> But it could have more than 1 connected component 2) Edges >= Vertices
@parishapranayraj9978
@parishapranayraj9978 6 жыл бұрын
can you please put the proof of correctness of the algorithm .
@greerhanshaw
@greerhanshaw 7 жыл бұрын
Thanks for the great video!
@GeeksforGeeksVideos
@GeeksforGeeksVideos 7 жыл бұрын
You're welcome, Greer! :)
@MushafAli-rc2su
@MushafAli-rc2su Жыл бұрын
A great explanation thankfull to you❤
@kmishy
@kmishy 2 жыл бұрын
3:00 Should you change it to- For any visited vertex
@parthgupta5058
@parthgupta5058 4 жыл бұрын
wouldnt it just stuck in an infinite loop if there is an edge with u v equal
@coderbuddy3875
@coderbuddy3875 3 жыл бұрын
Nice explanation 👍
@mohitsoni4925
@mohitsoni4925 5 ай бұрын
It will be better if we also explain why the statement is sufficient enough to justify a cycle in graph. This will help us to not memorise the statement but visualise it.
@mohammedelhag7565
@mohammedelhag7565 Жыл бұрын
thank you, appreciate the hard work
@shubhammalhotra7244
@shubhammalhotra7244 Жыл бұрын
For every visited vertex v, if there is an adjacent u. Such that u is already visited & u is not the parent of v. So, cycle in the graph is present/
@mujtabaamer6542
@mujtabaamer6542 5 жыл бұрын
What is the output
@ela2316
@ela2316 3 ай бұрын
true if there's a cycle otherwise false
@anirudhreddybasani3555
@anirudhreddybasani3555 5 жыл бұрын
Then why didn't we used the same logic for directed graphs..there we've used a recursion stack and here we're just using a parent variable...
@soumavanag5025
@soumavanag5025 5 жыл бұрын
Anirudh reddy Basani consider the following directed graph and do a dry run: (1,2),(2,3),(1,3) Note: there is no directed edge in undirected graph, so tracking of parent is enough which is not the case in directed graphs.
@Aditya-pl3xg
@Aditya-pl3xg 2 жыл бұрын
you can all you need to do is run a dfs for each vertex in a loop and that's it. actually this way will cover for case of both the undirected graph (since it'll exit in first iteration itself) and for non-connected graphs too. (but don't tell GfG of this common sense they will write a shitty code full of classes and OOPS on their website that'll confuse everyone 😁)
@rohitk2629
@rohitk2629 4 жыл бұрын
Wrong! After backtracking from 1 to 0. 0 will again check its each connected node if its visited or not. since 1 is visited and not parent it will return true.
@sumanthchakravarthi6853
@sumanthchakravarthi6853 3 жыл бұрын
why would you check for 1 again it will move to 2 right?
@brkekr
@brkekr 3 жыл бұрын
Well it does not; because in isCyclicUtil, the first if checks if there is any UNVISITED node, if yes, it recurs from there without evaluating the else if (else if checks if there is a visited children, what you said would be true in case of else-if and if statements swapping places )
@vaibhavimacherla1603
@vaibhavimacherla1603 6 жыл бұрын
it checks only for triangles???
@LawZist
@LawZist 6 жыл бұрын
i would like also to know
@KjellBoeije
@KjellBoeije 6 жыл бұрын
It detects a cycle whenever a node has a child node that's already visited, and is not it’s parent. If I'm correct this would work for cycles with any number of nodes.
@GalibFida
@GalibFida 5 жыл бұрын
the cycles in this graph are triangles but they don't have to be cycle of three vertices; a cycle can have any number of vertices and this algorithm will still work
@akhilvegunta6263
@akhilvegunta6263 4 жыл бұрын
No it will check for any figure if it forms a cycle.
@hamidansari9331
@hamidansari9331 4 жыл бұрын
@@akhilvegunta6263 you r late
@xuantungnguyen9719
@xuantungnguyen9719 2 жыл бұрын
your code failed in this case g = Graph(3) g.addEdge(1, 2) g.addEdge(2, 3) g.addEdge(3, 1)
@sjrohan4042
@sjrohan4042 Жыл бұрын
bahut loud video hai🤣
@jdleanne
@jdleanne 4 жыл бұрын
This is wrong. He is indeed using BFS not DFS, as it was traversing around the neighbor
@kasyapdharanikota8570
@kasyapdharanikota8570 2 жыл бұрын
i also felt that he is using bfs instead of DFS
@dylankrejci9965
@dylankrejci9965 10 ай бұрын
@@kasyapdharanikota8570 this graph is kinda weird in that the way its laid out, BFS and DFS are essentially the same. It's DFS in the sense that each node visited follows directly from its parent, but the organization of this particular graph also means that you are in a sense checking the parent and then all of its children in order. The best way to distinguish from within the context of this video is the fact that the parent of node 4 is 3, whereas in BFS it would be 2. Hope this helps
@adnanhaque1871
@adnanhaque1871 Ай бұрын
​@@dylankrejci9965 well BFS uses a queue FIFO while DFS uses stack LIFO. so not the same!
Introduction to Graph Theory: A Computer Science Perspective
16:26
How AI Discovered a Faster Matrix Multiplication Algorithm
13:00
Quanta Magazine
Рет қаралды 1,4 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Вечный ДВИГАТЕЛЬ!⚙️ #shorts
00:27
Гараж 54
Рет қаралды 14 МЛН
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 12 МЛН
Flattening a Linked List | GeeksforGeeks
11:07
GeeksforGeeks
Рет қаралды 62 М.
Cycle in Undirected Graph Graph Algorithm
12:23
Tushar Roy - Coding Made Simple
Рет қаралды 144 М.
ML Was Hard Until I Learned These 5 Secrets!
13:11
Boris Meinardus
Рет қаралды 234 М.
How Dijkstra's Algorithm Works
8:31
Spanning Tree
Рет қаралды 1,3 МЛН