1. Unit IV : Geometric Algorithms
Syllabus :
• Prerequisites – Basic properties of line, intersection of
line, line segment, polygon. Line segment properties.
• Determining segment intersection in time complexity
(nlogn)
• Convex Hull problem – formulation, solving by Graham
scan algorithm, Jarvis’s march algorithm
• Closest pair of points problem – formulation, solving by
Divide and Conquer method
2. Unit IV : Geometric Algorithms
Computational Geometry :
• Computational geometry is the branch of computer science that
studies algorithms for solving geometric problems.
Applications :
• In modern Engineering and Mathematics, computational
geometry has applications in following diverse fields:
Computer Graphics
Robotics
VLSI Design
Computer Aided Design
Molecular Modeling
Metallurgy
Manufacturing
Textile Layouts
Forestry
Statistics
3. Unit IV : Geometric Algorithms
Computational Geometry :
• The input to a computational geometry problem is typically
a description of a set of geometric objects, such as a set of
points, a set of line segments, or the vertices of a polygon
in counterclockwise order.
• The output is often a response to a query about the objects,
such as whether any of the lines intersect, or perhaps a new
geometric object, such as the convex hull (smallest
enclosing convex polygon) of the set of points.
4. Unit IV : Geometric Algorithms
Computational Geometry :
Scope of chapter :
• We shall study few computational geometry algorithms
(Geometric Algorithms) in two dimensions, i.e. in a plane
• We shall represent each input object by a set of points
{p1, p1, …, pn} where each pi = (xi, yi) and xi, yi ε R. For
example, n-vertex polygon P by a sequence {p1, p1, …, pn}
of its vertices in order of their appearance on the boundary
of P.
• Computational geometry can also apply to 3-dimensions
and even higher dimensional spaces, but these problems
and solutions are difficult to visualize.
5. Unit IV : Geometric Algorithms
Computational Geometry :
Line Segment Properties :
• Convex combinations : A convex combination of two distinct
points p1 = (x1, y1) and p2 = (x2, y2) is any point p3 = (x3, y3)
such that for some α in the range 0 ≤ α ≤ 1, we have x3= αx1 +
(1 - α)x2 and y3= αy1 + (1 - α)y2. We can also write this as p3=
αp1 + (1 - α)p2. Thus p3 is any point that is on the line passing
through p1 and p2 and is on or between p1 and p2 on the line.
• Line segment : Given two distinct points p1and p2, the line
segment p1p2 (over line) is the set of convex combinations of p1
and p2. We call p1 as left endpoint and p2 as right endpoints of
segment p1p2.
• Directed segment : Directed segment p1p2 (over right arrow) is
referred as the vector p2 with p1 as the origin (0, 0).
6. Unit IV : Geometric Algorithms
Computational Geometry :
Line Segment Properties contd…:
• Computational geometry algorithms requires answers to
questions about the properties of line segments. These
questions are:
1. Given two directed segments p0p1 and p0p2, is
p0p1 clockwise from p0p2, with respect to their
common endpoint p0?
2. Given two line segments p0p1 and p1p2, if we
traverse p0p1,and then p1p2, do we make a left turn
at point p1?
3. Do line segments p1p2 and p3p4 intersect?
7. Unit IV : Geometric Algorithms
Computational Geometry :
Line Segment Properties contd…:
• We can answer each question in O(1) time, since the input
size of each question is O(1). Moreover methods use only
additions, subtractions, multiplications and comparisons.
Divisions and trigonometric functions are not used since
both of these functions are expensive and prone to
problems of round-off error.
• To answer these questions the method which avoids
division or trigonometric function is more accurate and that
is use of cross products.
8. Unit IV : Geometric Algorithms
Computational Geometry :
Cross Product :
• Consider vectors p1 and p2 as shown below
p1+p2
p2(2, 4)
p2(5, 2)
(0.0)
• We can interpret the cross product p1x p2 as the signed area of the
parallelogram formed by points (0,0), p1, p2 and p1+ p2.
• An equivalent but more useful definition gives the cross product as
the determinant of a matrix.
9. Unit IV : Geometric Algorithms
Computational Geometry :
Cross Product contd…:
• p1xp2 = |x1 x2| = x1y2 – x2y1 = - p2xp1
|y1 y2|
• If p1xp2 is positive then p1 is clockwise from p2 with
respect to origin (0,0) and if p1xp2 is negative then p1 is
counterclockwise from p2 with respect to origin (0,0).
• In above example cross product p1xp2 = 16 i.e. positive
hence p1 is clockwise from p2.
• If cross product is zero then vectors p1and p2 are collinear
pointing in either the same or opposite direction.
10. Unit IV : Geometric Algorithms
Computational Geometry :
Determining whether directed segment p0p1 is clockwise from p0p2:
• To determine whether a directed segment p0p1 is closer to a
directed segment p0p2 in a clockwise or counterclockwise
direction w.r.t. common endpoint p0, we simply translate to
use p0 as the origin and compute cross product of p1‘ and
p2‘.
p1‘x p2‘ = (p1 – p0) x (p2 – p0)
= |(x1 – x0) (x2 – x0)|
|(y1 – y0) (y2 – y0)|
• If cross product is positive then p1‘ is clockwise from p2‘ &
if cross product is negative then p1‘ is counterclockwise
from p2‘.
• This answers Question 1 above.
11. Unit IV : Geometric Algorithms
Computational Geometry :
Determining whether consecutive segments turn left or right :
• To determine whether consecutive line segment p0p1 and
p1p2 turn left or right at point p1
• In other words we want a method to determine which way
a given angle p0 p1 p2 turns.
• Cross product allow us to answer this question too, without
computing the angle.
• This answers Question 2 above.
p2 p2 Check whether directed segment p0 p2
is clockwise or counterclockwise
p1 p1 relative to directed segment p0 p1. A
Counter Clockwise positive cross product (p2 – p0) x
Clockwise p0 p0 (p1 – p0) indicates p0 p2 is clockwise
w.r.t. p p and it makes a right turn at p .
12. Unit IV : Geometric Algorithms
Determining whether two line segments intersect :
• To determine whether two line segments intersect, we check whether
each segment straddles the line containing the other. A line segment
p1p2 straddles a line if point p1 lies on one side of the line and point
p2 lies on the other side. A boundary case arises if p1 and p2 lies
directly on the line.
• Thus two line segments intersect if and only if either or both of the
following conditions hold.
1. Each segment straddles the line containing other.
2. An endpoint of one lies on the other segment (Boundary
case).
• Following procedures implement this idea:
1. DIRECTION : computes relative orientations
2. ON-SEGMENT : determines whether a point known to be
collinear with a segment lies on that segment.
3. SEGMENT-INTERSECT : returns TRUE if segments p1p2
and p3p4 intersect, otherwise returns FALSE
13. Unit IV : Geometric Algorithms
Determining whether two line segments intersect :
Algorithm DIRECTION (pi, pj, pk)
// pi and pj are endpoints of one line segment and pk is one
// point (endpoint) of the other line segment
{ return (pk- pi )x(pj- pi ) // cross product O(1)
}
Algorithm ON-SEGMENT (pi, pj, pk)
// pi and pj are endpoints of one line segment and pk is one
// point (endpoint) of the other line segment
{ if (min(xi- xj) ≤ xk ≤ max(xi- xj)) AND // O(1)
(min(xi- xj) ≤ xk ≤ max(xi- xj))
return TRUE
else return FALSE
}
14. Unit IV : Geometric Algorithms
Determining whether two line segments intersect :
Algorithm SEGMENT-INTERSECT (p1, p2, p3 , p4)
// algorithm returns TRUE if p1p2 and p3p4 intersect,
// otherwise it returns FALSE
{ d1= DIRECTION (p3, p4, p1) // if +ve, p1 is left to p3p4 //O(1)
d2= DIRECTION (p3, p4, p2) // if - ve, p2 is right to p3p4 //O(1)
d3= DIRECTION (p1, p2, p3) // if +ve, p3 is left to p1p2 //O(1)
d4= DIRECTION (p1, p2, p4) // if - ve, p4 is right to p1p2 //O(1)
if ((d1 > 0 AND d2 < 0) OR (d1 < 0 AND d2 > 0)) AND //O(1)
((d3 > 0 AND d4 < 0) OR (d3 < 0 AND d4 > 0))
return TRUE
elseif d1 = 0 AND ON-SEGMENT(p3, p4, p1) return TRUE; //O(1)
elseif d1 = 0 AND ON-SEGMENT(p3, p4, p1) return TRUE; //O(1)
elseif d1 = 0 AND ON-SEGMENT(p3, p4, p1) return TRUE; //O(1)
elseif d1 = 0 AND ON-SEGMENT(p3, p4, p1) return TRUE; //O(1)
else return FALSE;
} // Algorithm answers Qustion No 3 and its Time Complexity is O(1)
15. Unit IV : Geometric Algorithms
Determining whether two line segments intersect :
p1 p4 d4 < 0
d1 < 0
p2
p3 d2 > 0
d3 > 0 (a)
d1 = (p1- p3) x (p4- p3),
d2 = (p2- p3 ) x (p4- p3)
d3 = (p3- p1) x (p2- p1),
d4 = (p4- p1 ) x (p2- p1)
16. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
• We shall design an algorithm for determining whether
any two line segments in a set of given segments
intersect.
• Algorithm uses a technique known as “sweeping”, which
is common to many computational geometry algorithms.
• The algorithm runs in O(nlogn) time, where n is the
number of segments given. It determines only whether or
not any intersection exists, it does not print all the
intersections. We can develop an algorithm to print all the
intersections in time O(n2) in worst case.
17. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Sweeping :
• In sweeping an imaginary vertical sweep line that passes
through the given set of geometric objects, usually from left
to right.
• Sweeping provides a method for :
1. Ordering geometric objects, usually by placing them
into a dynamic data structure such as red-black tree and
2. Taking advantage of relationship among them
(objects).
• The line-segment-intersection algorithm considers all the
line segment endpoints in left to right order and checks for
an intersection each time it encounters an endpoint.
18. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Sweeping contd… :
• The algorithm makes to simplifying assumptions:
1. No input segment is vertical and
2. No three or more input segments intersect at a single
point.
• The line-segment-intersection algorithm can be modified
to take care of these simplifying assumptions, but it
becomes most difficult and challenging to prove the
correctness of the algorithm for such boundary conditions.
19. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Ordering segments :
• Since we assume that there are no vertical segments any
input segment intersecting a given vertical line intersects it
at a single point. Thus we can order the segments that
intersect a vertical sweep line according to the y-coordinate
of the point of intersection.
• Consider two segments s1 and s2. We say that these
segments are comparable at x if the vertical sweep line with
x-coordinate x intersects both of them.
• We say that s1 is above s2 at x, written as s1 ≥ x s2, if s1 and
s2 are comparable at x, and the intersection of s1 with the
sweep line at x is higher than the intersection of s2 with the
same sweep line or if s1 and s2 intersect at the same point.
20. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Ordering segments contd…: Example
| | | d
a | | |
| b | |
c | | |
| | | Relation ≥x is,
| | | Transitive and
r t u Reflexive
a ≥r c a ≥t b b ≥u u It is neither
b ≥t c Symmetric nor
a ≥t c Antisymmetric
Segment enters the ordering when its left endpoint is encountered
by the sweep and it leaves the preordering when its right
endpoint is encountered
Total preorder may differ for different values of x.
Segment d is not comparable.
21. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Ordering segments contd…:
What happens when two line segment intersect? :
e | | | | |
| | | | | i
g | | | | |
h | | | | |
f | | | | |
v z w
e ≥vf f ≥we
Since we assume that no three segments intersect at the
same point, and when sweep line passes through shaded
region the e and f are consecutive in its preorder when
segment g is deleted from the preorder.
22. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Moving the sweep line :
• Sweeping algorithms typically manage two sets of data.
1. The sweep line status gives the relationship among the
objects that the sweep line intersects
2. The event point schedule is a sequence of points called vent
points, which we order from left to right. Whenever sweep
line reaches the x-coordinate of an event point, the sweep
halts, processes the event point, and then resumes the sweep.
Sweep line status is updated only at the event points.
• We sort the segment endpoints by increasing x-coordinate and proceed
from left to right. If two or more endpoints are co-vertical, i.e. they
have the same x-coordinate, we break the tie by putting all the co-
vertical left endpoints before the co-vertical right endpoints. Within a
set of co-vertical left endpoints we put those with low y-coordinate
first, and we do the same for co-vertical right endpoints. This
arrangement can be achieved by modifying coordinate as (x, 0, y) for
left endpoint and (x, 1, y) for right endpoint. And then perform
sorting on modified coordinates.
23. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Moving the sweep line :
• When we encounter a segment’s left endpoint, we INSERT
the segment into the sweep line status, and we DELETE the
segment from the sweep line status when we encounter
right endpoint of the segment.
• We also need to check whether two consecutive segments
intersect each other when we insert a new segment in sweep
line status and when we delete a segment from sweep line
status and neighbors of that segment become consecutive
first time. (ABOVE and BELOW operations).
24. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Moving the sweep line :
• Thus sweep line status is a total preorder T (BST) for which we
require the following operations:
1. INSERT (T, s) : Insert segment s into T
2. DELETE (T, s) : Deletes segment s from T
3. ABOVE (T, s) : returns the segment immediately
above segment s in T.
4. BELOW (T, s) : returns the segment immediately
below segment s in T.
• It is possible for segments s1 and s2 to be mutually above each
other in the total preorder T; this situation can occur if s1 and s2
intersect at the sweep line (boundary case). In this case the two
segments may occur in either order in T
25. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Segment intersection pseudocode : Algorithm
Algorithm ANY-SEGMENTS-INTERSECT (S)
// Algorithm takes as input a set S of n line segments and
// returns the Boolean value TRUE if any pair of segments in S
// intersect, and it returns FALSE otherwise. A red-black tree
// maintains the total preorder T.
{ T = Ф;
sort the endpoints of the segments in S from left to right ,
breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y-coordinate first. // O(nlogn) using Merge or Heapsort
26. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Segment intersection pseudo code : Algorithm
Algorithm ANY-SEGMENTS-INTERSECT (S) … contd.
for each point p in the sorted list of endpoints // O(n)
if p is the left endpoint of a segment s
{ INSERT (T, s) // O(logn)
if (ABOVE(T, s) exists and intersects s) OR
(BELOW(T, s) exists and intersects s)
return TRUE; // O(1)
}
if p is the right endpoint of a segment s
{ if both (ABOVE(T, s) and (BELOW(T, s) exists
AND (ABOVE(T, s) intersect(BELOW(T, s)
return TRUE; // O(1)
DELETE (T, s) // O(logn)
}
return FALSE;
}
27. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Segment intersection pseudo code :
Analysis of ANY-SEGMENTS-INTERSECT
Time Complexity = O(logn) + O(logn) = O(logn)
Space Complexity = O(n) for preorder T
Note : Algorithm can be modified to print all intersecting
segments with time complexity O(n2).
28. Unit IV : Geometric Algorithms
Determining whether any pair of segments intersect :
Segment intersection pseudo code : Example
a | | | d| | e | |
| | | | | | |
| | | | | | |
| | | | | | |
| | c | | | | | f
| | | | | | |
| | | | | | |
| b | | | | | |
| | | | | | |
a a a d d e Algorithm ends with TRUE
b c a c d since segments d and b
b c b c intersect when segment c
b b deleted d and b become
consecutive first time.
29. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
• The convex hull of a set Q of points, denoted by CH(Q) is the
smallest convex polygon P for which each point in Q is on the
boundary of P or in its interior.
• Here implicit assumption is that all points in set Q are unique
and that Q contains at least three points which are not colinear.
• Intuitively we can think of each point in Q as being a nail.
sticking out from a board. The convex hull is then the shape
formed by a tight rubber band that surrounds all the nails.
• Following figure shows a set of points and its convex hull.
Points p0, p1, p3, p10, p12 represents convex hull.
30. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
. p10
. p6
. p9 . p7 . p5
p12 . . P11 . p8 . p4 . p3
. p2
.
. p1
p0 Convex Hull = {p0, p1, p3, p10, p12}
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
31. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Methods to compute convex hull :
• Incremental Method : In this method points are first sorted
from left to right, yielding a sequence {p1, p2, … , pn}. At the ith
stage we update the convex hull of the i-1 leftmost points,
CH({p1, p2, … , pi-1}), according to the ith point from the left,
thus forming CH({p1, p2, … , pi}). The running time of this
method is O(nlogn).
• Divide and Conquer Method : In this method, the set of n
points are divided into two subsets in O(n) time, one containing
n/2 leftmost points and one containing n/2 rightmost points,
recursively compute the convex hulls of the subsets. Then by
means of clever method, combine the hulls in O(n) time. The
running time of this method is O(nlogn)
32. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Methods to compute convex hull :
• Prune and Search Method : In this method, we find the upper
portion (or “upper chain”) of the convex hull by repeatedly
throwing out a constant fraction of the remaining points until
only the upper chain of convex hull remains. Similarly we obtain
lower chain. This method is asymptotically the fastest, if the
convex hull contains h vertices, it runs in only O(nlogh) time.
• Rotational Sweep Method : We shall discuss here two
algorithms namely, Graham’s scan and Jarvis’s march. Both of
them use this method with computation time O(nlogn) and
O(nlogh) respectively.
33. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Polar angle :
• The polar angle of a point p1 w.r.t. an origin point p0 is the angle
of the vector p1 - p0 in the usual polar coordinate system.
For example,
o The polar angle of (3, 5) w.r.t. (2, 4) is the angle of vector (1, 1)
i.e. (x1- x0, y1 - y0). The polar angle θ = tan(1/1) = Π /4 i.e. 45
degrees.
o The polar angle of (3, 3) w.r.t. (2, 4) is the angle of vector
(1, -1) i.e. (x1- x0, y1 - y0). The polar angle θ = tan(-1/1) = 7Π /4
i.e. 315 degrees.
34. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Cross product :
• Cross product of vectors p1 (x1, y1) and p2 (x2, y2) is defined as :
p1 x p2 = det | x1 x2|
| y1 y2|
= x1y2 – x2y1 = - p2 x p1
oIf p1 x p2 is positive , then p1 is clockwise from p2 w.r.t.
the origin (0, 0).
oIf p1 x p2 is negative , then p1 is counterclockwise from
p2 w.r.t. the origin (0, 0).
Example
35. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Graham’s Scan Algorithm :
• The algorithm solves the convex hull problem by maintaining a
stack S of candidate points. It pushes each point of the input set
Q on to the stack one time. And it eventually pops from the
stack each point that is not a vertex of CH(Q). When the
algorithm terminates, stack S contains exactly the vertices of
CH(Q), in counterclockwise order of their appearance on the
boundary.
• The algorithm Graham-Scan takes as input a set Q of points,
where |Q| ≥ 3. It calls the function TOP(S), which returns the
point on top of stack S without changing S, and NEXT-TO-
TOP(S), which returns the point one entry below the top of
stack S without changing S.
36. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Graham’s Scan Algorithm :
Algorithm Graham-Scan
1. Let p0 be the point in Q with the minimum y coordinate, or the
leftmost such point in case of tie.
2. Let (p1, p2, …, pm) be the remaining points in Q, sorted by
polar angle in counterclockwise order around p0 (if more than
one point has the same angle, remove all but the one that is
farthest from p0) // O(nlogn)
3. Let S be an empty stack
4. Push (p0, S)
5. Push (p1, S)
6. Push (p2, S)
37. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Graham’s Scan Algorithm contd … :
Algorithm Graham-Scan
7. for i = 3 to m do // O(m)
8. { while the angle formed by points NEXT-TO-TOP(S),
TOP(S) and pi, makes a non-left turn
9. POP (S) // O(m)
10. Push (pi, S) } // O(m)
11. Return S
Time Complexity : O(nlogn)
Space Complexity : O(n) space required for stack
38. Unit IV : Geometric Algorithms
Convex Hull Algorithms : Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0 Points sorted on value of polar angle
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
39. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
40. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
p10 .
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
41. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
42. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
43. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
44. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
45. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
46. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
47. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
48. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
49. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
50. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
51. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
52. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
53. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
54. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
55. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
56. Unit IV : Geometric Algorithms
Convex Hull Algorithms :Graham’s Scan Algorithm :
. p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
p0 Convex Hull = {p0, p1, p3, p10, p12}
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
57. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Jarvis’s March Algorithm :
• Jarvis’s march computes the convex hull of a set Q of points by a
technique known as Package wrapping or gift wrapping.
• The algorithm runs in time O(nh), where h is the number of vertices
of CH(Q). When h is o(logn), Jarvis’s march is asymptotically faster
than Graham’s scan.
• Intuitively, Jarvis’s march simulates wrapping a taut piece of paper
around the set Q. We start by taping the end of the paper to the lowest
point in the set, i.e. to the same point p0 with which we start
Graham’s scan. We know that this point must be a vertex of the
convex hull. We pull the paper to the right to make it taut, and then
we pull it higher until it touches a point in set Q. This point must also
be a vertex of the convex hull. Keeping the paper taut, we continue in
this way around the set of vertices until we come back to our oiginal
point p0
58. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Jarvis’s March Algorithm :
• More formally, Jarvis’s march builds a sequence H = (p0 , p1, …
ph-1) of the vertices of CH(Q). We start with p0 . As following
figure shows, the next vertex p1, in the convex hull has the smallest
polar angle with respect to p0 . In case of ties, we choose the point
farthest from p0 . Similarly, p2, has the smallest polar angle w.r. t. p1,
and so on. When we reach the highest vertex, say pk (breaking ties by
choosing the farthest such vertex), we have constructed the right
chain of CH(Q).
• To construct the left chain. We start at pk and choose pk+1 as the point
the smallest polar angle w.r.t. pk, but from the negative x-axis. We
continue like this, forming the left chain by taking polar angles from
the negative x-axis, until we come back to our original vertex p0.
59. Unit IV : Geometric Algorithms
Convex Hull Algorithms : Jarvis’s March Algorithm :
left chain right chain →
- x axis . p10
. p6
. p9 . p7 p5 .
p12 . . P11 . p8 p4 . . p3
. p2
.
. p1
left chain p0 right chain → Convex Hull = {p0, p1, p3, p10, p12}
A set of points Q = {p0, p1, … , p12} with its convex hull CH(Q)
60. Unit IV : Geometric Algorithms
Convex Hull Algorithms :
Jarvis’s March Algorithm : Analysis
• Jarvis’s march has a running time of O(nh). For each of h
vertices of CH(Q), we find the vertex with the minimum polar
angle. Each comparison between polar angles takes O(1) time.
Thus jarvis’s march takes O(nh) time.
Further Reading :
• Quick Hull :
Use of D & C strategy like Quick sort.
Time complexity O(nlogn)
• Parallel Algorithm for Convex Hull:
Time complexity O(logn) using n CREW PRAMs.
61. Unit IV : Geometric Algorithms
Finding the closest pair of points :
• Consider the problem of finding the closest pair of points
in a set Q of n ≥ 2 points.
• “Closest” refers to the usual Euclidean distance, the
distance between points p = √(x1 – x2)2 + (y1 – y2)2. If
points are coincident, then distance between them is zero.
• A brute-force closest pair algorithm simply looks at all C(n,
2) = O(n2) pairs of points.
• Here we shall discuss divide-and-conquer algorithm for
this problem, whose running time is described by the
recurrence T(n) = 2T(n/2) + O(n) and it has O(nlogn) time.
62. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Divide and Conquer Algorithm :
• Each recursive invocation of the algorithm takes as input a subset P
of Q and arrays X & Y each of which contains all the points of the
input subset P.
• The points in array X are sorted so that their x-coordinates are
monotonically increasing. Similarly array Y is sorted by
monotonically increasing y-coordinates.
• Note that in order to attain the O(nlogn) time bound, we cannot
afford to sort in each recursive call; if we did, the recurrence equation
would be T(n) = 2T(n/2) + O(nlogn) which solves to T(n) = O(nlog 2n).
• We shall use “presorting” to maintain this sorted order properly
without actually sorting in each recursive call.
• A given recursive invocation with inputs P, X, Y first checks whether
|P| ≤ 3. If so invocation simply performs the brute force method
discribed above for C(|P|, 2) pairs
63. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Divide :
• Find a vertical line l that bisects the point set P into two sets PL
and PR such that |PL| = upper ceil of (|P| / 2) and |PR| = lower
ceil of (|P| / 2).
• All points in PL are on or to the left of line l, and all points in PR
are on or to the right of l.
• Divide the array XL and XR, which contain the points of PL and
PR respectively sorted by monotonically increasing
x-coordinate.
• Similarly divide the array YL and YR, which contain the points
of PL and PR respectively sorted by monotonically increasing
y-coordinate.
64. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Conquer :
• Having divided P into PL and PR , make two recursive calls,
one to find closest pair of points in PL and the other to find
closest pair of points in PR.
• Inputs to first call are (PL, XL,YL) and inputs to second call
are (PR, XR, YR).
• Let the closest pair distances returned for PL and PR be δL
and δR respectively and δ = min (δL, δR ).
Combine :
• The closest pair is either the pair with distance δ found by
either recursive calls or it is a pair of points with one point
in PL and other in PR and whose distance is < δ, both points
of the pair must be within δ units of line l as shown below.
65. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Combine contd…:
. | PL | PR | .
. | 2δ | | .
. a-- | |e,e’ | c .
δ | | . PR | .
. | . PL | | .
b--| |f,f’ | d .
| . | l . |
These two points which are within distance δ must reside
within 2δ-wide vertical strip centered at line l.
66. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Combine contd….:
To find such a pair if it exists, we do the following :
1. Create an array Y’ which is the array Y with all points in
2δ-wide vertical strip. The array Y’ is sorted by y-
coordinate just as Y is.
2. For each point p in the array Y’ that are within δ units of p.
In this case for each point p, only maximum 7 points in Y’
that need to be considered. Compute the distance to each of
these 7 points from p and keep track of closest pair distance
δ’ found over all pairs of points in Y’.
3. If δ’ < δ, then vertical strip contains the closest pair than
that in the recursive calls found. Return this pair and
distance δ’, otherwise return the closest pair found by
recursive calls.
67. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Implementation of algorithm:
• To obtain O(nlogn) running time algorithm, our recurrence
should be T(n) = 2T(n/2) + O(n), where T(n) is the run ning
time for a set of n points.
• The main difficulty comes from ensuring that the arrays
XL, XR, YL, YR which are passed to recursive calls, are
sorted by proper coordinates and also that the array Y’ is
sorted by y-coordinate.
• If the array X that is received by recursive call is already
sorted, then we can easily divide divide setr P into PL and
PR in linear time.
68. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Implementation of algorithm contd….:
• Similarly arrays XL and XR can be created in linear time.
• Having partitioned P into PLand PR, it needs to form arrays
YL and YR which are sorted by y-coordinate in linear time.
We can do this by using the opposite method used in Merge
sort. Following pseudo code does this task:
69. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Implementation of algorithm contd….:
Algorithm split-Y (Y, YL, YR)
{ let YL[1 … Y.length] and YR[1 … Y.length] arrays;
YL.length = YR.length = 0;
for i = 1 to Y.length do
{ if Y[i] ЄPL.
YL.length = YL.length + 1
YL[YL.length] = Y[i];
else YR.length = YR.length + 1
YR[YR.length] = Y[i];
}
}
70. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Implementation of algorithm contd….:
• The only remaining question is how to get the points sorted
in the first place. We presort them i.e. we sort them once
for all before the first recursive call.
• We pass these sorted arrays into the first recursive call, and
from there we whittle them down through the recursive
calls as necessary.
• Presorting adds additional O(nlogn) time the total running
time of the algorithm, but now each step of recursion takes
linear time exclusively.
71. Unit IV : Geometric Algorithms
Finding the closest pair of points :
Analysis :
• Let T(n) be the running time of each recursive call
• Let T’(n) be the running time of the entire algorithm
• Then T’(n) = T(n) + O(nlogn) and
T(n) = 2T(n/2) + O(n) … if n >3
= O(1) … if n ≤ 3
Solution for T(n) = O(nlogn)
Therefore T’(n) = O(nlogn) + O(nlogn)
= O(nlogn)