
In
computational geometry, the gift wrapping algorithm is an
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
for computing the
convex hull of a given set of points.
Planar case
In the two-dimensional case the algorithm is also known as Jarvis march, after R. A. Jarvis, who published it in 1973; it has
O(''nh'')
time complexity
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
, where ''n'' is the number of points and ''h'' is the number of points on the convex hull. Its real-life performance compared with other convex hull algorithms is favorable when n is small or h is expected to be very small with respect to n. In general cases, the algorithm is outperformed by many others ( See
Convex hull algorithms
Algorithms that construct convex hulls of various objects have a broad range of applications in mathematics and computer science.
In computational geometry, numerous algorithms are proposed for computing the convex hull of a finite set of poin ...
).
Algorithm
For the sake of simplicity, the description below assumes that the points are in
general position
In algebraic geometry and computational geometry, general position is a notion of genericity for a set of points, or other geometric objects. It means the ''general case'' situation, as opposed to some more special or coincidental cases that are ...
, i.e., no three points are
collinear
In geometry, collinearity of a set of points is the property of their lying on a single line. A set of points with this property is said to be collinear (sometimes spelled as colinear). In greater generality, the term has been used for aligned o ...
. The algorithm may be easily modified to deal with collinearity, including the choice whether it should report only
extreme point
In mathematics, an extreme point of a convex set S in a real or complex vector space is a point in S which does not lie in any open line segment joining two points of S. In linear programming problems, an extreme point is also called vertex ...
s (vertices of the convex hull) or all points that lie on the convex hull. Also, the complete implementation must deal with
degenerate cases when the convex hull has only 1 or 2 vertices, as well as with the issues of limited
arithmetic precision
Significant figures (also known as the significant digits, ''precision'' or ''resolution'') of a number in positional notation are digits in the number that are reliable and necessary to indicate the quantity of something.
If a number expre ...
, both of computer computations and input data.
The gift wrapping algorithm begins with ''i''=0 and a point ''p
0'' known to be on the convex hull, e.g., the leftmost point, and selects the point ''p
i+1'' such that all points are to the right of the line ''p
i p
i+1''. This point may be found in ''O''(''n'') time by comparing
polar angles of all points with respect to point ''p
i'' taken for the center of
polar coordinates
In mathematics, the polar coordinate system is a two-dimensional coordinate system in which each point on a plane is determined by a distance from a reference point and an angle from a reference direction. The reference point (analogous to t ...
. Letting ''i''=''i''+1, and repeating with until one reaches ''p
h''=''p
0'' again yields the convex hull in ''h'' steps. In two dimensions, the gift wrapping algorithm is similar to the process of winding a string (or wrapping paper) around the set of points.
The approach can be extended to higher dimensions.
Pseudocode

algorithm jarvis(S) is
// ''S'' is the set of points
// ''P'' will be the set of points which form the convex hull. Final set size is i.
pointOnHull = leftmost point in S // which is guaranteed to be part of the CH(S)
i := 0
repeat
P
:= pointOnHull
endpoint := S
// initial endpoint for a candidate edge on the hull
for j from 0 to , S, do
// endpoint
pointOnHull is a rare case and can happen only when j
1 and a better endpoint has not yet been set for the loop
if (endpoint
pointOnHull) or (S
is on left of line from P
to endpoint) then
endpoint := S
// found greater left turn, update endpoint
i := i + 1
pointOnHull = endpoint
until endpoint = P
// wrapped around to first hull point
Complexity
The inner loop checks every point in the set ''S'', and the outer loop repeats for each point on the hull. Hence the total run time is
. The run time depends on the size of the output, so Jarvis's march is an
output-sensitive algorithm.
However, because the running time depends
linearly on the number of hull vertices, it is only faster than
algorithms such as
Graham scan
Graham's scan is a method of finding the convex hull of a finite set of points in the plane with time complexity O(''n'' log ''n''). It is named after Ronald Graham, who published the original algorithm in 1972. The algorithm finds all vertices ...
when the number ''h'' of hull vertices is smaller than log ''n''.
Chan's algorithm, another convex hull algorithm, combines the logarithmic dependence of Graham scan with the output sensitivity of the gift wrapping algorithm, achieving an asymptotic running time
that improves on both Graham scan and gift wrapping.
See also
*
Convex hull algorithms
Algorithms that construct convex hulls of various objects have a broad range of applications in mathematics and computer science.
In computational geometry, numerous algorithms are proposed for computing the convex hull of a finite set of poin ...
References
*
*
{{refend
Polytopes
Convex hull algorithms
Articles with example pseudocode