LCA - Lowest Common Ancestor

Sdílet
Vložit
  • čas přidán 5. 04. 2021
  • Tutorial on LCA algorithm. We use Binary Lifting to get O(N*log(N)) preprocessing and O(log(N)) to find the lowest common ancestor of two nodes in a tree.
    Binary Lifting video • Binary Lifting (Kth An...
    SPOJ problem www.spoj.com/problems/LCASQ/
    code github.com/Errichto/youtube/b...
    Two homework problems:
    1) Answer queries "find distance between two given nodes U and V" cses.fi/problemset/task/1135
    2) Given a tree with weighted edges (i.e. every edge has some value), answer queries "given two nodes U and V, find minimum weight along path U-V". (I don't have a source for this one).
    Coding live streams - / errichto
    FAQ - github.com/Errichto/youtube/w...
    Subscribe for more educational videos on algorithms, coding interviews and competitive programming.

Komentáře • 78

  • @Errichto
    @Errichto  Před 3 lety +34

    Here are two homework problems for you:
    1) Answer queries "find distance between two given nodes U and V" cses.fi/problemset/task/1135
    2) Given a tree with weighted edges (i.e. every edge has some value), answer queries "given two nodes U and V, find minimum weight along path U-V". (I don't have a source for this one).

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

      Oh, I tried to solve 1-st problem with python3, looks like I get it right, but on big input I stucked with TIME LIMIT EXCEEDED. But in Statistics sections I saw an accepted python solution. Any tips to speed up python code to fit into 1 second timeout?
      I am already:
      1) Used collections.deque for dfs
      2) Used array.array for up (partially), parents and depth arrays.
      3) sys.stdin.readline() instead of input()
      I will appreciate any help!

    • @prakash_77
      @prakash_77 Před 3 lety

      ​@@pycz I did with C++. 2 or 3 were getting TLE. It's probably because i/o is slow. I added these three at start of main function.
      1.) ios::sync_with_stdio(0);
      2.) cin.tie(0);
      3.) cout.tie(0);
      in C++ and got AC. There'll be something similar to that in Python also.

    • @lakshyamittal3098
      @lakshyamittal3098 Před 3 lety

      Is the second question a little bit like Sparse Table (RMQ)? But complexity for each query will be O(Log(N)) only because we still need to find the LCA. Am I right?

    • @exe2543
      @exe2543 Před 2 lety

      @@pycz I got TLE with Java as well on the big inputs, but when I ran it locally I got the correct result. I tried using an iterative rather than recursive DFS but it still TLE's. I'm just going to assume it's an issue with reading the inputs because I have implemented the actual algorithm itself correctly and it produces the correct result (I'm using BufferedReader and PrintWriter).

  • @allwell8570
    @allwell8570 Před 3 lety +16

    Generally, it is difficult to learn from extra-ordinary people like you. But you are exception to that also. Thank you so much.

  • @mukulkumar2316
    @mukulkumar2316 Před 3 lety +33

    After watching numerous errichto videos , whenever I explain my code to anyone else I get errichto's accent.
    Your videos are awesome.

  • @coefficient1359
    @coefficient1359 Před 3 lety +143

    Errichto should be in the UN protected list ❤.

  • @ksg7882
    @ksg7882 Před 3 lety +43

    I am simple man , i see Errichto is teaching something i will click it

  • @ShubhamKumar-me7xy
    @ShubhamKumar-me7xy Před 3 lety +3

    It's a great initiative by you to teach algorithms. Looking forward for more such great videos. Also add some more practice problems of whatever you explain.

  • @bethsuni4011
    @bethsuni4011 Před 3 lety

    Thanks for the lectures. I like this format a lot: short videos with explanation, code, and example.

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

    YES! A THOUSAND TIMES YES! Thank you soooooo much for making a video on LCA!

  • @saiprasadduduka2155
    @saiprasadduduka2155 Před 3 lety

    Some of your videos(like this one) are so well made. Thnx🔥

  • @saitejaballa6759
    @saitejaballa6759 Před 7 měsíci

    The way you explained the binary lifting is just awesome ❤❤

  • @glaucoa.9214
    @glaucoa.9214 Před 3 lety +4

    Errichto one of the best, I love his videos.

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

    I know how to do LCA with RMQ, but this solution is something else
    I love it to bits 💚💚

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

    Thank you Kamil for such a great explanation, keep up the good work:)

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

    Thank you i was searching for this after the binary lifting video 👍

  • @abdullahzaghmout5615
    @abdullahzaghmout5615 Před 3 lety

    Thank you for these videos. Keep on making them

  • @RajatSingh-dg8ov
    @RajatSingh-dg8ov Před 3 lety +1

    I know that these problems are very easy for you but trust me these are very hard for me.
    And if you are making it, I will watch it!. You are literal GOD in coding!.
    Please keep on making these very basic videos on concepts, I really liked your Binary Search video as well!.
    Thanks

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

    This guy doing a god's work...whoever is the god of cp

  • @nguyentrongvanviet
    @nguyentrongvanviet Před 2 lety

    thank you so much I can finally solve the question on cses
    hope more video like this from you

  • @josephwong2832
    @josephwong2832 Před 2 lety

    awesome explanation of LCA and Binary Lifting

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

    your explanation is great, thank u

  • @sunnyrawat931
    @sunnyrawat931 Před rokem

    Clear and concise. I love it.

  • @itz_me_imraan02
    @itz_me_imraan02 Před 2 lety

    His voice and explanation both are soothing🙂

  • @shivanshsrivastava9264
    @shivanshsrivastava9264 Před 11 měsíci

    Loved the explanation. Thanks a lot

  • @iPaperPlane
    @iPaperPlane Před 3 lety +6

    Can't believe this still *F R E E*

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

    Your contents are awesome ❤️

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

    This was the video which I wanted....thanks😊😊

    • @ani68
      @ani68 Před 3 lety

      I think these concepts are very easy for kamil....😅

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

    You are an amazing teacher

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

    Great content 🙌💯

  • @dtrade4787
    @dtrade4787 Před rokem

    Thanks man. Astounded by your brain

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

    Very Nice explanation. Thanks. Can you do a tutorial on heavy-light decomposition?

  • @srishtikdutta8946
    @srishtikdutta8946 Před 3 lety +6

    Please do some tutorials on centroid and heavy light decomposition.

  • @satyampal7235
    @satyampal7235 Před rokem

    Thanks it was helpful.

  • @AniketSingh-mt6zr
    @AniketSingh-mt6zr Před 11 měsíci

    Absolute beauty

  • @prathamkumar2968
    @prathamkumar2968 Před 3 lety

    The preprocessing of vector

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

      He meant every query is answered in O(logN)

  • @mizel_1121
    @mizel_1121 Před 3 lety

    I love you bro u help me a lot

  • @Harpreetsingh-qc8sf
    @Harpreetsingh-qc8sf Před 3 lety +3

    I am a beginner sir but really like your way hope i get green soon on cf

  • @wow4669
    @wow4669 Před rokem

    Thanks you😊

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

    2 videos in a week. 🎉

  • @GauravKumar-ue7nz
    @GauravKumar-ue7nz Před 2 lety

    Thank you so Much

  • @Kyanqy_Tarb3r
    @Kyanqy_Tarb3r Před 9 měsíci

    which compiler are you using in this video?

  • @vigneshs8738
    @vigneshs8738 Před 2 lety

    The j iterator should only go reverse order from Log -1 to 0 and not the other which works in binary lifting?

  • @seaorz
    @seaorz Před 3 lety

    Thanks!

  • @manojmaurya9683
    @manojmaurya9683 Před 3 lety

    People have their own kind hero.I have you #the best❤️

  • @mzmzgreen
    @mzmzgreen Před 3 lety

    What about time/space complexity of this algorithm? Is it logN like in a binary search? I guess worst case is O(N) when first node is a root and second node is the deepest leaf, right?

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

      We only have for-loops of size log(N) so it's O(log(N)) per query.
      Preprocessing takes O(N*log(N)) space & time.

  • @ramsankar585
    @ramsankar585 Před 3 lety

    can we move from lowest power to highest power? If not why?

  • @sumitrohilla1494
    @sumitrohilla1494 Před 2 lety

    Which drawing software is that...plz someone tell me

  • @vatsalagarwal2466
    @vatsalagarwal2466 Před 3 lety

    I have noticed one thing whicle actually coding for the problem. In the second loop, from LOG - 1 to 0, if we change this to 0 to LOG - 1, the program gives WA for soms tcs. Can someone explain why ? if the LCA is xth ancestor of a and b (after equalizing the depths), does it matter how we access the bits of x. It can be either fromt MSB or LSB. Please clear this concept.

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

      0 to LOG-1 in last for loop will not work. Because we are moving both nodes one by one to the level just below the lowest common ancestor.
      Let us call this level as 'Limiting level'.
      Suppose, we have to make 2 jumps to reach limiting level.
      As you start for loop from 0, you'll check for 2^0 jumps from initial position, then it is allowed (as they will not be equal) , therefore you will make jump. From that position, you can check for 2, 4 ,8... jumps. But we are just 1 jump away from limiting level.
      Therefore we can not reach limiting level here, that is why it is not working.

  • @anonymoussloth6687
    @anonymoussloth6687 Před 2 lety

    In the case of finding lca just once, doesn't this perform worse than the linear solution? Not only does it take extra space, but we do a linear DFS to pre process all parents and then find the lca

    • @notwatermango
      @notwatermango Před rokem

      for small cases maybe, for larger ones the linear is worse

  • @kongzilla2897
    @kongzilla2897 Před 3 lety

    Nice :)

  • @ananyapamde4514
    @ananyapamde4514 Před 2 lety

    May god bless you

  • @pavankumar-cy6mg
    @pavankumar-cy6mg Před 3 lety +1

    what if last for loop goes form 0 to n-1

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

    I'm tellin y'all, this guy is friends with Megamind

  • @rredy
    @rredy Před 4 měsíci

    You used to live in Suwałki?

  • @raven9965
    @raven9965 Před 3 lety

    im guessing your using a PC, can you share what you are using to
    sketch, hardware and program?

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

      github.com/Errichto/youtube/wiki/FAQ#hardware

  • @Harpreetsingh-qc8sf
    @Harpreetsingh-qc8sf Před 3 lety +1

    I am not able to get ideas quickly for simple adhoc problems of A B problems please suggest me some idea where to practicr for these ques.

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

      Codeforces (div2&3, edu), Atcoder (ABC), Leetcode and basically all the platforms.

    • @Harpreetsingh-qc8sf
      @Harpreetsingh-qc8sf Před 3 lety

      Thanks Errichto hope you win codejam this tym

  • @juan-tj1xf
    @juan-tj1xf Před 3 lety

    Geeeeezz...

  • @fruith_
    @fruith_ Před rokem

    Likes

  • @rugrid146
    @rugrid146 Před 3 lety

    COME TO BRAZIL !!!

  • @josealejandrovaroncarreno1692

    is beutiful

  • @hkt9100
    @hkt9100 Před 3 lety

    hackercup T-shirt lol

  • @amiproject
    @amiproject Před 3 lety

    T

  • @harryguanous7198
    @harryguanous7198 Před 3 lety

    Please heart me, i somehow arrived early