Binary lifting: Dynamic Programming on Trees

Sdílet
Vložit
  • čas přidán 7. 07. 2020
  • In this video, I provide a crisp and clear explanation of the concept of binary lifting which can be used to find the lowest common ancestor of two nodes in O(logN) time complexity.
    I will explain both the explanation and coding part in-depth.
    Code: ideone.com/Cg3bZT
    If you liked the explanation let me know in comments, share with your friends and if you haven't subscribed yet then please do subscribe :)
    Enjoy watching!!
    Super useful books for algo ds and programming fundamentals!
    1. Introduction to Algorithms by Cormen: amzn.to/35AmQqu
    2. The Algorithm Design Manual: amzn.to/2K9RGPq
    3. Fundamentals of Data Structures in C++: amzn.to/2LCwIsN
    4. Object-Oriented Programming by E Balagurusamy: amzn.to/2Xxmdtr
    5. Head First Java: amzn.to/39kb44K
    6. Cracking the coding interview: amzn.to/3iDOHLK
    7. Database System concepts: amzn.to/3pisuFQ
    8. Operating Systems: amzn.to/39fcmis
    9. Discrete Mathematics: amzn.to/2MlgCE6
    10. Compiler Design: amzn.to/3pkYvx2

Komentáře • 74

  • @amanranjan7825
    @amanranjan7825 Před měsícem

    Loved the way how u explain the problems by breaking them down and explaining it further. Thank you for such an amazing explanation.

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

    Simple and on point explanation . Thank you!

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

    20k subs congrats well explained need more tutorials on cses problem sets

  • @rohansinha2760
    @rohansinha2760 Před 2 lety

    Brilliant and simple way of explaining . Kudos !!!

  • @rashmidewangan8627
    @rashmidewangan8627 Před 3 lety

    Came here after watching multiple videos. Thanks !! Very clear explanation.

  • @saranshsingh5495
    @saranshsingh5495 Před 2 lety

    Very easy to grasp because of your approach.......
    Thanks for the great video

  • @sujoyseal195
    @sujoyseal195 Před 3 lety

    I think after completing your dp on playlist , I'll be able to solve most problems.video was Super useful as usual .

  • @bhavyachawla7176
    @bhavyachawla7176 Před 3 lety

    you've explained it so well !!thank you so much :)

  • @priyanshisharma1227
    @priyanshisharma1227 Před 3 lety

    very clear explanation....finally got the concept right!

  • @bilalkhan2298
    @bilalkhan2298 Před rokem +1

    Wish you make a proper graph and tree series just like you did for dynamic programming. That was superb..

  • @Study2_217
    @Study2_217 Před 10 měsíci +2

    great explanation Sir!!

  • @anuraggupta6277
    @anuraggupta6277 Před 3 lety

    Best explanation found so far....

  • @ramitgoel1363
    @ramitgoel1363 Před 2 lety

    Great Content!! , Not many teach like this with help of question !!, May it soon cross 1Million Subs

  • @nishantgoel3065
    @nishantgoel3065 Před 2 lety

    very simple and clear explanation thanx a lot!!!!!!!!!

  • @Anonymous-pj2mv
    @Anonymous-pj2mv Před 3 lety

    Bohot hi Tagda explaination .
    No challenge For this explaination

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

    This binary lifting concept is really awesome. Thanks for explaining it so nicely.

  • @NaitikMishra-ll7el
    @NaitikMishra-ll7el Před 9 měsíci

    such a clear explanation.....👍👍

  • @seeva92
    @seeva92 Před 3 lety

    @Kartik Arora Is it possible to solve this problem without binary lifting? Like collect all the queries indexed at each node. Now run a dfs, push the nodes to the path vector from the parent to the current node. Once it reaches the leaf, when we are recurring back, for the current node, we can compute the solution by comparing if k is lesser than pushed nodes vector size. Then store the solution for that query indexed at current node, then remove the current node from the path vector.

  • @neildahiya2524
    @neildahiya2524 Před 3 lety

    Awesome Explanation

  • @pranshunayak1792
    @pranshunayak1792 Před rokem

    Awesome content!

  • @anshusingh2473
    @anshusingh2473 Před rokem

    curated approach niceee
    😍😍😍

  • @Nikita-xk9fc
    @Nikita-xk9fc Před 3 lety

    wonderful explanation

  • @ravipatel3624
    @ravipatel3624 Před 3 lety

    Great Explanation man

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

    great content bhaiya . .

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

    Great work

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

    You are a legend man! Keep it up!

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

    Finally I got perfect playlist.
    Thanks a lot kartik

  • @aadityasharma9790
    @aadityasharma9790 Před 2 lety

    in love with ur content

  • @shivammaini4447
    @shivammaini4447 Před 3 lety

    Awesome explanation. Helps a lot.

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

      Thankyou Shivam!!
      Try solving LCA of two nodes in logN per query using this technique.
      I'll soon upload a video on that too.

  • @asthasatija728
    @asthasatija728 Před 3 lety

    Awesome! Quite good explanation!

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

    Bhaiya which hardware and software tools do you use to draw on screen???

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

    Finally a good video so that I can understand the concept 🙌🙌

  • @mridulkumar786
    @mridulkumar786 Před 3 lety

    Awesome, Thanks a lot

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

    Such a detailed explanation to understand the logic behind the concept as well as the code. Thanks a lot.

  • @mridulkaran345
    @mridulkaran345 Před 2 lety

    Amazing!

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

    Great content just sometimes its difficult to hear, sound is little low

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

    In the LCA question, if we have a small number of queries(let's say around 10), then is binary lifting helpful? I mean to say for small number of queries, isn't the simpler O(NQ) algorithm more efficient? (Considering the preprocessing time here is O(NlogN))

    • @AlgosWithKartik
      @AlgosWithKartik  Před 3 lety +8

      If number of queries are large, binary lifting is very helpful.
      Upto 50 or 100 queries, you can be happy with the brute force and get similar runtime.

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

    best content in only 21:38 min
    great

  • @surajpratapsingh34
    @surajpratapsingh34 Před rokem

    what is tree array holding and and what is the loop for this tree array. and since tree[src] is just an int then what are we looping here

  • @shashanktiwari4442
    @shashanktiwari4442 Před 3 lety

    Bhai awesome video....keep on uploading videos on DP and trees:)

  • @VasudevaK
    @VasudevaK Před 10 měsíci

    Can someone tell me why the power's is upper bound is 20? 18 should be fine right?

  • @FinanceMode14
    @FinanceMode14 Před rokem

    In Answer Query function control reaches end with no return value how to resolve this ?

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

    In the function that actually calculates the kth ancestor node, do we have to start from the biggest possible jump and keep dividing it by 2 at each step? I think starting from jump = 1 and doubling it works just fine and it is just as fast. I think it all depends on the actual value of k. Great video, thanks!

    • @AlgosWithKartik
      @AlgosWithKartik  Před 3 lety

      I am not entirely sure whether the doubling thing will work as efficiently or not. maybe if you explain it in more detail we can try discussing the complexity. I suspect it would be logN*logN instead of logN.

    • @aliaksandrhn1
      @aliaksandrhn1 Před 3 lety

      @@AlgosWithKartik Suppose the ancestor node for both nodes is 31 levels up from both nodes. Using the "biggest jump first" approach that you re showing, you will end up performing these jumps (in this order): 16+8+4+2+1=31. Totally valid. What I am saying is that you could also start from the lowest jump (jump to a parent) and double the jump size at each step: 1+2+4+8+16=31. I may be wrong but it seems to me that the efficiency and time complexity would be the same? The doubling approach may not be as efficient for other cases, I am not sure.
      It is not really a big deal, I've seen binary lifting implemented in these 2 ways.

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

      @@aliaksandrhn1 the doubling approach will be slower for some other jump sizes. For example: jump = 2^30.
      You'll end up doing 29+29 jumps(max jumps you will ever make in biggest jump first method is number of bits in an int i.e 32). Instead of just 1 jump.(I assume once you can't do a jump large enough you reset jumpsize to 1)
      Although it's a bit hard to prove but I still feel that the approach will be logN*logN
      Think about jump sizes as binary numbers and try putting a upper bound on the time Complexity.
      In practice you'll find both approaches will work good enough so that's alright :)

  • @shreyashachoudhary480
    @shreyashachoudhary480 Před 2 lety

    2 x-1 + 2 x-1 = 2x works like magic!

  • @ayushgarg5929
    @ayushgarg5929 Před 3 lety

    Please make a playlist on Game theory...It is no where available on CZcams

  • @Dineshkumar-uz4dw
    @Dineshkumar-uz4dw Před 2 lety +1

    Top knotch man

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

    Was having trouble understanding the 'Binary Lifting'. Now I don't. Thanks!

  • @ribhavbansal2942
    @ribhavbansal2942 Před rokem +1

    for(int child : tree[src])
    {
    if(child != par)
    binary_lifting(child, src);
    }
    Why we are checking condition of child ! = par??
    Please clear my doubt!!

    • @kira-nr3qj
      @kira-nr3qj Před 4 měsíci

      its like after once you have calculated all the power two parents of the src, you want to do the same for its children, therefore we have used the check child!=par, because now we want to find only the binalry lifted parents of the chidren

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

    please share link of code

  • @mridulkumar786
    @mridulkumar786 Před 3 lety

    when we are calling, binary_lifting(int src, int par)
    {
    up[src][0] = par;
    }
    with arguments (1,-1)
    so up[1][0] will be -1 but it should be 1 itself na? please clear my doubt bhaiya

    • @AlgosWithKartik
      @AlgosWithKartik  Před 3 lety

      actually the root of the tree has no parent.
      So I have used an invalid value for parent to represent this fact.
      There are many ways to implement the same functionality.

    • @mridulkumar786
      @mridulkumar786 Před 3 lety

      @@AlgosWithKartik but if we had to find 0th ancestor of root node then this method will be incorrect na ?

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

      @@mridulkumar786 0th ancestor of any node will be the node itself.
      Up[node][0] represents the 2^0 th =1st ancestor of node.
      Hope this helps!

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

    Loved someone picked cses

  • @sauravpandey599
    @sauravpandey599 Před 3 lety

    Why are you using windows 7

  • @omop5922
    @omop5922 Před rokem

    Kya phayda itni binary lifting ka abhi bhi ancient windows version use krte ho.

  • @VIJAYKUMAR-dj6kf
    @VIJAYKUMAR-dj6kf Před rokem

    explanation is nice but it would be great if you write the code instead of explaining the prewritten code..... baki sb changga si