Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and Data Structures lecture C(hex) Computational geometry: planar rectangular range query, planar convex hull Szymon Grabowski sgrabow@kis.p.lodz.pl.

Similar presentations


Presentation on theme: "Algorithms and Data Structures lecture C(hex) Computational geometry: planar rectangular range query, planar convex hull Szymon Grabowski sgrabow@kis.p.lodz.pl."— Presentation transcript:

1 Algorithms and Data Structures lecture C(hex) Computational geometry: planar rectangular range query, planar convex hull Szymon Grabowski Łódź, 2016

2 Computational geometry
Hard to define the field precisely. Geometric problems considered, in 2D, 3D and even in higher (arbitrarily high) dimensions. Historically, CG developed as a generalization of sorting and searching problems from 1-dim. (numerical) keys into multidimensional objects. Hot research area since the late 1970s (very influential Shamos’s PhD...). Already the 2-dimensional space is interesting enough...

3 Recommended literature
Dave Mount, CMSC 754 Computational Geometry, Fall 2002, lecture notes. Franco P. Preparata, Michael Ian Shamos, Geometria obliczeniowa. Wprowadzenie, Helion 2003, pp. 392 (in Polish). (Original title: Computational Geometry. An Introduction. Springer-Verlag, New York, 1985.) Joseph O'Rourke, Computational geometry in C, Cambridge University Press, 2nd edition, 1998. Mark de Berg, Marc van Krefeld, Mark Overmars, Otfried Schwarzkopf, Computational Geometry: Algorithms and Applications. Springer-Verlag, 2nd edition, 2000, pp. 367. Godfried T. Toussaint‘s web page: And Cormen et al. as usual.

4 CG objects Mostly straight or flat objects. Lines, line segments, polygons, planes and polyhedra. Also simple curved objects like circles. Representation of a polygon (for example) is the set of its vertices in a “walking order” (e.g., counter-clockwise).

5 Typical CG problems NN queries: Find the nearest neighbor of a point q from the given dataset S. Range queries: Find all the points in S located within the radius r from a given point q. Shortest path: Given a set of obstacles (e.g., in the plane), find the shortest obstacle-avoiding path from a given point s to a given point t.

6 Typical CG problems, cont’d
Convex hull. Triangulations (or other partitioning problems): Divide a complex object into a set of disjoint objects (e.g, any simple polygon into triangles). Simple concave polygon Complex polyon From:

7 Typical CG problems, cont’d
Convex hull in 3D (and in d dim.).

8 Typical CG problems, cont’d [ http://www. ams. sunysb
Point-in-polygon test: Is point q inside simple polygon P? Naïve test: O(n). Smart (CG): O(log n) (after O(n) polygon preprocessing, ie. makes sense amortized over many tests...). q P n-gon

9 Typical CG problems, cont’d [ http://www. ams. sunysb
Segment intersection: Given n line segments in the plane, determine: Does some pair intersect? (DETECT) Compute all points of intersection (REPORT) Naïve solution: O(n2). Smart (CG): O(n log n) detect, O(k + n log n) report (reporting is output-sensitive).

10 Typical CG problems, cont’d
Art gallery problems (Klee, 1973): For a given simple polygon of n vertices, locate the minimum number of guards in a way to have every point of the polygon visible. 12 vertices. (a) 3 guards required. (b) 4 guards required. From: [O'Rourke, Computational geometry in C]

11 Typical CG problems, cont’d [ http://cgm. cs. mcgill
Isothetic computational geometry (or rectilinear CG): Input data are such as line segments and polygons in which all the edges are either vertical or horizontal. Applications: VLSI design, image processing. Problem could be: given a set of isothetic (also called: orthogonal) rectangles in the plane, compute the boundary of the union of the rectangles (VERY IMPORTANT IN VLSI DESIGN). ...or the other way around: given an isothetic contour, break it into a union of a minimum number of rectangles.

12 The cross product of vectors A and B is the dashed area.

13 Cross product of vectors, cont’d
p1 x p2 is positive iff p1 is clockwise from p2 with respect to the origin (0,0). p1 x p2 can be zero – when?

14 Determining whether consecutive segments turn left or right
p2 p2 p1 p0 p1 p0 p0 p1 p2 – turn right or left? Simply calculate the cross product ( p2 – p0 ) x ( p1 – p0 ). If the product sign is positive, it means a right turn at p1 (picture above, right). Negative – left turn (pic. above, left). Zero – collinear points.

15 2D orthogonal range query
Problem: Given a planar set of points Q, count (or: report) all the points from Q which belong the the given Cartesian product [a, b] x [c, d]. Q given off-line. a, b, c, d – query parameters.

16 2D orthogonal range query, solution
Trivial idea (brute-force): No preprocessing, query answered in O(n) time. Better? Let’s sacrifice O(n2) preprocessing space and time. For simplicity, assume all the points are in the 1st quarter (i.e., non-negative coordinates). What do you propose..? Idea: pass the two axis-parallel lines through each point of Q. (Obtain a grid.) Precompute the answers for all n2 resulting rectangles. Let count(pi) denote the number of points from Q in the [0, pi(x)] x [0, pi(y)] rectangle.

17 2D orthogonal range query, solution, cont’d
With respect to the fig. at slide 14, the number N of points from Q in the rectangle [a, b] x [c, d] is given by the formula: N(P1, P2, P3, P4) = count(P4) – count(P2) – count(P3) + count(P1). The question now is: how to answer count(...) quickly? Recall: the axis-parallel lines are passed through each point of Q. For a given point p it is enough to find the point from Q with the largest x coordinate not larger than px. So with the y coordinate.

18 The red rectangle and the P1
The red rectangle and the P1..P4 rectangle contain the same set of points from Q

19 2D orthogonal range query, solution, cont’d
The points of Q should be kept sorted twice: once according to their x coordinate and once according to their y coordinate. Then answering count(...) requires 2 binary searches. 4 count(...) operations are needed for our query (but 4 is a constant...). In total we have O(log n) worst-case query time. What about the preprocessing? Clearly O(n2) in space but what about its time? Well, the naïve solution needs O(n3) time, but we can find the answers for the grid cells in O(1) amortized time per each (hint: do it incrementally), which decreases the preprocessing time to O(n2) as well.

20 2D orthogonal range query, even better?
The logarithmic query time is nice but the quadratic preprocessing costs are prohibitive for a large n... Fortunately, it is possible (Bentley & Shamos, 1977) to reduce the preprocessing time and space to O(n log n) with a reasonably mild query time increase, to O(log2 n + occ) (+occ for reporting). Better results exist, e.g., O(n log n),  > 0, space and O(log log n + occ) query time (Alstrup, 2000).

21 (Planar) convex hull problem
Given a set Q of n points in 2 dimensions, find the minimal convex polygon CH(Q) containing all the points from Q. Minimal since CH(Q) is a subset of any convex set containing all points of Q. More precisely, the problem is to list the h vertices of CH(Q) ordered along its boundary. h is an output parameter (can’t be guessed beforehand).

22 Convex hull example Elastic band analogy: the band contracts to form the “hull” of the convex

23 Convex hull applications [J
Convex hull applications [J. O'Rourke, Computational geometry in C, 2nd edition, 1998] 1. Collision avoidance. If a convex hull of a robot avoids collision with an obstacle, then so does the robot. Easier to plan trajectories. Faster collision detection in computer games.  2. Smallest (bounding) box. The smallest area rectangle that encloses a polygon has at least one side touching with the convex hull of the polygon, so computing the CH of the polygon is the first step. 3. Shape analysis. Shapes may be classified (for the purpose of matching) by some special structures that depend on the computation of their convex hulls. 4. A building block for many more complex CG algorithms (e.g., finding the diameter of a set of points).

24 Overall worst-case time: O(n3). Pretty bad.
A naïve algorithm There are n(n–1) / 2 = O(n2) pairs of points in Q, each of them corresponds to a unique line. For each line l (defined by some points p1, p2) we check in O(n) worst-case time if all the other points of Q lie on one side of l or not. If so, we say that (p1, p2) define a supporting line and this line segment will belong to CH(Q) (i.e., the points p1 and p2 will be somewhere on the output list, and they’ll be next to each other). We have at most n such pairs, joining them can be done, e.g., via sorting, i.e. in O(n log n) time. Overall worst-case time: O(n3). Pretty bad.

25 An illustration to the naïve algorithm
line l won’t belong to CH(Q) here l is OK

26 Quickhull algorithm (Preparata & Shamos, 1985)
This and following slides: from LN_4_divide&conquer.ppt

27 Quickhull, step-by-step (1/7)
Find extreme points above, below, left and right. Define a quadrilateral connecting extreme points, discard internal points.

28 Quickhull, step-by-step (2/7)
3. Process 4 triangles recursively.

29 Quickhull, step-by-step (3/7)
For each triangle, find vertex with greatest distance from the triangle baseline. Define another triangle and discard internal triangles.

30 Quickhull, step-by-step (4/7)
6. Process two triangles recursively.

31 Quickhull, step-by-step (5/7)

32 Quickhull, step-by-step (6/7)

33 Quickhull, step-by-step (7/7)

34 Quickhull, time complexities
Worst case: obviously O(n2) (why?). Best case: O(n), also obvious. Average case? This is harder. Assuming uniform point distribution in a unit square, the avg time is O(n log n). Quickhull was dubbed after quicksort. As with QS, the algorithm picks a “pivot” element, splits the data based on the pivot, and is recursively applied to each of the split sets. Also, as with quicksort, the pivot element is not guaranteed to split the data into equally sized sets, Worst case, closer look: O(nh), where h is the # of the CH(Q) boundary points.

35 Quickhull, final words [ http://www. cs. umd
Why practical? The intuition is that in many applications most of the points lie in the interior of the hull. For example, if the points are uniformly distributed in a unit square, then it can be shown that the expected number of points on the convex hull is O(log n). There are better algorithms, but QH can be used as a preliminary step (filter) before some other algorithm (in a hope to combine good worst case and practical speed).

36 Jarvis march (1973) The Jarvis march uses a (very intuitive) technique called gift wrapping. We can say it simulates wrapping a piece of paper around the set Q. We attach the end of the paper to the lowest point in Q, which will be denoted as p0. Note that p0 must belong to CH(Q). Each next point (until reaching the highest point of Q) must be not lower than its predecessor. Formally, p1 is the point which has the least polar angle with respect to p0. Continue in the same way, i.e., look for p2 with respect to p1, etc. Finally you’ll obtain the right chain of CH(Q). Left chain – analogously.

37 Jarvis march, example ([Cormen])

38 Jarvis march complexity
Finding p0: O(n) time. Finding p1: for each of n–1 remaining points its polar angle must be calculated and compared to the current min polar angle: O(1) time per each, i.e., O(n) in total. The convex hull of Q has h points, hence the overall complexity of the Jarvis march is O(nh) (worst, average and best case). For the worst case of h = O(n), we have O(n2).  Still, quite often h is small, so the Jarvis algorithm is practical (and very simple, isn’t it?).

39 The Graham scan (1972) [O’Rourke’98] reports: Perhaps the honor of the first paper published in the field of computational geometry should be accorded to Graham’s algorithm for finding the hull of points in two dimensions in O(n log n) time (Graham, 1972). In the late 1960s an application at Bell Laboratories required the hull of n  10,000 points, and they found the O(n2) algorithm in use too slow. Graham developed his simple algorithm in response to this need. Graham’s algorithm involves a stack. The stack S keeps candidate points. Each point of Q is pushed once onto the stack. At the end, S contains only the vertices of CH(Q), in counter-clockwise order of their appearance on the boundary. We’ll assume |Q|  3.

40 Graham scan pseudocode
From [Cormen], chapter 35.3

41 Graham scan example (from [Cormen])
After steps 1–6. p0 is the lowest point. The other points are sorted appropriately.

42 Graham scan example (b)
p2 rejected, as the ordered triple p1, p2, p3 didn’t form a left turn

43 Graham scan example (c)
p3 seems okay. Put onto the stack...

44 Graham scan example (d)
p4 kicked out

45 Graham scan example (e)
p10 some-where above p5 seem okay (so far...) Now we skip over the boring steps (f) and (g)...

46 Graham scan example (h)
p10 some-where above p7 and p8 got what they deserved

47 Graham scan example (i)
heavy hand of justice expels p5, finally

48 Graham scan example (l)
The rest is silence. After finding the last point, connect it with p0.

49 Graham scan complexity
Beware: we’ll use n symbol below for the number of points from Q, as opposed to the Cormen et al. pseudocode where m is used. Step 1: O(n) time. Step 2: O(n log n) time (sorting). Steps 3–6: O(1) time. Steps 7–10: ??? The external loop runs n–2 times. The # of passes over the internal loop varies: may be as large as O(n). Does it mean the total complexity is O(n2) ??!

50 Graham scan complexity, cont’d
Note that the time complexity of the steps 7–10 corresponds to the # of pop() calls in line 9. You cannot pop more items from a stack than you have pushed. And each point of Q is put onto the stack S only once. Hence the steps 7–10 need only O(n) time. In total we obtain O(n log n) worst-case time complexity (the sort is the bottleneck).


Download ppt "Algorithms and Data Structures lecture C(hex) Computational geometry: planar rectangular range query, planar convex hull Szymon Grabowski sgrabow@kis.p.lodz.pl."

Similar presentations


Ads by Google