G-12. Detect a Cycle in an Undirected Graph using DFS | C++ | Java

  Рет қаралды 215,067

take U forward

take U forward

Күн бұрын

Пікірлер: 221
@takeUforward
@takeUforward 2 жыл бұрын
Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞 Do follow me on Instagram: striver_79
@darveshchhatani1411
@darveshchhatani1411 2 жыл бұрын
Please complete the series ASAP
@Sumeet_100
@Sumeet_100 Жыл бұрын
Hello bhaiya I have doubt that why the time complexity is O(N+2*E) why not O(2*E)
@tasneemayham974
@tasneemayham974 8 ай бұрын
@@Sumeet_100 Because you are going for all nodes N AND(+) visiting all degrees means 2E.
@dipaligangawane980
@dipaligangawane980 2 жыл бұрын
Really very good lecture series. Waiting for the next lectures.
@cinime
@cinime 2 жыл бұрын
Understood! So fantastic explanation as always, thank you very much!!
@harshvardhansingh2272
@harshvardhansingh2272 2 жыл бұрын
understood striver was able to do this by dfs by myself after watching and getting the concept from last video Amazing content as always
@prakhaxr
@prakhaxr Жыл бұрын
wah nanhe striver
@singhharsh8019
@singhharsh8019 Жыл бұрын
@@prakhaxr sure, I'll take it as a compliment🙂
@abhinavtomar8518
@abhinavtomar8518 Жыл бұрын
Amazing explanation bhaiya really amazing. Was trying to understand it from many source but was not able to understand properly. Really enjoy your teaching. Keep going bhaiya
@vikasbagri1225
@vikasbagri1225 2 жыл бұрын
understood it very well Your explanation is awesome
@selva-qs9dw
@selva-qs9dw 3 ай бұрын
Without seeing a code i had doned Credits goes to striver who has an talent to make it as easy entire dsa for us....❤
@casinarro
@casinarro Жыл бұрын
It really feels good learning from you
@rachitchaddha999
@rachitchaddha999 Жыл бұрын
Bhaiya ur teaching style is awesome.
@DivineVision201
@DivineVision201 2 жыл бұрын
understood bhaiya. Are there more videos coming for Graph in this playlist? It would be very helpful if there are more to come.. But great work..
@anushravishankar9741
@anushravishankar9741 Жыл бұрын
Understanding more and more each day that I'm not smart enough to understand this high level logic Spent 1 year to understand, still haven't understood anything
@mount2020
@mount2020 Жыл бұрын
kya hi hai bhai..u make to feel the concept..loving ur content so much
@jaishriharivishnu
@jaishriharivishnu 4 ай бұрын
the concept of more than two parent to detect cycle is also good...... If in any adjacency list, if we find two or more visited node during BFS or DFS in a undirected graph then it will have a cycle. bool detectcycle(int i, vector&vis, vectoradj[]){ bool iscycledetected=false; vis[i]=1; int count=0; for(auto K:adj[i]){ if(vis[K])count++; } if(count>1)iscycledetected=true;// this is the check for cycle for(auto K:adj[i]){ if(!vis[K]){ bool x1=detectcycle(K,vis,adj); iscycledetected=x1|iscycledetected; } } return iscycledetected; } bool isCycle(int V, vector adj[]) { // This is the main function vectorvis(V); for(int i=0; i
@vikramragunathan6392
@vikramragunathan6392 Жыл бұрын
The way you explained. Just wow 😃😃
@VikasBagri-i5j
@VikasBagri-i5j 2 ай бұрын
understood, thanks for this amazing series
@vishankpathariya5926
@vishankpathariya5926 3 ай бұрын
great explanation bhaiya, totally understood
@anshusharma11
@anshusharma11 Жыл бұрын
Thanks for the wonderful explanation.
@muskangupta5873
@muskangupta5873 5 ай бұрын
got the intution because of your rotten oranges videos, i felt it is almost similar to that, thanks understood
@tasneemayham974
@tasneemayham974 8 ай бұрын
PERFECTTTT STRIVERRR!!!! UNDERSTOOODDDD
@KUMARSAURABH-s5i
@KUMARSAURABH-s5i 4 ай бұрын
Amazing session Understood!!
@AjayYadav-xi9sj
@AjayYadav-xi9sj 2 жыл бұрын
When is the possible date of completion of the series.
@chandramoulipanyam5805
@chandramoulipanyam5805 Жыл бұрын
Classic style of Explanation
@koushikkumar481
@koushikkumar481 Ай бұрын
understood Striver Bhaiya ❤❤
@rishabhgupta9846
@rishabhgupta9846 Жыл бұрын
understood,great explanation
@DevashishJose
@DevashishJose 8 ай бұрын
Understood, Thank you so much
@fairoossahfeerulwasihf1139
@fairoossahfeerulwasihf1139 Ай бұрын
will it work fine even for this case where 2 nodes are there each points each other ond forming cycle?
@hashcodez757
@hashcodez757 2 ай бұрын
"UNDERSTOOD BHAIYA!!"
@sripriyapotnuru5839
@sripriyapotnuru5839 2 жыл бұрын
Thank you, Striver 🙂
@RVcalisthenics
@RVcalisthenics 23 күн бұрын
understood striver
@jagratgupta8392
@jagratgupta8392 Жыл бұрын
understood sir very nice
@shivanshsingh176
@shivanshsingh176 2 жыл бұрын
Instead of "if(dfs(adjacentNode, node, visited, adj) == true) return true; ", if we directly return the dfs function, then it is incorrect. Whu so ? I think It should be same
@devanshchauhan7065
@devanshchauhan7065 Жыл бұрын
ya exactly, same doubt, can someone explain please!
@gautamsingh2599
@gautamsingh2599 Жыл бұрын
in the above statement it the output of the function is true it will directly return it and if you return at last it may or may not return true always
@Jaydeeeeeeep
@Jaydeeeeeeep Жыл бұрын
Because detect function will return true or false for every component between one to n, Lets say first component has no cycle, but second one has a cycle. But according to your code, it will not go to second component, it will just return false after iterating in first component. So, if detect function sends a true, then only you should return true, else nothing
@vijaymehrotra3236
@vijaymehrotra3236 Жыл бұрын
in directly returning it will always return something but in correct method shone in video it will only return true if the dfs function is true
@AgrimArtofficial
@AgrimArtofficial 6 ай бұрын
This question can also be done by using the concept V=E+1 in cyclic undirected graph using dfs
@roshansah691
@roshansah691 2 жыл бұрын
Nice explanation, i understood 👍
@abhichi
@abhichi 2 жыл бұрын
Understood!!More!!✌🏻
@codeman3828
@codeman3828 5 ай бұрын
Thanks for the series
@supriyamanna715
@supriyamanna715 2 жыл бұрын
understood! Thanks man
@RohitKumar-dz8dh
@RohitKumar-dz8dh 2 ай бұрын
Understood 😊
@govindsharma7696
@govindsharma7696 Жыл бұрын
Understood bhaiya 👍
@adebisisheriff159
@adebisisheriff159 9 ай бұрын
Amazing....... Always
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@nathonluke4058
@nathonluke4058 2 жыл бұрын
Loved it and understood
@them.pranay7196
@them.pranay7196 Ай бұрын
super duper hit video
@codewithom11
@codewithom11 Жыл бұрын
Thank You So Much Sir😇
@subhadeepghosh2813
@subhadeepghosh2813 2 жыл бұрын
plz make a video on snake and ladder.love u bro
@sumeetubale3923
@sumeetubale3923 Жыл бұрын
why the time complexity is O(N+ 2*E) instead of that why not O(2*E) ??
@gouravchaki7990
@gouravchaki7990 Жыл бұрын
Cuz a loop runs from 1->n to check whether node is visited or not, if not visited the dfs is performed.
@utkarshpatidar167
@utkarshpatidar167 Жыл бұрын
Understood 🙌🏻
@debanjan10
@debanjan10 Жыл бұрын
understood sir
@fmkhandwala39
@fmkhandwala39 2 жыл бұрын
UNDERSTOOD!
@Learnprogramming-q7f
@Learnprogramming-q7f 5 ай бұрын
thank you Bhaiya
@fadebeyond
@fadebeyond 2 жыл бұрын
Understood Bhaiya.
@parshchoradia9909
@parshchoradia9909 Жыл бұрын
Understood Sir!
@coolgaurav5163
@coolgaurav5163 2 ай бұрын
Understoood
@ANURAGSINGH-nl2ll
@ANURAGSINGH-nl2ll Жыл бұрын
understood thank you
@shubhankarmondal8690
@shubhankarmondal8690 2 жыл бұрын
Understood 🙌🔥
@parthdurgude2617
@parthdurgude2617 29 күн бұрын
understood!
@harshwardhanpatki846
@harshwardhanpatki846 Жыл бұрын
Understood sir 🙇‍♂️
@Joy-b6r
@Joy-b6r 4 ай бұрын
Isn't the both conditions are same inside the loop why we need if condition inside if pls explain...,
@utsavkumar6048
@utsavkumar6048 Жыл бұрын
understood sirji
@vaibhavchauhan1930
@vaibhavchauhan1930 2 жыл бұрын
understood, Thank you so much
@adityasaxena6971
@adityasaxena6971 Жыл бұрын
Understood💯
@sanketmudholkar2889
@sanketmudholkar2889 2 жыл бұрын
Bold me "UNDERSTOOD"
@tahmidulislamomi1398
@tahmidulislamomi1398 Жыл бұрын
Superb
@mriduljain6809
@mriduljain6809 Жыл бұрын
Understood Bhaiya
@himanshugupta-mz2yk
@himanshugupta-mz2yk Жыл бұрын
Bhaiya Python code in coding ninja is not accepted but code of other languages are accepted
@TravelTracksByDebo
@TravelTracksByDebo 2 жыл бұрын
understood bhaiya
@Anony.musk01
@Anony.musk01 Жыл бұрын
one doubt, why do you write functions under private? any specific reason for that?
@omkarshendge5438
@omkarshendge5438 10 ай бұрын
you dont want that function to be exposed somewhere else! you specifically want dfs or dfs for this solution class only and other classes if made can have their methods likewise. yes u can add the function under public too not an issue but as far as clean code is concerned its better to add under private "Methods that serve as the inner workings of your class and don’t output or display information." read this carefully for private
@pratapsingh9638
@pratapsingh9638 Жыл бұрын
striver please make videos on topic like segment tree
@atharvadeshmukh6328
@atharvadeshmukh6328 8 ай бұрын
Understood 😄
@yashgupta6797
@yashgupta6797 2 жыл бұрын
understood sir!
@codewithsahil978
@codewithsahil978 Жыл бұрын
bhaiya ji maja agaya
@lineshmalkam224
@lineshmalkam224 Жыл бұрын
understood
@manasranjanmahapatra3729
@manasranjanmahapatra3729 2 жыл бұрын
Understood!
@shreyarawatvlogs6920
@shreyarawatvlogs6920 9 ай бұрын
I'm not understanding why my code is not working on GFG
@sarangkumarsingh7901
@sarangkumarsingh7901 11 күн бұрын
Understood
@mriduljain1981
@mriduljain1981 Жыл бұрын
understood.
@ashishbhardwaj4334
@ashishbhardwaj4334 Жыл бұрын
UNDERSTOOD
@DilpreetSingh-cs7yz
@DilpreetSingh-cs7yz 2 жыл бұрын
Why writing the fucntion in private ?
@udaypratapsingh8923
@udaypratapsingh8923 2 жыл бұрын
to make it private
@pihus3498
@pihus3498 Жыл бұрын
understood :)
@Niki_0011
@Niki_0011 5 ай бұрын
Understood ++
@kritisingh3194
@kritisingh3194 2 жыл бұрын
Understood. :)
@VEDANSHSHARMA-o6k
@VEDANSHSHARMA-o6k 6 күн бұрын
ur awesome
@ritikkoshta5792
@ritikkoshta5792 Жыл бұрын
understood 😁😁
@Simran_048
@Simran_048 3 ай бұрын
Simple intution is : It's not the parent still already visited that means cycle exists
@Yash-uk8ib
@Yash-uk8ib 2 жыл бұрын
Sir, a small doubt? Lets say the for loop calls dfs fn X times where X
@sumerrawat6947
@sumerrawat6947 2 жыл бұрын
X + O(N+2E) hoga bro , and X
@Yash-uk8ib
@Yash-uk8ib 2 жыл бұрын
@@sumerrawat6947 thanks sir!! Pr ek baat or btadijiye Aap bol rhe ho ki har component ki complexity addup hui hai To sir is hisab se complexity O(c1+2e1) + O(c2+2e2) ..... O(x+2ex ) honi chiye thi? Agr yhi hai to kya ye eqn hi X + O(N+2E) ke equal hai?
@sumerrawat6947
@sumerrawat6947 2 жыл бұрын
@@Yash-uk8ib dekh bhai Lets say we have 4 connected components so total 4 calls will be made complexity of each component for iteration 1: O(N1+2E1) for iteration 2: O(N2+2E2) for iteration 3 O(N3+2E3) for iteration 4 O(N4+2E4) as the number of nodes are different in each componenet total TIME COMPLEXITY = 4 + O(N1+N2+N3+N4) + 2(E1+E2+E3+E4) = 4+O(N + 2E ) or X + O(N+2E) where X is the total number of calls N is total number of nodes in WHOLE GRAPH E is total edges in WHOLE GRAPH
@MdSarfrazcs
@MdSarfrazcs 2 жыл бұрын
@@sumerrawat6947 can you suggest a playlist for time complexity of recursion and graph .
@gururajsavalgi4496
@gururajsavalgi4496 Ай бұрын
nice
@abhinavanand1904
@abhinavanand1904 Жыл бұрын
Can anyone tell me that why striver has used a vector of array for the visited array instead of a single vector/array in C++ code, or its just mistyped while coding ??
@poorvikac3280
@poorvikac3280 Жыл бұрын
Seems like a tying mistake. Passing int vis[] to a vector vis[] doesn't make sense and will throw an error.
@kholiyamails1257
@kholiyamails1257 Жыл бұрын
thanks thanks thanks 😅
@ayushgarg9099
@ayushgarg9099 Жыл бұрын
A simple question if anyone can help me understand! If we are given a source node and we are performing DFS on a connected undirected graph(i.e. a single component), how is the time complexity O(N+2E), shoudn't it be just O(2E) which is summation of degrees?
@aprajitakumari3745
@aprajitakumari3745 Жыл бұрын
For N nodes we are traversing in the wrost case
@anshumaan1024
@anshumaan1024 Жыл бұрын
@@aprajitakumari3745 because for every node, we are traversing all its adjacent nodes
@reee896
@reee896 Жыл бұрын
So In the worst case the 2E will not be done cause worst case means every will be a component in itself
@aniket6676
@aniket6676 2 жыл бұрын
Hey Striver, I had a doubt, why do we need to store the parent of each node? Can't we just count the number of neighboring nodes, of a particular node, that are visited, if the count is more than 1, for any node, then we can conclude that the cycle is present? Plz let me know if I am missing out on something in my approach. Following is the implementation for the above logic: bool detectDfs(int start, vector adj[], int vis[]){ vis[start] = 1; int visCnt = 0; bool flag = false; for(auto it : adj[start]){ if(++visCnt > 1) flag = true; else if(!vis[it]){ flag = flag || detectDfs(it, adj, vis); } if(flag == true) return flag; } return flag; } bool isCycle(int V, vector adj[]) { // Code here int vis[V] = {0}; for(int i = 0; i < V; i++){ if(!vis[i] && detectDfs(i, adj, vis)) return true; } return false; }
@kanishkraj5640
@kanishkraj5640 2 жыл бұрын
No..There is possibility when there are more than one visited and the graph will not contain cycle...Refer GFG Article for such example.
@sviswesh3555
@sviswesh3555 Жыл бұрын
@@kanishkraj5640 I understood @Aniket 's qn but couldn't understand your answer. How can a node be visited, not be the parent, and yet no cycle is formed?
@shripandey4120
@shripandey4120 Жыл бұрын
I think, you are right.
@illusionhex1200
@illusionhex1200 Жыл бұрын
In this example you can visit back to the parent node if not stored them. 1 ( 2 3) 2 (1 3) Here you will go to 2 from 1 and again 1 from 2 and you got your cycle when there is no cycle at all.
@arindammandal1513
@arindammandal1513 Жыл бұрын
No, if u have a graph with one node and 3 outwards edge , then it doesnt mean its a cycle right? like 1--->2,3,4 so u visit 2,3 from 1 and visit counts become 2 > 1 , does that mean it has a cycle? No
@anonymousboy2868
@anonymousboy2868 3 ай бұрын
understooddd
@akashsahu2571
@akashsahu2571 Жыл бұрын
yws
@AniketSharma-ct5fv
@AniketSharma-ct5fv Жыл бұрын
Don't you think all @all that visited array should be sent with reference ! As without reference ! The function check / detect is running for every element / vertex ! As the vis array is a copy in all the dfs calls !
@rutwikmore7462
@rutwikmore7462 Жыл бұрын
you are right
@aymanalali8983
@aymanalali8983 Жыл бұрын
arrays are auto referenced
@Jaydeeeeeeep
@Jaydeeeeeeep Жыл бұрын
​@@aymanalali8983is that Right, I wanted to know more about this
@SatyamKumar-bw4vi
@SatyamKumar-bw4vi Жыл бұрын
Hare Krishna.! Great Video
@addityasharma6426
@addityasharma6426 2 жыл бұрын
understood :-)
@kushagramishra3026
@kushagramishra3026 2 жыл бұрын
"Understood"
@isheep9025
@isheep9025 Жыл бұрын
Using non recursive DFS: class Solution { public: bool solver(int V,vector & visited,vector adj[],int child,int parent) { stack st; st.push({child,parent}); while(!st.empty()) { auto p=st.top(); int child=p.first; int parent=p.second; st.pop(); if(visited[child]==false) visited[child]=true; for(int ele:adj[child]) { if(!visited[ele]) { st.push({ele,child}); } else if(ele!=parent) return true; } } return false; } public: // Function to detect cycle in an undirected graph. bool isCycle(int V, vector adj[]) { vector visited(V,0); for(int i=0;i
@anuraggulati2167
@anuraggulati2167 2 жыл бұрын
uinderstood bhaiya
@anuragC819
@anuragC819 2 жыл бұрын
sir ek saath release kar do na videos (T - T)
@artifice_abhi
@artifice_abhi Жыл бұрын
understood++;
@gamerglobe4839
@gamerglobe4839 Жыл бұрын
can someone please help me to find out why this code is not working.. class Pair{ int first,second; public Pair(int first,int second){ this.first=first; this.second=second; } } class Solution { boolean dfscdetect(int node,int parent,ArrayList adj,int vis[]){ vis[node]=1; for(int nbs:adj.get(node)){ if(vis[nbs]==0){ return dfscdetect(nbs,node,adj,vis) || false; } else{ if(nbs!=parent) return true; } } return false; } // Function to detect cycle in an undirected graph. public boolean isCycle(int V, ArrayList adj) { int vis[]=new int[V]; for(int i=0;i
@d1vyanshu
@d1vyanshu Жыл бұрын
In the dfsdetect function you should return true only when dfsdetect(...) is true, otherwise it will prematurely return false if it is false.
@lakshsinghania
@lakshsinghania Жыл бұрын
@@d1vyanshu then why for bfs it was working without writing == true ? can u explain
G-13. Distance of nearest cell having 1 | 0/1 Matrix | C++ | Java
20:21
G-19. Detect cycle in a directed graph using DFS | Java | C++
17:22
take U forward
Рет қаралды 238 М.
Who’s the Real Dad Doll Squid? Can You Guess in 60 Seconds? | Roblox 3D
00:34
Life hack 😂 Watermelon magic box! #shorts by Leisi Crazy
00:17
Leisi Crazy
Рет қаралды 80 МЛН
G-11. Detect a Cycle in an Undirected Graph using BFS | C++ | Java
20:19
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 112 М.
Lecture 88: Cycle Detection in Undirected Graphs || Using BFS and DFS
32:03
CodeHelp - by Babbar
Рет қаралды 141 М.
G-17. Bipartite Graph | BFS | C++ | Java
18:29
take U forward
Рет қаралды 205 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 395 М.
G-10. Rotten Oranges | C++ | Java
22:30
take U forward
Рет қаралды 326 М.
Who’s the Real Dad Doll Squid? Can You Guess in 60 Seconds? | Roblox 3D
00:34