Dynamic Programming | Set 4 (Longest Common Subsequence) | GeeksforGeeks

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • Explanation for the article: www.geeksforgeeks.org/dynamic-...
    This video is contributed by Kanika Gautam.

Komentáře • 122

  • @LordShish
    @LordShish Před 6 lety +50

    How is anyone supposed to think of this solution themselves?

  • @fredb2312
    @fredb2312 Před 6 lety +181

    I don't understand why all these algorithm videos never explain how to get the solution. It's always something random like "max of top and left cell" without ever explaining what those values are. It's like the people making these videos do not even understand themselves.

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

      Geeks for geeks is a superb Place to learn CS concepts.

    • @AnumitKaur0211
      @AnumitKaur0211 Před 5 lety +16

      I totally agree. Having read some more, what I understand is this:
      The matrix (or table) called LCS[][] created here to fill in the values works like this - row is the length of sequence L1 +1 and column is the length of sequence L2 +1.
      Now if the value at i-1 (subtracting 1 because the matrix is 1 row and 1 column extra than the original lengths of the sequence) of L1 matches with value of j-1 at L2, it means we have a common subsequence (of length 1). Now if there is any sequence common prior to this character, then that would be at i-1 and j-1, i.e., at length less than the current of L1 and L2 (pre computed in LCS[][]). Hence the solution- LCS[i][j] = LCS[i-1][j-1] + 1.
      If the character does not match, then either L1 has more characters (uptil i) matching L2 (uptil j-1) or vice versa. We choose whatever is the maximum of two.
      Let me know if the explanation is not at its best.

    • @mehoneybadger999
      @mehoneybadger999 Před 4 lety

      ur comment got so many likes ,but still dont get you .the video was everything to me for the lcs problem, please ask with details.

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

      Just look at the recurrence relation, you'll understand why.
      EDIT: Refer to some other source, he doesn't explain what the subproblems are and only how to get to the solution.

    • @Prashantkumar-hy1no
      @Prashantkumar-hy1no Před 4 lety +1

      I think you know the answer ... practice. I am also unable to understand but I think how dynamic programming should be approached is you will have to go through what we are trying to avoid here. In this case, repeated computation of same things while using recursion. Now we need to overcome this overhead by computing only once for each. We can keep track of how much match has happened before current iteration using a lookup table and proceed using those previous calculations. The best way to store the data of matches two strings is a matrix(2d array).

  • @indowestlife
    @indowestlife Před 6 lety +59

    You just told us what to do, not why you did it.

    • @mitagavade7591
      @mitagavade7591 Před 5 lety +4

      @@StarGazerHere Um, what? She might as well be no techie hired by the team solely to record the video.
      This is what 'guys' do I guess, insane remarks.

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

      @@mitagavade7591 He isn't wrong, girls remembered all long English and History answers back at school too. Seems like they have better memory.

    • @yashgandhi9698
      @yashgandhi9698 Před 4 lety

      @@mitagavade7591 fight fight fight!!!! XD
      don't pay attention to such comments

  • @fleroviumgaming
    @fleroviumgaming Před 5 lety +12

    At 6:42 you can use that G. You say "1 came from the left cell" but it didn't. It is 1 because G matches G, and the diagonal was 0. You skip that G for no reason.

    • @lordtalosgaming1448
      @lordtalosgaming1448 Před rokem +2

      No, she's right. She counted the G you're talking about in the previous column. No G was skipped in the video.

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

    Hello
    Can u please explain
    why u picked max(leftcell,upcell) when characters dont match
    Why we have to increment value of diagonal by 1 when characters match.

  • @TM-qc5oz
    @TM-qc5oz Před 7 lety +34

    did u just read it out aloud from wikipedia but with ur own example. Please explain the approach, so that we can understand how to think of similar problems.

    • @preetdabre3945
      @preetdabre3945 Před 6 lety

      Tanmay Misra This is a specific problem and not a general idea. If you're looking for the general concept the videos at the beginning of the playlist will help you out in understanding the approach.

    • @asifgarhi3117
      @asifgarhi3117 Před 6 lety

      And you still have people giving thumbs up :D

    • @akhleshsoni9874
      @akhleshsoni9874 Před 4 lety

      Approach comes from practice do more

    • @TM-qc5oz
      @TM-qc5oz Před 4 lety

      Akhlesh soni In order to practice effectively, we need to understand the approach

    • @kunalyadav2624
      @kunalyadav2624 Před 4 lety

      @@TM-qc5oz you are right. I would suggest you to visit the article mentioned in the description to understand the aproach.

  • @travelwithtechy98
    @travelwithtechy98 Před 5 lety

    while traversing back from 4 at Y,A how to distinguish that it came diagonally or max(top, left)?

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

    I was reading the explanation of how this works but I could not get properly how the table actually gets filled. This video hit the spot. Thanks!

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

    hey can u please provide the code snippet for this video.along with how to print the longest common sub sequence string

  • @terigopula
    @terigopula Před 6 lety +12

    Please do not forget to mention the time and space complexity of the algorithms that you teach, since it is just as important. Nice explanation by the way :)

  • @cpcp6117
    @cpcp6117 Před 6 lety +10

    you explain the procedure but did not explain how you came up with that procedure..
    please explain that also so that we can think the algorithm that solve particular problem...

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

    The table should be drawn in reverse i.e "AGGTAB" in row wise & "GXTXAYB" in column wise, after that if we wanna print the output by printing the matrix that would be the actual table. Thank you

  • @radhasingh3549
    @radhasingh3549 Před 2 lety

    Thank you so much for the short and clear explaination :)

  • @abdkumar1300
    @abdkumar1300 Před 6 lety

    what is the code to find the longest subsequence?

  • @himanshuthakur5979
    @himanshuthakur5979 Před 7 lety

    if there is more than one longest subsequence then what we will do?plese help us

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

    Great Explanation...Thank You!

  • @arpitkesarwani6123
    @arpitkesarwani6123 Před 5 lety

    superb way of explaining..nd awesome ppt

  • @vatsalmankodi9050
    @vatsalmankodi9050 Před 7 lety +1

    Excellent explanation! It was very helpful. Thank You.

  • @harikiranvusirikala4214
    @harikiranvusirikala4214 Před 7 lety +4

    What if their are two or more common sequences of same length???

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

      If two sub seqeucnes are of same length then they get concatenated and that is the resulting answer or longest common sub sequence. clear ?
      s1 = abc pqr abc
      s2 = abc xyz abc
      LCS = longest common subsequence is = abcabc.

    • @HA-bj5ck
      @HA-bj5ck Před 6 lety

      There can be more lcs's our moto is to find the length and using the table we can find the different lcs also at the same time
      Suppose thre are two longest common sub sequences like for example
      String 1: abcde
      String b: acbde
      here longest commom subsequence is of length 4 i.e abde and acde

  • @anushkagupta5917
    @anushkagupta5917 Před 4 lety

    what will be the time complexity?

  • @likheshmahajan4804
    @likheshmahajan4804 Před 2 lety

    @Kanika, The video that you posted is same as DSA - Tabulation from GFG, why repeat?
    Expectation from this video was to print the LCS or return a List , I don't see either mentioned in this video.

  • @silongliu2962
    @silongliu2962 Před 7 lety +2

    The video is great! I read a lot of articles do not see how to find out the largest common sub-sequence, but I saw your video, got it...thank you...thank you...thank you

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

    Nice video, really well explained :)

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

    Some videos are really good by i am seriously struggling with the ones by Kanika Gautam, she says a lot of stuff incorrectly, please get them made again by other faculty.

  • @amarkhairwar2893
    @amarkhairwar2893 Před 6 lety

    if both left and top has equal value then which one i choose......if i choose either one then is there any fault???

    • @naganikhilbijjala1000
      @naganikhilbijjala1000 Před 6 lety

      You can choose either of them. It was explained clearly over here 6:33. There is no fault it is perfectly alright.

  • @HaKazzaz
    @HaKazzaz Před 5 lety +5

    *For everyone asking why the matrix is built this way* :
    When you are using dynamic programming you need to understand the recursive algorithm you would like to optimize.
    I'll explain the non-genius solution where you go letter by letter from the start of each string.
    The DP answer will be built bottom-up which would make the table fill from the end of the string as the vid suggests.
    In this case (x is L1, y = L2):
    LCS = {
    1 + LCS(i+1, j+1) ; IF x[i]==y[j]
    max(LCS(i+1, j), LCS(i, j+1)) ; ELSE
    }
    This is the top-down approach.
    Now if you would like to convert the recursive solution to a matrix you need to take a bottom-up approach. And so the algorithm will look as follows:
    LCS = [N][M] // N = len(x), M = len(y)
    if x[i]==y[j]:
    LCS[i][j] = 1 + LCS[i-1][j-1]
    else:
    LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1])
    Note that the matrix has to have a initialization row and column where all values are 0 in order to have an answer for i-1 and j-1 in the first run.
    So if you are coding you should also use the case:
    if (x[i] == 0 || y[j] == 0) -> LCS[i][j] = 0
    You can easily see the correlation between the recursive and the DP solutions.
    Just keep practicing, it is a skill worth working for and not black magic (as some may feel after watching this video).

  • @donovankeating8577
    @donovankeating8577 Před 7 lety

    Why do we have to get the max of L[i-1][j] and L[i][j-1] if no commonality is found? Is it because of the 0's in the first row and column? Because for other cases, the max is always the cell to the left. Am I right?

    • @indowestlife
      @indowestlife Před 6 lety

      This is because you would want to carry the last matched character in both the strings. We would take the maximum between the top and the left because then that would mean that you are selecting the length of the two possible subsequences.

  • @UCHS_CH
    @UCHS_CH Před 4 lety

    What is cost of lcs?

  • @snehashishpaul2740
    @snehashishpaul2740 Před 7 lety

    Nice explanation. Really made it simple to learn and code.

  • @imtheultimategod
    @imtheultimategod Před 7 lety

    Thank you for this video. Made my day!

  • @AdityaSharma-tq6kj
    @AdityaSharma-tq6kj Před 5 lety

    If we want to print the sub sequence, what should we do

  • @ashickakonjee8861
    @ashickakonjee8861 Před 7 lety

    Thank you so much !

  • @panjc8543
    @panjc8543 Před 4 lety

    why didnt u show the thought process

  • @asakiran1958
    @asakiran1958 Před 6 lety +4

    at 5:09 LCS[i-1][j] is top cell and LCS[i][j-1] is left cell

  • @mauzi4473
    @mauzi4473 Před 7 lety +2

    can you please post the algorithm to find the sequnce also :);
    anyway thanks for explaining in such a simple way;

    • @reetigarg7398
      @reetigarg7398 Před 7 lety

      hackdown cinema adda I second that

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 7 lety +1

      If you are asking about the printing of the subsequence, you can fund it here: www.geeksforgeeks.org/printing-longest-common-subsequence/

  • @rebelutionarygaming8875

    Thank you for the explanation

  • @PrayingForYourWellBeing

    I think the else if condition should be "else if (X[i] == Y[j])", right?

    • @reetigarg7398
      @reetigarg7398 Před 7 lety +2

      Rohan Shah Nope, since you're assigning 0's to the 0th column and 0th row, It'll be i-1 and j-1

    • @Thejaswi01
      @Thejaswi01 Před 6 lety

      Your intuition is correct. L[i][0] or L[0][j] is for the cases of either string being empty i.e., one of them is empty string so common subsequence length is 0. This addition adds an extra row and column.
      So L[i][j] actually refers to the strings ending at X[i-1] and Y[j-1]. Hence we compare those.

  • @ridowanahad1464
    @ridowanahad1464 Před 4 lety

    thanks for this video..

  • @hailenders
    @hailenders Před 7 lety +1

    Thank you for explaining those algorithms so simply! 😀

  • @zikraiqbal5409
    @zikraiqbal5409 Před 7 lety

    Very Useful ...Thank you so much

  • @SunilLangtad
    @SunilLangtad Před 7 lety

    wonderfully explained! Thank you! :-)

  • @imhashir
    @imhashir Před 6 lety

    Thanks a lot!!!

  • @b.sainathreddy4567
    @b.sainathreddy4567 Před 4 lety

    Superb explanation

  • @unboxordinary
    @unboxordinary Před 7 lety +1

    I didn't get what you meant by 4 is not max in top and left cell :/ !! 4 >3 though....please clear that.
    UPDATE :
    SHE SAYS...... IF THE CURRENT VALUE IS NOT THE MAXIMUM OF TOP AND LEFT CELL...which is ...IF THE CURRENT VALUE IS NOT THE *(MAXIMUM OF TOP AND LEFT CELL)*

    • @yashwanthsoodini4538
      @yashwanthsoodini4538 Před 7 lety +1

      4 > 3 does not mean 4 is max(3, 3) . max(3, 3) is 3.

    • @unboxordinary
      @unboxordinary Před 7 lety

      Yashwanth Soodini yes i understood it later and updated in comments as well :)

  • @yackawaytube
    @yackawaytube Před 4 lety

    Calm down. The table explanation is very clear to me. Think about what she said at @1:20, the table is just a way to capture the recursive calculation.

  • @amitrawat2915
    @amitrawat2915 Před 5 lety

    Mam i think there is a mistake in your algorithm....in the second IF condition (x[i]==y[j]) not ( x[ i-1]==y[j-1] ).

    • @sohel_naikawadi
      @sohel_naikawadi Před 5 lety

      It is correct. The x and y are the two arrays which have the input chars and it starts from 0 index. The loop will reach that condition when both i and j are not equal to zero. i.e at i=j=1. So x[0] and y[0] will be compared and the result will be stored at the L[i][j] i.e L[1][1]=L[0][0]+1 and then L[1][2]=L[0][1]+1 ...if they match.

  • @Jainanchit888
    @Jainanchit888 Před 6 lety

    Thanks,I got it.

  • @naganikhilbijjala1000
    @naganikhilbijjala1000 Před 6 lety

    When I saw the article. I totally scared and thought to quit. But after seeing this video I understood everything pin pointedly. Excellent explanation. Thank you so much GeeksforGeeks.

  • @preetdabre3945
    @preetdabre3945 Před 6 lety

    This is great, GeeksforGeeks is great! Please keep going.
    ~A genuine learner.

  • @mumbaikachallenge5526
    @mumbaikachallenge5526 Před 2 lety

    Thanks❤️

  • @ganeshchandrameesala257
    @ganeshchandrameesala257 Před 3 lety +2

    gotta give credit for using dark mode all the way

  • @envoy9377
    @envoy9377 Před 2 lety

    TY

  • @adityatrivediii
    @adityatrivediii Před 4 lety

    It was hard to understand the concept of 2D table, I had to check from other sources and think hard. You have shown algo, but also explain better. Nonetheless Thanks

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

    Even i can read the slides.

  • @pablocom
    @pablocom Před 3 lety

    At least for me, Memoization with DepthFirstSearch solution for Longest Common Subsequence is much easier to understand

  • @RahulKumar-es6oy
    @RahulKumar-es6oy Před 7 lety

    there are another approach that have o(n) time complexity and better understandable.

  • @badgameryt4086
    @badgameryt4086 Před 5 měsíci

    Explanation is awasome.but written part that shown in screen that is so distrubing.that is not neccssary to add.

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

    Very good explanation which is easy to follow and saves a lot of time. Well done Kanika!!!

  • @bhushanshinde8358
    @bhushanshinde8358 Před 4 lety

    M I supposed to learn the algorithm...where is the logic..

  • @user-qr8sd8hu7e
    @user-qr8sd8hu7e Před 6 lety

    Very nice video! Well Done.

  • @amberkumar2359
    @amberkumar2359 Před 7 lety

    Awesome Explanation!!

  • @abhinavverma8432
    @abhinavverma8432 Před 5 lety

    This is not the correct solution.I tried with different examples and I got wrong answer.Example check for 'aggtab' and 'gxtgayb' it should return 5 but it is returning 4.

  • @abdulahadshaikh8495
    @abdulahadshaikh8495 Před 6 lety

    Thanks nice tutorial

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

    i could see a lot of people complaining about the approach, and how she derived that. Please take a look at this czcams.com/video/ASoaQq66foQ/video.html and again come back to this video.
    Let me know if that did help
    And BTW Kanika Gautam is really bad at explaining. Other g4g faculties are fine.

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

    Bhai ladki padha rhi hai kya usse algorithm pooch rhe tmlog...
    Whitehat junior ka scam na dekha tmlogo ne
    Kuch nahi aata hoga inko bhai.
    Galti se aagaya Mai bhi.🤦‍♂️

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

    Love your Indian accent.

  • @sivar4300
    @sivar4300 Před 7 lety

    sweet n short