Identifying and sorting jordan sequences 1 Jordan curve

  • Slides: 28
Download presentation
Identifying and sorting jordan sequences 1

Identifying and sorting jordan sequences 1

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

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 2

Jordan sequence The sequence x 1, x 2, . . . , xn of

Jordan sequence The sequence x 1, x 2, . . . , xn of the intersection points of a jordan curve C with the x-axis in the order in which they occur on C. 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 3

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

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

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

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

Application: Polygon clipping Return the intersection of a polygon with a rectangular window (a

Application: Polygon clipping Return the intersection of a polygon with a rectangular window (a collection of polygons) 6

Polygon clipping Return the intersection of a polygon with a rectangular window (a collection

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

Polygon clipping algorithm 8

Polygon clipping algorithm 8

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

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 9

Polygon triangulation Useful for triangulating simple polygons. 10

Polygon triangulation Useful for triangulating simple polygons. 10

Assume to simplify presentation Curve nowhere tangent to the x-axis. Curve start below the

Assume to simplify presentation Curve nowhere tangent to the x-axis. Curve start below the x-axis (if not reflect) 12

Nested parenthesis Each pair {x 2 i-1 , x 2 i} for i =

Nested parenthesis Each pair {x 2 i-1 , x 2 i} for i = 1, . . , n/2 corresponds to part of C going from the x-axis up and back to the x-axis. 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 13

Nested parenthesis (cont( The sequence is a jordan sequence iff different pairs {x 2

Nested parenthesis (cont( The sequence is a jordan sequence iff different pairs {x 2 i-1 , x 2 i} {x 2 j-1 , x 2 j} cannot cross Cross: either or 14

Upper and lower trees The parent of {x 2 i-1 , x 2 i}

Upper and lower trees The parent of {x 2 i-1 , x 2 i} is the “immediate” enclosing pair. This defines a forest. Add {- , } to make it a tree. 15

Upper and lower trees (example( - 1 2 3 6 7 4 5 8

Upper and lower trees (example( - 1 2 3 6 7 4 5 8 9 12 10 11 13 21 14 17 15 16 18 20 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 16

The algorithm processes x 1, x 2, . . . , xn one at

The algorithm processes x 1, x 2, . . . , 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 {- , x 1, } 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) 17

The algorithm (cont( Find the point x that follows xi-1 in the sorted list.

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 cases Case A: lj < xi-1 < rj < xi lj xi-1 rj xi Stop, this is not a jordan sequence 18

The algorithm (cont( Case B: lj < xi-1 < xi < rj lj xi-1

The algorithm (cont( Case B: lj < xi-1 < xi < rj lj xi-1 xi rj Make {xi-1 , xi} the new last child of {xj-1 , xj}. Insert xi after xi-1 in the sorted list 19

The algorithm (cont( Case C: xi-1 < xi < lj xi-1 xi lj rj

The algorithm (cont( Case C: xi-1 < xi < lj xi-1 xi lj rj 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 20

The algorithm (cont( Case D: xi-1 < lj < xi xi-1 lj rj lk

The algorithm (cont( Case D: xi-1 < lj < xi xi-1 lj rj lk xi rk 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. 21

The algorithm (cont( Case D: xi-1 < lj < xi lp xi-1 lj rj

The algorithm (cont( Case D: xi-1 < lj < xi lp xi-1 lj rj lk rk rp xi 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. 22

The algorithm (cont( Case D: xi-1 < lj < xi lp xi-1 lj rj

The algorithm (cont( Case D: xi-1 < lj < xi lp xi-1 lj rj lk rk xi rp 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. 23

The algorithm (cont( If i is odd we work similarly with the lower tree.

The algorithm (cont( If i is odd we work similarly with the lower tree. 24

The algorithm (example( 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20,

The algorithm (example( 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 - 1 2 3 6 4 7 5 12 13 15 16 21 17 18 19 20 25

The algorithm (example( 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20,

The algorithm (example( 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 - 1 2 3 6 4 7 12 13 14 5 15 16 21 18. 5 17 18 19 20 26

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

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 2 3 6 4 7 5 8 9 12 10 11 13 21 14 17 15 16 18 20 27

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

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})) In general we can say that we perform n-1 operations of the form “remove a sublist/insert a pair” each costs O(1 + log(min{d, s-d} + 1 )) for some 0 d s if it operates on a list of length s. How much time it takes to do n/2 such operations of this form on an initial list of size 1 ? 28

Analysis (Cont( Assume all elements are there to begin with. This would just make

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(x 1)/x 1 + log(x 2)/(x 2) + … log(xk)/xk The xi decrease exponentially : xi ≤ xi-1/2 Therefore the sum is bounded by log(n/2)/n/2 + log(n/4)/(n/4) + … log(2)/2 = O(1) 29