Why Floyd's cycle detection algorithm works? Detecting loop in a linked list.

Sdílet
Vložit
  • čas přidán 23. 06. 2017
  • Why Floyd's cycle detection algorithm works? Detecting loop in a linked list. You have to check whether there is a cycle in a linked list and find out the starting node of the cycle or the loop. You can also find out the length of the loop. Also known as Tortoise and Hare algorithm.
    Detect loop in linked list :- • Detect loop in linked ...

Komentáře • 256

  • @bharaniakella7734
    @bharaniakella7734 Před 7 lety +222

    This guy should be a youtube celebrity! People like him who share knowledge should be made famous.

  • @Photon7908
    @Photon7908 Před 5 lety +162

    I guess one minor point that I couldn't find in his video is this:
    (m+k) = integer*l -> this means, if you have p at the kth node and q at the start, by the time q travels m+k nodes from the start, p will finish one complete revolution on that loop.
    i.e., p and q will coincide at the kth node again.
    But if q only travels m nodes, p has travelled only (integer*l)- k nodes as well. So if p started at the kth node, and q at the start, they will meet at the start of the loop.
    (I hope this makes sense as it does in my head lol)
    Thanks for the video Vivek!

    • @vaibhavrbs
      @vaibhavrbs Před 5 lety +3

      ``But if q only travels m nodes, p has travelled only (integer*l)- k nodes as well. So if p started at the kth node, and q at the start, they will meet at the start of the loop.``
      can you explain this part again please, this is not clear to me,

    • @untwaddle
      @untwaddle Před 5 lety +35

      @@vaibhavrbs p has already made k steps from the start of loop so to reach the start point again, it should travel (int * l) - k. it equals that q will make only m steps to reach the start point.
      other words, we had equation: m + k = int * l and then we subtract k from both sides. equation is still correct, now q will make m steps to reach the start point and at this place it will meet p. (p made (int*l)-k steps)

    • @sadmansakib007
      @sadmansakib007 Před 4 lety +11

      You are absolutely right. Something was missing in that video but THAT"s the main point!

    • @mhp3bozis
      @mhp3bozis Před 4 lety +10

      Honestly think THIS should be the main focus, thanks for pointing out

    • @kaustavgalacticos
      @kaustavgalacticos Před 4 lety +3

      You missed one minor key point as well, ASSUMING q is equal to p after q starts from 0 and p starts from k, and *your rest of the logic*.

  • @KM-star
    @KM-star Před 5 lety +8

    OMG! This is the 1st video I am watching of this person and I am in love with his way of explaining things!
    I hope the rest of his videos are equally good.
    I can now recollect some friend recommending this person's videos.
    @Vivekanand ...You definitely have a fan base!

  • @PankajSaini-pi9ko
    @PankajSaini-pi9ko Před 5 lety +31

    worth spending 24 minutes , good work.

  • @ankitvishnoi8670
    @ankitvishnoi8670 Před 5 lety +2

    I really enjoy your videos and your videos are helping a lot of people as we. I recommended your channel to many of my colleague and college friends. Keep up the good work. ;)

  • @lokeshjaliminche
    @lokeshjaliminche Před 4 lety

    Good to see you after a long time! Great work.
    Good going!! :)

  • @ashwanigupta5412
    @ashwanigupta5412 Před 3 lety +3

    Hi Vivekanand sir, can't thank you enough for making this video. I have seen almost half a dozen videos on this online and after this I can understand the logic behind it fully. Worth spending 24 minutes. Kudos to you!

  • @blackswordsman9745
    @blackswordsman9745 Před 6 lety +1

    Your videos are really helpful. Thanks a lot for making them :)

  • @danielstatler954
    @danielstatler954 Před rokem

    after a couple rewinds, i finally got it. i doubt i could ever come up with this algorithm on my own. good vid

  • @akki4u
    @akki4u Před 5 lety +1

    Your explanation is so good that makes learning easy. Great work my friend!

  • @leolin5479
    @leolin5479 Před 4 lety +1

    You really save me from struggling with the textbooks and any other recourses I could find, thanks !

  • @robb1931
    @robb1931 Před 4 lety +3

    Finally, there's a video that helps me understand this algorithm. Thanks a lot!

  • @ananyaarya2465
    @ananyaarya2465 Před 3 lety

    Thank you so much

  • @denys_kovpaka
    @denys_kovpaka Před 2 lety

    I like the way you explained this. Thank you a lot)

  • @chrisdanan2183
    @chrisdanan2183 Před 2 lety

    Wonderful explanation - thank you very much.

  • @ShwetaNaganath
    @ShwetaNaganath Před 6 lety +1

    Thank you. i was struggling with this since long.

  • @omkar.at.office
    @omkar.at.office Před rokem

    Awesome explanation! Thanks 🙏

  • @tushargarg3765
    @tushargarg3765 Před 3 lety

    Thanks for such a Nice Explanation.

  • @rehaanredkar1489
    @rehaanredkar1489 Před 3 lety

    this man is unbelievable , the way he explains is just mind blowing

  • @anandkulkarni2111
    @anandkulkarni2111 Před 6 lety +3

    doing good job!! excellent work keep them coming !!!

  • @rohitkumarvarma4952
    @rohitkumarvarma4952 Před 2 lety

    Thanks, mann! Finally understood this concept clearly!

  • @columbiars
    @columbiars Před 6 lety

    Wow, excellent explanation dude! I was trying to figure out why this algorithm worked, and finally I found the right answer. Keep going this videos!

  • @algorithmimplementer415
    @algorithmimplementer415 Před 2 lety +1

    I am convinced that I understood the explanation. Thank you!

  • @gugliamo
    @gugliamo Před 2 lety

    this was great, thank you!

  • @ankitvarmait
    @ankitvarmait Před 6 lety +1

    Thank you for wonderful explanation

  • @anubhavsinha8048
    @anubhavsinha8048 Před 4 lety

    Very nice explanation!

  • @glassanddiaphane
    @glassanddiaphane Před 4 lety

    This is awesome, this really helped me to finally understand it, thank you.

  • @DeepakGupta-pl6bc
    @DeepakGupta-pl6bc Před 2 lety

    Excellent explanation!

  • @secretman1787
    @secretman1787 Před 3 lety

    Excellent explanation !!!

  • @ayushsakure6098
    @ayushsakure6098 Před rokem

    Very well explained.

  • @reyou7
    @reyou7 Před 5 lety

    Wow man this was just amazing, thanks a ton!

  • @elizkotadia9350
    @elizkotadia9350 Před 7 lety

    Great Job !!! Crystal clear explanation !!

  • @caio-jl6qw
    @caio-jl6qw Před 3 lety

    WOW, very very VERY good explanations, thank u for these videos !!!!!!!!!!!!

  • @arpitamandal6469
    @arpitamandal6469 Před 3 lety

    thanks for explaining it so well..

  • @loba8924
    @loba8924 Před 2 lety

    Thanks. Nice explanation

  • @DurgaShiva7574
    @DurgaShiva7574 Před 4 lety

    you are seriously amazing bro...i love your knowledge and teaching way, keep up the good work..

  • @azam148
    @azam148 Před 5 lety

    Very clear explanation!

  • @kirthanasingh635
    @kirthanasingh635 Před 3 lety

    Your Explanation is amazing Sir!! Keep making videos

  • @LogEekumanAgination
    @LogEekumanAgination Před 5 lety

    That was beautiful and made me happy thank you so much

  • @jaibhambri6853
    @jaibhambri6853 Před 3 lety

    Very Nice Explanation

  • @shubhamjadhav2656
    @shubhamjadhav2656 Před 4 lety

    very well explanation!

  • @mariammohamed176
    @mariammohamed176 Před 2 lety

    Thanks, it is the best explanation I have seen so far

  • @kbhoyi
    @kbhoyi Před 2 lety +1

    OMG - Such a beautiful explanation! I'm listening to Vivek Sir's video first time. I'm sure, I'm in for a treat when I will search for other videos of his teaching. Stay blessed Sir.

  • @mangeshgupta5677
    @mangeshgupta5677 Před 4 lety

    Great explaination thank you

  • @shashankmehul02
    @shashankmehul02 Před 4 lety

    Such wonderful explanation.Very helpful.

  • @null-wp1le
    @null-wp1le Před 3 lety

    Thank you so much for this video.

  • @farabikurmanshady8486
    @farabikurmanshady8486 Před 5 lety

    Clear explanation! Thanks!

  • @amitvaghela1456
    @amitvaghela1456 Před 3 lety

    Great simplified explanations.

  • @truthprevails899
    @truthprevails899 Před 4 lety

    Thanks for the explanation!

  • @iam_kundan
    @iam_kundan Před 6 lety

    Excellent video !!

  • @kishantiwari3221
    @kishantiwari3221 Před 3 lety +1

    Thank You sir!

  • @isaacc9487
    @isaacc9487 Před 3 lety

    This is very clear and detailed, thanks a lot.

  • @NarendraKolink
    @NarendraKolink Před 5 lety +2

    really helpful... Thanks a lot

  • @jamesclark1207
    @jamesclark1207 Před 4 lety

    Really excellent explanation - thank you

  • @aterribleyoutuber9039

    Thanks a lot! I was struggling to understand this at first!

  • @SoumikNandi
    @SoumikNandi Před 4 lety +1

    One Point to mention.!!
    Rather than saying that Distance of P traveled would be Double the Distance of Q because "P's speed is twice than that of Q" kind of makes it incorrect statement. What we can explain is
    Let's say they start their journey at time T = 0. Now when they meet the time elapsed would be equal (Why? Because the time frame is absolute. Doesn't matter how they move they will meet only when they are in the same position at the SAME TIME)
    So, we know S = D/T (Speed = Distance/Time which also means T = D/S), from the equation we can say
    Tp (Time taken for P to reach the meeting point) = Tq ( Time taken for Q to reach the meeting point)
    or
    (m + pl + k)/2 = (m+ql+k)/1
    Now work up to get, ---> (m+k) = l * (p-2q)
    NOTE: Another thing worth noting is that (2q-p) is also an integer but it'll never take the form of (2q-p) (Why? Because the left-hand side has a dimension of length (scalar) and hence cannot be negative).. Now, why is that (2q-p) is likely to be negative? Because P's speed is greater than Q. So its likely that P would travel the loop more number of times than Q will
    Hence,
    p > q/2 --> (Why q/2 ? follows from the above discussion)
    Or, (p - 2q) > 0 but (q-2p) < 0
    It was just a suggestion!. The explanation is wonderful. It's just a part where I felt needed a little bit more clarification

  • @___vandanagupta___
    @___vandanagupta___ Před 3 lety +1

    your teaching style is amazing ! i dont know why u dont have million subs

  • @aynurmukhambetova1708
    @aynurmukhambetova1708 Před 4 lety

    it finally became clear! thanks a lot!

  • @neeraj33negi
    @neeraj33negi Před 3 lety

    Thanks, this is very detailed.

  • @dragosc8202
    @dragosc8202 Před 3 lety

    Thanks! YOU make my day

  • @liangchen8238
    @liangchen8238 Před 4 lety

    Best explanation I've seen so far!

  • @kingdey9136
    @kingdey9136 Před 4 lety +1

    Thank you!

  • @varungunda2177
    @varungunda2177 Před 7 lety

    Thank you very much Sir.. It was very helpful :)

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt Před 3 lety

    very well explained thank you..............................

  • @winstonloke2860
    @winstonloke2860 Před 4 lety +1

    Hi,
    I have no knowledge about algo, but i do have a question.
    in detecting the start of the loop, what happens if Q started at say, a different index than stated?
    P will not equal Q throughout the loop.

  • @pratikghorpade6853
    @pratikghorpade6853 Před 6 lety

    Thank you sir ..... Great explanation ....

  • @adityaraghav8693
    @adityaraghav8693 Před 4 lety

    This was awesome 👍

  • @asgheralishaik
    @asgheralishaik Před 3 lety

    nice explannation.Thanks

  • @vadimtitko7087
    @vadimtitko7087 Před 4 lety

    Thanks a lot! It really helps ;)

  • @ashishdukare1313
    @ashishdukare1313 Před 3 lety

    Awesome explanation Sir : )

  • @monisattili7642
    @monisattili7642 Před 7 lety

    Thank you , keep continue

  • @availkrishmytube
    @availkrishmytube Před 3 lety

    Excellent!

  • @saravanansarangan7035
    @saravanansarangan7035 Před 4 lety +1

    I got same confusion after seen the solution on leetcode. How they meet eachother at the beginning of the cycle but now it is clear. Thanks Viveka you are great...

  • @jeezz4128
    @jeezz4128 Před 3 lety

    I didn't see such a clr explanation ever .... keep going sir

  • @shamanthakrishnakg1978

    Thank You very much.

  • @yogeshvaishnav7856
    @yogeshvaishnav7856 Před 3 lety

    Bestest explaination ever!

  • @abhishekshrm100
    @abhishekshrm100 Před 6 lety

    very nice explanation

  • @chakrapanisudarshan3027

    Brilliant Explanation in simple words.....

  • @swatisahu1073
    @swatisahu1073 Před 2 lety

    Wow! Thanks.

  • @shwetasingh7906
    @shwetasingh7906 Před 7 lety +6

    Thanks sir

  • @ngc35ster
    @ngc35ster Před 2 lety

    Thanks man, this is way more clear than the leetcode answer

  • @karanb2067
    @karanb2067 Před 4 lety

    heroic explanation

  • @adityasingh11156
    @adityasingh11156 Před 4 lety

    Thanks Sir
    You make it understand in very simple way

  • @jamietherooster
    @jamietherooster Před 7 lety +26

    You are very good, I am english but find your videos far clearer than any native english speaker's video's. How do you gain the knowledge of these algorithms ? do you have a book ? I purchased 'introduction to algorithms' but find it very technical and hard to understand.

    • @sharansrivatsa210
      @sharansrivatsa210 Před 5 lety +6

      Honestly, I think its just going through this book called CLRS and preparing by just looking at more and more problems and how to solve them
      If you go to leetcode.com and start solving I'm sure you will start to get an intuition
      Also, these are algorithms that are used and proved. Its a lot about just learning these.
      geeksforgeeks is a good resource too.

  • @pratiknaik261
    @pratiknaik261 Před rokem

    Thanks sir ek no!!💯💯

  • @ShivamKendre-fc3su
    @ShivamKendre-fc3su Před 4 lety

    What a explanation!!!

  • @rahulchudasama9363
    @rahulchudasama9363 Před 4 lety

    Nice explanation ...

  • @ishanksharma2785
    @ishanksharma2785 Před 3 lety

    How will the slow pointer traverse the cycle multiple times?
    The distance for slow pointer should be m+k right? Since the slow pointer won't traverse the loop again.

  • @himanshujhamb
    @himanshujhamb Před 7 lety

    Nice explanation

  • @omarmneimneh983
    @omarmneimneh983 Před 5 lety

    THANK YOU!

  • @kbhargavi4400
    @kbhargavi4400 Před 3 lety

    thank you, sir, you explained such a confusing concept very well! :)

  • @prabhat2342
    @prabhat2342 Před 3 lety

    Just awesome😊

  • @naseemahmad2167
    @naseemahmad2167 Před 6 lety

    Great explanation sir I salute you it's help me allot

  • @da_sci3839
    @da_sci3839 Před 3 lety

    1. Best explanation I've found to describe this! No other explanation made sense.
    2. I love the way you say "loop" XD.

  • @KhanSlayer
    @KhanSlayer Před 5 lety

    Very clear and concise explanation. Nice work.

  • @intechshala5929
    @intechshala5929 Před 3 lety

    kyaa bataya hain aaapne jo koi na samgha saka aapne samgha diya bohot sahi

  • @mohan-ri6ze
    @mohan-ri6ze Před 3 lety

    Understood well

  • @cliffordbamert7728
    @cliffordbamert7728 Před 2 lety

    thank you

  • @XXX-nd2zz
    @XXX-nd2zz Před 5 lety

    clear explanation :D!!

  • @hailong5871
    @hailong5871 Před 5 lety

    You are awesome!!!