CompGeom - PowerPoint PPT Presentation

1 / 66
About This Presentation
Title:

CompGeom

Description:

Problems come from: graphics, robotics, VLSI design, CAD-CAM, pattern placement ... if each segment straddles the line containing the other: by straddling we mean ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 67
Provided by: giampier
Category:

less

Transcript and Presenter's Notes

Title: CompGeom


1
CompGeom
  • Computational Geometry study algorithms to solve
    geometric problems.
  • Problems come from graphics, robotics, VLSI
    design, CAD-CAM, pattern placement on restricted
    real-estate, bio-informatics, etc
  • This introduction will consider 2-dimensional
    problems only points in R2 will be represented
    as ordered pairs of real numbers p (x, y).
    Polygons will be represented as finite, ordered
    sequences of pairs P (p0, p1, ,pn-1).

2
CompGeom
  • This chapter will cover a number of results,
    primarily related to finding the intersections of
    a finite collection of line segments, computing
    the convex hull of a finite collection of points,
    and, finally, finding the closest pair of points
    in a finite collection of points.
  • Most of the results require the identification of
    points in terms of (finite representation) real
    numbers and so are susceptible to some of the
    aspects of numerical instability.

3
CompGeom
  • Def. A convex combination of two points p1
    (x1, y1) and p2 (x2, y2) is any point p3 (x3,
    y3) such that p3 (x3, y3) (a x1 (1 - a)
    x2, a y1 (1 - a) y2), 0 a 1.
  • We sometimes write p3 a p1 (1 - a) p2.
  • p3 is any point on the line segment connecting p1
    to p2, denoted by , with endpoints p1 and
    p2. The directed segment will be denoted by
    If p1 (0, 0) - the origin - the directed
    segment will also be called the vector p2.

4
CompGeom
  • We shall ask the following questions
  • Given directed segments and , is
    clockwise from with respect to their
    common endpoint p0?
  • Given line segments and , if we
    traverse and then , do we make a left turn
    at p1?
  • Do line segments and intersect?
  • We expect the questions to be answered in time
    O(1), and without the use of divisions or
    trigonometric functions both are expensive and
    suffer from numerical approximation problems.

5
CompGeom
  • Cross Products. In 3-dimensions, the
    cross-product of two vectors is another vector,
    perpendicular to the plane of the original two.
    In two dimensions, we will interpret the cross
    product as a real number, the signed area of the
    parallelogram formed by the points (0, 0), p1, p2
    and p1 p2 (x1 x2, y1 y2). We can
    describe this number as the determinant of a
    matrix
  • If is positive, then p1 is clockwise
    from p2 with respect to the origin if it is
    negative, then p1 is counterclockwise from p2.
    (Ex. 33.1-1)

6
CompGeom
  • If the vectors are said to be
    collinear.
  • -------
  • Cross product as an area clockwise and
    counterclockwise regions.

7
CompGeom
  • For the relationship between two directed
    segments and first translate so p0 is
    at the origin.
  • p1 p1 - p0 (x1 - x0, y1 - y0) (x1,
    y1) p2 p2 - p0 (x2 - x0, y2 - y0)
    (x2, y2)
  • If the cross product is positive, then is
    clockwise from if negative, it is
    counterclockwise.

8
CompGeom
  • Left and Right Turns. Consider two consecutive
    line segments and . Determine
    whether, in moving from the first to the second,
    one turns left or right ( determine the angle
    p0p1p2). The same picture as in the previous
    slide provides the answer
  • Compute the cross product
    If negative we turn left positive right
    collinear if 0.

9
CompGeom
  • Segment Intersection. This is done in several
    stages. Let and be the given
    segments.
  • We check if each segment straddles the line
    containing the other by straddling we mean that
    one endpoint of the segment is on one side of the
    line, the other on the other side.
  • We check if an endpoint of one segment lies on
    the other segment.
  • It can be seen that we have necessary and
    sufficient conditions for intersection - these
    are geometric conditions which have to be
    translated into analytical ones, with a limited
    set of operations.

10
CompGeom
  • We will use this procedure

11
CompGeom
  • We first compute four directions, which will let
    us determine whether a point to the left or right
    of a segment we move pi to the origin and use
    the vectors pk - pi and pj - pi, with pk - pi
    giving us the direction vector of the point
    outside the segment. If a point is collinear
    with segment, we can check if the point is on the
    segment itself.

12
CompGeom
  • We have to make sure that all possible
    configurations are taken care of, and the next
    slide gives a few of the geometric examples it
    does not exhaust all of them. It is left for the
    reader to complete the set of figures, and make
    sure that the cases examined in line 5 of
    Segments-Intersect cover all possibilities.

13
CompGeom
14
CompGeom
  • Intersections of Pairs of Segments. We are given
    a finite collection of segments and want to
    determine if any two segments in the collection
    intersect.
  • We could solve the problem by checking each
    segment against every other one, which would
    result in an W(n2) algorithm, where n is
    cardinality of the set of segments. We would
    like an algorithm that povides a yes-no answer
    in time as close as possible to linear (the usual
    O(n lg n) will be acceptable).
  • We introduce the notion of a sweep line.

15
CompGeom
  • The sweep line is a vertical line that sweeps
    through the segments of the set from left to
    right, along a dimension. This line will allow
    us to order segments with respect to each other
    and to decide when a segment needs to be
    considered and when it is not relevant to the
    intersection question.

16
CompGeom
  • Assumption there are no vertical segments.
    Since the number of segments is finite, it is
    clear that some direction will not appear. Take
    such a direction as the vertical so this does
    not reduce the generality of the arguments.
  • An implication of this assumption is that any
    segment that intersects a vertical sweep line
    must do so in just one point. This allows us to
    order the segments that intersect the sweep line
    according to the y-coordinate of the
    intersection.
  • Assumption no three segments intersect at the
    same point. Eliminating this assumption would
    just add work

17
CompGeom
  • Some Definitions. Two line segments s1 and s2 are
    said to be comparable at x if the vertical sweep
    line with x-coordinate x intersects both of them.
  • We say that s1 is above s2 at x, denoted by s1 gtx
    s2, if s1 and s2 are comparable at x and the
    intersection of s1 with the sweep line at x is
    higher (larger y-coordinate) than that of s2.
  • Note for any given x, the relation gtx is a total
    order on the segments that intersect the sweep
    line at x.
  • A segment enters the ordering when the sweep line
    meets its left endpoint, and leaves it when it
    encounters the right endpoint.

18
CompGeom
  • What happens when the sweep line sweeps over the
    intersection of two segments the picture below
    tells us that the positions of the two segments
    in the total order become reversed. Since we are
    assuming that no three segments intersect, the
    intersection of the two segments with the sweep
    line must, at some point, be consecutive (no
    other segment - sweep line intersection) (w.r.t.
    gtx) both before intersection and after it.

19
CompGeom
  • We will have to manage (efficiently) two sets of
    data
  • The sweep-line status the relationships among
    the segments intersected by the sweep-lin.
  • The event-point schedule the sequence of
    x-coordinates, from left to right, that defines
    the successive halting positions of the
    sweep-line. Each such halting position is an
    event point.
  • Note changes to sweep-line status (and order of
    segments under the total order) occur only at
    event points (so we have a finite set of event
    points, of cardinality 2n).

20
CompGeom
  • Event-point ordering sort the segment endpoints
    by increasing x-coordinate, from left to right.
    If two segments have the same endpoint (they are
    co-vertical), break the tie by putting all
    covertical left endpoints before the right
    endpoints (what if you did the opposite?).
    Within the set of covertical left endpoints, put
    those with lower y-coordinate first do the same
    with the covertical right endpoints.
  • Insert a segment into the sweep-line status when
    the left endpoint is encountered remove it when
    the right endpoint is encountered.
  • When two segments first become consecutive in the
    total order, check for intersection.

21
CompGeom
  • In order to maintain the total order we need
  • A set T, initially empty.
  • Insert(T, s) insert a segment s into T
  • Delete(T, s) delete a segment s from T
  • Above(T, s) return the segment immediately
    above s in T
  • Below(T, s) return the segment immediately
    below s in T
  • How do we implement this? Because of the total
    order (gtx), we are looking at a (balanced AVL or
    Red-Black) Binary Search Tree all operations
    would have complexity O(lg n).

22
CompGeom
  • The only problem with this is that the
    relationship depends on x the usual total order
    depends on static key comparisons. When you
    Insert, you navigate left or right until you are
    attached as a new leaf when you Delete, you
    Delete either and interior node or a leaf. The
    Above and Below operations would return,
    respectively, the leftmost right descendant and
    the rightmost left descendant (or viceversa) (or
    a parent).
  • Ex. 33.2-2 asks you to extend these ideas to a
    comparison that is based on cross-products you
    can determine whether a segment is clockwise or
    counterclockwise of an existing one (this is a
    static property) - how do you use this to
    determine Above and Below?

23
CompGeom
  • The Algorithm

24
CompGeom
  • If the point p whose segment s is inserted lies
    on another segment s, we have a boundary case
    just place s and s consecutively in T - Above or
    Below does not matter

25
CompGeom
  • Correctness. We need to show that the call
    Any-Segments-Intersect(S) returns TRUE iff there
    is an intersection among the segments of S.
  • If Any-Segments-Intersect(S) returns TRUE then
    there is an intersection between two input
    segments.
  • Pf. If it does return TRUE, then two segments
    must have intersected (we assume that the check
    for intersection between two segments is correct
    that was supposedly resolved earlier).
  • 2. If there is an intersection between two
    segments in S, then Any-Segments-Intersect(S)
    returns TRUE.
  • Pf. This is a little harder

26
CompGeom
  • Suppose there is at least one intersection (the
    case of none will be considered later). Let p be
    the leftmost intersection point, with ties broken
    by choosing the one with the lowest y-coordinate.
    Let a and b be the segments that intersect at p.
    Since no intersections occur to the left of p,
    the order given by T is correct at all points to
    the left of p. The assumption that no three
    segments intersect at p implies that a and b will
    become consecutive in the total order at some
    line with x-coordinate z (to the left of p or
    going through p in the boundary case). There must
    be a segment endpoint q on the sweep-line z that
    is the event-point at which a and b become
    consecutive wrt gtx .

27
CompGeom
  • To see this in more detail, there are,
    essentially, two cases to take care of
  • 1. p is an event point. The two relevant
    configurations are (there is the obvious third
    one, with b ending at p, but it will fall in the
    other case - also recall that insertion is
    processed before deletion)

p
a
b
p
a
b
28
CompGeom
  • This means that, with p on the sweep line z, if
    there is another intersection point p on the
    same sweep line (with y-coordinate higher than p,
    by the ordering), the new event due to p (this
    assumes p IS an event point, too) will not be
    processed until the event due to p will be
    processed, and so cannot interfere with the total
    order. If p is not an event point, nothing else
    will happen.
  • Insert(T, b) will put b adjacent to a in the
    total order, and one of the calls Below(T, b),
    Above(T, b) must produce a, with the subsequent
    check for intersection returning TRUE.

29
CompGeom
  • 2. p is NOT an event point (or is an event point
    for deletion - right endpoint of a segment).
  • This has 2 subcases.
  • 2a. There are no segments in the wedge between
    a and b (and before p) when the latter of the two
    (say b) is picked up by the sweep line. Then b
    (using the point which is the left endpoint of b)
    is inserted into the data structure T as being
    immediately above or below a. One of the
    calls Above(T, b), Below(T, b) returns true,
    triggering the check for Intersection, which
    returns TRUE.

b
p
a
30
CompGeom
  • 2b. There are segments in the wedge between a
    and b (and to the left of p) when the latter of
    the two (say b) is picked up by the sweep line.
    When b is inserted it will be put above (in
    this picture) a pre-existing segment (say c). If
    one of the calls Above(T, b) and Below(T, b)
    returns a segment that intersect b - to the right
    of p by definition of p - the procedure returns
    TRUE. If not, we continue the sweep. At some
    point we reach the rightmost endpoint of the last
    segment interior to the wedge q.

b
c
p
q
a
31
CompGeom
  • At that moment the data structure T contains a,
    b, and c in such a way that a and b are not yet
    immediately adjacent in the total order gtx. When
    c is removed, there are no segments stored in T
    between a and b. When we check for Above(T, c)
    and Below(T, c) immediately before removal of c,
    we must thus produce both a and b. Checking for
    intersection will force a return of TRUE.
  • It should be clear that Case 2 takes care of the
    situation where p is the right endpoint of a
    segment.
  • At no point do we update T unless at an event
    point.

32
CompGeom
  • Note it is the dynamic changes in the data
    structure T at each event point that determine
    how the relationship between segments works out
    the initial positioning of a segment occurs on
    insertion, based on the position of the initial
    point of the segment in relation to the segments
    as stored in the Binary Search Tree (above
    counterclockwise below clockwise). If two
    intersecting segments are not stored adjacent to
    each other, the removal of intermediate
    non-intersecting segments will eventually leave
    the two adjacent. It is right before the last
    removal that the test produces the intersection.
  • Draw more pictures and play with them

33
CompGeom
  • Summarizing Either the event-point q is
    processed by Any-Segments-Intersect or not. If it
    is, there are two possibilities
  • Either a or b is inserted into T. The segment
    not inserted is either above or below of the one
    just inserted.
  • Segments a and b are already in T, and a segment
    between them is deleted from T, making a and b
    consecutive.
  • In either case the intersection is found and the
    procedure returns TRUE.

34
CompGeom
  • If q is not processed, the procedure must have
    returned, on finding a previous intersection.
    This is possible - for example in a configuration
    as shown below.
  • We are now done.

a
c
p
b
q
35
CompGeom
  • Running Time. Line 2 (sorting) takes O(n lg n).
    Since there are 2n points, the loop iterates at
    most 2n times. If we assume we can use a
    Balanced Binary Search Tree, each iteration takes
    O(lg n) for the tree operations, and O(1) for the
    other operations..
  • Total O(n lg n).

36
CompGeom
  • Convex Hulls. The convex hull Q of a set q of
    points is the smallest convex polygon P for which
    each point of Q is either on the bounday of P or
    in its interior. CH(Q) denotes the convex hull of
    Q.
  • We will look at two algorithms (among the many
    available) to compute convex hulls. They will
    have different asymptotic properties one will
    exhibit better behavior for hulls that contain
    many of the points in Q, the other will exhibit
    better behavior for hulls where the points of Q
    are overwhelmingly interior points. The problem
    is that distinguishing the proper case a priori
    may not be realistic.

37
CompGeom
  • Q p0, p1, , p12 and CH(Q), the convex hull.

38
CompGeom
  • Grahams Scan. This algorithm maintains a stack
    of points. The points in the input set are
    checked one-by-one with the two topmost points on
    the stack. Unsuitable points on the stack ( not
    vertices of the hull) are popped and discarded.
    The new point is inserted. At the end, the stack
    contains exactly the vertices in CH(Q), in
    counterclockwise order (traversed from bottom to
    top).
  • To have a convex polygon Q 3.
  • We will need some functions to peek at the top
    elements of the stack without removing them
    Top(S) and Next-To-Top(S)

39
CompGeom
  • The Algorithm

40
CompGeom
  • The polar angle is measured in radians and lies
    in 0,p). You must sort the points by polar angle
    (O(Q lg Q)), after computing the polar angle.
    This can be done by computing the cross-product
    (normalizing for length 1), and observing that
    the cross product (the sine of the angle between
    the normalized vectors) will increase as we move
    counterclowise from a 0-angle to a p /2 one and
    then decrease again to p the first set of points
    have x-coordinate larger than that of p0, the
    second, smaller. We can see that computing polar
    angles will cost O(Q).
  • If two or more points have the same polar angle,
    then use only the one farthest from p0 the
    others must be interior.

41
CompGeom
  • The problem with this approach is that
    normalization will introduce errors and require
    both divisions and the computation of square
    roots. Another (better) approach would be to use
    a Balanced Binary Search Tree insert into the
    right place by just doing clockwise-counterclockw
    ise tests, which can be done without adding
    further numerical instabilities, and then do an
    inorder tree sweep to extract the sorted set of
    points. If two points are on the same line from
    p0, this appears during the comparisons (their
    cross product vanishes), and the farthest one can
    be computed by comparing the cross-products with
    p0. Time O(Q lg Q).

42
CompGeom
  • Illustration of the procedure start from the
    lowest point and try to add points as the angle
    sweeps counterclockwise if concavity is
    introduced, drop the point responsible.

43
CompGeom
  • Continue until done the points dropped from the
    stack will not be on the convex hull.

44
CompGeom
  • Note checking for non-left turn rather than
    right turn lets us drop points that are collinear
    on the edges of the hull - unless a point is a
    left turning point it cannot be a vertex of the
    hull.
  • Theorem 33.1. (Correctness of Grahams Scan). If
    Graham-Scan is run on a set Q of points, Q 3,
    then, at termination, the stack S consists of,
    from bottom to top, exactly the vertices of CH(Q)
    in counterclockwise order.

45
CompGeom
  • Proof. We first have to check that the setup
    part (lines 1-5) does what we want, setting up
    the correct preconditions for the loop. We then
    must identify an appropriate loop invariant,
    and show that it remains invariant as we progress
    through the loop. At termination, we must show
    that the appropriate postcondition (implying the
    validity of the procedure) is satisfied.
  • Setup. After line 2 we have a sequence of points
    ltp1, p2, , pmgt. Define Qi p0, p1, , pi i
    2, 3, , m. The points in Q - Qm are the ones
    removed because they share polar angle with
    others and represent vectors that cannot end on
    the hull (in CH(Q) CH(Qm) ).

46
CompGeom
  • We need to show that, at the end, the stack S
    consists of the vertices of CH(Qm) in
    counterclockwise order from bottom to top. Note
    that p0, p1 and pm are vertices of CH(Qm) CH(Q)
    and that p0, p1 and pi are vertices of CH(Qi).
  • This gives us an idea for the loop invariant at
    the start of each iteration of the loop (lines
    6-9), the stack S consists of, from bottom to
    top, the vertices of CH(Qi-1) in
    counterclockwise order.
  • Loop Precondition
  • We must verify that the loop invariant is true
    right before executing the first pass through the
    loop.

47
CompGeom
  • At that time (line 6) the stack S consists of p0,
    p1 and p2. And this is exactly the convex hull of
    Q3-1 Q2 p0, p1, p2. By construction (the
    sequence of Pushes), they are in counterclockwise
    order from bottom to top.
  • Maintenance of Invariant. Right at the beginning
    of the loop, the top of the stack contains the
    point pi-1 (which could be p2, or a pj with j gt
    2). The while loop checks for non-left turns and
    pops the top of the stack until it finds one (It
    could have an empty body). At the end of the
    while loop, and before pi is pushed, the top of S
    is some pj, with some pk below it (we cant have
    fewer than two elements left in the stack - why?).

48
CompGeom
  • At this point the stack contains exactly the
    points inserted after the jth iteration of the
    for loop. At this point, by the loop invariant, S
    contained exactly the vertices in CH(Qj) in
    counterclockwise order from bottom to top. At
    this point, pis polar angle wrt p0 is greater
    than pjs and the angle pkpjpi is a left turn.
    Pushing pi puts in S exactly the vertices of
    in the correct counterclockwise
    order.

49
CompGeom
  • We need to prove that
    Let pt be any point that was popped on the
    latest (ith) iteration of the for loop, let pr be
    the point just below it on the stack (maybe even
    pj). The angle prptpi makes a non-left turn, and
    the polar angle of pt relative to p0 is greater
    than that of pr. pt must be either interior to
    the triangle p0,pr,pi or on the line segment
    , but is not a vertex of the triangle.

50
CokmpGeom
  • Thus pt cannot be a vertex of CH(Qi), which
    implies that CH(Qi - pt) CH(Qi). If we let Pi
    denote the set of vertices popped during the ith
    iteration of the for loop, we must have CH(Qi -
    Pi) CH(Qi). But , and we
    conclude that
  • We have just shown that, at the very end of the
    ith iteration, the stack contains exactly CH(Qi),
    in counterclockwise order from bottom to top.
  • The invariant now holds for the next iteration.
  • Termination. When the loop ends, i m 1. The
    loop invariant implies that S contains exactly
    the vertices of CH(Qm), in counterclockwise order
    from bottom to top.

51
CompGeom
  • Running time. Let n Q. Line 1 (finding the
    start point) takes time Q(n). Line 2 (sort the
    points in polar angle order) takes O(n lg n) by
    several sorting algorithms. Removing all but the
    farthest point along the same polar line will
    cost O(n). Lines 3-5 take O(1) time (three
    Pushes). The for loop is executed at most n - 3
    times (m n - 1), with one Push on each pass
    (Push is O(1)). Since you cannot Pop what you did
    not Push, an immediate aggregate analysis shows
    that the while loop is executed O(n) times.
  • Total O(n lg n) sorting is the most expensive
    part.

52
CompGeom
  • Jarviss March. This is a different algorithm,
    that uses a technique inspired by how people wrap
    a package. Since it will be seen that the
    algorithm runs in time O(nh), where n Q and h
    CH(Q), it will be a good algorithm in case
    the number of points on the hull is small w.r.t.
    to the total o(lg n).
  • Start as in Grahams Scan with the leftmost
    lowest point p0. We now want to build a sequence
    of points H p0, p1, , ph-1 which will be
    CH(Q). How? Find the point p1 with the smallest
    polar angle w.r.t. p0. If there is more than one
    such, take the farthest one. The same scheme as
    used for Grahams scan will give us the ordering.

53
CompGeom
  • A priority queue would be enough, allowing us to
    obtain the appropriate p1 in time O(n), while
    eliminating all points that are collinear on
    lines from p0, and are not the farthest from p0.
    Once we have p1, we repeat the construction (p1
    is now the origin, so the polar angle is the
    counterclockwise angle from the horizontal at p1)
    to obtain the point p2 , the next point on CH(Q),
    with the smallest positive polar angle w.r.t. p1.
    Again O(n). The only problem is that, as we
    compute the points pi, the y-coordinate increases
    up to a maximum. After that, the angles would
    exceed p, and so, after we add the highest point,
    we begin computing angles from the negative
    x-axis direction.

54
CompGeom
  • Hull points computation and base angle switch.

55
CompGeom
  • It should be clear that, each time, we add a
    point on the Convex Hull, and that we are not
    missing any such points - this would require a
    bit of proof, but is easy to supply show that,
    at each stage, as we add the newly found point
    pi1 to CH(Qi), the directed line between pi, the
    last point added to get CH(Qi), and the newly
    found point pi1 has all other points of Q to
    its left. Thus we have CH(Qi1). Since each
    reconstruction of the priority queue produces a
    hull point, the claimed time complexity follows.

56
CompGeom
  • Closest Pair of Points. Given a finite
    collection of points in the plane, we want to
    find two that are closest. The obvious
    algorithm- compute all pairs of distances and
    pick (one of) the pair(s) associated with the
    smallest - has an asymptotic cost of O(n2).
  • Can we do better? How about divide-and-conquer?
  • Split the set into two roughly equal subsets,
    find (recursively) a closest pair in each, pick
    the pair with the smaller distance and you are
    done or are you? You may have an even closer
    pair where one of the points is in one subset,
    the other in the other, and this approach would
    have missed it Can we fix it - without losing
    the asymptotics?

57
CompGeom
  • Divide and conquer. Let Q be the given set. Let
    be the set of points to be examined. Let X
    and Y be two arrays that contain all the points
    of P. The elements of X are the points of P
    sorted by increasing x-coordinate the elements
    of Y are the points of P sorted by increasing
    y-coordinate. We have to be careful that the
    sorting by coordinate occurs only once.
    Otherwise, sorting by comparisons, we are led to
    the recurrence relation T(n) 2T(n/2) Q(n lg
    n). Ex. 4.4-2 gives that T(n) is in Q(n lg2 n)
    wrong answer

58
CompGeom
  • Termination when the set P has cardinality 3 or
    less, use the dumb algorithm compute all three
    (or one) edges return a pair of points
    associated with the shortest edge length. If P
    1, we must return an error. This latter case
    could occur only at the beginning.
  • Divide if P has cardinality greater than 3 (P
    gt 3), find a vertical line l and divide P into
    two subsets, PL and PR, with
    All the points in PL are either to the
    left of or on the line l, while all points in PR
    are either to the right of or on l. As P is
    divided, so we must divide X and Y into XL, XR,
    YL and YR, where the elements of PL and PR are
    respectively sorted by increasing x and
    y-coordinates.

59
CompGeom
  • Conquer. Make two recursive calls to find a
    closest pair in PL and one in PR. Inputs of the
    first call PL, XL, YL inputs of the second PR,
    XR, YR. Let the closest pair distances returned
    for PL and PR be dL and dR, respectively. Let d
    min(dL, dR).
  • Combine. This is the tricky part -
    procrastinating did not buy us an easy collapse
    of the problem When the two pairs return, a
    closest pair for the combined sets PL and PR is
    either one of the two returning pairs or a pair
    where one of the points belongs to PL and the
    other to PR. The algorithm must determine if such
    a pair, with distance less than d exists.

60
CompGeom
  • If such a pair exists, both points in it must be
    within a distance d of l. From the picture, they
    must both reside in a strip of width 2d centered
    on the line.

61
CompGeom
  • To find them, we proceed as follows
  • Create an array Y containing all the points of
    Y in the 2d-wide vertical strip. The sorting by
    y-coordinate is maintained.
  • Starting from the first point p in Y (with the
    smallest y-coordinate), look for points that
    could be at a distance lt d from it. From picture
    b) on the previous slide, there are at most 7
    other points that could be within d of p compute
    the distances, and move to the next p. At the end
    you may have found a pair with a distance d lt d,
    or not.
  • If d lt d, return the new pair and d, if not,
    return the old pair and d.

62
CompGeom
  • Correctness.
  • Termination occurs as soon as P 3 - we will
    never process recursivley a set of cardinality 1.
  • On return from recursion, the examination of
    each point the 2d-strip around the line l will
    not require comparing more than 7 pairs of points
    and their distances. To prove this, suppose
    that, at some level of the recursion, the closest
    points are and Then d dist(pL,
    pR) lt d. pL must be on l or to the left of l and
    less than d units away pR must be on l or to the
    right of l and less than d units away. Also, pL
    and pR must be within d units of each other
    vertically.

63
CompGeom
  • This implies that pL and pR are in a
    rectangle centered on l. How many points of P can
    reside within this rectangle? Since all points
    are at least d apart, only 4 points of PL can be
    found in the left half of the rectangle, and 4
    points of PR can be found in the right half. The
    points must be, respectively, at the 4 corners of
    the two squares - where we allow for duplicate
    points at the corners that lie on l. Any other
    configuration will have fewer than 8 points. 7
    distance computations and comparisons will
    provide a decision. This needs to be repeated
    for each point in Y.

64
CompGeom
  • Running Time. We have to solve a few problems in
    order to set up a recursion relation with the
    desired asymptotics.
  • 1. Starting from X and Y we must pass sorted
    arrays XL, XR, YL and YR to the recursive calls.
    This is not difficult since X is sorted, finding
    l to define PL and PR with the proper
    cardinalities is easy just split X in the
    middle. This gives XL, XR, PL and PR in linear
    time. Constructing YL and YR can be carried out
    via the pseudo-code on the next slide.

65
CompGeom
  • The time to perform this splitting is clearly
    linear.
  • We can presort the points in X and Y in time O(n
    lg n).
  • 2. We saw that the return from the recursion
    will require O(n) time to process the points near
    the splitting line

66
CompGeom
  • After the initial pre-sorting, the recursion
    relation must be
  • From this we have and the
    presorting doe not change the asymptotics, except
    for the multiplicative constant.
Write a Comment
User Comments (0)
About PowerShow.com