Convex Hull Algorithm - Graham Scan and Jarvis March tutorial

preview_player
Показать описание
Given a set of points on a 2 dimensional plane, a Convex Hull is a geometric object, a polygon, that encloses all of those points. The vertices of this polygon maximize the area while minimizing the circumference.

Note that if some additional points were to be included, then the covering area is reduced while the circumference is increased. Likewise, if some of the vertices of the Convex Hull are omitted, then the resulting polygon won’t cover all of the points in the set.

The Convex Hull has a wide variety of applications, ranging from image recognition in robotics to determining animal’s home range in ethology. And in this tutorial we are going to look at how to calculate the Convex Hull using two different algorithms. The first one is called “Graham Scan” while the second is called “Jarvis March”, also known as "gift wrapping algorithm". Both of them are conceptually fairly simple, but there are a few tricky implementation pitfalls that I will point out.

Then it sorts the remaining points, ordering them by the angle that they make relative to the bottom most point and the horizontal. You can see that the angles would range from 0 to 180 degrees, given that the reference point is the bottom most one in the set.

Finally, the algorithm loops over those points in sorted order, effectively scanning in the counterclockwise direction. Each point is placed on a stack, but only if it makes a line with a counterclockwise turn relative to the previous 2 points on the stack. If that is not the case, then the previous point is popped off the stack. The algorithm continues popping points off of the stack until the resulting turn is counterclockwise.

Graham Scan Java source code:

Let’s talk about some of the implementation details that you’ll need to handle. First of all, to determine if it’s a clockwise turn or not, you could calculate the degrees of the angles using trigonometry and then compare them. But we don’t really care what the actual angles are. We just want to know if it’s a clockwise turn or not. To do just that and save a few CPU cycles in the process, we can make use of a cross product.

If you don’t recall, the cross product of two vectors V & W calculates the signed area of the parallelogram that is formed if you connect the two vectors like so. If V is on the right of W, then the cross product ends up positive. But if V is on the left of W, then it’s negative.

But then, instead of doing any kind of sorting, it just loops through all of the points again in a brute force way to find the point that makes the smallest counterclockwise angle in reference to the previous vertex. It simply repeats this iteration through all of the points until all of the vertices are determined and it gets back to the starting point.

Jarvis March source code in Java:

Looping through every point for each vertex may seem like a lot more inefficient, but the algorithm terminates as soon as it finds all of the vertices. This means that if the number of vertices is small, in particular, less than log of n, then it’ll perform better than the Graham Scan algorithm.

More formally, the running time of Jarvis March is O(n * h) where h is the number of vertices.

Of course, if the number of vertices is large, such as when all of the points are along the perimeter, then the running time turns for the ugly of O(n^2).

By the way, the function for finding the point with the smallest counterclockwise angle is exactly the same as the one used previously that makes use of the cross product. As a little bonus, dealing with collinear points is a little easier because here you just need to pick the point that is furthest away distance wise, without needing to worry about the slope of the line.

Timothy Chan’s paper for divide and conquer algorithm:

Written and narrated by Andre Violentyev
Рекомендации по теме
Комментарии
Автор

Both Jarvis March and Graham Scan algorithms are very well explained. Amazingly, in less that 8 minutes! Thanks and keep up the good work!

anigorgorian
Автор

best explanation on Jarvis and Graham scan algorithm

ahszen
Автор

This is by far the best explanation on Jarvis and Graham scan algorithm yet precise

Mrfredondieki
Автор

i watched my professors recording of about an hour just for graham scan and still didn't understand a single thing and this good sir here taught me both algorithms in 7 minutes, respect

kingrajput
Автор

The best possible explanation of both algorithms; The clear and exact visual representation of the processes, made it easier to understand. The explanation was quite precise and to the point.

EvanMilliken
Автор

This is the best explaination of Graham Scan algorithm on youtube. Thanks a lot!

krayush
Автор

A big big thanks to you for explaining it in such a simple way. Didn't think it'd be this simple. Thank You!

gargnakshatra
Автор

one of the best channel, explanation is simple and precise.

rajneeshverma
Автор

Nice Explanation of convex hull & its algorithms, till now I seen.
Thank you🙏

abhishekkangane
Автор

Wel done! Your videos are the clearest explanations of the big picture of every algorithm. They are kept simple but complete and without understatements.

jataman
Автор

This is the best explanation i could find on the internet 👏

AbhinavRawal
Автор

So far, this is the best explanation of the algorithm I've seen!

deniskhakimov
Автор

I found this channel a few months ago and was stunned. Everything is well-explained, much better than in my classes. I've learned a lot. Thank you so much!!!

hadung
Автор

By far the best explanation of these two algorithms.

viz_dugz
Автор

What an incredibly good explanation, you really thought about which segments of the explanation are harder to understand and explained them really well, thank you so much for the video!

mathisawesome
Автор

Incredible how you can explain these fairly complex subjects with such simplicity. I am glad that I found your channel! Very good content.

RandomGuyPlay
Автор

THANKYOU for explaining something so complex in such simple words.

shivangisharma
Автор

Great Video! It makes my time save from boring 1 hour lecture of my university professor.

parthodasporag
Автор

this is so much better than my professor, I believe all professor should use animation for visualization of algorithms just like you did!

tianrungou
Автор

Thank you, I love teachers like you. Amazing

kiwichi
join shbcf.ru