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

Поделиться
HTML-код
  • Опубликовано: 14 янв 2025

Комментарии • 80

  • @insidecode
    @insidecode  Год назад +3

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

  • @danieloladele1433
    @danieloladele1433 3 года назад +14

    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  3 года назад

      Wow, thanks!

    • @vakman9497
      @vakman9497 9 месяцев назад +1

      Speak for yourself I still don’t get it 😂

  • @insidecode
    @insidecode  3 года назад +20

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

  • @stanverlaan1147
    @stanverlaan1147 3 года назад +11

    Nicely explained and great animations as well. Good job!

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

  • @דודיוס
    @דודיוס Год назад +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.

  • @rounakkhuntia1923
    @rounakkhuntia1923 2 года назад +5

    I was not able to understand the rectangle part.

  • @sandeepjnv13
    @sandeepjnv13 3 года назад +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  3 года назад +6

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

  • @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!

  • @ipranavprashant
    @ipranavprashant 8 месяцев назад +1

    Woah that was some good visualization!

  • @shashwatjohri1503
    @shashwatjohri1503 Год назад +2

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

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

  • @milanpatel6240
    @milanpatel6240 2 года назад +1

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

  • @tapasranjannayak2366
    @tapasranjannayak2366 Год назад +1

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

  • @gautamdangodra4392
    @gautamdangodra4392 Год назад +1

    Amazing explanation ! 👏👏

  • @bibeksharma600
    @bibeksharma600 3 года назад +2

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

  • @haritejanarayanabhatla5355
    @haritejanarayanabhatla5355 3 года назад +1

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

  • @leducphuclong
    @leducphuclong 8 месяцев назад

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

  • @sabarivasanvelayutham9256
    @sabarivasanvelayutham9256 3 года назад +1

    Wonderful explanation 👏

  • @durgakaruturi3407
    @durgakaruturi3407 3 месяца назад

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

  • @shahabsalem7272
    @shahabsalem7272 2 года назад +1

    Great explanation and the visualisations helped a lot!

  • @Thahira-yc2ys
    @Thahira-yc2ys 6 месяцев назад

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

  • @nyanhtetaung78
    @nyanhtetaung78 5 месяцев назад

    very clear after watching this.

  • @uDontSayXx
    @uDontSayXx 3 года назад +1

    Thanks for the clear explanation! Amazing vids :)

  • @notonlyit1361
    @notonlyit1361 2 года назад

    Just brilliant! Thank you!

  • @rodrigomarchi9755
    @rodrigomarchi9755 Год назад

    great video man congrats, keep going!

  • @arquier
    @arquier 3 года назад +1

    Thanks, very well explained

  • @AndyWallWasWeak
    @AndyWallWasWeak 6 месяцев назад

    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

  • @immortalpuffin6643
    @immortalpuffin6643 3 года назад +1

    Nice video, keep up the good work!

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

  • @alfredocossetin5154
    @alfredocossetin5154 Год назад

    great video, very well explained.

  • @ANGEL_GFREENS
    @ANGEL_GFREENS 2 года назад

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

  • @suhanikalra3532
    @suhanikalra3532 3 года назад

    thanks for the clear explanation

  • @akarigale173
    @akarigale173 Год назад

    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?

  • @ismahanemerzouk2451
    @ismahanemerzouk2451 3 года назад

    the best explanation ever

  • @SK-qn5ry
    @SK-qn5ry 10 месяцев назад

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

  • @zeyadzakaria5223
    @zeyadzakaria5223 2 года назад

    Nice explanation

  • @ghostwar9103
    @ghostwar9103 Год назад

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

  • @user-ig1tf5bx2e
    @user-ig1tf5bx2e 3 года назад

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

  • @AMANMODAN
    @AMANMODAN Год назад

    Nicely explained but i found it tough

  • @hailai1053
    @hailai1053 3 года назад

    thanks a lot bro

  • @wizrom3046
    @wizrom3046 2 года назад

    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  2 года назад

      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

  • @Ali-qm8ix
    @Ali-qm8ix Год назад

    wonderful

  • @weirdo9236
    @weirdo9236 2 года назад

    Wonderful

  • @mrinmoybanik5598
    @mrinmoybanik5598 Год назад

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

  • @puffvayne
    @puffvayne 3 года назад

    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!

  • @gabealberro6081
    @gabealberro6081 Год назад

    great vid

  • @mohammadyahya78
    @mohammadyahya78 11 месяцев назад

    Wow!

  • @Mert_991
    @Mert_991 17 дней назад

    aga aksan yapmaya kasmasan iyi video

  • @zholud
    @zholud Год назад

    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  Год назад

      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 Год назад

      @@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 7 месяцев назад

    Best video