G-11. Detect a Cycle in an Undirected Graph using BFS | C++ | Java

Поділитися
Вставка
  • Опубліковано 12 вер 2024

КОМЕНТАРІ • 278

  • @takeUforward
    @takeUforward  2 роки тому +119

    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

    • @aeroabrar_31
      @aeroabrar_31 Рік тому +5

      @takeUforward
      Please use Queue as a horizontal data structure , being confused as like stack..

    • @rex027zz
      @rex027zz Рік тому

      understood

    • @tasneemayham974
      @tasneemayham974 7 місяців тому

      @@aeroabrar_31 No I think if he started using horizontal data structure we will get confused because we are used to drawing it out like that from all previous lectures and series!

  • @rajneeshdubey5779
    @rajneeshdubey5779 6 місяців тому +62

    We can also solve it without storing the parent , A particular node can be marked visited only by its parent so when we reach a node and traverse all its adjacent node and find more than one visited ,that shows this node has more than one parent or we can simply say this particular node is also getting visited from another direction and in that case we can say there is cycle presert .
    class Solution {
    public:
    // Function to detect cycle in an undirected graph.
    bool bfs(int node,vector &vis, vector adj[]){
    queue q;
    q.push(node);
    vis[node]=1;
    while(!q.empty()){
    int frnt= q.front();
    q.pop();
    int cnt=0;
    for(auto it:adj[frnt]){
    if(!vis[it]){
    q.push(it);
    vis[it]=1;
    }
    else {
    cnt++;
    }
    }
    if(cnt>1) return true;
    }
    return false;
    }
    bool isCycle(int V, vector adj[]) {
    // Code here
    vector vis(V,0);
    for(int i=0;i

    • @AbhishekKumar-yv6ih
      @AbhishekKumar-yv6ih 5 місяців тому +1

      Nice Idea: since there can only be one parent. If 'else' part runs more than once, it means there is a cycle.

    • @IshaanJoshi-ms9mb
      @IshaanJoshi-ms9mb 4 місяці тому +2

      thanks for sharing this alternative, its always good to know more than one way to solve a problem.

    • @jaishriharivishnu
      @jaishriharivishnu 3 місяці тому

      nice

    • @gautamgupta7148
      @gautamgupta7148 3 місяці тому

      I still don't get it how. How will you know parent is visiting that node. If graph is like 1 then 2 3 and then 4 5 connected respectively then if we are at 4 then 2 is already visited. and then it will go in else case will do ++ and after that return true

    • @jaishriharivishnu
      @jaishriharivishnu 3 місяці тому

      @@gautamgupta7148 can't explain properly here.... But while iterating in the adj list... If there are more than two visited nodes, then it will have cycle..... This is same concept as described by the guy above

  • @iflifewasmovie4766
    @iflifewasmovie4766 2 роки тому +34

    Thank you for this series and seriously this series is much much finer than the previous one!! Thank you.

  • @The_Shubham_Soni
    @The_Shubham_Soni Рік тому +13

    People told me that graph is a hard topic and many people failed to do so, but Striver's Graph series is just Amazing it looks quite easy for me. ♥

    • @tasneemayham974
      @tasneemayham974 7 місяців тому

      I look at the comments in LeetCode's Discussion tab when they talk about DP and I go like: why the hell is everybody scared of DP. It's not that hard.
      ONLY HAPPENS WHEN YOU LEARN FROM STRIVERRR1!!!!!!!

  • @GAMIX7
    @GAMIX7 2 роки тому +24

    Hey Striver, amazing video, there's one suggestion: You don't need to loop once in boolean array, as in Java the values are false by default, same for int[] array its filled with 0 initially,
    Well, in DP I use Arrays.fill(arr, -1); //Internally this method runs a loop and saves much code space.
    Edit: we can also use if( !vis[i] ) //this will check if it is false or not, to check if the node is already visited or not!
    Love your videos! Thanks

    • @takeUforward
      @takeUforward  2 роки тому +25

      Thanks, I was googling and writing java code. Will keep this in mind.

  • @dhruvbajaj5079
    @dhruvbajaj5079 Рік тому +19

    This is coder's gold mine. Thank you for such great explanations.

  • @sbrosgamerz3470
    @sbrosgamerz3470 Рік тому +15

    This Guy is like the greatest teacher for any student......Great Work Guruji 😉☺️

  • @lavanyam3224
    @lavanyam3224 6 місяців тому +2

    11:50 Wow the climax of the movie!! Amazing explanation striver. Thankyou :)

  • @ajazahmed5079
    @ajazahmed5079 Рік тому +4

    really great explanations covering all the edge cases and imp. small concepts in depth. You are really great bhaiya!

  • @iamnoob7593
    @iamnoob7593 3 дні тому

    Just came for revision , Man u r a legend So amazing explanation.

  • @kanishkpandey6427
    @kanishkpandey6427 2 роки тому +7

    3 saal graph aadha adhura padha...ab raj baba ki kripa h ki roz ek video dekh ke confidence aa jata h XD

  • @tasneemayham974
    @tasneemayham974 7 місяців тому +1

    11:52 Bro! It seems like someone stole ur child! LOLLLL!! Only Striver can have that much fun and enthusiasm in a lesson!! Thank youuu sir for making us smile and learn at the same time!!!

  • @surojitsantra7627
    @surojitsantra7627 10 днів тому

    If for a node, any 2 connected nodes are already visited then we can say it is cycle. Because one of them will be parent and other one is already visited by some other node.
    For example, 2->3,4,5
    Let say 3 and 4 already visited then one of them must be parent and other one is visited by some other node.
    In that way we xan avoid to store the parent info in the queue.

  • @stith_pragya
    @stith_pragya 9 місяців тому +2

    Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @cinime
    @cinime 2 роки тому +2

    Understood! Awesome explanation as always, thank you so much!!

  • @beinghappy9223
    @beinghappy9223 Рік тому +1

    Best explanation on Detecting Cycle Using BFS

  • @bhavkushwaha
    @bhavkushwaha Місяць тому +1

    Thankyou Striver, Understood!

  • @beginnertopro7265
    @beginnertopro7265 Рік тому +3

    Bhaiya your teaching style is awesome 🔥🔥🔥🔥🔥🔥🔥,
    No Any youtuber can explain like u 😊😊

  • @umeshkaushik710
    @umeshkaushik710 2 роки тому +2

    Awesome explanation bhaiya. You inspire us a lot. Hope to meet you someday.😇😇

  • @hareshnayak7302
    @hareshnayak7302 2 місяці тому

    Understood, ea pura wow wala solution 😄.

  • @atharvadeshmukh6328
    @atharvadeshmukh6328 7 місяців тому +1

    Understood, thanks!

  • @bhuvneshwargarg93
    @bhuvneshwargarg93 4 місяці тому

    Hi Striver, Great explanation. I have one query and i.e. To solve any problem if we are using queue, in that case do we need to use queue with array or queue with linked list? Specially when programming with javascript. Thanks.

  • @CellerCity
    @CellerCity 3 місяці тому

    Thanks! I was stuck at this problem for too long.

  • @gokulakrishnan5096
    @gokulakrishnan5096 2 роки тому +4

    Thanks for the lecture. Just a quick question, if there is a time limitation, is there any difference if I just do the older graph series? What would you recommend? ( I have only 1-2 weeks remaining)

    • @takeUforward
      @takeUforward  2 роки тому +10

      Yes you can, just check the problems that I add here

    • @gokulakrishnan5096
      @gokulakrishnan5096 2 роки тому +2

      @@takeUforward thanks a lot for the quick response!

  • @harshitsamdhani2426
    @harshitsamdhani2426 Рік тому

    understood repeating is helpful and overall explanation is best that I could find in youtube

  • @morganyu3391
    @morganyu3391 2 роки тому +1

    Understood bhaiya!

  • @aditiagarwal3222
    @aditiagarwal3222 Рік тому +3

    hiii.. will this logic still work if there are only 2 nodes in the graph and a cycle exists between them such that 0->1 and 1->0? because in that case the iterator will always be equals to parent??

    • @SHRUGX_YT
      @SHRUGX_YT Рік тому

      I too have the same doubt! please comment if you get the answer elsewhere.

    • @adityamohite9023
      @adityamohite9023 Рік тому +3

      This is an undirected graph thus the condition u mentioned will always be true for all the edges . Hence we have to detect the cycle in the graph as a whole with undirected edges which will always be formed with 3 or more edges.

    • @aditiagarwal3222
      @aditiagarwal3222 Рік тому

      Alright thank you

    • @nipunrawat7137
      @nipunrawat7137 Рік тому

      i guess that cannot be considered as a cycle.

  • @ishangujarathi10
    @ishangujarathi10 Рік тому

    amazing intuition as always!!!tysm

  • @anjani-d6p
    @anjani-d6p Місяць тому

    keep up the good work

  • @it_08amanagarwal35
    @it_08amanagarwal35 2 роки тому +1

    Excellent Lecture bro ❤❤❤❤❤

  • @impatientgaming9868
    @impatientgaming9868 8 місяців тому

    really good explanation and visualization.

  • @hashcodez757
    @hashcodez757 29 днів тому

    "UNDERSTOOD BHAIYA!!"

  • @DevashishJose
    @DevashishJose 7 місяців тому

    Understood, Thank you so much.

  • @rudranshagg2580
    @rudranshagg2580 6 місяців тому

    Doubt in TC:
    We are not multiplying and are only adding the TC O(V) for looping on the visited vector , reason given by striver for that is because
    we are not going to call that for each node as most nodes will be visited when starting from a node , but my doubt is still hum call toh number of
    nodes ke liye hi krenge na vis vector vale loop mein voh alag baat h ki dfs() function call kis kis ke liye hoga 🤷‼

  • @introvert3487
    @introvert3487 2 роки тому

    Extremely well as always

  • @randomvariate21
    @randomvariate21 Рік тому +1

    very well
    understood

  • @Niki_0011
    @Niki_0011 4 місяці тому

    Understood ❤❤❤

  • @user-ci6dd9wl6l
    @user-ci6dd9wl6l 4 місяці тому

    1- remove from the graph the nodes that have less than 2 connections
    2- count the connections
    3- count the nodes
    4 - if the connections are equals or more than the nodes you have a cycle
    how is this method called?🙃

  • @nathonluke4058
    @nathonluke4058 2 роки тому +1

    Understood.....🔥

  • @harshvardhansingh2272
    @harshvardhansingh2272 2 роки тому

    understood striver
    great explanation

  • @codeman3828
    @codeman3828 4 місяці тому

    Awesome explanation

  • @abhijeetkumar2970
    @abhijeetkumar2970 Рік тому +1

    understood

  • @abhinavkumar6584
    @abhinavkumar6584 2 роки тому +1

    understood. PS: I guess there wasn't any need to pass V in bfs function

  • @shrutyshruty7669
    @shrutyshruty7669 2 роки тому +1

    Understood
    Can you please tell till when will you upload all the remaining videos.

  • @riddhibandyopadhyay584
    @riddhibandyopadhyay584 Рік тому +1

    Understood!!!! :D

  • @RohitKumar-dz8dh
    @RohitKumar-dz8dh Місяць тому

    Understood 😊

  • @jatilyadav4000
    @jatilyadav4000 Рік тому

    understood.. Thank you soo much

  • @anveshakarn4402
    @anveshakarn4402 Рік тому

    bhya , @8:25 , u said someone at the same level shd have touched it, bcoz it's an even cycle, but if it's an odd length cycle, someone at same or a prior level should have touched it, would follow right? m confiused at that.. @take U forward ?

  • @ishanupadhyay5351
    @ishanupadhyay5351 Рік тому +1

    Sir, why the visited array is not passed by reference in the function?

    • @backbencher1209
      @backbencher1209 Рік тому +2

      because array is already passed by reference .

  • @potassium_cyanide_boy8515
    @potassium_cyanide_boy8515 Рік тому +4

    bhai gusse me bhi arre the lagra beech beech me

  • @suheabkhan2546
    @suheabkhan2546 2 роки тому

    understood very well 💖

  • @mdsameer533
    @mdsameer533 Рік тому +1

    understood sir !

  • @UECAshutoshKumar
    @UECAshutoshKumar Рік тому +1

    Thank you sir

  • @manavnaharwal1921
    @manavnaharwal1921 2 роки тому

    Understood! 🙌

  • @_hulk748
    @_hulk748 Рік тому

    Thankyou sir understood🙏❤🙇‍♂

  • @AJ-xc3ks
    @AJ-xc3ks 10 місяців тому

    Outstanding..

  • @avtarchandra2407
    @avtarchandra2407 2 роки тому

    as always LIT!

  • @rupalitiwari5012
    @rupalitiwari5012 2 роки тому

    Hi, Why are you making another video on the same topic that you already covered 1 year ago, which one should we choose to watch?

  • @rajpattern
    @rajpattern 7 місяців тому +1

    Am I the only one who doesn't understand why he uses the + sign instead of the * sign in time complexity?? Please help if you understand!

  • @vikasbagri1225
    @vikasbagri1225 2 роки тому

    understood it very well

  • @nishanmurarka3137
    @nishanmurarka3137 Рік тому +1

    what if instead of using a parent variable we use a map to check all the current elemets present in the queue and if the element is visited and also present in the map then it cannot be parent so must be a cycle

  • @shubhambhatt2704
    @shubhambhatt2704 Рік тому +1

    12:01 can anyone please help me with that logic?
    I dont get what he is trying to say there

    • @no_name45689
      @no_name45689 Рік тому +1

      Well, Parent here means the previous node. And suppose, if next node is already visited but it is not previous node then it means, cycle exist.

  • @AbdulKadir-bh3el
    @AbdulKadir-bh3el 2 роки тому

    You are just amazing!!!, Plz keep uploading such a premium content.

  • @vishalharne7674
    @vishalharne7674 Рік тому

    you are just amazing bro i had watched babbar DSA 90 videos i do tell you that you have very nice skill of teaching i am not comparing but ya you are like
    suryakumar yadav
    kam me jyada ka maja
    to the point straight to the concept

  • @mirdulswarup9065
    @mirdulswarup9065 Рік тому +2

    can someone tell me why the visited array is not being passed as reference ?

    • @visheshkaran7083
      @visheshkaran7083 9 місяців тому

      Because in c++ arrays are already passed as reference you don't need to write &

  • @sanketatmaram
    @sanketatmaram 4 дні тому

    understood!

  • @harshdasila6680
    @harshdasila6680 2 роки тому +1

    understooodddd

  • @khushikalra9846
    @khushikalra9846 Рік тому

    please make playlists for string as well

  • @adebisisheriff159
    @adebisisheriff159 8 місяців тому

    Thanks Striver

  • @pratikshadhole6694
    @pratikshadhole6694 Рік тому +1

    perfect

  • @javierdavidhernandezestrad7334
    @javierdavidhernandezestrad7334 5 місяців тому

    Thanks, understood

  • @LearnerForever123
    @LearnerForever123 Рік тому +1

    Understood

  • @parshchoradia9909
    @parshchoradia9909 Рік тому

    Understood Sir!

  • @akashjha822
    @akashjha822 2 роки тому

    Understood striver ❤️

  • @sachinvarma9949
    @sachinvarma9949 3 місяці тому

    god of coding for me

  • @AbhishekKumar-xi6jd
    @AbhishekKumar-xi6jd 7 місяців тому

    Noted ✌🏼

  • @ANURAGSINGH-nl2ll
    @ANURAGSINGH-nl2ll Рік тому

    understood thank you

  • @arunmahajan1028
    @arunmahajan1028 5 днів тому +1

    11:57
    🥶

  • @Ayush_Tomar01
    @Ayush_Tomar01 Рік тому +1

    Bro what is the difference between if(adjacentNode != parent) and if(parent != adjacentNode) Plz tell 🙏🙏

  • @Kaurs_Life
    @Kaurs_Life 9 місяців тому

    Million Thanks

  • @Temporary_handle_10
    @Temporary_handle_10 Рік тому

    Bhaiya, why is the clarity so less?? while writing code in gfg??

  • @sumitworld918
    @sumitworld918 7 місяців тому

    Understood 😃

  • @KartikeyTT
    @KartikeyTT 6 днів тому

    tysm sir

  • @user-vx4wj2iq6u
    @user-vx4wj2iq6u 3 місяці тому

    What is the use of parent array here

  • @Sarkar.editsz
    @Sarkar.editsz Рік тому

    Thanks a lot bhaiya

  • @vasuoberoi1810
    @vasuoberoi1810 2 роки тому

    which whiteboard u are using?

  • @MARK-hw8jm
    @MARK-hw8jm 3 місяці тому +2

    Why not dfs??

  • @karanveersharma4643
    @karanveersharma4643 10 місяців тому +1

    am i the only one who isn't able to understand the formate in which input are supplied in the question?

  • @muditkhanna8164
    @muditkhanna8164 Рік тому

    does self cycle exist in this graph?

  • @ganeshmula4508
    @ganeshmula4508 Рік тому

    understood sir🙏

  • @soumya1713
    @soumya1713 2 роки тому +1

    Waiting for your surprise #independentWithStiver.

  • @manasranjanmahapatra3729
    @manasranjanmahapatra3729 2 роки тому

    Understood!

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l 4 місяці тому

    thank you bhaiya

  • @mriduljain6809
    @mriduljain6809 Рік тому

    Understood Bhaiya

  • @adityasaxena6971
    @adityasaxena6971 Рік тому

    Understood💯💯💯

  • @googleit2490
    @googleit2490 Рік тому

    Potential Mistake: Ugnoring to consider there may be more than one disjoint set in the graph
    Sep'5, 2023 10:34 pm

  • @studynewthings1727
    @studynewthings1727 4 місяці тому

    Understood.

  • @gautamsaxena4647
    @gautamsaxena4647 4 місяці тому

    understood bhaiya

  • @suhaanbhandary4009
    @suhaanbhandary4009 Рік тому

    understood!!

  • @kritisingh3194
    @kritisingh3194 2 роки тому

    Understood. :)