Knuth-Morris-Pratt(KMP) Pattern Matching(Substring search) Part2

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ต.ค. 2024
  • This video is specifically about building prefix array for KMP. For KMP video go to • Knuth-Morris-Pratt(KMP...
    / tusharroy25
    github.com/mis...
    github.com/mis...

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

  • @Aditya-mk5sm
    @Aditya-mk5sm 8 ปีที่แล้ว +183

    I think this is one of the most required explanations for KMP. It is missing almost everywhere else and the algorithm is being blindly followed incase a mismatch occurs. Thanks a LOT.

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

      Pls sir lyrics show in down side b/c problems solution not seeing properly thanking you

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

      @Akhil Raj the time complexity is 0(m+n) as, we are traversing the whole array once ,the the given pattern and the constructed array.
      I think now you have understood .

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

      @@lavishgarg4274 But if there are lot of backtraversals in bigger array(worst case scenario), Will it not be: O(m+x). x being number of steps taken back.

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

      @@SearchingPeaceInside Hello, The worst case scenario is which it has to back travel in every case, so, in either step we either increase our iterator, or we slide our pattern to be matched. Worst case, you'll slide the pattern once for each iteration, hence this will be O(2*N) which is basically O(N) solution. I hope I'm correct :)

  • @nehaliacharya7257
    @nehaliacharya7257 3 ปีที่แล้ว +8

    Even Abdul Bari didn't explain it in so much detail. Best explanation on the planet!!!! Thank you so much I thought I'll never understand this algorithm but you've made it so easy. Thanks again!

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

      Exactly..this guuuy explained this topic easily without confusion

  • @rakeshvarma8091
    @rakeshvarma8091 8 หลายเดือนก่อน +2

    He's best. I have seen several other videos but none of them has explained why we have to look at previous index and then find out the longest suffix which is also prefix.
    Reason is :
    Let's say we have come across a mismatch at some "j" & "i" , j < i :
    1. character at index "j" is not matching with character at index "i".
    2. We know that the prefix (0, j-1) matches with (i-j, i-1).
    3. Now we have to look at what is the longest suffix which is also prefix for the substring (0, j-1). And this will also be the longest suffix for string ending at index "i-1". This is the trick.
    Thank you Tushar!! You nailed it!! More power to you!!

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

    I've spent more than a week reading articles and watching tutorials to figure out how the hell these values came from but here it is explained in just 9 minutes. Clearest explanation seen so far!!

  • @saranshsawhney6115
    @saranshsawhney6115 9 ปีที่แล้ว +36

    Tried like 15 tutorials before this one ! and my search ends :) superb keep it up

  • @nikhilnvj
    @nikhilnvj 9 ปีที่แล้ว +14

    Way better than other complicated explanations.
    Please make a series on tough interview questions asked in Google, Amazon etc.

  • @malharjajoo7393
    @malharjajoo7393 7 ปีที่แล้ว +14

    the interesting thing to notice is that the logic is very similar for when we are creating the preprocessed array and when we are matching the pattern.

  • @ahsan_kamal
    @ahsan_kamal 9 ปีที่แล้ว

    Finally with this video i understood the whole logic behind building LPS[ ] array. Very Clear and Concise explanation.
    Thanks.

  • @Elektroingenieu
    @Elektroingenieu 8 ปีที่แล้ว

    I rarely comment.
    "This is by far the best explaination on KMP algorithm I found on TH-cam. And I've watched lots of other videos."

  • @mattcay
    @mattcay 8 ปีที่แล้ว

    Thanks for both those videos. YT recommended me this video. I came in only hearing about KMP and 20 minutes later came out with a good grasp of the algorithm. Great work :)

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

    so lucky to find you...simple and clear way Tushar Sir.

  • @fazithfouseen1548
    @fazithfouseen1548 7 ปีที่แล้ว +4

    i was totally upset in the part 1 now i am fully cleared....thank you tushar....

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

    Thank Tushar sir. You are doing amazing job for us. I go through the many tutorials but didn't understand KMP. Now its completely clear. Thanks a lot.

  • @zonghanxu1625
    @zonghanxu1625 8 ปีที่แล้ว

    Normally, I don't comment. But you are just so amazing. Plz keep uploading such kind of videos. Thank you so much

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

    Excellent explanation! The basic idea is to use the already calculated prefix array (or say Partially Match Table on somewhere) when you are calculating/building the prefix array, if I understand that correctly. Thank you very much!

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

    A great example is always better than a lot of talking information. Thank you!

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

    Thanks! This was a great video. Computing the failure function is the trickiest part of the KMP algorithm!

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

    Sir a BIG THANKS FROM THAPAR UNIVERSITY, PATIALA. YOU DESERVE A GOLD MEDAL. PLEASE BECOME A PROFESSOR HERE.

    • @267praveen
      @267praveen 3 ปีที่แล้ว

      Degree hogi poori Teri kaka?

  • @KrishnaKishore
    @KrishnaKishore 9 ปีที่แล้ว +3

    Pretty much useful for everything, Competitive Programming and Interviews. :)
    Keep the work going on.

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

    Thanks Tushar for a clear and concise video explanation

  • @Devilfairy1
    @Devilfairy1 7 ปีที่แล้ว +1

    This is an explanation that could have only been done if you have deep understanding of KMP. Amazing explanation.

  • @balsal8520
    @balsal8520 8 ปีที่แล้ว +3

    It's a awesome exaplanation of KMP and i really thank's a lot.

  • @陳翰儒-d5m
    @陳翰儒-d5m 3 ปีที่แล้ว

    Man, you gotta realize that how much you help me, thanks you very much , you're my god!!

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

    Way better than any other explanation article or tutorial on the internet (definitely true for a rookie like me).

  • @Lisa-kk6go
    @Lisa-kk6go 6 ปีที่แล้ว

    The best video I saw about KMP array. It's better if you can give more details about why we handle the case in this way when i and j doesn't match.

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

    Finding the value of pi for last index clears all d doubts!!!!!!
    themks a lawt!!!

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

    Finally someone talks about what the number stands for...thank you!

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

    FOR LAST MISTMATCH :
    The first 11 are same with last 11 i.e
    0-10 = 6-16 now if 11 and 17 were same we could say 0-11 = 6 -17 but that's not the case
    so we see how many char are same in first 0-10 which is lps[10] = 5
    so 0-4 = 6-10 or 0-4 = 12 -16 as 6-10 = 12 -16
    so if 5 and 17 were equal we could say 0-5 = 12-17 but again not the case
    so how many are equal in first 0-4 i.e = 3
    so 0-2 = 14-16 and 3 = 17 so 0-3 = 14-17 i.e. 4 length
    ps: Thank you Tushar for a rly good explanation!

  • @Priyanshgupta1161
    @Priyanshgupta1161 9 ปีที่แล้ว

    surely One of the best videos on KMP algo.
    keep it up..

  • @kidou123456
    @kidou123456 7 ปีที่แล้ว +2

    This one is really helpful to understand why j keeps backtracking to j - 1 position! Thanks a ton!

  • @Official-tk3nc
    @Official-tk3nc 4 ปีที่แล้ว +1

    Well done! Gautam Gambir

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

    I am wondering why no any new videos are coming up now?
    Great channel.

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

    lol it took me like an entire day to try and figure this out ... thanks a lot for the explanation !

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

    Awesome explanation, Loved it!!!

  • @prodev7401
    @prodev7401 9 ปีที่แล้ว +20

    please make video on DP bitmasks

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

    Key logic - What happens on a mismatch ?
    What we are really doing when we reset j = A[j-1] is considering prefixes and suffixes of the longest prefix that we found on the first mismatch ... this way we are first considering the longest suffix( thats a prefix ) and then the 2nd longest , and then the 3rd longest and soon until j=0.

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

      Hi malhar. I am still not quite sure here. Why we can safely reset j = A[j - 1]? 'j' here points to the end of longest prefix (it's also suffix) of s[0:i] (substring we have seen so far). For example, in the last part of video. When we move i from 16 to 17 (j from 10 to 11) and find s[j] != s[i], why we can safely jump from j = 10 to j = A[j - 1] = 5 instead of j = 10 to j = 9? How can we know that j = 6, 7, 8, 9 are all impossible as the candidate prefix length and safely discard them?
      Could you explain this part? Thanks very much!

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

      +yusong shen -
      Ok ,Let me clarify -
      1) I feel your concept of "safety" is slightly incorrect , please correct me if I'm wrong.
      On seeing a mismatch ,"Safety" should mean that we should not skip past any length of the pattern
      and should start at j=0.
      2) According to your question , at j=10 , if you are going to j = 5 , then dont you think that should be "safer" than going to j=9 ?
      3) "Why is it safe to skip past j=0,1,2,3,4" is what I feel you should have asked.
      In short - I feel going backward in the pattern ( and then checking for matching ) should be safer than going forward ( and then checking for matching )

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

      To explain why his method is correct -
      1) When you get a mismatch , if j > 0 , then that means before the mismatch , you matched upto a lenght of pattern "j-1" ( if j > 0 )
      I will refer to j and i as "mismtach index" for now. ... where s[i] ! = s[j].
      2) The suffix of that pattern ( from 0 to j ) is also the prefix of the pattern just before the mismatch index i.
      ( Just see the last bit of the video - I am referrring to "acaca" from index 0 to 4, and 12 to 16.
      3) Hence on seeing the mismatch , from 1) above , you know that you have "safely" matched from index 0 to 4 ( inclusive of 0 and 4 ) in the pattern. This is the info given by preprocessed array.
      why he keeps resetting j = A[j-1] is slightly trickier but intuitively ( and can be understood by drawing it out ) by what I explained in the ansewer above.

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

      How do we know that the characters from j=0 to j=4 will match those from j=12 to j=16?

  • @medhagautam
    @medhagautam 8 ปีที่แล้ว

    nicely explained, i tried it from coreman earlier, but now its more clear to me ... thanks dear :)

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

    I love so much, Tushar.

  • @AdityaKumar-be7hx
    @AdityaKumar-be7hx 7 ปีที่แล้ว

    Extremely edifying. Keep up the good work.

  • @manjutakhar4496
    @manjutakhar4496 8 ปีที่แล้ว

    Best Explanation finally i Got the solution

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

    TY for all your videos and my BSC =)

  • @debabhishek
    @debabhishek 8 ปีที่แล้ว

    thanks Tushar you explained it well.

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

    your published this video on my birthday...14 june ..

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

    best explanation ever.

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

      meh could be better

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

    Helped me a lot. Thank you!

  • @ashfaqiftakher4486
    @ashfaqiftakher4486 8 ปีที่แล้ว

    Great explanation Tushar Roy. Keep uploading awesome tutorials like this one. :)

  • @higorbotelho3483
    @higorbotelho3483 8 ปีที่แล้ว

    You are really helping me study ! Thank you for this.

  • @pdmang
    @pdmang 8 ปีที่แล้ว

    Thank you Tushar !

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

    Sir g kia samhjaya hai
    Me apka bahut bara fan hogya hun

  • @vivekmathur2532
    @vivekmathur2532 8 ปีที่แล้ว

    Very nicely explained.Thanks sir

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

    Sir, if you are reading this, i HIGHLY suggests you to make videos on Correctness Proof of all algorithms. It is the most important topic most of the people miss. Please sir, my humble request.

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

    Excellent explanation!

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

    Great Explanantion really

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

    Very helpful video. Thank you!

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

    finally I understood why we go to j-1 after looping through 6:08 to 8:30

    • @267praveen
      @267praveen 3 ปีที่แล้ว

      What you understood?

  • @satishreddy4893
    @satishreddy4893 8 ปีที่แล้ว

    thank u very much ...great explanation with good examples.

  • @robealamalik7763
    @robealamalik7763 8 ปีที่แล้ว

    nice video..it clears my concepts...thanks

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

    This is great! Helped me a lot. Thanks

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

    excellent expalanation sir

  • @bodhisattva1924
    @bodhisattva1924 6 ปีที่แล้ว +4

    amazing video.
    Question:
    if we do such jumps to find candidate J for each i...
    then how are we saying this Prefix construction is O(n).
    for each i isnt there a possibility that we might do j jumps (till we reach j=0)
    and hence it is O(n^2)

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

      I also has the same question. Can someone explain this please?

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

    Fabulous explaination !!!

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

    very nice explanation

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

    thank you so much clarifying sir

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

    your videos are awsome !
    helped me such in graphs and also kmp

  • @pooja21790
    @pooja21790 7 ปีที่แล้ว +1

    bahut bdiya roy
    bhai

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

    Beautifully explained. Thank you so much :)

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

    Thanks for the nice video man!

  • @coding_entertainment
    @coding_entertainment 11 หลายเดือนก่อน

    you are superb sir

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

    sir i think last one will 3 instead of 4. sir please explain if i m wrong

  • @tibormikita
    @tibormikita 8 ปีที่แล้ว

    beautifully explained, thank you sir

  • @anyagupta5713
    @anyagupta5713 9 ปีที่แล้ว +1

    Sir again awesome video.... sir when you are going to make videos on graph ? eagerly waiting to watch graph videos....

  • @marcellooliva786
    @marcellooliva786 9 ปีที่แล้ว +1

    Good stuff! At the very end, after you set tmp[17] = 4; do you increment 'j' since the values 'c' match? In other words, is j==4 at the very end? This becomes important if we have a longer input.

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

    Thank you so much! It helped me a lot.

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

    Thank you for explaining

  • @Deepakyadav-jr3gk
    @Deepakyadav-jr3gk 7 ปีที่แล้ว +1

    Great Explanation.

  • @AndreKhan716
    @AndreKhan716 7 ปีที่แล้ว +1

    Thank you! Great job :)

  • @samaryadav7208
    @samaryadav7208 8 ปีที่แล้ว

    as always.. great explaination!!

  • @winnering
    @winnering 11 หลายเดือนก่อน

    I wonder if the last index value needs to be calculated, because we always only need the previous index value. Such as in this example, there is no need to figure out the value 4 for index 17.

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

    Nice work Tushar!

  • @sumitkesarwani4930
    @sumitkesarwani4930 8 ปีที่แล้ว

    Awesome tushar...

  • @pritamghosh270
    @pritamghosh270 9 ปีที่แล้ว

    thank you sir..ur tutorials helps me lot>>>>...

  • @djbanerjee2401
    @djbanerjee2401 8 ปีที่แล้ว

    thank you tushar

  • @utuk123
    @utuk123 8 ปีที่แล้ว

    +Tushar Roy they are used for polynomial evaluations and related stuff

  • @pavankhubani4191
    @pavankhubani4191 8 ปีที่แล้ว +1

    Nice explanation...

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

    Finally, My search ends after watching more than 10 videos...

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

    Thanks A Lot Sir!!

  • @rishabhsachdeva8508
    @rishabhsachdeva8508 8 ปีที่แล้ว

    Awesome video

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

    Nice tut ever seen!!

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

    this video just helped me...thnx

  • @sharifabahar6257
    @sharifabahar6257 8 ปีที่แล้ว

    Thanks a lot . very very excellent and beneficial .

    • @sharifabahar6257
      @sharifabahar6257 8 ปีที่แล้ว

      Hello , May you change it to C++ code ? I mean the thing that you wrote for KMP substring search from a file . It is my DSA group project but i tried too much could not . if u can . thank you so much .

    • @sharifabahar6257
      @sharifabahar6257 8 ปีที่แล้ว

      Our group project is about searching sub string from a file using KMP algorithm in c++ .
      I tried a lot to convert your coding from java to c++ but could not . May be i need more time to do it . But i have a lot of things to do from other subjects . If you can plz help us . again tnqs a lot .

    • @sharifabahar6257
      @sharifabahar6257 8 ปีที่แล้ว

      ok thanks a lot .

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

    Thank you so much Sir ...

  • @adabalasairam4344
    @adabalasairam4344 8 ปีที่แล้ว

    very helpful

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

    excellent

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

    really helped me a lot .... thanks bro!!!

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

    You are the best.. Thanks (y)

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

    Awesome man!

  • @utuk123
    @utuk123 8 ปีที่แล้ว

    awesome video Tushar Sir.If u could please give a video on fast fourier transforms and inverse of fft

  • @AshishKaushikOfficial
    @AshishKaushikOfficial 8 ปีที่แล้ว +2

    Thank u man !! :)

  • @muhamm3tkara
    @muhamm3tkara 8 ปีที่แล้ว

    Thanks a lot!

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

    it took me a while to grasp the idea of the border, which starts after the longest prefix, which is also the suffix. Ie: abbcxabby. we only need to compare the y to the x, since we've matched all previously (hence, x is a border of ****x****). If x doesn't match with y, then do the recursive comparison from the first block of **** (starting again at the border within the block, hoping that something in suffix of abb in abby to match the prefix of first block ****. Do this until we reaches 0 and it will be a trivial problem of either 1 (matched 1 char), or 0 (matched none).
    oh, and when you match something, you can only increment by 1 from the last result. There's a formal proof for that but i think it's just intuitive...helps keeping track of the indices in a simple manner.

    • @267praveen
      @267praveen 3 ปีที่แล้ว

      Why will you compare y with x. You should be hitting mismatch at y comparison to C when you start backtracking. Not sure how you reached situation you have to compare y with x