SlideShare a Scribd company logo
1 of 181
Download to read offline
Analysis of Algorithms
Computational Geometry
Andres Mendez-Vazquez
November 30, 2015
1 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
2 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
3 / 90
Computational Geometry
Motivation
We want to solve geometric problems!!!
4 / 90
Field of Application
VLSI design - Generation for Fast Voronoi Diagrams for Massive
Layouts Under Strict Distances to avoid Tunneling Effects!!
5 / 90
Field of Application
Databases - Octrees for fast localization of information in database
tables
6 / 90
Field of Application
Synthetic Biology - Geometric Algorithms to Obtain new DNA
configurations for Molecular Machines
7 / 90
Field of Application
Computer Graphics for more engaging Virtual Environments - For
example: Bump Mapping!!!
8 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
9 / 90
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 90
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 90
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 90
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 90
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 90
Example
Polygon
11 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
12 / 90
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 90
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 90
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 90
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 90
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
14 / 90
Cross Product
Question!!!
Given two directed segments −−→p0p1 and −−→p0p2,
Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?
Cross Product
Cross product p1 × p2 as the signed area of the parallelogram formed
by
15 / 90
Cross Product
Question!!!
Given two directed segments −−→p0p1 and −−→p0p2,
Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?
Cross Product
Cross product p1 × p2 as the signed area of the parallelogram formed
by
15 / 90
Cross Product
Question!!!
Given two directed segments −−→p0p1 and −−→p0p2,
Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?
Cross Product
Cross product p1 × p2 as the signed area of the parallelogram formed
by
15 / 90
Cross Product
A shorter representation
p1 × p2 = det p1 p2 =det
x1 x2
y1 y2
= x1y2 − x2y1 = −p2 × p1
Thus
if p1 × p2 is positive, then p1 is clockwise from p2.
if p1 × p2 is negative, then p1 is counterclockwise from p2.
16 / 90
Cross Product
A shorter representation
p1 × p2 = det p1 p2 =det
x1 x2
y1 y2
= x1y2 − x2y1 = −p2 × p1
Thus
if p1 × p2 is positive, then p1 is clockwise from p2.
if p1 × p2 is negative, then p1 is counterclockwise from p2.
16 / 90
Cross Product
A shorter representation
p1 × p2 = det p1 p2 =det
x1 x2
y1 y2
= x1y2 − x2y1 = −p2 × p1
Thus
if p1 × p2 is positive, then p1 is clockwise from p2.
if p1 × p2 is negative, then p1 is counterclockwise from p2.
16 / 90
Regions
Clockwise and Counterclockwise Regions
Figure: Darker counterclockwise; lighter clockwise with respect to p
17 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
18 / 90
Turn Left or Right
Question
Given two line segments −−→p0p1 and −−→p1p2,
if we traverse −−→p0p1 and then −−→p1p2, do we make a left turn at point p1?
19 / 90
Turn Left or Right
Simply use the following idea
Compute cross product (p2 − p0) × (p1 − p0)!!!
This translates p0 to the origin!!!
What about (p2 − p0) × (p1 − p0) = 0?
Left Turn = counterclockwise; Right Turn = clockwise
20 / 90
Turn Left or Right
Simply use the following idea
Compute cross product (p2 − p0) × (p1 − p0)!!!
This translates p0 to the origin!!!
What about (p2 − p0) × (p1 − p0) = 0?
Left Turn = counterclockwise; Right Turn = clockwise
20 / 90
Turn Left or Right
Simply use the following idea
Compute cross product (p2 − p0) × (p1 − p0)!!!
This translates p0 to the origin!!!
What about (p2 − p0) × (p1 − p0) = 0?
Left Turn = counterclockwise; Right Turn = clockwise
20 / 90
Turn Left or Right
Simply use the following idea
Compute cross product (p2 − p0) × (p1 − p0)!!!
This translates p0 to the origin!!!
What about (p2 − p0) × (p1 − p0) = 0?
Left Turn = counterclockwise; Right Turn = clockwise
counterclockwise clockwise
20 / 90
Code for this
We have the following code
Direction(pi, pj, pk)
1 return (pk − pi) × (pj − pi)
21 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
22 / 90
Intersection
Question
Do line segments −−→p1p2 and −−→p3p4 intersect?
Very Simple!!! We have two possibilities
1 Each segment straddles the line containing the other.
2 An endpoint of one segment lies on the other segment.
23 / 90
Intersection
Question
Do line segments −−→p1p2 and −−→p3p4 intersect?
Very Simple!!! We have two possibilities
1 Each segment straddles the line containing the other.
2 An endpoint of one segment lies on the other segment.
23 / 90
Intersection
Question
Do line segments −−→p1p2 and −−→p3p4 intersect?
Very Simple!!! We have two possibilities
1 Each segment straddles the line containing the other.
2 An endpoint of one segment lies on the other segment.
23 / 90
Case I This summarize the previous two possibilities
The segments straddle each other’s lines.
Oposite Signs
Figure: Using Cross Products to find intersections
24 / 90
Case II No intersection
The segment straddles the line, but the other does not straddle the
other line
Same Signs
Figure: Using Cross Products to find that there is no intersection
25 / 90
Code
Code
Segment-Intersection(p1, p2, p3, p4)
1 d1 = Direction (p3, p4, p1)
2 d2 = Direction (p3, p4, p2)
3 d3 = Direction (p1, p2, p3)
4 d4 = Direction (p1, p2, p4)
5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0) and
6 (d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0))
7 return TRUE
Figure: The Incomplete Code, You still need to test for endpoints over the
segment
26 / 90
Code
Code
Segment-Intersection(p1, p2, p3, p4)
1 d1 = Direction (p3, p4, p1)
2 d2 = Direction (p3, p4, p2)
3 d3 = Direction (p1, p2, p3)
4 d4 = Direction (p1, p2, p4)
5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0) and
6 (d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0))
7 return TRUE
Figure: The Incomplete Code, You still need to test for endpoints over the
segment
26 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
27 / 90
Sweeping
Sweeping
Use an imaginary vertical line to pass through the n segments with events
x ∈ {r, t, u}:
Figure: Vertical Line to Record Events
28 / 90
Thus
This can be used to record events given two segments s1 and s2
Event I: s1 above s2 at x, written s1 x s2.
This is a total preorder relation for segment intersecting the
line at x.
The relation is transitive and reflexive.
Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or
both (if s1 and s2 intersect at x)
29 / 90
Thus
This can be used to record events given two segments s1 and s2
Event I: s1 above s2 at x, written s1 x s2.
This is a total preorder relation for segment intersecting the
line at x.
The relation is transitive and reflexive.
Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or
both (if s1 and s2 intersect at x)
29 / 90
Thus
This can be used to record events given two segments s1 and s2
Event I: s1 above s2 at x, written s1 x s2.
This is a total preorder relation for segment intersecting the
line at x.
The relation is transitive and reflexive.
Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or
both (if s1 and s2 intersect at x)
29 / 90
Thus
This can be used to record events given two segments s1 and s2
Event I: s1 above s2 at x, written s1 x s2.
This is a total preorder relation for segment intersecting the
line at x.
The relation is transitive and reflexive.
Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or
both (if s1 and s2 intersect at x)
29 / 90
Example
Example: a r c a t c
Figure: Vertical Line to Record Events
30 / 90
Change in direction
When e and f intersect, e v f and f w e. In the Shaded Region,
any sweep line will have e and f as consecutive
V z w
Figure: Vertical Line to Record Events
31 / 90
Moving the sweep line
Something Notable
Sweeping algorithms typically manage two sets of data.
Sweep-line status
The sweep-line status gives the relationships among the objects that the
sweep line intersects.
Event-point schedule
The event-point schedule is a sequence of points, called event points,
which we order from left to right according to their x-coordinates.
As the sweep progress from left to right, it stops and processes each
event point, then resumes.
It is possible to use a min-priority queue to keep those event points
sorted by x-coordinate.
32 / 90
Moving the sweep line
Something Notable
Sweeping algorithms typically manage two sets of data.
Sweep-line status
The sweep-line status gives the relationships among the objects that the
sweep line intersects.
Event-point schedule
The event-point schedule is a sequence of points, called event points,
which we order from left to right according to their x-coordinates.
As the sweep progress from left to right, it stops and processes each
event point, then resumes.
It is possible to use a min-priority queue to keep those event points
sorted by x-coordinate.
32 / 90
Moving the sweep line
Something Notable
Sweeping algorithms typically manage two sets of data.
Sweep-line status
The sweep-line status gives the relationships among the objects that the
sweep line intersects.
Event-point schedule
The event-point schedule is a sequence of points, called event points,
which we order from left to right according to their x-coordinates.
As the sweep progress from left to right, it stops and processes each
event point, then resumes.
It is possible to use a min-priority queue to keep those event points
sorted by x-coordinate.
32 / 90
Moving the sweep line
Something Notable
Sweeping algorithms typically manage two sets of data.
Sweep-line status
The sweep-line status gives the relationships among the objects that the
sweep line intersects.
Event-point schedule
The event-point schedule is a sequence of points, called event points,
which we order from left to right according to their x-coordinates.
As the sweep progress from left to right, it stops and processes each
event point, then resumes.
It is possible to use a min-priority queue to keep those event points
sorted by x-coordinate.
32 / 90
Sweeping Process
First
We sort the segment endpoints by increasing x-coordinate and
proceed from left to right.
However, sometimes they have the same x-coordinate (Covertical)
If two or more endpoints are covertical, we break the tie by putting all the
covertical left endpoints before the covertical right endpoints.
33 / 90
Sweeping Process
First
We sort the segment endpoints by increasing x-coordinate and
proceed from left to right.
However, sometimes they have the same x-coordinate (Covertical)
If two or more endpoints are covertical, we break the tie by putting all the
covertical left endpoints before the covertical right endpoints.
Covertical Left
End Points
Covertical Right
End Points
33 / 90
Then
Second
Within a set of covertical left endpoints, we put those with lower
y-coordinates first, and we do the same within a set of covertical right
endpoints.
Covertical Left
End Points
Covertical Right
End Points
FIRST
34 / 90
Then
Process
1 When we encounter a segment’s left endpoint, we insert the segment
into the sweep-line status.
2 We delete the segment from the sweep-line status upon encountering
its right endpoint.
Thus
Whenever two segments first become consecutive in the total preorder, we
check whether they intersect.
35 / 90
Then
Process
1 When we encounter a segment’s left endpoint, we insert the segment
into the sweep-line status.
2 We delete the segment from the sweep-line status upon encountering
its right endpoint.
Thus
Whenever two segments first become consecutive in the total preorder, we
check whether they intersect.
35 / 90
Then
Process
1 When we encounter a segment’s left endpoint, we insert the segment
into the sweep-line status.
2 We delete the segment from the sweep-line status upon encountering
its right endpoint.
Thus
Whenever two segments first become consecutive in the total preorder, we
check whether they intersect.
35 / 90
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 90
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 90
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 90
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 90
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 90
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 90
What the algorithm does?
Moving the sweeping line discretely - Event-point schedule
Here, moving through the x-axis
discrete events
Using the RBT
to Store the Info
about the segment
order
37 / 90
Event-Point Schedule Implementation
For this
We can use a Priority Queue using lexicographic order
The interesting part is the Sweeping-Line Satus
Because the way we build the balanced tree
38 / 90
Event-Point Schedule Implementation
For this
We can use a Priority Queue using lexicographic order
The interesting part is the Sweeping-Line Satus
Because the way we build the balanced tree
38 / 90
Sweep-Line Status
The Above and Below relation
TIME
Sweeping Line
39 / 90
Sweeping Line Status Implementation
Use the following relation of order to build the binary tree
Given a segment x, then you insert y
Case I if y is counterclockwise, it is below x (Go to the left).
Case II if y is clockwise, it is above x (Go to the Right)
In addtion
If you are at a leaf do the insertion, but also insert the leaf at the left or
right given the insertion.
40 / 90
Sweeping Line Status Implementation
Use the following relation of order to build the binary tree
Given a segment x, then you insert y
Case I if y is counterclockwise, it is below x (Go to the left).
Case II if y is clockwise, it is above x (Go to the Right)
In addtion
If you are at a leaf do the insertion, but also insert the leaf at the left or
right given the insertion.
40 / 90
Example
We insert th first element in the circular leaves list
41 / 90
Example
We insert a inner node after binary search
Below Above
42 / 90
Example
Similar
Below Above
43 / 90
Example
Etc....
Below Above
Trigger
Rebalancing
in the inner
nodes
44 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 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−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
46 / 90
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T
(Sweeping Line Data Structure) is correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 90
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T
(Sweeping Line Data Structure) is correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 90
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T
(Sweeping Line Data Structure) is correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 90
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T
(Sweeping Line Data Structure) is correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 90
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T
(Sweeping Line Data Structure) is correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 90
Now, we have two possbibilities
Case I
a
b
p
z SWEEPING LINE
48 / 90
Case I
Moreover
z is to the left of p or goes through p.
In addition
There is a endpoint q where a and b become consecutive.
49 / 90
Case I
Moreover
z is to the left of p or goes through p.
In addition
There is a endpoint q where a and b become consecutive.
a
b
p
z
q
49 / 90
Finally
Then a and b
They become consecutive in the total pre-order of a sweep line.
50 / 90
Case II
We have that q is a left endpoint where a and b stop being
consecutive
a
b
p
z
q
51 / 90
Correctness about the order given by T
Then, given the two following cases
1 if p is in the sweep line ⇒ p == q.
2 If q is at the left of p, and it is the nearest left one.
52 / 90
Correctness about the order given by T
Then, given the two following cases
1 if p is in the sweep line ⇒ p == q.
2 If q is at the left of p, and it is the nearest left one.
52 / 90
Do we mantain the correct preorder?
We have that given that p is first
Then, it is processed first becacuse the lexicographic order.
Therefore, two cases can happen
1 The point is processed - then the algorithm returns true
2 If the event is not processed - then the algorithm must have returned
true
53 / 90
Do we mantain the correct preorder?
We have that given that p is first
Then, it is processed first becacuse the lexicographic order.
Therefore, two cases can happen
1 The point is processed - then the algorithm returns true
2 If the event is not processed - then the algorithm must have returned
true
53 / 90
Handling Case I
Segments a and b are already in T , and a segment between them in
the total pre-order is deleted, making a and b to become consecutive
a
b
p
z
qI
a
I
b
a
b
54 / 90
When is this detected?
In the following lines of the code
Lines 8–11 detect this case.
55 / 90
Handling Case II
Either a or b is inserted into T, and the other segment is above or
below it in the total pre-order.
a
b
p
z
q
56 / 90
When is this detected?
In the following lines of the code
Lines 4–7 detect this case.
57 / 90
Finally
If event point q is not processed
It must have found an earlier intersection!!!
Therefore
If there is an intersection Any-Segment-Intersect returns true all the time
58 / 90
Finally
If event point q is not processed
It must have found an earlier intersection!!!
Therefore
If there is an intersection Any-Segment-Intersect returns true all the time
58 / 90
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
59 / 90
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
59 / 90
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
59 / 90
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
59 / 90
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
59 / 90
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
59 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
60 / 90
Convex Hull
Convex Hull
Given a set of points, Q, find the smallest convex polygon P such
that Q ⊂ P. This is denoted by CH(Q).
Bunch of Points in 2D
61 / 90
Convex Hull
Convex Hull
Given a set of points, Q, find the smallest convex polygon P such
that Q ⊂ P. This is denoted by CH(Q).
Their Convex Hull
62 / 90
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
63 / 90
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
63 / 90
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
63 / 90
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
63 / 90
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
63 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
64 / 90
Graham’s Scan
Graham’s Scan Basics
It keeps a Stack of candidate points.
It Pops elements that are not part of the CH(Q).
Whatever is left in the Stack is part of the CH(Q).
65 / 90
Graham’s Scan
Graham’s Scan Basics
It keeps a Stack of candidate points.
It Pops elements that are not part of the CH(Q).
Whatever is left in the Stack is part of the CH(Q).
65 / 90
Graham’s Scan
Graham’s Scan Basics
It keeps a Stack of candidate points.
It Pops elements that are not part of the CH(Q).
Whatever is left in the Stack is part of the CH(Q).
65 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
66 / 90
Example
Sort points using the smallest to largest polar coordinate
67 / 90
Example
Push the first points into the stack
68 / 90
Example
ccw(p1, p2, p3) > 0, do not get into the loop and push p3 into S
clockwise
69 / 90
Example
ccw(p2, p3, p4) > 0, do not get into the loop and push p4 into S
clockwise
70 / 90
Example
Counterclockwise - Pop p4
counterclockwise
71 / 90
Example
Clockwise - Push p5
clockwise
72 / 90
Example
Counterclockwise - Pop p5 and push p6
counterclockwise
73 / 90
Example
Clockwise push p7
clockwise
74 / 90
Example
Counterclockwise pop p7 and push p8
counterclockwise
75 / 90
Example
Clockwise push p9
clockwise
76 / 90
Example
Clockwise pop p9 and push p10
counterclockwise
77 / 90
Example
Keep going until you finish
78 / 90
We have the following theorem for correctness of the
algorithm
Theorem 33.1 (Correctness of Graham’s scan)
If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then at
termination, the stack S consists of, from bottom to top, exactly the
vertices of CH(Q) in counterclockwise order.
The proof is based in loop invariance
I leave this to you to read!!!
79 / 90
We have the following theorem for correctness of the
algorithm
Theorem 33.1 (Correctness of Graham’s scan)
If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then at
termination, the stack S consists of, from bottom to top, exactly the
vertices of CH(Q) in counterclockwise order.
The proof is based in loop invariance
I leave this to you to read!!!
79 / 90
Using Aggregate Analysis to obtain the complexity
O (n log2 n)
Complexity
1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar
angles and cross product.
2 Lines 3-6 take O (1).
For the Lines 7-10
The for loop executes at most n − 3 times because we have |Q| − 3 points
left
80 / 90
Using Aggregate Analysis to obtain the complexity
O (n log2 n)
Complexity
1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar
angles and cross product.
2 Lines 3-6 take O (1).
For the Lines 7-10
The for loop executes at most n − 3 times because we have |Q| − 3 points
left
80 / 90
Using Aggregate Analysis to obtain the complexity
O (n log2 n)
Complexity
1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar
angles and cross product.
2 Lines 3-6 take O (1).
For the Lines 7-10
The for loop executes at most n − 3 times because we have |Q| − 3 points
left
80 / 90
In addition
Given
PUSH takes O(1) time:
Each iteration takes O (1) time not taking in account the time spent
in the while loop in lines 8-9.
Then
The for loop take overall time O(n) time
Here is the aggregate analysis
Here, we will prove that the overall time for all the times the while loop is
touched by the for loop is going to be O (n).
81 / 90
In addition
Given
PUSH takes O(1) time:
Each iteration takes O (1) time not taking in account the time spent
in the while loop in lines 8-9.
Then
The for loop take overall time O(n) time
Here is the aggregate analysis
Here, we will prove that the overall time for all the times the while loop is
touched by the for loop is going to be O (n).
81 / 90
In addition
Given
PUSH takes O(1) time:
Each iteration takes O (1) time not taking in account the time spent
in the while loop in lines 8-9.
Then
The for loop take overall time O(n) time
Here is the aggregate analysis
Here, we will prove that the overall time for all the times the while loop is
touched by the for loop is going to be O (n).
81 / 90
Aggregate Analysis
We have that
For i = 0, 1, ..., n, we push each point pi into the stack S exactly once.
Remember Multipop?
We can pop at most the number of items that we push on it.
Thus
At least three points p0, p1 and pm are never popped out of the stack!!!
pm is the last point being taken in consideration!!! With m ≤ n
82 / 90
Aggregate Analysis
We have that
For i = 0, 1, ..., n, we push each point pi into the stack S exactly once.
Remember Multipop?
We can pop at most the number of items that we push on it.
Thus
At least three points p0, p1 and pm are never popped out of the stack!!!
pm is the last point being taken in consideration!!! With m ≤ n
82 / 90
Aggregate Analysis
We have that
For i = 0, 1, ..., n, we push each point pi into the stack S exactly once.
Remember Multipop?
We can pop at most the number of items that we push on it.
Thus
At least three points p0, p1 and pm are never popped out of the stack!!!
pm is the last point being taken in consideration!!! With m ≤ n
82 / 90
Aggregate Analysis
Thus
We have m − 2 POP operations are performed in total!!! If we had pushed
m elements into S.
Thus, each iteration of the while loop
It performs one POP, and there are at most m − 2 iterations of the while
loop altogether.
Now
Given that the test in line 8 takes O(1) times, each call of the POP takes
O(1) and m ≤ n − 1.
83 / 90
Aggregate Analysis
Thus
We have m − 2 POP operations are performed in total!!! If we had pushed
m elements into S.
Thus, each iteration of the while loop
It performs one POP, and there are at most m − 2 iterations of the while
loop altogether.
Now
Given that the test in line 8 takes O(1) times, each call of the POP takes
O(1) and m ≤ n − 1.
83 / 90
Aggregate Analysis
Thus
We have m − 2 POP operations are performed in total!!! If we had pushed
m elements into S.
Thus, each iteration of the while loop
It performs one POP, and there are at most m − 2 iterations of the while
loop altogether.
Now
Given that the test in line 8 takes O(1) times, each call of the POP takes
O(1) and m ≤ n − 1.
83 / 90
Aggregate Analysis
We have that
The total time of the while loop is O (n).
Finally
The Running Time of GRAHAM − SCAN is O (n log2 n)
84 / 90
Aggregate Analysis
We have that
The total time of the while loop is O (n).
Finally
The Running Time of GRAHAM − SCAN is O (n log2 n)
84 / 90
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
85 / 90
Jarvis’ March
Jarvis’ March Basics
It computes CH by using
A technique called Package Wrapping.
At each point calculate the minimum polar angle.
Create a left and right chain with the convex hull points.
86 / 90
Jarvis’ March
Jarvis’ March Basics
It computes CH by using
A technique called Package Wrapping.
At each point calculate the minimum polar angle.
Create a left and right chain with the convex hull points.
86 / 90
Jarvis’ March
Jarvis’ March Basics
It computes CH by using
A technique called Package Wrapping.
At each point calculate the minimum polar angle.
Create a left and right chain with the convex hull points.
86 / 90
Jarvis’ March
Jarvis’ March Basics
It computes CH by using
A technique called Package Wrapping.
At each point calculate the minimum polar angle.
Create a left and right chain with the convex hull points.
86 / 90
Formally
Jarvis’s march builds a sequence
H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q)
First
We start with p0 the next vertex p1 in the convex hull has the smallest
polar angle with respect to p0.
Next
p2 has the smallest polar angle with respect to p1.
87 / 90
Formally
Jarvis’s march builds a sequence
H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q)
First
We start with p0 the next vertex p1 in the convex hull has the smallest
polar angle with respect to p0.
Next
p2 has the smallest polar angle with respect to p1.
87 / 90
Formally
Jarvis’s march builds a sequence
H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q)
First
We start with p0 the next vertex p1 in the convex hull has the smallest
polar angle with respect to p0.
Next
p2 has the smallest polar angle with respect to p1.
87 / 90
Now
Then
When we reach the highest vertex, 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, then we choose pk+1 as the point with the smallest polar
angle with respect to pk negative , but from the negative x-axis.
Next
The next point is selected in the same manner until we have reached p0.
88 / 90
Now
Then
When we reach the highest vertex, 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, then we choose pk+1 as the point with the smallest polar
angle with respect to pk negative , but from the negative x-axis.
Next
The next point is selected in the same manner until we have reached p0.
88 / 90
Now
Then
When we reach the highest vertex, 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, then we choose pk+1 as the point with the smallest polar
angle with respect to pk negative , but from the negative x-axis.
Next
The next point is selected in the same manner until we have reached p0.
88 / 90
Jarvis’ March
Example
LEFT CHAIN RIGHT CHAIN
Figure: Wrapping the Gift. Here the Right Chain finishes at p6, then
the Left Chain is started
89 / 90
Complexity
Something Notable
Complexity O (hn)
h number of points in CH.
O (n) for finding the minimum angle and the farthest point by
y-axis
90 / 90
Complexity
Something Notable
Complexity O (hn)
h number of points in CH.
O (n) for finding the minimum angle and the farthest point by
y-axis
90 / 90
Complexity
Something Notable
Complexity O (hn)
h number of points in CH.
O (n) for finding the minimum angle and the farthest point by
y-axis
90 / 90
Complexity
Something Notable
Complexity O (hn)
h number of points in CH.
O (n) for finding the minimum angle and the farthest point by
y-axis
90 / 90

More Related Content

What's hot

Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
Turing machine Introduction
Turing machine IntroductionTuring machine Introduction
Turing machine IntroductionAram Rafeq
 
Circular linked list
Circular linked listCircular linked list
Circular linked listdchuynh
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Power series convergence ,taylor & laurent's theorem
Power series  convergence ,taylor & laurent's theoremPower series  convergence ,taylor & laurent's theorem
Power series convergence ,taylor & laurent's theoremPARIKH HARSHIL
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Sequential multiplication
Sequential multiplicationSequential multiplication
Sequential multiplicationTaqwa It Center
 
Reflection, Scaling, Shear, Translation, and Rotation
Reflection, Scaling, Shear, Translation, and RotationReflection, Scaling, Shear, Translation, and Rotation
Reflection, Scaling, Shear, Translation, and RotationSaumya Tiwari
 
recurrence relations
 recurrence relations recurrence relations
recurrence relationsAnurag Cheela
 

What's hot (20)

Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Finite automata
Finite automataFinite automata
Finite automata
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Red black tree
Red black treeRed black tree
Red black tree
 
Turing machine-TOC
Turing machine-TOCTuring machine-TOC
Turing machine-TOC
 
Turing machine Introduction
Turing machine IntroductionTuring machine Introduction
Turing machine Introduction
 
Graph algorithm
Graph algorithmGraph algorithm
Graph algorithm
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Power series convergence ,taylor & laurent's theorem
Power series  convergence ,taylor & laurent's theoremPower series  convergence ,taylor & laurent's theorem
Power series convergence ,taylor & laurent's theorem
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Sequential multiplication
Sequential multiplicationSequential multiplication
Sequential multiplication
 
Reflection, Scaling, Shear, Translation, and Rotation
Reflection, Scaling, Shear, Translation, and RotationReflection, Scaling, Shear, Translation, and Rotation
Reflection, Scaling, Shear, Translation, and Rotation
 
convex hull
convex hullconvex hull
convex hull
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Quick sort
Quick sortQuick sort
Quick sort
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
 

Viewers also liked

View classification of medical x ray images using pnn classifier, decision tr...
View classification of medical x ray images using pnn classifier, decision tr...View classification of medical x ray images using pnn classifier, decision tr...
View classification of medical x ray images using pnn classifier, decision tr...eSAT Journals
 
Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Ashish Jha
 
Line Segment Intersections
Line Segment IntersectionsLine Segment Intersections
Line Segment IntersectionsBenjamin Sach
 
Convex Hull Algorithm Analysis
Convex Hull Algorithm AnalysisConvex Hull Algorithm Analysis
Convex Hull Algorithm AnalysisRex Yuan
 
How to use SVM for data classification
How to use SVM for data classificationHow to use SVM for data classification
How to use SVM for data classificationYiwei Chen
 
Basic guide to turf cricket pitch preparation
Basic guide to turf cricket pitch preparationBasic guide to turf cricket pitch preparation
Basic guide to turf cricket pitch preparationDebbie-Ann Hall
 
Mri brain image segmentatin and classification by modified fcm &amp;svm akorithm
Mri brain image segmentatin and classification by modified fcm &amp;svm akorithmMri brain image segmentatin and classification by modified fcm &amp;svm akorithm
Mri brain image segmentatin and classification by modified fcm &amp;svm akorithmeSAT Journals
 
Chapter 9 morphological image processing
Chapter 9   morphological image processingChapter 9   morphological image processing
Chapter 9 morphological image processingAhmed Daoud
 
Image Classification And Support Vector Machine
Image Classification And Support Vector MachineImage Classification And Support Vector Machine
Image Classification And Support Vector MachineShao-Chuan Wang
 
Patent Basics and Intellectual Property Rights
Patent Basics and Intellectual Property Rights Patent Basics and Intellectual Property Rights
Patent Basics and Intellectual Property Rights Rahul Dev
 
Support Vector Machines for Classification
Support Vector Machines for ClassificationSupport Vector Machines for Classification
Support Vector Machines for ClassificationPrakash Pimpale
 

Viewers also liked (14)

Lec12
Lec12Lec12
Lec12
 
Svm V SVC
Svm V SVCSvm V SVC
Svm V SVC
 
View classification of medical x ray images using pnn classifier, decision tr...
View classification of medical x ray images using pnn classifier, decision tr...View classification of medical x ray images using pnn classifier, decision tr...
View classification of medical x ray images using pnn classifier, decision tr...
 
Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)
 
Line Segment Intersections
Line Segment IntersectionsLine Segment Intersections
Line Segment Intersections
 
Convex Hull Algorithm Analysis
Convex Hull Algorithm AnalysisConvex Hull Algorithm Analysis
Convex Hull Algorithm Analysis
 
How to use SVM for data classification
How to use SVM for data classificationHow to use SVM for data classification
How to use SVM for data classification
 
Basic guide to turf cricket pitch preparation
Basic guide to turf cricket pitch preparationBasic guide to turf cricket pitch preparation
Basic guide to turf cricket pitch preparation
 
Mri brain image segmentatin and classification by modified fcm &amp;svm akorithm
Mri brain image segmentatin and classification by modified fcm &amp;svm akorithmMri brain image segmentatin and classification by modified fcm &amp;svm akorithm
Mri brain image segmentatin and classification by modified fcm &amp;svm akorithm
 
Svm my
Svm mySvm my
Svm my
 
Chapter 9 morphological image processing
Chapter 9   morphological image processingChapter 9   morphological image processing
Chapter 9 morphological image processing
 
Image Classification And Support Vector Machine
Image Classification And Support Vector MachineImage Classification And Support Vector Machine
Image Classification And Support Vector Machine
 
Patent Basics and Intellectual Property Rights
Patent Basics and Intellectual Property Rights Patent Basics and Intellectual Property Rights
Patent Basics and Intellectual Property Rights
 
Support Vector Machines for Classification
Support Vector Machines for ClassificationSupport Vector Machines for Classification
Support Vector Machines for Classification
 

Similar to 26 Computational Geometry

Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmShilpa Hait
 
planes and distances
planes and distancesplanes and distances
planes and distancesElias Dinsa
 
2 d transformations by amit kumar (maimt)
2 d transformations by amit kumar (maimt)2 d transformations by amit kumar (maimt)
2 d transformations by amit kumar (maimt)Amit Kapoor
 
Finite elements : basis functions
Finite elements : basis functionsFinite elements : basis functions
Finite elements : basis functionsTarun Gehlot
 
Hermite cubic spline curve
Hermite cubic spline curveHermite cubic spline curve
Hermite cubic spline curveDeepak Antil
 
Trigonometric Function of General Angles Lecture
Trigonometric Function of General Angles LectureTrigonometric Function of General Angles Lecture
Trigonometric Function of General Angles LectureFroyd Wess
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithmPooja Dixit
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in indiaEdhole.com
 
Recurrence_Theory.pptx studdents nees free
Recurrence_Theory.pptx studdents nees freeRecurrence_Theory.pptx studdents nees free
Recurrence_Theory.pptx studdents nees freeWhite44420
 
Orthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processOrthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processgidc engineering college
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
straight lines
straight lines straight lines
straight lines david
 
Class 14 3D HermiteInterpolation.pptx
Class 14 3D HermiteInterpolation.pptxClass 14 3D HermiteInterpolation.pptx
Class 14 3D HermiteInterpolation.pptxMdSiddique20
 
CLOSEST PAIR (Final)
CLOSEST PAIR (Final)CLOSEST PAIR (Final)
CLOSEST PAIR (Final)Aruneel Das
 
Gmat quant topic 6 co ordinate geometry solutions
Gmat quant topic 6 co ordinate geometry solutionsGmat quant topic 6 co ordinate geometry solutions
Gmat quant topic 6 co ordinate geometry solutionsRushabh Vora
 

Similar to 26 Computational Geometry (20)

26 Computational Geometry
26 Computational Geometry26 Computational Geometry
26 Computational Geometry
 
Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithm
 
planes and distances
planes and distancesplanes and distances
planes and distances
 
1513 circles
1513 circles1513 circles
1513 circles
 
2 d transformations by amit kumar (maimt)
2 d transformations by amit kumar (maimt)2 d transformations by amit kumar (maimt)
2 d transformations by amit kumar (maimt)
 
Finite elements : basis functions
Finite elements : basis functionsFinite elements : basis functions
Finite elements : basis functions
 
Hermite cubic spline curve
Hermite cubic spline curveHermite cubic spline curve
Hermite cubic spline curve
 
Trigonometric Function of General Angles Lecture
Trigonometric Function of General Angles LectureTrigonometric Function of General Angles Lecture
Trigonometric Function of General Angles Lecture
 
Convex hull
Convex hullConvex hull
Convex hull
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
 
2. la recta
2. la recta2. la recta
2. la recta
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
 
Recurrence_Theory.pptx studdents nees free
Recurrence_Theory.pptx studdents nees freeRecurrence_Theory.pptx studdents nees free
Recurrence_Theory.pptx studdents nees free
 
Orthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processOrthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth process
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
straight lines
straight lines straight lines
straight lines
 
Class 14 3D HermiteInterpolation.pptx
Class 14 3D HermiteInterpolation.pptxClass 14 3D HermiteInterpolation.pptx
Class 14 3D HermiteInterpolation.pptx
 
CLOSEST PAIR (Final)
CLOSEST PAIR (Final)CLOSEST PAIR (Final)
CLOSEST PAIR (Final)
 
Gmat quant topic 6 co ordinate geometry solutions
Gmat quant topic 6 co ordinate geometry solutionsGmat quant topic 6 co ordinate geometry solutions
Gmat quant topic 6 co ordinate geometry solutions
 
Parabola
ParabolaParabola
Parabola
 

More from Andres Mendez-Vazquez

01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectorsAndres Mendez-Vazquez
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issuesAndres Mendez-Vazquez
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiationAndres Mendez-Vazquez
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep LearningAndres Mendez-Vazquez
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learningAndres Mendez-Vazquez
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusAndres Mendez-Vazquez
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusAndres Mendez-Vazquez
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesAndres Mendez-Vazquez
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variationsAndres Mendez-Vazquez
 

More from Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Recently uploaded

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 

Recently uploaded (20)

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 

26 Computational Geometry

  • 1. Analysis of Algorithms Computational Geometry Andres Mendez-Vazquez November 30, 2015 1 / 90
  • 2. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 2 / 90
  • 3. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 3 / 90
  • 4. Computational Geometry Motivation We want to solve geometric problems!!! 4 / 90
  • 5. Field of Application VLSI design - Generation for Fast Voronoi Diagrams for Massive Layouts Under Strict Distances to avoid Tunneling Effects!! 5 / 90
  • 6. Field of Application Databases - Octrees for fast localization of information in database tables 6 / 90
  • 7. Field of Application Synthetic Biology - Geometric Algorithms to Obtain new DNA configurations for Molecular Machines 7 / 90
  • 8. Field of Application Computer Graphics for more engaging Virtual Environments - For example: Bump Mapping!!! 8 / 90
  • 9. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 9 / 90
  • 10. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 90
  • 11. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 90
  • 12. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 90
  • 13. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 90
  • 14. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 90
  • 16. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 12 / 90
  • 17. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 90
  • 18. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 90
  • 19. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 90
  • 20. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 90
  • 21. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 90
  • 22. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 14 / 90
  • 23. Cross Product Question!!! Given two directed segments −−→p0p1 and −−→p0p2, Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0? Cross Product Cross product p1 × p2 as the signed area of the parallelogram formed by 15 / 90
  • 24. Cross Product Question!!! Given two directed segments −−→p0p1 and −−→p0p2, Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0? Cross Product Cross product p1 × p2 as the signed area of the parallelogram formed by 15 / 90
  • 25. Cross Product Question!!! Given two directed segments −−→p0p1 and −−→p0p2, Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0? Cross Product Cross product p1 × p2 as the signed area of the parallelogram formed by 15 / 90
  • 26. Cross Product A shorter representation p1 × p2 = det p1 p2 =det x1 x2 y1 y2 = x1y2 − x2y1 = −p2 × p1 Thus if p1 × p2 is positive, then p1 is clockwise from p2. if p1 × p2 is negative, then p1 is counterclockwise from p2. 16 / 90
  • 27. Cross Product A shorter representation p1 × p2 = det p1 p2 =det x1 x2 y1 y2 = x1y2 − x2y1 = −p2 × p1 Thus if p1 × p2 is positive, then p1 is clockwise from p2. if p1 × p2 is negative, then p1 is counterclockwise from p2. 16 / 90
  • 28. Cross Product A shorter representation p1 × p2 = det p1 p2 =det x1 x2 y1 y2 = x1y2 − x2y1 = −p2 × p1 Thus if p1 × p2 is positive, then p1 is clockwise from p2. if p1 × p2 is negative, then p1 is counterclockwise from p2. 16 / 90
  • 29. Regions Clockwise and Counterclockwise Regions Figure: Darker counterclockwise; lighter clockwise with respect to p 17 / 90
  • 30. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 18 / 90
  • 31. Turn Left or Right Question Given two line segments −−→p0p1 and −−→p1p2, if we traverse −−→p0p1 and then −−→p1p2, do we make a left turn at point p1? 19 / 90
  • 32. Turn Left or Right Simply use the following idea Compute cross product (p2 − p0) × (p1 − p0)!!! This translates p0 to the origin!!! What about (p2 − p0) × (p1 − p0) = 0? Left Turn = counterclockwise; Right Turn = clockwise 20 / 90
  • 33. Turn Left or Right Simply use the following idea Compute cross product (p2 − p0) × (p1 − p0)!!! This translates p0 to the origin!!! What about (p2 − p0) × (p1 − p0) = 0? Left Turn = counterclockwise; Right Turn = clockwise 20 / 90
  • 34. Turn Left or Right Simply use the following idea Compute cross product (p2 − p0) × (p1 − p0)!!! This translates p0 to the origin!!! What about (p2 − p0) × (p1 − p0) = 0? Left Turn = counterclockwise; Right Turn = clockwise 20 / 90
  • 35. Turn Left or Right Simply use the following idea Compute cross product (p2 − p0) × (p1 − p0)!!! This translates p0 to the origin!!! What about (p2 − p0) × (p1 − p0) = 0? Left Turn = counterclockwise; Right Turn = clockwise counterclockwise clockwise 20 / 90
  • 36. Code for this We have the following code Direction(pi, pj, pk) 1 return (pk − pi) × (pj − pi) 21 / 90
  • 37. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 22 / 90
  • 38. Intersection Question Do line segments −−→p1p2 and −−→p3p4 intersect? Very Simple!!! We have two possibilities 1 Each segment straddles the line containing the other. 2 An endpoint of one segment lies on the other segment. 23 / 90
  • 39. Intersection Question Do line segments −−→p1p2 and −−→p3p4 intersect? Very Simple!!! We have two possibilities 1 Each segment straddles the line containing the other. 2 An endpoint of one segment lies on the other segment. 23 / 90
  • 40. Intersection Question Do line segments −−→p1p2 and −−→p3p4 intersect? Very Simple!!! We have two possibilities 1 Each segment straddles the line containing the other. 2 An endpoint of one segment lies on the other segment. 23 / 90
  • 41. Case I This summarize the previous two possibilities The segments straddle each other’s lines. Oposite Signs Figure: Using Cross Products to find intersections 24 / 90
  • 42. Case II No intersection The segment straddles the line, but the other does not straddle the other line Same Signs Figure: Using Cross Products to find that there is no intersection 25 / 90
  • 43. Code Code Segment-Intersection(p1, p2, p3, p4) 1 d1 = Direction (p3, p4, p1) 2 d2 = Direction (p3, p4, p2) 3 d3 = Direction (p1, p2, p3) 4 d4 = Direction (p1, p2, p4) 5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0) and 6 (d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)) 7 return TRUE Figure: The Incomplete Code, You still need to test for endpoints over the segment 26 / 90
  • 44. Code Code Segment-Intersection(p1, p2, p3, p4) 1 d1 = Direction (p3, p4, p1) 2 d2 = Direction (p3, p4, p2) 3 d3 = Direction (p1, p2, p3) 4 d4 = Direction (p1, p2, p4) 5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0) and 6 (d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)) 7 return TRUE Figure: The Incomplete Code, You still need to test for endpoints over the segment 26 / 90
  • 45. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 27 / 90
  • 46. Sweeping Sweeping Use an imaginary vertical line to pass through the n segments with events x ∈ {r, t, u}: Figure: Vertical Line to Record Events 28 / 90
  • 47. Thus This can be used to record events given two segments s1 and s2 Event I: s1 above s2 at x, written s1 x s2. This is a total preorder relation for segment intersecting the line at x. The relation is transitive and reflexive. Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or both (if s1 and s2 intersect at x) 29 / 90
  • 48. Thus This can be used to record events given two segments s1 and s2 Event I: s1 above s2 at x, written s1 x s2. This is a total preorder relation for segment intersecting the line at x. The relation is transitive and reflexive. Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or both (if s1 and s2 intersect at x) 29 / 90
  • 49. Thus This can be used to record events given two segments s1 and s2 Event I: s1 above s2 at x, written s1 x s2. This is a total preorder relation for segment intersecting the line at x. The relation is transitive and reflexive. Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or both (if s1 and s2 intersect at x) 29 / 90
  • 50. Thus This can be used to record events given two segments s1 and s2 Event I: s1 above s2 at x, written s1 x s2. This is a total preorder relation for segment intersecting the line at x. The relation is transitive and reflexive. Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or both (if s1 and s2 intersect at x) 29 / 90
  • 51. Example Example: a r c a t c Figure: Vertical Line to Record Events 30 / 90
  • 52. Change in direction When e and f intersect, e v f and f w e. In the Shaded Region, any sweep line will have e and f as consecutive V z w Figure: Vertical Line to Record Events 31 / 90
  • 53. Moving the sweep line Something Notable Sweeping algorithms typically manage two sets of data. Sweep-line status The sweep-line status gives the relationships among the objects that the sweep line intersects. Event-point schedule The event-point schedule is a sequence of points, called event points, which we order from left to right according to their x-coordinates. As the sweep progress from left to right, it stops and processes each event point, then resumes. It is possible to use a min-priority queue to keep those event points sorted by x-coordinate. 32 / 90
  • 54. Moving the sweep line Something Notable Sweeping algorithms typically manage two sets of data. Sweep-line status The sweep-line status gives the relationships among the objects that the sweep line intersects. Event-point schedule The event-point schedule is a sequence of points, called event points, which we order from left to right according to their x-coordinates. As the sweep progress from left to right, it stops and processes each event point, then resumes. It is possible to use a min-priority queue to keep those event points sorted by x-coordinate. 32 / 90
  • 55. Moving the sweep line Something Notable Sweeping algorithms typically manage two sets of data. Sweep-line status The sweep-line status gives the relationships among the objects that the sweep line intersects. Event-point schedule The event-point schedule is a sequence of points, called event points, which we order from left to right according to their x-coordinates. As the sweep progress from left to right, it stops and processes each event point, then resumes. It is possible to use a min-priority queue to keep those event points sorted by x-coordinate. 32 / 90
  • 56. Moving the sweep line Something Notable Sweeping algorithms typically manage two sets of data. Sweep-line status The sweep-line status gives the relationships among the objects that the sweep line intersects. Event-point schedule The event-point schedule is a sequence of points, called event points, which we order from left to right according to their x-coordinates. As the sweep progress from left to right, it stops and processes each event point, then resumes. It is possible to use a min-priority queue to keep those event points sorted by x-coordinate. 32 / 90
  • 57. Sweeping Process First We sort the segment endpoints by increasing x-coordinate and proceed from left to right. However, sometimes they have the same x-coordinate (Covertical) If two or more endpoints are covertical, we break the tie by putting all the covertical left endpoints before the covertical right endpoints. 33 / 90
  • 58. Sweeping Process First We sort the segment endpoints by increasing x-coordinate and proceed from left to right. However, sometimes they have the same x-coordinate (Covertical) If two or more endpoints are covertical, we break the tie by putting all the covertical left endpoints before the covertical right endpoints. Covertical Left End Points Covertical Right End Points 33 / 90
  • 59. Then Second Within a set of covertical left endpoints, we put those with lower y-coordinates first, and we do the same within a set of covertical right endpoints. Covertical Left End Points Covertical Right End Points FIRST 34 / 90
  • 60. Then Process 1 When we encounter a segment’s left endpoint, we insert the segment into the sweep-line status. 2 We delete the segment from the sweep-line status upon encountering its right endpoint. Thus Whenever two segments first become consecutive in the total preorder, we check whether they intersect. 35 / 90
  • 61. Then Process 1 When we encounter a segment’s left endpoint, we insert the segment into the sweep-line status. 2 We delete the segment from the sweep-line status upon encountering its right endpoint. Thus Whenever two segments first become consecutive in the total preorder, we check whether they intersect. 35 / 90
  • 62. Then Process 1 When we encounter a segment’s left endpoint, we insert the segment into the sweep-line status. 2 We delete the segment from the sweep-line status upon encountering its right endpoint. Thus Whenever two segments first become consecutive in the total preorder, we check whether they intersect. 35 / 90
  • 63. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 90
  • 64. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 90
  • 65. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 90
  • 66. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 90
  • 67. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 90
  • 68. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 90
  • 69. What the algorithm does? Moving the sweeping line discretely - Event-point schedule Here, moving through the x-axis discrete events Using the RBT to Store the Info about the segment order 37 / 90
  • 70. Event-Point Schedule Implementation For this We can use a Priority Queue using lexicographic order The interesting part is the Sweeping-Line Satus Because the way we build the balanced tree 38 / 90
  • 71. Event-Point Schedule Implementation For this We can use a Priority Queue using lexicographic order The interesting part is the Sweeping-Line Satus Because the way we build the balanced tree 38 / 90
  • 72. Sweep-Line Status The Above and Below relation TIME Sweeping Line 39 / 90
  • 73. Sweeping Line Status Implementation Use the following relation of order to build the binary tree Given a segment x, then you insert y Case I if y is counterclockwise, it is below x (Go to the left). Case II if y is clockwise, it is above x (Go to the Right) In addtion If you are at a leaf do the insertion, but also insert the leaf at the left or right given the insertion. 40 / 90
  • 74. Sweeping Line Status Implementation Use the following relation of order to build the binary tree Given a segment x, then you insert y Case I if y is counterclockwise, it is below x (Go to the left). Case II if y is clockwise, it is above x (Go to the Right) In addtion If you are at a leaf do the insertion, but also insert the leaf at the left or right given the insertion. 40 / 90
  • 75. Example We insert th first element in the circular leaves list 41 / 90
  • 76. Example We insert a inner node after binary search Below Above 42 / 90
  • 79. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 80. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 81. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 82. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 83. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 84. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 85. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 86. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 87. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 88. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 89. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 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−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 90
  • 90. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 46 / 90
  • 91. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T (Sweeping Line Data Structure) is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 90
  • 92. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T (Sweeping Line Data Structure) is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 90
  • 93. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T (Sweeping Line Data Structure) is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 90
  • 94. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T (Sweeping Line Data Structure) is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 90
  • 95. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T (Sweeping Line Data Structure) is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 90
  • 96. Now, we have two possbibilities Case I a b p z SWEEPING LINE 48 / 90
  • 97. Case I Moreover z is to the left of p or goes through p. In addition There is a endpoint q where a and b become consecutive. 49 / 90
  • 98. Case I Moreover z is to the left of p or goes through p. In addition There is a endpoint q where a and b become consecutive. a b p z q 49 / 90
  • 99. Finally Then a and b They become consecutive in the total pre-order of a sweep line. 50 / 90
  • 100. Case II We have that q is a left endpoint where a and b stop being consecutive a b p z q 51 / 90
  • 101. Correctness about the order given by T Then, given the two following cases 1 if p is in the sweep line ⇒ p == q. 2 If q is at the left of p, and it is the nearest left one. 52 / 90
  • 102. Correctness about the order given by T Then, given the two following cases 1 if p is in the sweep line ⇒ p == q. 2 If q is at the left of p, and it is the nearest left one. 52 / 90
  • 103. Do we mantain the correct preorder? We have that given that p is first Then, it is processed first becacuse the lexicographic order. Therefore, two cases can happen 1 The point is processed - then the algorithm returns true 2 If the event is not processed - then the algorithm must have returned true 53 / 90
  • 104. Do we mantain the correct preorder? We have that given that p is first Then, it is processed first becacuse the lexicographic order. Therefore, two cases can happen 1 The point is processed - then the algorithm returns true 2 If the event is not processed - then the algorithm must have returned true 53 / 90
  • 105. Handling Case I Segments a and b are already in T , and a segment between them in the total pre-order is deleted, making a and b to become consecutive a b p z qI a I b a b 54 / 90
  • 106. When is this detected? In the following lines of the code Lines 8–11 detect this case. 55 / 90
  • 107. Handling Case II Either a or b is inserted into T, and the other segment is above or below it in the total pre-order. a b p z q 56 / 90
  • 108. When is this detected? In the following lines of the code Lines 4–7 detect this case. 57 / 90
  • 109. Finally If event point q is not processed It must have found an earlier intersection!!! Therefore If there is an intersection Any-Segment-Intersect returns true all the time 58 / 90
  • 110. Finally If event point q is not processed It must have found an earlier intersection!!! Therefore If there is an intersection Any-Segment-Intersect returns true all the time 58 / 90
  • 111. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 59 / 90
  • 112. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 59 / 90
  • 113. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 59 / 90
  • 114. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 59 / 90
  • 115. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 59 / 90
  • 116. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 59 / 90
  • 117. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 60 / 90
  • 118. Convex Hull Convex Hull Given a set of points, Q, find the smallest convex polygon P such that Q ⊂ P. This is denoted by CH(Q). Bunch of Points in 2D 61 / 90
  • 119. Convex Hull Convex Hull Given a set of points, Q, find the smallest convex polygon P such that Q ⊂ P. This is denoted by CH(Q). Their Convex Hull 62 / 90
  • 120. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 63 / 90
  • 121. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 63 / 90
  • 122. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 63 / 90
  • 123. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 63 / 90
  • 124. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 63 / 90
  • 125. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 64 / 90
  • 126. Graham’s Scan Graham’s Scan Basics It keeps a Stack of candidate points. It Pops elements that are not part of the CH(Q). Whatever is left in the Stack is part of the CH(Q). 65 / 90
  • 127. Graham’s Scan Graham’s Scan Basics It keeps a Stack of candidate points. It Pops elements that are not part of the CH(Q). Whatever is left in the Stack is part of the CH(Q). 65 / 90
  • 128. Graham’s Scan Graham’s Scan Basics It keeps a Stack of candidate points. It Pops elements that are not part of the CH(Q). Whatever is left in the Stack is part of the CH(Q). 65 / 90
  • 129. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 130. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 131. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 132. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 133. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 134. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 135. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 136. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 137. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 66 / 90
  • 138. Example Sort points using the smallest to largest polar coordinate 67 / 90
  • 139. Example Push the first points into the stack 68 / 90
  • 140. Example ccw(p1, p2, p3) > 0, do not get into the loop and push p3 into S clockwise 69 / 90
  • 141. Example ccw(p2, p3, p4) > 0, do not get into the loop and push p4 into S clockwise 70 / 90
  • 142. Example Counterclockwise - Pop p4 counterclockwise 71 / 90
  • 143. Example Clockwise - Push p5 clockwise 72 / 90
  • 144. Example Counterclockwise - Pop p5 and push p6 counterclockwise 73 / 90
  • 146. Example Counterclockwise pop p7 and push p8 counterclockwise 75 / 90
  • 148. Example Clockwise pop p9 and push p10 counterclockwise 77 / 90
  • 149. Example Keep going until you finish 78 / 90
  • 150. We have the following theorem for correctness of the algorithm Theorem 33.1 (Correctness of Graham’s scan) If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then at termination, the stack S consists of, from bottom to top, exactly the vertices of CH(Q) in counterclockwise order. The proof is based in loop invariance I leave this to you to read!!! 79 / 90
  • 151. We have the following theorem for correctness of the algorithm Theorem 33.1 (Correctness of Graham’s scan) If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then at termination, the stack S consists of, from bottom to top, exactly the vertices of CH(Q) in counterclockwise order. The proof is based in loop invariance I leave this to you to read!!! 79 / 90
  • 152. Using Aggregate Analysis to obtain the complexity O (n log2 n) Complexity 1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar angles and cross product. 2 Lines 3-6 take O (1). For the Lines 7-10 The for loop executes at most n − 3 times because we have |Q| − 3 points left 80 / 90
  • 153. Using Aggregate Analysis to obtain the complexity O (n log2 n) Complexity 1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar angles and cross product. 2 Lines 3-6 take O (1). For the Lines 7-10 The for loop executes at most n − 3 times because we have |Q| − 3 points left 80 / 90
  • 154. Using Aggregate Analysis to obtain the complexity O (n log2 n) Complexity 1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar angles and cross product. 2 Lines 3-6 take O (1). For the Lines 7-10 The for loop executes at most n − 3 times because we have |Q| − 3 points left 80 / 90
  • 155. In addition Given PUSH takes O(1) time: Each iteration takes O (1) time not taking in account the time spent in the while loop in lines 8-9. Then The for loop take overall time O(n) time Here is the aggregate analysis Here, we will prove that the overall time for all the times the while loop is touched by the for loop is going to be O (n). 81 / 90
  • 156. In addition Given PUSH takes O(1) time: Each iteration takes O (1) time not taking in account the time spent in the while loop in lines 8-9. Then The for loop take overall time O(n) time Here is the aggregate analysis Here, we will prove that the overall time for all the times the while loop is touched by the for loop is going to be O (n). 81 / 90
  • 157. In addition Given PUSH takes O(1) time: Each iteration takes O (1) time not taking in account the time spent in the while loop in lines 8-9. Then The for loop take overall time O(n) time Here is the aggregate analysis Here, we will prove that the overall time for all the times the while loop is touched by the for loop is going to be O (n). 81 / 90
  • 158. Aggregate Analysis We have that For i = 0, 1, ..., n, we push each point pi into the stack S exactly once. Remember Multipop? We can pop at most the number of items that we push on it. Thus At least three points p0, p1 and pm are never popped out of the stack!!! pm is the last point being taken in consideration!!! With m ≤ n 82 / 90
  • 159. Aggregate Analysis We have that For i = 0, 1, ..., n, we push each point pi into the stack S exactly once. Remember Multipop? We can pop at most the number of items that we push on it. Thus At least three points p0, p1 and pm are never popped out of the stack!!! pm is the last point being taken in consideration!!! With m ≤ n 82 / 90
  • 160. Aggregate Analysis We have that For i = 0, 1, ..., n, we push each point pi into the stack S exactly once. Remember Multipop? We can pop at most the number of items that we push on it. Thus At least three points p0, p1 and pm are never popped out of the stack!!! pm is the last point being taken in consideration!!! With m ≤ n 82 / 90
  • 161. Aggregate Analysis Thus We have m − 2 POP operations are performed in total!!! If we had pushed m elements into S. Thus, each iteration of the while loop It performs one POP, and there are at most m − 2 iterations of the while loop altogether. Now Given that the test in line 8 takes O(1) times, each call of the POP takes O(1) and m ≤ n − 1. 83 / 90
  • 162. Aggregate Analysis Thus We have m − 2 POP operations are performed in total!!! If we had pushed m elements into S. Thus, each iteration of the while loop It performs one POP, and there are at most m − 2 iterations of the while loop altogether. Now Given that the test in line 8 takes O(1) times, each call of the POP takes O(1) and m ≤ n − 1. 83 / 90
  • 163. Aggregate Analysis Thus We have m − 2 POP operations are performed in total!!! If we had pushed m elements into S. Thus, each iteration of the while loop It performs one POP, and there are at most m − 2 iterations of the while loop altogether. Now Given that the test in line 8 takes O(1) times, each call of the POP takes O(1) and m ≤ n − 1. 83 / 90
  • 164. Aggregate Analysis We have that The total time of the while loop is O (n). Finally The Running Time of GRAHAM − SCAN is O (n log2 n) 84 / 90
  • 165. Aggregate Analysis We have that The total time of the while loop is O (n). Finally The Running Time of GRAHAM − SCAN is O (n log2 n) 84 / 90
  • 166. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 85 / 90
  • 167. Jarvis’ March Jarvis’ March Basics It computes CH by using A technique called Package Wrapping. At each point calculate the minimum polar angle. Create a left and right chain with the convex hull points. 86 / 90
  • 168. Jarvis’ March Jarvis’ March Basics It computes CH by using A technique called Package Wrapping. At each point calculate the minimum polar angle. Create a left and right chain with the convex hull points. 86 / 90
  • 169. Jarvis’ March Jarvis’ March Basics It computes CH by using A technique called Package Wrapping. At each point calculate the minimum polar angle. Create a left and right chain with the convex hull points. 86 / 90
  • 170. Jarvis’ March Jarvis’ March Basics It computes CH by using A technique called Package Wrapping. At each point calculate the minimum polar angle. Create a left and right chain with the convex hull points. 86 / 90
  • 171. Formally Jarvis’s march builds a sequence H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q) First We start with p0 the next vertex p1 in the convex hull has the smallest polar angle with respect to p0. Next p2 has the smallest polar angle with respect to p1. 87 / 90
  • 172. Formally Jarvis’s march builds a sequence H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q) First We start with p0 the next vertex p1 in the convex hull has the smallest polar angle with respect to p0. Next p2 has the smallest polar angle with respect to p1. 87 / 90
  • 173. Formally Jarvis’s march builds a sequence H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q) First We start with p0 the next vertex p1 in the convex hull has the smallest polar angle with respect to p0. Next p2 has the smallest polar angle with respect to p1. 87 / 90
  • 174. Now Then When we reach the highest vertex, 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, then we choose pk+1 as the point with the smallest polar angle with respect to pk negative , but from the negative x-axis. Next The next point is selected in the same manner until we have reached p0. 88 / 90
  • 175. Now Then When we reach the highest vertex, 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, then we choose pk+1 as the point with the smallest polar angle with respect to pk negative , but from the negative x-axis. Next The next point is selected in the same manner until we have reached p0. 88 / 90
  • 176. Now Then When we reach the highest vertex, 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, then we choose pk+1 as the point with the smallest polar angle with respect to pk negative , but from the negative x-axis. Next The next point is selected in the same manner until we have reached p0. 88 / 90
  • 177. Jarvis’ March Example LEFT CHAIN RIGHT CHAIN Figure: Wrapping the Gift. Here the Right Chain finishes at p6, then the Left Chain is started 89 / 90
  • 178. Complexity Something Notable Complexity O (hn) h number of points in CH. O (n) for finding the minimum angle and the farthest point by y-axis 90 / 90
  • 179. Complexity Something Notable Complexity O (hn) h number of points in CH. O (n) for finding the minimum angle and the farthest point by y-axis 90 / 90
  • 180. Complexity Something Notable Complexity O (hn) h number of points in CH. O (n) for finding the minimum angle and the farthest point by y-axis 90 / 90
  • 181. Complexity Something Notable Complexity O (hn) h number of points in CH. O (n) for finding the minimum angle and the farthest point by y-axis 90 / 90