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

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024
  • 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 ...

ความคิดเห็น • 256

  • @bharaniakella7734
    @bharaniakella7734 7 ปีที่แล้ว +221

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

    • @jackzhu6482
      @jackzhu6482 4 ปีที่แล้ว +1

      can't agree more, very well explanation!

    • @kingdey9136
      @kingdey9136 4 ปีที่แล้ว

      he is a celebrity already

    • @sarvodaykumar2723
      @sarvodaykumar2723 3 ปีที่แล้ว

      Must be

  • @Photon7908
    @Photon7908 6 ปีที่แล้ว +164

    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 5 ปีที่แล้ว +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 5 ปีที่แล้ว +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 4 ปีที่แล้ว +11

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

    • @mhp3bozis
      @mhp3bozis 4 ปีที่แล้ว +10

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

    • @kaustavgalacticos
      @kaustavgalacticos 4 ปีที่แล้ว +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*.

  • @PankajSaini-pi9ko
    @PankajSaini-pi9ko 6 ปีที่แล้ว +31

    worth spending 24 minutes , good work.

  • @ashwanigupta5412
    @ashwanigupta5412 3 ปีที่แล้ว +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!

  • @KM-star
    @KM-star 5 ปีที่แล้ว +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!

  • @robb1931
    @robb1931 4 ปีที่แล้ว +3

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

  • @leolin5479
    @leolin5479 4 ปีที่แล้ว +1

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

  • @saravanansarangan7035
    @saravanansarangan7035 5 ปีที่แล้ว +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...

  • @rehaanredkar1489
    @rehaanredkar1489 3 ปีที่แล้ว

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

  • @jamietherooster
    @jamietherooster 7 ปีที่แล้ว +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 5 ปีที่แล้ว +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.

  • @da_sci3839
    @da_sci3839 3 ปีที่แล้ว

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

  • @danielstatler954
    @danielstatler954 ปีที่แล้ว

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

  • @jeezz4128
    @jeezz4128 3 ปีที่แล้ว

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

  • @algorithmimplementer415
    @algorithmimplementer415 3 ปีที่แล้ว +1

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

  • @___vandanagupta___
    @___vandanagupta___ 4 ปีที่แล้ว +1

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

  • @ankitvishnoi8670
    @ankitvishnoi8670 5 ปีที่แล้ว +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. ;)

  • @ananyaarya2465
    @ananyaarya2465 3 ปีที่แล้ว

    Thank you so much

  • @intechshala5929
    @intechshala5929 3 ปีที่แล้ว

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

  • @kbhoyi
    @kbhoyi 3 ปีที่แล้ว

    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.

  • @learningpool5918
    @learningpool5918 3 ปีที่แล้ว +2

    This man is gold. But I like watching it at 1.75x speed

  • @SoumikNandi
    @SoumikNandi 4 ปีที่แล้ว +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

  • @mariammohamed176
    @mariammohamed176 2 ปีที่แล้ว

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

  • @robertr6054
    @robertr6054 4 ปีที่แล้ว

    Just a minor point. When you’re doing the difference step you can just look at (m+k)=L*P. 2q will always be 0, as for any sized loop q will never be able to complete a full rotation before p catches up to it. P will also never be able to skip over Q in a loop. You can look at the most favorable example, if q enter the loop as p has just skipped over it. Before q will be able to traverse the rest of the loop to make a full rotation p will already have done so and be equal to q.

  • @apiltamang2767
    @apiltamang2767 5 ปีที่แล้ว +4

    props for setting up the video.
    I think it'd help your videos tremendously if you prepared your speech in advance on the idea you're talking about, and especially so for a convolved topic like this, rather than just give an impromptu performance. I think that would help the video be more concise, and perhaps even better explained.
    Regardless, first time learning this material and I think I understand it just fine. So you must have done it right, albeit with some effort :)

  • @moghalsalwar6803
    @moghalsalwar6803 ปีที่แล้ว

    I feel bore even for 10mins video ..but I didn't feel bore this tym when I am seeing this particular video...Thank U Sir

  • @denys_kovpaka
    @denys_kovpaka 2 ปีที่แล้ว

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

  • @aterribleyoutuber9039
    @aterribleyoutuber9039 2 ปีที่แล้ว

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

  • @akki4u
    @akki4u 5 ปีที่แล้ว +1

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

  • @lokeshjaliminche
    @lokeshjaliminche 4 ปีที่แล้ว

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

  • @konstantinrebrov675
    @konstantinrebrov675 4 ปีที่แล้ว +1

    Indian professors are awesome!

  • @caio-jl6qw
    @caio-jl6qw 3 ปีที่แล้ว

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

  • @kirthanasingh635
    @kirthanasingh635 3 ปีที่แล้ว

    Your Explanation is amazing Sir!! Keep making videos

  • @chrisdanan2183
    @chrisdanan2183 2 ปีที่แล้ว

    Wonderful explanation - thank you very much.

  • @davidtheprogrammer
    @davidtheprogrammer 2 ปีที่แล้ว

    Thanks so much. I wish we could derive floyd's cycle algorithm instead of assuming it's correct and making the assumptions from there.

  • @remmargorpp
    @remmargorpp 3 ปีที่แล้ว

    Bestest explaination ever!

  • @17ANP
    @17ANP 5 ปีที่แล้ว +5

    Best Explanation !
    One question : How are we so sure that P and Q will meet after m steps on the loop. Suppose I remove one node from the cycle, it becomes m-1 steps for P. Or if we consider the whole scenario from the starting (taking one node less from cycle), its the behavior of the algorithm that they would both meet at a point at m distance?

    • @toancaro
      @toancaro 3 ปีที่แล้ว

      I have the same question. Why are we sure that P and Q meet each other after m steps.

    • @fwan0697
      @fwan0697 2 ปีที่แล้ว

      @@toancaro ever figure it out?

  • @ngc35ster
    @ngc35ster 2 ปีที่แล้ว

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

  • @columbiars
    @columbiars 7 ปีที่แล้ว

    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!

  • @EskoLuontola
    @EskoLuontola 4 ปีที่แล้ว +1

    I found the latter half of the proof lacking. It should also have proven that m = l - k (plus some multiple of l), so that both p and q will meet at the start of the loop after travelling the same amount.

    • @EskoLuontola
      @EskoLuontola 4 ปีที่แล้ว

      Photon7908 explained the missing part.

  • @codestorywithMIK
    @codestorywithMIK 4 ปีที่แล้ว

    Let us suppose the length of the list which does not contain the loop be s, length of the loop be t and the ratio of fast_pointer_speed to slow_pointer_speed be k.
    Let the two pointers meet at a distance j from the start of the loop.
    So, the distance slow pointer travels = s + j. Distance the fast pointer travels = s + j + m * t (where m is the number of times the fast pointer has completed the loop). But, the fast pointer would also have traveled a distance k * (s + j) (k times the distance of the slow pointer).
    Therefore, we get k * (s + j) = s + j + m * t.
    s + j = (m / k-1)t.
    Hence, from the above equation, length the slow pointer travels is an integer multiple of the loop length.
    For greatest efficiency , (m / k-1) = 1 (the slow pointer shouldn't have traveled the loop more than once.)
    therefore , m = k - 1 => k = m + 1
    Since m is the no.of times the fast pointer has completed the loop , m >= 1 . For greatest efficiency , m = 1.
    therefore k = 2.
    if we take a value of k > 2 , more the distance the two pointers would have to travel.

  • @tushargarg3765
    @tushargarg3765 3 ปีที่แล้ว

    Thanks for such a Nice Explanation.

  • @liangchen8238
    @liangchen8238 4 ปีที่แล้ว

    Best explanation I've seen so far!

  • @devtorch
    @devtorch 4 ปีที่แล้ว +1

    I am on my knees and have tears in my eyes :)

  • @DeepakGupta-pl6bc
    @DeepakGupta-pl6bc 2 ปีที่แล้ว

    Excellent explanation!

  • @anubhavsinha8048
    @anubhavsinha8048 4 ปีที่แล้ว

    Very nice explanation!

  • @rohitkumarvarma4952
    @rohitkumarvarma4952 3 ปีที่แล้ว

    Thanks, mann! Finally understood this concept clearly!

  • @krisnadiputra93
    @krisnadiputra93 4 ปีที่แล้ว +1

    This guy has just saved me from a sleepless night lol

  • @blackswordsman9745
    @blackswordsman9745 6 ปีที่แล้ว +1

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

  • @ShwetaNaganath
    @ShwetaNaganath 6 ปีที่แล้ว +1

    Thank you. i was struggling with this since long.

  • @jaibhambri6853
    @jaibhambri6853 3 ปีที่แล้ว

    Very Nice Explanation

  • @vedvish3916
    @vedvish3916 2 ปีที่แล้ว +1

    Really awesome explanations,thank you.........,If possible please makes a playlist on stacks and queue.

  • @amitvaghela1456
    @amitvaghela1456 4 ปีที่แล้ว

    Great simplified explanations.

  • @ashishdukare1313
    @ashishdukare1313 3 ปีที่แล้ว

    Awesome explanation Sir : )

  • @DurgaShiva7574
    @DurgaShiva7574 4 ปีที่แล้ว

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

  • @omkar.at.office
    @omkar.at.office ปีที่แล้ว

    Awesome explanation! Thanks 🙏

  • @mahesh23343
    @mahesh23343 4 ปีที่แล้ว +3

    Boy is son of mother.
    Mother is mother of boy.
    Man is husband of mother.
    Mother is wife of man.
    You can go to Delhi from Punjab.
    You can also go to Punjab from Delhi!!
    This video could be easily completed 5-6 minutes earlier if he wasn't repeating everything so many times!
    But got the concept clear, so thanks. But please try to be a little faster than this. God bless youtube developers for creating speed control.

    • @blasttrash
      @blasttrash 3 ปีที่แล้ว +1

      install video speed controller extension. it allows you to go beyond 2x speed.

    • @mahesh23343
      @mahesh23343 3 ปีที่แล้ว

      @@blasttrash noted.

  • @bittuxoxo
    @bittuxoxo 4 ปีที่แล้ว

    sir yur vvideos are the best..i really get to understand how yu teach good one sir .i will be hoping for a recursion video explain it in depth

  • @shashankmehul02
    @shashankmehul02 4 ปีที่แล้ว

    Such wonderful explanation.Very helpful.

  • @najimahmed8923
    @najimahmed8923 5 ปีที่แล้ว

    #suggession: Complexity analysis would make the series complete. Please do another video to do complexity analysis!!! Great analysis! Thanks much!

  • @ayushsakure6098
    @ayushsakure6098 ปีที่แล้ว

    Very well explained.

  • @arpitamandal6469
    @arpitamandal6469 3 ปีที่แล้ว

    thanks for explaining it so well..

  • @anandkulkarni2111
    @anandkulkarni2111 7 ปีที่แล้ว +3

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

  • @adityasingh11156
    @adityasingh11156 5 ปีที่แล้ว

    Thanks Sir
    You make it understand in very simple way

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt 3 ปีที่แล้ว

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

  • @isaacc9487
    @isaacc9487 3 ปีที่แล้ว

    This is very clear and detailed, thanks a lot.

  • @karanb2067
    @karanb2067 4 ปีที่แล้ว

    heroic explanation

  • @winstonloke2860
    @winstonloke2860 4 ปีที่แล้ว +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.

  • @saurabhshukla4900
    @saurabhshukla4900 5 ปีที่แล้ว

    Nice explanation but it would have been great if you would have considered hare step to be some a*times tortoise and position of hare = b starting position. This will show that the for a=2 and b= any whole number , they will always meet.

  • @ankitvarmait
    @ankitvarmait 6 ปีที่แล้ว +1

    Thank you for wonderful explanation

  • @loba8924
    @loba8924 2 ปีที่แล้ว

    Thanks. Nice explanation

  • @mp0157
    @mp0157 5 ปีที่แล้ว

    This is the best video dealing with Floyd's Cycle Detection Algorithm out there! Vivekanand, hats off to you for making this explanation so simple and straightforward! Thank you :)

  • @kishantiwari3221
    @kishantiwari3221 3 ปีที่แล้ว +1

    Thank You sir!

  • @jamesclark1207
    @jamesclark1207 4 ปีที่แล้ว

    Really excellent explanation - thank you

  • @mangeshgupta5677
    @mangeshgupta5677 4 ปีที่แล้ว

    Great explaination thank you

  • @surendrapandey4660
    @surendrapandey4660 2 ปีที่แล้ว

    This video doesn't show up in top searches of the topic. It makes me think youtube algo is not working properly

  • @chunkwanchan5503
    @chunkwanchan5503 10 หลายเดือนก่อน

    my goat!
    1) when p travels m + k , q meet p (via observation)
    2) Because m + k = always start of loop (proof mathematically)
    Therefore, when p meet q, also start of the loop, is it correct?

  • @99ansh
    @99ansh 4 ปีที่แล้ว

    how is it gauranteed that both pointers will meet?

  • @shubhamjadhav2656
    @shubhamjadhav2656 4 ปีที่แล้ว

    very well explanation!

  • @glassanddiaphane
    @glassanddiaphane 4 ปีที่แล้ว

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

  • @kingdey9136
    @kingdey9136 4 ปีที่แล้ว +1

    Thank you!

  • @elizkotadia9350
    @elizkotadia9350 7 ปีที่แล้ว

    Great Job !!! Crystal clear explanation !!

  • @chakrapanisudarshan3027
    @chakrapanisudarshan3027 7 ปีที่แล้ว

    Brilliant Explanation in simple words.....

  • @sina7601
    @sina7601 2 ปีที่แล้ว

    The second part's explanation was made unnecessarily complex. You have the math and all you need is m = l (p-2q) - k and instead of k you can say K = l - z then you will end up with m = l (p-2q) - l + z and then you can go for modulo l on both sides which would result in m = 0 - 0 + z (mod l), hence you have proven that if you move m steps from start you would also travel from the intersection point z steps (ignoring the cycles that you make)

  • @yys3082
    @yys3082 3 ปีที่แล้ว

    Now, I know how it works. Thanks

  • @ShivamKendre-fc3su
    @ShivamKendre-fc3su 4 ปีที่แล้ว

    What a explanation!!!

  • @rahulchudasama9363
    @rahulchudasama9363 4 ปีที่แล้ว

    Nice explanation ...

  • @kbhargavi4400
    @kbhargavi4400 4 ปีที่แล้ว

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

  • @NarendraKolink
    @NarendraKolink 5 ปีที่แล้ว +2

    really helpful... Thanks a lot

  • @KhanSlayer
    @KhanSlayer 6 ปีที่แล้ว

    Very clear and concise explanation. Nice work.

  • @AMARSINGH-lj4rd
    @AMARSINGH-lj4rd 4 ปีที่แล้ว

    great explanation , thanks you saved me viveka !!! :-)

  • @azam148
    @azam148 5 ปีที่แล้ว

    Very clear explanation!

  • @dragosc8202
    @dragosc8202 3 ปีที่แล้ว

    Thanks! YOU make my day

  • @chienhsiang-hung
    @chienhsiang-hung 2 ปีที่แล้ว

    you deserve more subs

  • @gugliamo
    @gugliamo 2 ปีที่แล้ว

    this was great, thank you!

  • @null-wp1le
    @null-wp1le 4 ปีที่แล้ว

    Thank you so much for this video.

  • @truthprevails899
    @truthprevails899 4 ปีที่แล้ว

    Thanks for the explanation!

  • @asgheralishaik
    @asgheralishaik 4 ปีที่แล้ว

    nice explannation.Thanks

  • @shwetasingh7906
    @shwetasingh7906 7 ปีที่แล้ว +6

    Thanks sir

  • @DDOMohit
    @DDOMohit 3 ปีที่แล้ว

    sir i dont even know your name but thanks a lot you made my day i loved it and a little suggestion have a intro it will help you grow.

  • @MagicalCreationAviCreation
    @MagicalCreationAviCreation 4 ปีที่แล้ว

    Very interesting question
    Why fldy works for ... ?