Binary lifting: Dynamic Programming on Trees

Поділитися
Вставка
  • Опубліковано 25 лис 2024
  • 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

КОМЕНТАРІ • 78

  • @amanranjan7825
    @amanranjan7825 7 місяців тому +1

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

  • @jaishriharivishnu
    @jaishriharivishnu 4 місяці тому

    Initially... I made an array of 2e5 x 2e5 (obviously Runtime error) then i stored all level information, for each node... This took N^2 and then for each query O(1).....
    Now I can Optimise this using Binary Lifting.... Thank you bhaiya:)

  • @divy04
    @divy04 5 місяців тому

    beautiful explanation, came here for binary lifting only ,now binge watching tne playlist

  • @kirtikagupta4899
    @kirtikagupta4899 4 роки тому +6

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

  • @tejasghone5118
    @tejasghone5118 3 роки тому +4

    Simple and on point explanation . Thank you!

  • @rashmidewangan8627
    @rashmidewangan8627 3 роки тому

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

  • @Study2_217
    @Study2_217 Рік тому +2

    great explanation Sir!!

  • @himanshujain9082
    @himanshujain9082 4 роки тому +7

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

  • @amritsvlogs6784
    @amritsvlogs6784 4 роки тому +3

    Finally I got perfect playlist.
    Thanks a lot kartik

  • @Anonymous-pj2mv
    @Anonymous-pj2mv 3 роки тому

    Bohot hi Tagda explaination .
    No challenge For this explaination

  • @debdattabiswas4902
    @debdattabiswas4902 4 роки тому +5

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

  • @bilalkhan2298
    @bilalkhan2298 Рік тому +1

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

  • @ashutoshdixit2985
    @ashutoshdixit2985 3 роки тому +2

    best content in only 21:38 min
    great

  • @saranshsingh5495
    @saranshsingh5495 3 роки тому

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

  • @jnayehsirine6222
    @jnayehsirine6222 4 місяці тому

    i have wtached 4 videos and urs is the best bro

  • @sujoyseal195
    @sujoyseal195 3 роки тому

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

  • @anuraggupta6277
    @anuraggupta6277 3 роки тому

    Best explanation found so far....

  • @rohansinha2760
    @rohansinha2760 2 роки тому

    Brilliant and simple way of explaining . Kudos !!!

  • @yourbestie4138
    @yourbestie4138 3 роки тому +1

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

  • @ramitgoel1363
    @ramitgoel1363 3 роки тому

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

  • @jaswanthsabbisetti6820
    @jaswanthsabbisetti6820 3 роки тому +2

    You are a legend man! Keep it up!

  • @priyanshisharma1227
    @priyanshisharma1227 3 роки тому

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

  • @nishantgoel3065
    @nishantgoel3065 3 роки тому

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

  • @NaitikMishra-ll7el
    @NaitikMishra-ll7el Рік тому

    such a clear explanation.....👍👍

  • @anshusingh2473
    @anshusingh2473 2 роки тому

    curated approach niceee
    😍😍😍

  • @sg1192k
    @sg1192k 4 роки тому +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  4 роки тому +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.

  • @creativegiant148
    @creativegiant148 3 роки тому +4

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

  • @ImSaneperson
    @ImSaneperson 3 роки тому +1

    great content bhaiya . .

  • @anurag6219
    @anurag6219 3 роки тому +1

    Great work

  • @pranshunayak1792
    @pranshunayak1792 Рік тому

    Awesome content!

  • @neildahiya2524
    @neildahiya2524 4 роки тому

    Awesome Explanation

  • @ravipatel3624
    @ravipatel3624 3 роки тому

    Great Explanation man

  • @aadityasharma9790
    @aadityasharma9790 2 роки тому

    in love with ur content

  • @bhavyachawla7176
    @bhavyachawla7176 3 роки тому

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

  • @shreyashachoudhary480
    @shreyashachoudhary480 3 роки тому

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

  • @Nikita-xk9fc
    @Nikita-xk9fc 4 роки тому

    wonderful explanation

  • @shivammaini4447
    @shivammaini4447 4 роки тому

    Awesome explanation. Helps a lot.

    • @AlgosWithKartik
      @AlgosWithKartik  4 роки тому +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.

  • @seeva92
    @seeva92 3 роки тому

    @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.

  • @shashanktiwari4442
    @shashanktiwari4442 4 роки тому

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

    • @AlgosWithKartik
      @AlgosWithKartik  4 роки тому +1

      Thanks bhai :)
      sure will add more problems soon :)

  • @ashishbiswal456
    @ashishbiswal456 3 роки тому +1

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

  • @rizalrovins
    @rizalrovins 4 роки тому +1

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

  • @asthasatija728
    @asthasatija728 4 роки тому

    Awesome! Quite good explanation!

  • @Dineshkumar-uz4dw
    @Dineshkumar-uz4dw 3 роки тому +1

    Top knotch man

  • @ribhavbansal2942
    @ribhavbansal2942 2 роки тому +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 10 місяців тому

      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

  • @mridulkaran345
    @mridulkaran345 3 роки тому

    Amazing!

  • @surajpratapsingh34
    @surajpratapsingh34 Рік тому

    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

  • @mridulkumar786
    @mridulkumar786 4 роки тому

    Awesome, Thanks a lot

  • @ayushgarg5929
    @ayushgarg5929 4 роки тому

    Please make a playlist on Game theory...It is no where available on UA-cam

  • @aliaksandrhn1
    @aliaksandrhn1 3 роки тому +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  3 роки тому

      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 3 роки тому

      @@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  3 роки тому +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 :)

  • @shubhamkumargupta3478
    @shubhamkumargupta3478 3 роки тому +1

    Loved someone picked cses

  • @VasudevaK
    @VasudevaK Рік тому

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

  • @FinanceMode14
    @FinanceMode14 2 роки тому

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

  • @anubhavkalia5130
    @anubhavkalia5130 4 роки тому +1

    please share link of code

  • @mridulkumar786
    @mridulkumar786 4 роки тому

    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  4 роки тому

      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 4 роки тому

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

    • @AlgosWithKartik
      @AlgosWithKartik  4 роки тому +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!

  • @VIJAYKUMAR-dj6kf
    @VIJAYKUMAR-dj6kf 2 роки тому

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

  • @sauravpandey599
    @sauravpandey599 4 роки тому

    Why are you using windows 7

  • @omop5922
    @omop5922 Рік тому

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