Gift Wrapping Algorithm (Convex Hull)

Поділитися
Вставка
  • Опубліковано 6 чер 2024
  • Seeing as how Christmas is around the corner, I thought you guys might enjoy a quick video on how to wrap a rather intangible gift: a random distribution of points.
    This was actually an incredibly fun code to write / I love Chan's algorithm. It's one of the most beautifully simple yet clever ideas I have seen in a while. Of course, I say that about every algorithm, so who knows when I'm telling the truth!
    Anyway, thanks again for popping in and watching / I'll see you next time!
    The music came from Josh Woodward (sped up 1.5 times):
    www.joshwoodward.com/
    Please feel free to follow me on Twitter:
    / leiosos
    Twitch (where I do all the simulations):
    / leioslabs
    My second youtube channel (where I put the streams):
    ua-cam.com/channels/Ff6.html...
    or Github:
    github.com/leios
    The code I used is available here:
    github.com/leios/simuleios/tr...
    (sorry it's so messy)
    Also, discord:
    / discord
  • Наука та технологія

КОМЕНТАРІ • 89

  • @Keldor314
    @Keldor314 4 роки тому +18

    A random distribution of points! Just what I've always wanted!

  • @LeiosLabs
    @LeiosLabs  7 років тому +29

    Hey guys, I published this video and then immediately went to sleep (sorry!), but it was seriously a bunch of fun to program, and I would love to make videos like this some more!
    Anyway, let me know what you think / I hope you guys are having a wonderful day!

  • @DrawCuriosity
    @DrawCuriosity 7 років тому +49

    This video was a gift to my mind! It takes skill to explain three algorithms so well and succinctly as you did, and that's one great gift to have ;)

    • @LeiosLabs
      @LeiosLabs  7 років тому +4

      Haha, you are awesome! =)

    • @dragoncurveenthusiast
      @dragoncurveenthusiast 6 років тому

      Draw Curiosity nice to see a fellow biologist on this channel! :-)

  • @mfaraday4044
    @mfaraday4044 4 роки тому +4

    When I figured out this video , I was legitimately stunned how wonderfully you explained.
    Thank you Sir
    My biggest gift for today is that I found this channel

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

    Phenomenal video, easy, engaging and to the point !

  • @b.f.skinner4383
    @b.f.skinner4383 3 роки тому +1

    Trying to learn convex sets and this really helped with the intuition thanks :)

  • @MD-ji7dh
    @MD-ji7dh 10 місяців тому

    This dude had an intro a story and still managed to explain 3 algorithms in under 3 minutes. Crazy

  • @nishadkumar7322
    @nishadkumar7322 7 років тому +4

    Wowww. You just solved my never ending confusion in 2 minutes. Thanks a lot!! \m/

    • @LeiosLabs
      @LeiosLabs  7 років тому +3

      Visualizations help sometimes! =_

  • @DeirdreSM
    @DeirdreSM 5 років тому +1

    Thank you for the explanation of Graham. I'd understood Jarvis and the Chan approach, but not that step in the middle.

  • @albertwang5974
    @albertwang5974 6 років тому +3

    Cool, supper cool!!! we can use these algorithm to improve the efficiency of image object recognition.

    • @LeiosLabs
      @LeiosLabs  6 років тому

      Woah! That's cool! I hope it works! =)

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

    Thanks for explaining Chan's algorithm

  • @zebcode
    @zebcode 6 років тому +1

    LOVE THIS! THANK YOU!

    • @LeiosLabs
      @LeiosLabs  6 років тому

      This was one of my favorite episodes to make! I'm glad you liked it!

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

      @Michael Darrow Haha, he probably was!

  • @colemanhk
    @colemanhk 4 роки тому +2

    Quick question. For Graham Scan, what does "removing any vertex that points outward" refer to? I think it should be "removing any vertex that points inward". (My understanding inward = points toward the center of the hull; outward= points out from the center of the hull). Thanks

  • @MrRudale
    @MrRudale 7 років тому +15

    Interesting video. When looking at each distribution of random points, people are able to 'wrap' them, so the visual system must be doing some kind of algorithm too. I wonder how the human brain solves this problem, and whether that can be used to make more efficient algorithms.

    • @LeiosLabs
      @LeiosLabs  7 років тому +6

      Actually, I have been wondering something similar. In addition, is there a good machine learning approach to this? It's incredibly interesting to think about!

    • @goroth01
      @goroth01 6 років тому +9

      Unfortunately the human brain works fundamentally differently from how a computer does. A CPU is a massively serial processing machine, capable of billions of serial calculations per second, with limited parallel processing. Your brain, on the other hand, has limited serial processing, capable of perhaps 100 calculations per second on a single 'thread,' but makes up for that limitation with a massively parallel processing strategy. With an estimated 100 billion neurons, each firing on average once per second, you get an initial napkin-math estimate of around 100 billion calculations per second across the brain.
      Your brain has another trick up its sleeve, though. Each neuron can project to thousands of other neurons, and the signal of one neuron can increase or attenuate the signal from other neurons at a single synapse. Therefore each single synapse can also be considered to be a calculation. And there are somewhere on the order of 10^15 (one quadrillion) synapses in the human brain.
      Furthermore, a CPU is a general-purpose calculator, with algorithms being contained in instructions fed from memory. The brain, on the other hand, has the majority of (known) algorithms stored simply in its circuit architecture. The retina, for instance, has a specifically designed architecture that does much of the initial 'heavy lifting' of visual processing, without the need for any kind of instructions or memory. Therefore, to have a CPU approach a problem the same way the human brain does, it would either have to inefficiently emulate parallel processing (as is the case in neural network algorithms) or have a completely new architecture that emphasizes massive parallel processing.

    • @ore2236
      @ore2236 5 років тому

      You could use GPUs for the parallel calculation aspect, they're better than CPUs
      It feels a bit like those linear regression problems, i think neural networks could work fine on this, but i'm not an expert

  • @DogeisCut
    @DogeisCut 5 років тому

    I love these dots!

  • @the-er1cd
    @the-er1cd 2 роки тому

    Im somewhat embarrassed about how much I've thought about this problem specifically :)

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

    Jarvis algorithm was a little hard to implement, the notion of max angle leads to orientation and lots of nuances that are a little annoying around angles and computing, but I am glad i finally was able to implement it, just so if anyone want to do it too, try to stick to the law of cosines and use clockwise orientation determination to know if you want the inside or outside angle, arctan was a mess

  • @KingFredrickVI
    @KingFredrickVI 7 років тому +3

    I have no idea what this is used for but it seems pretty legit mate lol

    • @LeiosLabs
      @LeiosLabs  7 років тому +2

      Actually, the hulls are used a lot in a number of different places, although it's not obvious where and why. One example is in robotics. The robot has a complicated obstacle course to run through and needs an optimal path around everything. That path will end up being a convex hull around the random points.

  • @AmineZyad
    @AmineZyad 6 років тому

    Any idea about the complexity of those three methods? I can see from the last example that Graham found the convex hull quicker than the others.

    • @LeiosLabs
      @LeiosLabs  6 років тому +2

      Chan's: O(nlogh)
      Graham: O(nlogn)
      Jarvis: O(nh)
      n -- number of points
      h -- size of hull
      Chan's should be faster than both. The animations are not indicative of performance.

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

    Very nice

  • @guilhermegondin151
    @guilhermegondin151 6 років тому +2

    Nice gift, even more special since it's random, so no one got the same gift as the other (or nearly no one) XD

    • @LeiosLabs
      @LeiosLabs  6 років тому +1

      Haha, I'm glad you liked it!

  • @sau002
    @sau002 5 років тому

    Excellent

  • @thebetterselfeveryday
    @thebetterselfeveryday 5 років тому

    Best explanation!!!!!

  • @glowiever
    @glowiever 5 років тому

    What about expanding polytope algorithm?

  • @dhruvarya3891
    @dhruvarya3891 6 років тому +1

    Sweet treat for the brain.

    • @LeiosLabs
      @LeiosLabs  6 років тому

      Yeah, I really liked these algorithms! They were super fun to implement!

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

    I love it

  • @viforcarry8372
    @viforcarry8372 6 років тому

    I had to do it for on a project, and I found another way to make a convex hull:- join the dots of the min, the max of x and y.
    - then for each line of the polygon calculate the max distance to it and add it to the polygon.
    After some iteration, you got the convex hull.

    • @viforcarry8372
      @viforcarry8372 6 років тому

      After doing some research, I'm not the first to use this method, it's called quickhull and have O(nlogn) efficiency.

    • @LeiosLabs
      @LeiosLabs  6 років тому

      Yeah! Quickhull is amazing! We didn't get to it here, but it's definitely on the list for the AAA.

    • @viforcarry8372
      @viforcarry8372 6 років тому +1

      The other cool thing is that this method can be generalized in a n-dimension space

    • @LeiosLabs
      @LeiosLabs  6 років тому

      But I thought it was tricky for >3D, right? I remember running into it a few days ago for knot theory algorithms...

    • @viforcarry8372
      @viforcarry8372 6 років тому +1

      Leiosos, I don't know I didn't test it already

  • @thob
    @thob 7 років тому +2

    If you can find an outermost point in the set, can't you just then rotate it all in one direction and keep selecting the outermost point until you find the first one again, and then unite them all?

    • @KingFredrickVI
      @KingFredrickVI 7 років тому +4

      You'd end up with an infinite turn problem. You'd have to figure out how much to turn. 1 degree? 5 degree? 0.0001 degree? Too little it takes a very long time and too large and you miss 1/2 the points.
      Not to mention that every time you rotate by, say 0.5 degree, you have to calculate the sin and cos then apply that calculation to every point, every iteration.

    • @LeiosLabs
      @LeiosLabs  7 років тому +1

      Yeah, this is one of the problems, I think. Basically, these algorithms guarantee you will find the convex hull by using a discrete method. Unfortunately, they are not as intuitive as our continuous minds want them to be (which is super cool, when you think about it).

    • @andywright8803
      @andywright8803 5 років тому

      KingFredrickVI Not really. Simply rotate clockwise by say 1 degree. If it doesn't pass another point, rotate by another degree, otherwise if just one point has been crossed, you have located the next point in the wrapping. If you have crossed more than one, go anticlockwise by half a degree etc.
      Seeing the algorithm considering points in the middle just makes my head hurt and my brain scream that this is so inefficient

    • @TheFlynCow
      @TheFlynCow 5 років тому +1

      And how do you determine if a point has been "crossed"?
      Also, if a better algorithm is really that simple, do you think the mathematicians mentioned in the video, with multiple awards and honors, couldn't come up with it?

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

    Thank you

  • @johnnyboy90528
    @johnnyboy90528 5 років тому +2

    I love it. :3

    • @LeiosLabs
      @LeiosLabs  5 років тому +1

      Thanks! I'll see what other algorithmic gifts I can find lying around...

  • @guilhermegondin151
    @guilhermegondin151 6 років тому

    Just one question, you've said that chan's algorithm is the most efficient one, but in the video The graham's algorithm ends early.
    Is this because of the diferent distributions or was it intencional?
    Also, would be nice if you mention the asymptotic analysis, just for a numerical reference of how better one is then another.

    • @guilhermegondin151
      @guilhermegondin151 6 років тому

      By "was it intencional" I mean, did you chose some special case in with it would do better for some reason?

    • @LeiosLabs
      @LeiosLabs  6 років тому +1

      Yeah, we have 2/3 of these in the algorithm archive: www.algorithm-archive.org/chapters/computational_geometry/gift_wrapping/jarvis_march/jarvis_march.html
      I am working on getting a better description of these.
      Basically Chan's is faster because it uses the graham scan where it is useful (large number of points), and it uses the jarvis march where it is useful (small number)

  • @n484l3iehugtil
    @n484l3iehugtil 6 років тому +3

    Can you explain why the hybrid algorithm is faster than either of the two previous algorithms by themselves?

    • @LeiosLabs
      @LeiosLabs  6 років тому +3

      Jarvis March is best when there is a small number of particles, Graham Scan is best when there is a large number, so we use Graham scan when there's a large number of particles so that we can read a small number of particles into the Jarvis March. I hope that makes sense.

    • @n484l3iehugtil
      @n484l3iehugtil 6 років тому +2

      Thanks for replying. But I wonder why would you want to read a small number of particles into the Jarvis March when you can just solve the problem outright using purely the Graham Scan?

    • @gabrielmello3293
      @gabrielmello3293 5 років тому +1

      If you have a million cases of small number of particles, you're gonna want to use the fastest one on small number of particles.

  • @Abdega
    @Abdega 5 років тому

    I’m gonna try ordering a Graham Scan at Denny’s

  • @joulesjams20
    @joulesjams20 7 років тому +3

    Why is Chad's algorithm better? Wouldn't it be easier to use Jarvis algorithm as Chad's algorithm has Jarvis algorithm within it

    • @joulesjams20
      @joulesjams20 7 років тому +1

      gustorn but why?

    • @LeiosLabs
      @LeiosLabs  7 років тому

      Yeah, I ended up just ignoring the complexity cases. I should make a video explaining them in more depth and then link to that video from now on. I just need a way to make the cases much easier to understand.

  • @Asdayasman
    @Asdayasman 5 років тому

    These algorithms do a lot of checking the wrong points... What if you just found the right point first time?
    Start with the leftmost point, draw a line from it to the top of the space, then rotate it clockwise about the leftmost point by an angle. If there are no points to the left of the line, repeat the rotation. If there are more than one point to the left of the line, divide the angle by 2 and rotate the line about the leftmost point anticlockwise, and repeat the check. If there is exactly one point to the left of the line, that's the right point. Continue on from there, drawing the line with the slope equal to the line drawn from the first point to the second point.
    There'd probably be some shortcut you could take if multiple points were on the same line of the convex hull.
    Surely I'm not the first to think of this?

    • @ThePowerExcess
      @ThePowerExcess 5 років тому

      This won't work on higher dimensions than 2D. Convex hulls problems arise in high dimensional data. This is more of a toy example.

    • @LeiosLabs
      @LeiosLabs  5 років тому

      So, that implementation seems to work fine in a physical model, but computationally, it is precisely the same as the Jarvis march (unless I misunderstood).

    • @Asdayasman
      @Asdayasman 5 років тому

      The march is a comprehensive check over every point, right? My method is a search, somewhat akin to a binary search.
      Someone made the point of higher dimensions being a problem, but I'm not sure I believe that; represent the rotation of the bisection as a matrix instead, and give the bisection n-1 dimensions, where n is the amount of dimensions in the search space. Instead of searching for one point, you'd search for n-1 points, (so a 3D space would have you building a triangle), one at a time. Once you find one point, you rotate around the shape you've built up to that point until you hit the total of n-1, then pick an edge off which to build the next shape and carry on.
      I'unno, it _feels_ like there's something in this.

    • @TheFlynCow
      @TheFlynCow 5 років тому +1

      The problem lies within determining wether a point is on the "left" or "right" side of the line.

  • @freeshavaacadooo1095
    @freeshavaacadooo1095 5 років тому +3

    Graham's seems like the most efficient.

    • @LeiosLabs
      @LeiosLabs  5 років тому +1

      Chan's is literally graham's when it's most efficient and jarvis's when it's most efficient. That's why it's *slightly* better.

  • @LigiaCeballos
    @LigiaCeballos 6 років тому

    thank you TTwTT

  • @TebiByyte
    @TebiByyte 6 років тому

    What about quickhull?

    • @LeiosLabs
      @LeiosLabs  6 років тому

      Oh yeah. Love that one. We intend to put it in the algorithm archive after quicksort.

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

    when will you send me my dots?

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

    first algorithm was O(n2) runtime. the scan was O(nlogn). I believe the last one is at best O(n sqrt(n)) and averages to O(nlogn) runtime, but there are ways to get an average O(nsqrt(n)) runtime complexity which is provably the best you can achieve for gift wrapping.
    Simplest one is divide the region into sqrt(n) sections of sqrt(n) points each. You can do this in n*sqrt(n) time by selecting a random sqrt(n) points via blue noise and using a voronoi diagram to seperate the regions. Then for each of these sqrt(n) regions the problem recurses. Once you get down to regions of 5 points or less, use the scan method to wrap them, then recurse out and wrap again until you've wrapped the full thing.
    The cost here is n*sqrt(n) for the voronoi seperation. Then everything else after doesn't contribute to runtime complexity. the recursive step adds sqrt(n) * sqrt(n) sqrt(sqrt(n)) which is really n^(5/4), which is less than the n^(3/2) and does not contribute to the runtime complexity. Using the general rule that on average for a collection of n points, the hull surrounding it will contain on average sqrt(n) points (intuition being perimeter vs area), the scan which was before nlogn will now take sqrt(n)logn, which is also less than nlogn and thus doesn't contribute to runtime complexity.
    There are certainly more effecient ways to impliment giftwrapping with O(nsqrtn) runtime, but this is the simplest one to explain I think

  • @europetruck5879
    @europetruck5879 6 років тому

    U desirve a subscribe just for your voice . Plus for explenatin you are giving to us

    • @LeiosLabs
      @LeiosLabs  5 років тому

      I am glad you liked the video!

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

    Sir ap urdu lecture b provide kr dayn gift wraping

  • @ankushmenat
    @ankushmenat 5 років тому

    Do this in 3d

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

    too short

  • @andywright8803
    @andywright8803 5 років тому

    Why not just start with the leftmost point and a vertical line, then gradually rotate the line about that point clockwise until it intersects with another point, then continue to rotate about the new point until you intersect a new point and continue until you have gone all the way round . You're welcome.

    • @LeiosLabs
      @LeiosLabs  5 років тому

      I think the main problem with this implementation is recognizing what intersects with what. I can imagine that would be costly computationally.