28
1 Identifying and sorting jordan sequences

1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

  • View
    217

  • Download
    3

Embed Size (px)

Citation preview

Page 1: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

1

Identifying and sorting jordan sequences

Page 2: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

2

Jordan curve

Jordan curve in the plane: A continous curve that does not intersect itself

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Page 3: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

3

Jordan sequence

The sequence x1,x2,.....,xn of the intersection points of a jordan curve C with the x-axis in the order in which they occur on C.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19

Page 4: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

4

Our problem

An efficient algorithm which would recognize and sort a jordan sequence.

Page 5: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

5

A special case: sort the intersections of an n-sided simple polygon with a line.

Page 6: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

6

Application: Polygon clipping

Return the intersection of a polygon with a rectangular window (a collection of polygons)

Page 7: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

7

Polygon clipping

Return the intersection of a polygon with a rectangular window (a collection of polygons)

Page 8: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

8

Polygon clipping algorithm

Page 9: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

9

You find the intersections by the order they occur on the polygon.

But you also need them by the order they occur on the rectangle

Page 10: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

10

Polygon triangulation

Useful for triangulating simple polygons.

Page 11: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

12

Assume to simplify presentation

Curve nowhere tangent to the x-axis.

Curve start below the x-axis (if not reflect)

Page 12: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

13

Nested parenthesisEach pair {x2i-1 ,x2i} for i = 1,....,n/2 corresponds to part of C going from the x-axis up and back to the x-axis.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19

Page 13: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

14

Nested parenthesis (cont)

The sequence is a jordan sequence iff different pairs {x2i-1 ,x2i} {x2j-1 ,x2j} cannot cross

Cross:

either

or

Page 14: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

15

Upper and lower trees

The parent of {x2i-1 ,x2i} is the “immediate” enclosing pair.This defines a forest .Add {-, } to make it a tree.

Page 15: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

16

Upper and lower trees (example)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19

-

1 6 7 12 13 21

2 3 4 5 8 9 10 1114 17

15 16 18 20

Page 16: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

17

The algorithm

The algorithm processes x1,x2,.....,xn one at a time building the upper tree, the lower tree, and a list of the points in sorted order.

Initialization:

Make {-, } the only pair in both trees.

Initialize the sorted list to be {-, x1, }

For i = 2,....n process xi as follows

Suppose i is even, i.e. {xi-1 , xi} is to be added to the upper tree

Assume xi-1 < xi (otherwise it is symmetric)

Page 17: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

18

The algorithm (cont)

Find the point x that follows xi-1 in the sorted list.

Find the pair {xj-1 , xj} in the upper tree such that x {xj-1 , xj}

Let {lj , rj } = {xj-1 , xj}

Apply one of the following 4 casesCase A: lj < xi-1 < rj < xi

lj rjxi-1 xi

Stop, this is not a jordan sequence

Page 18: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

19

The algorithm (cont)

Case B: lj < xi-1 < xi < rj

lj rjxi-1 xi

Make {xi-1 , xi} the new last child of {xj-1 , xj}.

Insert xi after xi-1 in the sorted list

Page 19: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

20

The algorithm (cont)

Case C: xi-1 < xi < lj

lj rjxi-1 xi

Insert {xi-1 , xi} into the list of siblings of {xj-1 , xj} just before {xj-1 , xj}

Insert xi after xi-1 in the sorted list

Page 20: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

21

The algorithm (cont)

Case D: xi-1 < lj < xi

lj rjxi-1

In the list of siblings of {xj-1 , xj} find the last pair {xk-1 , xk} such that lk < xi

If rk > xi then it is not a jordan sequence.

xi lk rk

Page 21: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

22

The algorithm (cont)

Case D: xi-1 < lj < xi

lj rjxi-1

Otherwise if {xk-1 , xk} is the last child of its parent pair {xp-1 , xp} and rp < xi it is not a jordan sequence.

xi lk rk rp lp

Page 22: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

23

The algorithm (cont)

Case D: xi-1 < lj < xi

lj rjxi-1

Remove from the list of siblings of {xj-1 , xj} the sublist from {xj-1 , xj} to {xk-1 , xk} (inclusive) and replace it by {xi-

1 , xi}. Make the removed sublist the list of children of {xi-1 , xi}.

Insert xi after rk into the sorted list.

xi lk rk rp lp

Page 23: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

24

The algorithm (cont)

If i is odd we work similarly with the lower tree.

Page 24: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

25

The algorithm (example)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19

-

-

1 6 7 12 13 21

2 3 4 517 1815 16 19 20

Page 25: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

26

The algorithm (example)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19

-

-

1 6 7 12 13 21

2 3 4 5

17 1815 16

19 2014 18.5

Page 26: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

27

Representation

The sorted list of processed numbers is a doubly linked list.

Each list of siblings in the upper or lower trees is represented by a homogenous finger search tree.

-

1 6 7 12 13 21

2 3 4 5 8 9 10 1114 17

15 16 18 20

Page 27: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

28

Analysis

Inserting a pair before or after some other pair takes O(1) time.

Finding and removing a sublist of d pairs from a list of s pairs takes O(1 + log(min{d,s-d}))

We perform at most n/2 operations of the form “remove a sublist/insert a pair” each costs O(1 + log(min{d,s-d} )) for some 1 d s if it operates on a list of length s.

What is the total time ?

Page 28: 1 Identifying and sorting jordan sequences. 2 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself 123456789101112131415161718192021

29

Analysis (Cont)

Assume all elements are there to begin with. This would just make the split operations more expensive.

Charge the split evenly to the items in the smaller part, if d is the length of the smaller part then each element is charged log(d)/d

The total charge per element is at most

log(x1)/x1 + log(x2)/(x2) + … log(xk)/xk

The xi decrease exponentially : xi ≤ xi-1/2

Therefore the sum is at most Σ k/2k = O(1)