• Keine Ergebnisse gefunden

Graham’s scan (1972)

N/A
N/A
Protected

Academic year: 2021

Aktie "Graham’s scan (1972)"

Copied!
87
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Convex hulls in R2

andor Kisfaludi-Bak

Computaional Geometry Summer semester 2020

(2)

Overview

(3)

Overview

Problem definition

(4)

Overview

Problem definition

Computaitonal models, input and output

(5)

Overview

Problem definition

Naive algorithm

Computaitonal models, input and output

(6)

Overview

Problem definition

Naive algorithm

Graham’s scan

Computaitonal models, input and output

(7)

Overview

Problem definition

Naive algorithm

Graham’s scan

Computaitonal models, input and output

Chan’s algorithm

(8)

Convex hull

Notations, definitions

Rd is d-dimensional Euclidean space P = {p1, . . . , pn} set of n points

X Rd is convex if for any p, q X we have pq X

(9)

Convex hull

Notations, definitions

Rd is d-dimensional Euclidean space P = {p1, . . . , pn} set of n points

X Rd is convex if for any p, q X we have pq X

conv(P) =

minimum convex set containing P

intersection of convex sets containing P 1p1 + · · · + αnpn | αi 0 and Pn

i=1 αi = 1}

Convex hull:

(10)

Convex hull

Notations, definitions

Rd is d-dimensional Euclidean space P = {p1, . . . , pn} set of n points

X Rd is convex if for any p, q X we have pq X

conv(P) =

minimum convex set containing P

intersection of convex sets containing P 1p1 + · · · + αnpn | αi 0 and Pn

i=1 αi = 1}

P Convex hull:

(11)

Convex hull

Notations, definitions

Rd is d-dimensional Euclidean space P = {p1, . . . , pn} set of n points

X Rd is convex if for any p, q X we have pq X

conv(P) =

minimum convex set containing P

intersection of convex sets containing P 1p1 + · · · + αnpn | αi 0 and Pn

i=1 αi = 1}

P

conv(P) Convex hull:

(12)

Real RAM vs. Word RAM

Real RAM Word RAM

(13)

Real RAM vs. Word RAM

arbitrary real numbers words of size Θ(log n)

Real RAM Word RAM

(14)

Real RAM vs. Word RAM

arbitrary real numbers words of size Θ(log n)

no rounding/floor, no modulo realisticoperations (shifts, etc)

Real RAM Word RAM

(15)

Real RAM vs. Word RAM

arbitrary real numbers words of size Θ(log n)

no rounding/floor, no modulo realisticoperations (shifts, etc) Real inputs and outputs,

can extend with

., ln(.)

Exact arithmetic for

rational inputs with + − ∗/

Real RAM Word RAM

(16)

Real RAM vs. Word RAM

arbitrary real numbers words of size Θ(log n)

no rounding/floor, no modulo realisticoperations (shifts, etc) Real inputs and outputs,

can extend with

., ln(.)

Exact arithmetic for

rational inputs with + − ∗/

Unrealistic power Too restrictive?

Real RAM Word RAM

(17)

Convex hull: input and output

Input: Points with coordianate pairs (x, y) R2 (e, π), (3, 3), (2.95, 2.9), (

11, 3.05), (π, e)

p1

p2

p3

p5

p4

(18)

Convex hull: input and output

Input: Points with coordianate pairs (x, y) R2 (e, π), (3, 3), (2.95, 2.9), (

11, 3.05), (π, e)

p1

p2

p3

p5

p4 p1, p4, p5, p3

Output: ”corners” in clockwise order

smallest Q P s.t. conv(Q) = conv(P)

(19)

Convex hull: input and output

Input: Points with coordianate pairs (x, y) R2 (e, π), (3, 3), (2.95, 2.9), (

11, 3.05), (π, e)

p1

p2

p3

p5

p4 p1, p4, p5, p3

non-corner point on

bondary: not in output!

p6 Output: ”corners” in clockwise order

smallest Q P s.t. conv(Q) = conv(P)

(20)

Convex hull: input and output

Input: Points with coordianate pairs (x, y) R2 (e, π), (3, 3), (2.95, 2.9), (

11, 3.05), (π, e)

p1

p2

p3

p5

p4 p1, p4, p5, p3

non-corner point on

bondary: not in output!

p6 Output: ”corners” in clockwise order

smallest Q P s.t. conv(Q) = conv(P)

Everything works with rational inputs on Word RAM!

(21)

Naive algorithm

(22)

Naive Algorithm

Suppose no 3 points on one line. (no collinear triples)

(23)

Naive Algorithm

Suppose no 3 points on one line. (no collinear triples)

p

p0

q

In O(1) time, decide if q is on left or right side of line pp0

sgn

px py 1 p0x p0y 1 qx qy 1

(24)

Naive Algorithm

Suppose no 3 points on one line. (no collinear triples)

p

p0

q

In O(1) time, decide if q is on left or right side of line pp0

sgn

px py 1 p0x p0y 1 qx qy 1

Naive Convex Hull in R2 For each p, p0 P ,

check if all q P \ {p, p0} is on the left of line pp0. If yes, then p0 follows p in conv(P ).

Assemble and output the hull

(25)

Naive Algorithm

Suppose no 3 points on one line. (no collinear triples)

p

p0

q

In O(1) time, decide if q is on left or right side of line pp0

Running time: n2

· (n 2) · O(1) = O(n3) sgn

px py 1 p0x p0y 1 qx qy 1

Naive Convex Hull in R2 For each p, p0 P ,

check if all q P \ {p, p0} is on the left of line pp0. If yes, then p0 follows p in conv(P ).

Assemble and output the hull

(26)

Graham’s scan (1972)

(27)

Graham’s Scan idea

Suppose points have distinct x-coordinates.

Let p1, . . . , pn: points sorted with increasing x-coordinates.

(28)

Graham’s Scan idea

Suppose points have distinct x-coordinates.

Let p1, . . . , pn: points sorted with increasing x-coordinates.

p1, pn are on convex hull

p1 pn

(29)

Graham’s Scan idea

Suppose points have distinct x-coordinates.

Let p1, . . . , pn: points sorted with increasing x-coordinates.

p1, pn are on convex hull

p1 pn

Upper hull

part of the hull after p1 and before pn in clockwise order

Lower hull

(30)

Graham’s Scan idea

Suppose points have distinct x-coordinates.

Let p1, . . . , pn: points sorted with increasing x-coordinates.

p1, pn are on convex hull

p1 pn

Upper hull

part of the hull after p1 and before pn in clockwise order

Lower hull Idea:

Add points left to right, update upper hull after each addition

(31)

Graham’s Scan: update

p1

Right turn

pi

Left turn

p1

pi

(pi is below last hull segment) (pi is above last hull segment)

(32)

Graham’s Scan: update

p1

Right turn

Add pi to the upper hull pi

Left turn

p1

pi

(pi is below last hull segment) (pi is above last hull segment)

(33)

Graham’s Scan: update

p1

Right turn

Add pi to the upper hull pi

Left turn

p1

pi

(pi is below last hull segment) (pi is above last hull segment)

(34)

Graham’s Scan: update

p1

Right turn

Add pi to the upper hull pi

Add pi but remove previous hull point until left turn disappears

Left turn

p1

pi

(pi is below last hull segment) (pi is above last hull segment)

(35)

Graham’s Scan: update

p1

Right turn

Add pi to the upper hull pi

Add pi but remove previous hull point until left turn disappears Similalrly for lower hull, after adding pi:

while last three points of lower hull q, q0, pi are a right turn:

remove the middle point q0

Left turn

p1

pi

(pi is below last hull segment) (pi is above last hull segment)

(36)

Graham’s Scan: pseudocode + runtime

Sort P by increasing x-coordinates Add p1, p2 to U and L

for i = 3 to n do

Add pi to U and L

while last three pts of U form left turn do Remove pt preceding pi from U

while last three pts of L form right turn do Remove pt preceding pi from L

return L and reverse of U

(37)

Graham’s Scan: pseudocode + runtime

Sort P by increasing x-coordinates Add p1, p2 to U and L

for i = 3 to n do

Add pi to U and L

while last three pts of U form left turn do Remove pt preceding pi from U

while last three pts of L form right turn do Remove pt preceding pi from L

return L and reverse of U Running time:

Sorting O(n log n)

(38)

Graham’s Scan: pseudocode + runtime

Sort P by increasing x-coordinates Add p1, p2 to U and L

for i = 3 to n do

Add pi to U and L

while last three pts of U form left turn do Remove pt preceding pi from U

while last three pts of L form right turn do Remove pt preceding pi from L

return L and reverse of U Running time:

Sorting

Each p P is:

added once to U (same for L)

removed at most once from U (same for L)

O(n log n)

O(n) O(n) Triplets checked in While loop heads O(n)

(39)

Graham’s Scan: pseudocode + runtime

Sort P by increasing x-coordinates Add p1, p2 to U and L

for i = 3 to n do

Add pi to U and L

while last three pts of U form left turn do Remove pt preceding pi from U

while last three pts of L form right turn do Remove pt preceding pi from L

return L and reverse of U Running time:

Sorting

Each p P is:

added once to U (same for L)

removed at most once from U (same for L)

O(n log n)

O(n) O(n) Triplets checked in While loop heads O(n)

Running time: O(n log n)

(40)

Graham’s Scan: correctness

Claim

After each iteration of main loop, U is upper hull of p1, . . . , pi.

(41)

Graham’s Scan: correctness

pi is added to U X Claim

After each iteration of main loop, U is upper hull of p1, . . . , pi. Induction on i. Works for i 2.

Suppose U is the upper hull of p1, . . . , pi−1

pi U

q

q0

pi−1

Gray is empty

(42)

Graham’s Scan: correctness

pi is added to U X Claim

After each iteration of main loop, U is upper hull of p1, . . . , pi. Induction on i. Works for i 2.

Suppose U is the upper hull of p1, . . . , pi−1

q0, pi−1, pi ”left turn” pi−1 is below q0pi. Similarly, q0 is below qpi

All deleted vertices are below the new U.

pi U

q

q0

pi−1

Gray is empty

(43)

Graham’s Scan: correctness

pi is added to U X Claim

After each iteration of main loop, U is upper hull of p1, . . . , pi. Induction on i. Works for i 2.

Suppose U is the upper hull of p1, . . . , pi−1

q0, pi−1, pi ”left turn” pi−1 is below q0pi. Similarly, q0 is below qpi

All deleted vertices are below the new U.

pi U

q

q0

pi−1

Gray is empty

old U and all of p1, . . . pi−1 are on or below new U

(44)

Graham’s Scan: non-general position

Instead of deleting for left turn in U:

Delete if straight or left turn

collinear triple:

q q0 pi

(45)

Graham’s Scan: non-general position

Instead of deleting for left turn in U:

Delete if straight or left turn

collinear triple:

q q0 pi

(46)

Graham’s Scan: non-general position

Instead of deleting for left turn in U:

Delete if straight or left turn

collinear triple:

q q0 pi

equal x-coordiantes:

(47)

Graham’s Scan: non-general position

Instead of deleting for left turn in U:

Delete if straight or left turn

collinear triple:

q q0 pi

equal x-coordiantes:

Use lexicographic order for initial sort.

(x, y) <lex (x0, y0) iff x < x0 (x = x0 y < y0)

(48)

Graham’s Scan: non-general position

Instead of deleting for left turn in U:

Delete if straight or left turn

collinear triple:

q q0 pi

equal x-coordiantes:

Use lexicographic order for initial sort.

(x, y) <lex (x0, y0) iff x < x0 (x = x0 y < y0)

p1, pn are still on the hull.

Upper hull U: part of hull after p1 and before pn in cw order

(49)

Practicalities

Collinear triples are common (grids!)

(50)

Practicalities

Collinear triples are common (grids!)

Almost collinear triples are also common!

(51)

Practicalities

Collinear triples are common (grids!)

Almost collinear triples are also common!

P uniform random in [0, 1]2: E( max

p,q,r∈P ^pqr) = π O(1/n3)

not peer-reviewed source

(52)

Practicalities

Collinear triples are common (grids!)

Almost collinear triples are also common!

Naive floating point implementation of Real RAM:

false positives and false negatives

P uniform random in [0, 1]2: E( max

p,q,r∈P ^pqr) = π O(1/n3)

not peer-reviewed source

(53)

Practicalities

Collinear triples are common (grids!)

Almost collinear triples are also common!

Naive floating point implementation of Real RAM:

false positives and false negatives

P uniform random in [0, 1]2: E( max

p,q,r∈P ^pqr) = π O(1/n3)

Good software libraries (e.g. CGAL) can protect you.

not peer-reviewed source

(54)

Chan’s algorithm (1996)

(55)

Output-sensitive algorithm

Input size: natural lower bound for most problems

Output size: same! (but useless for decsion problems)

(56)

Output-sensitive algorithm

Input size: natural lower bound for most problems

Output size: same! (but useless for decsion problems)

An alg. that is faster if the output is small.

running time expressed as function of input and output size Output-sensitive algorithm:

(57)

Output-sensitive algorithm

Example: enumerating spanning trees of a graph in

O(nn−2 · n) vs. O(#of trees + n + |E|) time Input size: natural lower bound for most problems

Output size: same! (but useless for decsion problems)

An alg. that is faster if the output is small.

running time expressed as function of input and output size Output-sensitive algorithm:

(58)

Output-sensitive algorithm

Here: h is # of convex hull vertices.

Can we get O(n + h) as running time?

Example: enumerating spanning trees of a graph in

O(nn−2 · n) vs. O(#of trees + n + |E|) time Input size: natural lower bound for most problems

Output size: same! (but useless for decsion problems)

An alg. that is faster if the output is small.

running time expressed as function of input and output size Output-sensitive algorithm:

(59)

Output-sensitive algorithm

Here: h is # of convex hull vertices.

Can we get O(n + h) as running time?

What about O(n + h log h)?

Example: enumerating spanning trees of a graph in

O(nn−2 · n) vs. O(#of trees + n + |E|) time Input size: natural lower bound for most problems

Output size: same! (but useless for decsion problems)

An alg. that is faster if the output is small.

running time expressed as function of input and output size Output-sensitive algorithm:

(60)

Output-sensitive convex hull algorithms

Kirkpatrick–Seidel (’86): O(n log h). Proven optimal!

(61)

Output-sensitive convex hull algorithms

Kirkpatrick–Seidel (’86): O(n log h). Proven optimal!

Chan (’96): O(n log h), and also ”works” in 3d.

(62)

Output-sensitive convex hull algorithms

Kirkpatrick–Seidel (’86): O(n log h). Proven optimal!

Chan (’96): O(n log h), and also ”works” in 3d.

Based on the Gift wrapping algorithm:

start from p1, and go around finding next hull vertex.

Find p1: O(n)

H1 = p1

(63)

Output-sensitive convex hull algorithms

Kirkpatrick–Seidel (’86): O(n log h). Proven optimal!

Chan (’96): O(n log h), and also ”works” in 3d.

Based on the Gift wrapping algorithm:

start from p1, and go around finding next hull vertex.

Find p1: O(n)

H1 = p1

Given H1, . . . , Hi, find Hi+1 s.t. ^Hi−1HiHi+1 is

maximized O(n)

Hi Hi−1

(64)

Output-sensitive convex hull algorithms

Kirkpatrick–Seidel (’86): O(n log h). Proven optimal!

Chan (’96): O(n log h), and also ”works” in 3d.

Based on the Gift wrapping algorithm:

start from p1, and go around finding next hull vertex.

Find p1: O(n)

H1 = p1

Given H1, . . . , Hi, find Hi+1 s.t. ^Hi−1HiHi+1 is

maximized O(n)

Hi Hi−1

Repeated h times

O(nh) time in total

(65)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

(66)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

(67)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

Compute convex hull of each Pj with Graham’s scan.

(68)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

Compute convex hull of each Pj with Graham’s scan.

(69)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

Compute convex hull of each Pj with Graham’s scan.

(70)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

Compute convex hull of each Pj with Graham’s scan.

(71)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

Gift wrapping:

Find largest angle Hi−1Hiqj with qj conv(Pj) for each j, pick best Hi−1

Hi

Compute convex hull of each Pj with Graham’s scan.

(72)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

Gift wrapping:

Find largest angle Hi−1Hiqj with qj conv(Pj) for each j, pick best Hi−1

Hi

dn/me · O(m log m)

Compute convex hull of each Pj with Graham’s scan.

(73)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

Gift wrapping:

Find largest angle Hi−1Hiqj with qj conv(Pj) for each j, pick best Hi−1

Hi

dn/me · O(m log m)

hdn/me · O(log m)

Compute convex hull of each Pj with Graham’s scan.

(74)

Chan’s algorithm

Let m ∈ {1, . . . , n} be a parameter.

Group P into groups of size m

⇒ dn/me groups: P1, P2, . . .

Gift wrapping:

Find largest angle Hi−1Hiqj with qj conv(Pj) for each j, pick best Hi−1

Hi

dn/me · O(m log m)

hdn/me · O(log m)

Time to find tangent of conv(Pj) Compute convex hull of each Pj with Graham’s scan.

(75)

Chan’s algorithm: running time and tangent finding

dn/me · O(m log m) + hdn/me · O(log m)

(76)

Chan’s algorithm: running time and tangent finding

dn/me · O(m log m) + hdn/me · O(log m)

= O(n log m + (h/m)n log m)

(77)

Chan’s algorithm: running time and tangent finding

dn/me · O(m log m) + hdn/me · O(log m)

= O(n log m + (h/m)n log m) setting m = h:

= O(n log h)

(78)

Chan’s algorithm: running time and tangent finding

dn/me · O(m log m) + hdn/me · O(log m)

= O(n log m + (h/m)n log m) setting m = h:

= O(n log h) Tangent finding in O(log m) time?

q1 q2

q3 q4

q5

q6

p

(79)

1. Set m = h

(80)

1. Set m = h

2. Let Pj = {p(j−1)m+1, . . . , pjm}.

(81)

1. Set m = h

How to set m = h?

?

2. Let Pj = {p(j−1)m+1, . . . , pjm}.

(82)

1. Set m = h

How to set m = h?

?

2. Let Pj = {p(j−1)m+1, . . . , pjm}.

h

X

m=1

cn log m = Ω(hn)...

Idea 1: Let m = 1, 2, . . . , n, run giftwrapping for m steps Stop when gift is wrapped. (Then m h holds.)

(83)

1. Set m = h

How to set m = h?

?

2. Let Pj = {p(j−1)m+1, . . . , pjm}.

h

X

m=1

cn log m = Ω(hn)...

Idea 1: Let m = 1, 2, . . . , n, run giftwrapping for m steps Stop when gift is wrapped. (Then m h holds.)

Too slow!

(84)

1. Set m = h

How to set m = h?

?

2. Let Pj = {p(j−1)m+1, . . . , pjm}.

h

X

m=1

cn log m = Ω(hn)...

Idea 1: Let m = 1, 2, . . . , n, run giftwrapping for m steps Stop when gift is wrapped. (Then m h holds.)

Too slow!

dlog he

X

i=1

cn log 2i =

dlog he

X

i=1

cni = Θ(n log2 h)...

Idea 2: Let m = 21, 22, 23, . . . , 2log n, run wrapping for m steps

(85)

1. Set m = h

How to set m = h?

?

2. Let Pj = {p(j−1)m+1, . . . , pjm}.

h

X

m=1

cn log m = Ω(hn)...

Idea 1: Let m = 1, 2, . . . , n, run giftwrapping for m steps Stop when gift is wrapped. (Then m h holds.)

Too slow!

dlog he

X

i=1

cn log 2i =

dlog he

X

i=1

cni = Θ(n log2 h)...

Idea 2: Let m = 21, 22, 23, . . . , 2log n, run wrapping for m steps

h

X

m=1

cn log m = Ω(hn)...

Too slow!

(86)

1. Set m = h

How to set m = h?

?

2. Let Pj = {p(j−1)m+1, . . . , pjm}.

h

X

m=1

cn log m = Ω(hn)...

Idea 1: Let m = 1, 2, . . . , n, run giftwrapping for m steps Stop when gift is wrapped. (Then m h holds.)

Too slow!

dlog he

X

i=1

cn log 2i =

dlog he

X

i=1

cni = Θ(n log2 h)...

Idea 2: Let m = 21, 22, 23, . . . , 2log n, run wrapping for m steps

h

X

m=1

cn log m = Ω(hn)...

Too slow!

dlog log he

X

i=0

cn log 22i =

dlog log he

X

i=0

cn2i

= cn(2dlog log he+1 1) = O(n log h) Idea 3: Let m = 220, 221, 222, . . . , 22dlog logne, do m wrap-steps

(87)

Chan’s algorithm: Recap

for i = 1 to dlog log ne do m = 22i

for j = 1 to dn/me do Create group Pj

(q1j, q2j, . . . , ) = Graham(Pj) H1 =leftmost point in P

for s = 2 to m do

for j = 1 to dn/me do

qj = T angentF ind(Hs−1, (q1j, q2j, . . . )) Hs = point qj maximizing ^Hs−2Hs−1qj if Hs = H1 then return (H1, . . . , Hs−1)

Referenzen

ÄHNLICHE DOKUMENTE

It signals the central control or the processor (depending on the Medium Data Processing System) to au- tomatically translate EBCDIC to BCL informa- tion as it is

signal on this line indicates that the external equipment has accepted the word of information and will turn off the output data ready signal at the com- puter; this causes

In determining the priority of control units which operate multiple devices with different priority rules (for example, a 2821 that attaches both class 2 and class

Das Problem ist, acht Damen auf einem Schachbrett so anzuordnen, dass keine die andere

Input port lines and output port lines are accessed at 16-pin DIP sockets on the card.. A reset line is

Address. The DMA interrupt enable bit must be cleared in the Interrupt Mask Register for the interrupt to be recognized. This bit will remain active and must

The Data Read is delayed from Data Write and may be shifted into an external register by using the negative-going edge of the Data Read Shift Clocks or the

With a parallel printer port, three serial ports (one standard and two optional), and a game port, the Magic I/O packs more features and connectors on a true