26
Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis 3.2.12 [email protected]

Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

Advanced Algorithm Design and Analysis (Lecture 10)

SW5 fall 2007Simonas Šaltenis [email protected]

Page 2: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 2

Voronoi diagrams

Main goals of the lecture: to understand the concept of Voronoi

diagrams and their key properties; to understand the doubly-connected edge

list data structure used to represent space subdivisions;

to understand the main ideas of the Fortune's algorithm, an example of an advanced plane sweep algorithm

Page 3: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 3

Nearest-Neighbor Queries

Nearest-neighbor query: Given a set of multi-dimensional points S and a

query point q, return a point p ∈ S, closest to q:• That is ∀ a∈S d q , pd q ,a

q

p

Page 4: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 4

Nearest-Neighbor Queries

Applications: Find a closest gas station Similarity search: find an image or a song that

is most similar to a given one.• Represent each image (song) as a feature vector

(multi-dimensional point)

How can we process NN queries in R-trees, kd-trees or similar structures? Note: both structures were designed with range

queries in mind

Page 5: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 5

NN Querying in R-trees

Two algortihtms can be used: Best-first:

• Uses O(n) memory in worst-case• Is optimal for a given instance of the data structure,

but runs in O(n) in worst-case

Depth-first:• Uses only O(log n) memory• Runs in O(n) in worst-case

What if we want a O(log n) worst case performance for NN queries?

Page 6: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 6

Voronoi Region, Diagram

Consider a set S of objects S = {s1, s2, ..., sn}

Voronoi cell of si:

Voronoi diagram Vor(S): partition of the plane into cells V(si) Named after: Georgy Voronoi (1908), but can be

traced back to Descartes (XVII century) a.k.a Dirichlet tesselation

V si={p : d p , sid p , s j ,∀ j≠i}

Page 7: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 7

Voronoi Diagram: Example

Page 8: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 8

Voronoi Region: Closer Look

hij: perpendicular bisector of segment (si, sj)

Hij: half-plane delimited by hij and containing si

Hij = {p : p is closer to si than sj}

V si=∩H ij

Page 9: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 9

Voronoi Diagram: Properties

Voronoi diagram is connected, its edges are segements or halflines Except for the degenerate case. Which?

Each vertex is a center of a circle going thorough three (or more) adjacent sites.

Page 10: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 10

Voronoi Diagram: Size

Max size of one cell: n – 1 edges... But the total number of vertices ≤2n–5 and

the total number of edges ≤3n–6 Proof using the Euler's formula – for any planar

ambeded graph:• mv – me + mf = 2

Page 11: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 11

Using Voronoi Diagrams

Nearest-neighbor queries can be answered in worst-case O(log n) time using point location data structres on top of Voronoi diagrams

NN queries using R-trees storing Voronoi cells (still O(n) in worst case).

Closest-pair in linear time: Members of the closest-pair can be found in

adjacent Voronoi cells A lot of applications in GIS, etc.

Page 12: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 12

Relations to Other Concepts

Convex Hull: Sites in unbounded cells of the Voronoi diagram

define the convex hull of all the sites! (Why?)

Page 13: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 13

Relations to Other Concepts

Delaunay triangulation – the dual of Voronoi diagram: Has a number of nice properties such as that the

triangles are “not thin”

Page 14: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 14

Exercise

Draw a Voronoi diagram

2

3

5

4

1

Page 15: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 15

Doubly-Connected Edge List

Data structure for Voronoi diagrams:• Each edge is represented as two half-edges – for both

directions • Vertex entry:

• Coordinates• IncidentEdge – pointer to an arbitrary half-edge that has this

vertex as an origin

• Face (cell) entry:• Boundary – pointer to some half-edge on its boundary

• Half-edge entry:• Origin – pointer to an origin vertex• Twin – pointer to a corresponding oposite half-edge• IncidentFace – pointer to the bounded face (cell) to the left• Next – pointer to the next half-dege on the boundary• Prev – pointer to the previous half-edge on the boundary

• The whole diagram has to be put into a bounding box...

Page 16: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 16

Computing Voronoi Diagrams

How do we compute a Voronoi Diagram? Naïve: compute each voronoi cell by doing a

bunch of half-plane intersections• Running time?

Divide-and-conquer• O(n log n)

Fortune's algorithm, a plane sweep algorithm• O(n log n)

Page 17: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 17

Fortune's Algorithm

Sweeps the line from top to bottom What part of the Voronoi diagram can we be

sure of when the sweepline is at a specific position? Above the beach line:

• An arc Ai is defined by a visited site si:

• For any point a on Ai: d(a,si) = d(a,l) where l is the sweepline

• A breakpoint between two arcs traces an edge of a voronoi diagram

Page 18: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 18

Site Events

What are the events? When does the combinatorial structure of the beach line change? When does a new arc appear? Only when a new site is reached: site event

Page 19: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 19

Site Events

There can be at most 2n–1 arcs in the beachline (Why?)

Actions on a site event: Update a beachline (one new arc, two new

break-points) Add an edge to the Voronoi diagram (not

connected to anything yet...)

Page 20: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 20

Circle Events

When does an arc disappear from the beachline? When it's length becomes zero: a circle event For each new triplet of adjacent arcs we have to

compute when such event will take place and insert it into the event queue

Page 21: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 21

Circle Events

Actions on Circle Events: Update a beachline (remove an arc)

• Check for new triplets of arcs

Add a vertex, connect two edges corresponding to the two merging breakpoints to this vertex and add an edge to the Voronoi diagram• The only time when a vertex is added to the Voronoi

diagram

Page 22: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 22

Circle Events

Does every new triplet result in a definite circle event? Diverging breakpoints – no circle event is

generated

False alerts – the event is inserted into the queue but is removed before being processed

Page 23: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 23

Data Structures

How do we represent the beach line?

s1

s2

s3

s1 s2 s3 s2

s1,s2 s3,s2

s2,s3 Balanced binary search tree T Leaf node represents an arc:

• Site generating an arc• Pointer to a future circle event in

the event queue or nil

Internal node represents a brake point:• Pair of points• Pointer to a half-edge being

traced out in the Voronoi diagram

Page 24: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 24

Event Queue

All site events are inserted when the algorithm starts

All new triples of arcs are investigated when processing any event, to look for circle events. They are inserted into the queue

Circle event: The lowest point of the circle Pointer to a corresponding arc (leaf node in T)

Page 25: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 25

Running Time, Space

Running time: Processing each event: O(log n) Number of events:

• Site events: n• Processed circle events: at most 2n – 5

Total time O(n log n)

Space (beach line + queue): O(n)

Page 26: Advanced Algorithm Design and Analysis (Lecture 1)people.cs.aau.dk/~simas/aalg07/slides/aalg10.pdf · Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2007 Simonas Šaltenis

AALG, lecture 10, © Simonas Šaltenis, 2007 26

Exercise

Show event queue and beach line tree

Mark edges and vertices of the Voronoi diagram that have been created

What happens, when site event (5) is processed?

2

3

5

4

1