How to find the closest pair of points in O(nlogn)? - Inside code

Поділитися
Вставка
  • Опубліковано 28 жов 2024

КОМЕНТАРІ • 79

  • @insidecode
    @insidecode  Рік тому +3

    Discover the new graph theory algorithms course: inscod.com/graphalgo
    🔴
    / \
    🔵-🔴
    | |
    🔴-🔵

  • @danieloladele1433
    @danieloladele1433 2 роки тому +11

    Wow, Finally a video that explains very well. This is the best so far, even a novice in programming would understand the logic.

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

      Wow, thanks!

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

      Speak for yourself I still don’t get it 😂

  • @insidecode
    @insidecode  3 роки тому +18

    Fix: At 11:20, the generic form starts with aT(n/b) not aT(n/2)

  • @ayushmansingh2650
    @ayushmansingh2650 2 роки тому +7

    damn man just amazing explanation of this concept I have never found such a clear explanation for closest pairs among million of points.

  • @stanverlaan1147
    @stanverlaan1147 3 роки тому +11

    Nicely explained and great animations as well. Good job!

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

    Man you are incredible in all your videos. This the most interpretive content, i have ever watched ❤💖

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

      Thanks a lot for your comment!

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

    First video I check of yours. I liked it, it's very informative, keep it up!

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

      Thanks a lot! Don't hesitate to watch other ones

  • @דודיוס
    @דודיוס Рік тому +5

    Hey,first of all thanks for the amazing explanation!!!
    I've 1 question: in 3:46you said that we need to check the points lower than the actual point, why is that? How did we already iterate through the higher points?
    Thanks!

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

      Because we sorted according to y coordinates. The first point we look is the one which is at top, others are all above. And when you go to second point, it is above of others and below of the first point.

  • @sandeepjnv13
    @sandeepjnv13 2 роки тому +23

    First of all i want to say that your explanation is of highest standard, almost in the category of 3Blue1Brown.
    What i wanted to ask you is do you use python manim lib to animate this?

    • @insidecode
      @insidecode  2 роки тому +6

      Hello, thanks for the compliment, nope, I use PowerPoint

  • @rounakkhuntia1923
    @rounakkhuntia1923 2 роки тому +5

    I was not able to understand the rectangle part.

  • @ipranavprashant
    @ipranavprashant 6 місяців тому +1

    Woah that was some good visualization!

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

    I have been asked the same question in Google phone screen. Unfortunately I didn't see your video earlier. Good work.

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

    Hey your algorithms are easily explained and optimal too ..thanks man

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

    Well explained and the code is very precise and understandable ! Cheers !!!

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

    Amazing explanation ! 👏👏

  • @leducphuclong
    @leducphuclong 6 місяців тому

    The best in topic I found, thank you so much !!

  • @nyanhtetaung78
    @nyanhtetaung78 3 місяці тому

    very clear after watching this.

  • @akarigale173
    @akarigale173 10 місяців тому

    Hi! It seems to me that you did not account the possibility of different points with equal x-coordiantes. It is incorrect to write xsorted_left = xsorted[:n//2], because xsorted[:n//2 + 1] may has the same x-coordiate. And what, for example, should we do if all the points have the same x-coordinate?

  • @AndyWallWasWeak
    @AndyWallWasWeak 3 місяці тому

    is there a way to modify this to get all pairs starting from the one of the 2 closest points and ending with the pair of the two furthest apart, i.e. get all pairs sorted by distance

  • @finchshi4342
    @finchshi4342 2 роки тому +1

    Excellent explanation! I just have one question though. When we get the sets of the points , why did we traverse the ysorted array instead of the xsorted array?

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

      Because the band is a vertical strip. Traversing the ysorted traverses the points from top to bottom.

  • @shahabsalem7272
    @shahabsalem7272 2 роки тому +1

    Great explanation and the visualisations helped a lot!

  • @tapasranjannayak2366
    @tapasranjannayak2366 11 місяців тому +1

    may someone please let me know how to made the animation or the tool used for the animantaion.

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

    Thanks for the clear explanation! Amazing vids :)

  • @durgakaruturi3407
    @durgakaruturi3407 13 днів тому

    Why do we need to create ysorted_left and ysorted_right if we are using just ysorted for in_band construction

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

    great video man congrats, keep going!

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

    i have a feeling the list comprehensions/other things you're wriitng in python will go n^2 anyway

  • @Thahira-yc2ys
    @Thahira-yc2ys 3 місяці тому

    Hi, why cant we take d/2 from both side of mid line?

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

    Wonderful explanation 👏

  • @user-ig1tf5bx2e
    @user-ig1tf5bx2e 3 роки тому

    Just discovered this , channel , neste3ref bik
    mor lcovid Insha'Allah ndiro match XD

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

    Just brilliant! Thank you!

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

    Could please provide information about the best worst and average cases of this algorithm????

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

    the best explanation ever

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

    Nice video, keep up the good work!

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

    great video, very well explained.

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

    Thanks, very well explained

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

    Nicely explained but i found it tough

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

    thanks for the clear explanation

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

    This algorithm only reduces the amount of bruteforcing to HALF of a simple brute force approach. So it doesnt save much time.
    I thought up a much faster approach; the killer in computation time is the square root, the whole pythag part really with the mult, mult, sqroot.
    So you dont do that.
    Instead;
    Brute force the lot, BUT just get the distances x1-x2 and y1-y2, the larger of these I call the simple distance. Very quick because it is just two subtractions.
    Then the real pythag hypotenuse distance can never be more than 1.41 times the length of the simple distance.
    Then keep a live track of the lowest simple distance, and any time a new lowest simple distance is found calc a "limit distance" of 1.5 times the simple distance.
    Any future point pair larger than that cannot possibly be the closest pair.
    So while the brute force is running; you just compile a short list of any pair where its simple distance is less than the best "limit distance".
    I wrote some C code and tested this. 100 random distributed points, means 4950 brute force tests (but they are incredibly fast tests now!) And in the majority of cases there were only 10 to 30 pairs that made it into the final list out of 4950 tests! So that has eliminated 99.5 percent of the pairs!
    Even better, you dont have to do the pythag sqroot etc on all 10 to 20 final pairs. Because you already have the smallest simple distance (and the limit distance) you only need to do the pythag testing on 1 to 7 of the final pairs! The average was around TWO pairs needing to be tested!
    This algorithm is far quicker than the divide and conquer algorithm in cases where the n (total number of points) is not too large.
    As an additional tweak, when brute force testing to get the two subtractions x1-x2 and y1-y2, you can abort the second half of the test (y) if the x simple distance is larger than the limit. You would have to run it in hardware and do time trials to see if that makes a significant difference. Depends a lot on array accessing time.

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

      Hello, your solution is interesting if proven right, but it's still in O(n²) time complexity, but the solution presented in the lecture is in O(nlogn), which is way faster for large values of n

  • @ghostwar9103
    @ghostwar9103 10 місяців тому

    can some one explain what is the meaning of recursif call the fuction to ger dL dan dR.

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

    Nice explanation

  • @SK-qn5ry
    @SK-qn5ry 7 місяців тому

    how to make such animation videos? what skills needed for this??

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

    great vid

  • @Ali-qm8ix
    @Ali-qm8ix 10 місяців тому

    wonderful

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

    Wonderful

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

    thanks a lot bro

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

    In CLRS textbook, it takes 5 pages to explain this, and I just get confused after reading page 1. Your video is so nice, although your speak speed is a bit quick. With 0.75 speed mode, I get so fxcking clear for this algo. thank you so so much!

  • @mohammadyahya78
    @mohammadyahya78 8 місяців тому

    Wow!

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

    How to extend this for points in higher dimensions and with other distance metrics like with L-inf ?

  • @zholud
    @zholud 11 місяців тому

    The two points that you found before can be as close to the middle line as they want, thus there is no contradiction in finding them in the rectangle two delta by delta. Second, in your explanation you omitted something re why one looks below some point. You said “let’s look at this point for example” and then out of blue that something has been processed already above it. Finally, Python sucks.

    • @insidecode
      @insidecode  11 місяців тому

      Hey I'm interested to discuss your comment point by point. So you're saying that we can find more than 6 points in the two delta by delta rectangle?

    • @zholud
      @zholud 11 місяців тому

      @@insidecode No. I am saying that the contradiction is due to the definition of the minimal distance, not because "it should have been found before" @6:10. Correct logic is "delta prime is smaller than delta, which contradicts that delta is the minimum distance". You lack a bit of structure in your reasoning which makes it hard to follow. You say six but draw 9. Forget to mention that stripe is sorted according to y coordinates and that the algorithm scans down...etc. Sorry. I shouldn't have been rude. Also, it's easy to show that it can't be more than 7 (+1) which would have made the explanation clearer (divide into 8 squares with sides delta/2 there will be two points inside the square => distance less than delta/sqrt(2) < delta => contradiction) - this is easier to explain and doesn't matter too much. It would then suffice to say that it can be shown that you don't need to scan more than 6 (+1).

  • @prakharmishra-m5r
    @prakharmishra-m5r 4 місяці тому

    Best video