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

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии • 305

  • @takeUforward
    @takeUforward  2 года назад +131

    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 2 года назад +5

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

    • @rex027zz
      @rex027zz Год назад

      understood

    • @tasneemayham974
      @tasneemayham974 10 месяцев назад

      @@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!

    • @shashanksingh3720
      @shashanksingh3720 Месяц назад

      why not striver_69

    • @getskill8336
      @getskill8336 29 дней назад

      Understood

  • @rajneeshdubey5779
    @rajneeshdubey5779 10 месяцев назад +78

    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 8 месяцев назад +4

      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 7 месяцев назад +2

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

    • @jaishriharivishnu
      @jaishriharivishnu 6 месяцев назад

      nice

    • @gautamgupta7148
      @gautamgupta7148 6 месяцев назад

      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 6 месяцев назад +1

      @@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

  • @The_Shubham_Soni
    @The_Shubham_Soni Год назад +33

    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 10 месяцев назад

      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!!!!!!!

  • @lavanya_m01
    @lavanya_m01 9 месяцев назад +2

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

  • @dhruvbajaj5079
    @dhruvbajaj5079 2 года назад +27

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

  • @iflifewasmovie4766
    @iflifewasmovie4766 2 года назад +38

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

  • @tasneemayham974
    @tasneemayham974 10 месяцев назад +2

    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!!!

  • @sbrosgamerz3470
    @sbrosgamerz3470 Год назад +20

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

  • @kanishkpandey6427
    @kanishkpandey6427 2 года назад +11

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

  • @ajazahmed5079
    @ajazahmed5079 Год назад +4

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

  • @stith_pragya
    @stith_pragya Год назад +2

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

  • @iamnoob7593
    @iamnoob7593 3 месяца назад

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

  • @GAMIX7
    @GAMIX7 2 года назад +28

    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 года назад +27

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

  • @beinghappy9223
    @beinghappy9223 2 года назад +1

    Best explanation on Detecting Cycle Using BFS

  • @surojitsantra7627
    @surojitsantra7627 3 месяца назад +1

    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.

  • @cinime
    @cinime 2 года назад +2

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

  • @harshitsamdhani2426
    @harshitsamdhani2426 2 года назад +1

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

  • @DhanushS-we6ql
    @DhanushS-we6ql 3 месяца назад

    finally understood after 1 hour ..thanks

  • @hareshnayak7302
    @hareshnayak7302 5 месяцев назад

    Understood, ea pura wow wala solution 😄.

  • @bhavkushwaha
    @bhavkushwaha 5 месяцев назад +1

    Thankyou Striver, Understood!

  • @CellerCity
    @CellerCity 6 месяцев назад

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

  • @atharvadeshmukh6328
    @atharvadeshmukh6328 10 месяцев назад +1

    Understood, thanks!

  • @it_08amanagarwal35
    @it_08amanagarwal35 2 года назад +1

    Excellent Lecture bro ❤❤❤❤❤

  • @apmotivationakashparmar722
    @apmotivationakashparmar722 Месяц назад

    Thank you So much Striver !

  • @ganeshkumarhegde4680
    @ganeshkumarhegde4680 2 месяца назад

    Awesome explanation man!! Keep doing the good work. Thanks

  • @harshvardhansingh2272
    @harshvardhansingh2272 2 года назад

    understood striver
    great explanation

  • @umeshkaushik710
    @umeshkaushik710 2 года назад +2

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

  • @morganyu3391
    @morganyu3391 2 года назад +1

    Understood bhaiya!

  • @beginnertopro7265
    @beginnertopro7265 2 года назад +3

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

  • @introvert3487
    @introvert3487 2 года назад

    Extremely well as always

  • @hashcodez757
    @hashcodez757 4 месяца назад

    "UNDERSTOOD BHAIYA!!"

  • @ishangujarathi10
    @ishangujarathi10 Год назад

    amazing intuition as always!!!tysm

  • @DevashishJose
    @DevashishJose 11 месяцев назад

    Understood, Thank you so much.

  • @impatientgaming9868
    @impatientgaming9868 Год назад

    really good explanation and visualization.

  • @suheabkhan2546
    @suheabkhan2546 2 года назад

    understood very well 💖

  • @subhamoybera6456
    @subhamoybera6456 Год назад

    Great explanation

  • @nathonluke4058
    @nathonluke4058 2 года назад +1

    Understood.....🔥

  • @_hulk748
    @_hulk748 Год назад

    Thankyou sir understood🙏❤🙇‍♂

  • @sachinvarma9949
    @sachinvarma9949 6 месяцев назад

    god of coding for me

  • @akashjha822
    @akashjha822 2 года назад

    Understood striver ❤️

  • @mdsameer533
    @mdsameer533 2 года назад +1

    understood sir !

  • @anjani-d6p
    @anjani-d6p 5 месяцев назад

    keep up the good work

  • @parshchoradia9909
    @parshchoradia9909 Год назад

    Understood Sir!

  • @UECAshutoshKumar
    @UECAshutoshKumar Год назад +1

    Thank you sir

  • @jatilyadav4000
    @jatilyadav4000 2 года назад

    understood.. Thank you soo much

  • @AbdulKadir-bh3el
    @AbdulKadir-bh3el 2 года назад

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

  • @princepal9930
    @princepal9930 2 года назад

    understood very well

  • @vikasbagri1225
    @vikasbagri1225 2 года назад

    understood it very well

  • @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

  • @AbhishekKumar-xi6jd
    @AbhishekKumar-xi6jd 10 месяцев назад

    Noted ✌🏼

  • @adebisisheriff159
    @adebisisheriff159 11 месяцев назад

    Thanks Striver

  • @DeadPoolx1712
    @DeadPoolx1712 25 дней назад

    UNDERSTOOD:

  • @manavnaharwal1921
    @manavnaharwal1921 2 года назад

    Understood! 🙌

  • @AJ-xc3ks
    @AJ-xc3ks Год назад

    Outstanding..

  • @Kaurs_Life
    @Kaurs_Life Год назад

    Million Thanks

  • @LBK3
    @LBK3 Год назад

    Understood ❤

  • @gangsta_coder_12
    @gangsta_coder_12 2 года назад

    Understood 👍

  • @javierdavidhernandezestrad7334
    @javierdavidhernandezestrad7334 8 месяцев назад

    Thanks, understood

  • @ganeshmula4508
    @ganeshmula4508 Год назад

    understood sir🙏

  • @priyanshvatsal9791
    @priyanshvatsal9791 Год назад

    Understood 😇

  • @mriduljain6809
    @mriduljain6809 Год назад

    Understood Bhaiya

  • @ANURAGSINGH-nl2ll
    @ANURAGSINGH-nl2ll Год назад

    understood thank you

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 8 месяцев назад

    thank you bhaiya

  • @suryakiran2970
    @suryakiran2970 Год назад

    UnderStood❤

  • @adityasaxena6971
    @adityasaxena6971 Год назад

    Understood💯💯💯

  • @gautamsaxena4647
    @gautamsaxena4647 7 месяцев назад

    understood bhaiya

  • @studynewthings1727
    @studynewthings1727 8 месяцев назад

    Understood.

  • @Sarkar.editsz
    @Sarkar.editsz Год назад

    Thanks a lot bhaiya

  • @pratikshadhole6694
    @pratikshadhole6694 2 года назад +1

    perfect

  • @yashshukla1637
    @yashshukla1637 9 дней назад

    Added to notes

  • @rishabhagarwal8049
    @rishabhagarwal8049 2 года назад

    Understood sir

  • @sajidrec
    @sajidrec Год назад

    understood 😘😘

  • @abhinavkumar6584
    @abhinavkumar6584 2 года назад +1

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

  • @p38_amankuldeep75
    @p38_amankuldeep75 2 года назад

    understood🧡🧡🧡

  • @ashishbhardwaj4334
    @ashishbhardwaj4334 Год назад

    UNDERSTOOD

  • @anveshakarn4402
    @anveshakarn4402 2 года назад

    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 ?

  • @KartikeyTT
    @KartikeyTT 3 месяца назад

    tysm sir

  • @suhaanbhandary4009
    @suhaanbhandary4009 Год назад

    understood!!

  • @harshdasila6680
    @harshdasila6680 2 года назад +1

    understooodddd

  • @nguyengiahuy6292
    @nguyengiahuy6292 Год назад +2

    i think another way to make the coding's easier is to set the visit value of a visited node as the previous node, that way you dont need to change the whole data type

    • @adityavaste8963
      @adityavaste8963 Год назад +2

      This can be a solution
      public boolean isCycle(int V, ArrayList adj) {
      boolean[] visited = new boolean[V];
      for (int i = 0; i < V; i++) {
      if (!visited[i] && checkCycle(i, adj, visited)) {
      return true;
      }
      }
      return false;
      }
      public boolean checkCycle(int start, ArrayList adj, boolean[] visited) {
      Queue queue = new LinkedList();
      queue.offer(start);
      visited[start] = true;
      while (!queue.isEmpty()) {
      int top = queue.poll();
      for (int v : adj.get(top)) {
      if (!visited[v]) {
      visited[v] = true;
      queue.add(v);
      } else if (v != top) {
      // If the adjacent node is visited and not the parent, it indicates a cycle.
      return true;
      }
      }
      }
      return false;
      }

    • @anmolverma075
      @anmolverma075 Год назад +1

      @@adityavaste8963 this ones actually easier to grasp!

  • @manasranjanmahapatra3729
    @manasranjanmahapatra3729 2 года назад

    Understood!

  • @rudranshagg2580
    @rudranshagg2580 9 месяцев назад

    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 🤷‼

  • @prajwalhorti3717
    @prajwalhorti3717 2 года назад

    understood!

  • @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.

  • @3zam656
    @3zam656 Год назад

    ☑Understood

  • @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.

  • @mriduljain1981
    @mriduljain1981 Год назад

    understood.

  • @vakhariyajay2224
    @vakhariyajay2224 2 года назад

    Thank you very much. You are a genius.

  • @bhuvneshwargarg93
    @bhuvneshwargarg93 7 месяцев назад

    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.

  • @khushikalra9846
    @khushikalra9846 Год назад

    please make playlists for string as well

  • @amangaur4231
    @amangaur4231 2 года назад

    Understooood

  • @shrutyshruty7669
    @shrutyshruty7669 2 года назад +1

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

  • @rishabhgupta9846
    @rishabhgupta9846 2 года назад

    understood ,great explanation

  • @avtarchandra2407
    @avtarchandra2407 2 года назад

    as always LIT!

  • @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!

  • @kritisingh3194
    @kritisingh3194 2 года назад

    Understood. :)

  • @riddhibandyopadhyay584
    @riddhibandyopadhyay584 2 года назад +1

    Understood!!!! :D

  • @DIVYANSHMISHRA08
    @DIVYANSHMISHRA08 2 года назад

    Understood

  • @arunmahajan1028
    @arunmahajan1028 3 месяца назад +1

    11:57
    🥶

  • @soumya1713
    @soumya1713 2 года назад +1

    Waiting for your surprise #independentWithStiver.