It is easy to see that the area`∩Uof each corresponding line in the primal plane can be computed inO(1)time.
So, all that is needed is to compute the number of points ofS lying below each such line. In the dual plane, the corresponds to determining the number of dual lines that lie below or above each vertex in the arrangement.
If we know the number of dual lines that lie strictly above each vertex in the arrangement, then it is trivial to compute the number of lines that lie below by subtraction.
We define a point to be atlevelk, denotedLk, in an arrangement if there are at mostk−1lines above this point and at mostn−klines below this point. Thek-th level of an arrangement is anx-monotone polygonal curve, as shown below. For example, the upper envelope of the lines is level 1 of the arrangement, and the lower envelope is levelnof the arrangement. Note that a vertex of the arrangement can be on multiple levels. (Also note that our definition of level is exactly one greater than our text’s definition.)
L3
L5
L1
Figure 75: Levels in an arrangement.
We claim that it is an easy matter to compute the level of each vertex of the arrangement (e.g. by plane sweep).
The initial levels at x = −∞are determined by the slope order of the lines. As the plane sweep proceeds, the index of a line in the sweep line status is its level. Thus, by using topological plane sweep, inO(n2)time we can compute the minimum and maximum level number of each vertex in the arrangement. From the order reversing property, for each vertex of the dual arrangement, the minimum level number minus 1 indicates the number of primal points that lie strictly below the corresponding primal line and the maximum level number is the number of dual points that lie on or below this line. Thus, given the level numbers and the fact that areas can be computed inO(1)time, we can compute the discrepancy inO(n2)time andO(n)space, through topological plane sweep.
Proof: We show that any pathπthat violates these conditions can be replaced by a slightly shorter path from stot. Since the obstacles are polygonal, if the path were not a polygonal curve, then there must be some pointpin the interior of free space, such that the path passing throughpis not locally a line segment. If we consider any small neighborhood aboutp(small enough to not containsortor any part of any obstacle), then since the shortest path is not locally straight, we can shorten it slightly by replacing this curved segment by a straight line segment jointing one end to the other. Thus,πis not shortest, a contradiction.
Thusπis a polygonal path. Suppose that it contained a vertexvthat was not an obstacle vertex. Again we consider a small neighbor hood aboutvthat contains no part of any obstacle. We can shorten the path, as above, implying thatπis not a shortest path.
From this it follows that the edges that constitute the shortest path must travel betweensandtand vertices of the obstacles. Each of these edges must have the property that it does not intersect the interior of any obstacle, implying that the endpoints must be visible to each other. More formally, we say that two pointspandqare mutually visibleif the open line segment joining them does not intersect the interior of any obstacle. By this definition, the two endpoints of an obstacle edge are not mutually visible, so we will explicitly allow for this case in the definition below.
Definition: Thevisibility graphofsandtand the obstacle set is a graph whose vertices aresandtthe obstacle vertices, and verticesvandware joined by an edge ifvandware either mutually visible or if(v, w)is an edge of some obstacle.
s s
t t
Figure 76: Visibility graph.
It follows from the above claim that the shortest path can be computed by first computing the visibility graph and labeling each edge with its Euclidean length, and then computing the shortest path by, say, Dijkstra’s algorithm (see CLR). Note that the visibility graph is not planar, and hence may consist ofΩ(n2)edges. Also note that, even if the input points have integer coordinates, in order to compute distances we need to compute square roots, and then sums of square roots. This can be approximated by floating point computations. (If exactness is important, this can really be a problem, because there is no known polynomial time procedure for performing arithmetic with arbitrary square roots of integers.)
Computing the Visibility Graph: We give anO(n2)procedure for constructing the visibility graph ofnline seg-ments in the plane. The more general task of computing the visibility graph of an arbitrary set of polygonal obstacles is a very easy generalization. In this context, two vertices are visible if the line segment joining them does not intersect any of the obstacle line segments. However, we allow each line segment to contribute itself as an edge in the visibility graph. We will make the general position assumption that no three vertices are collinear, but this is not hard to handle with some care. The algorithm isnotoutput sensitive. Ifkdenotes the number of edges in the visibility graph, then anO(nlogn+k)algorithm does exist, but it is quite complicated.
The text gives anO(n2logn)time algorithm. We will give anO(n2)time algorithm. Both algorithms are based on the same concept, namely that of performing an angular sweep around each vertex. The text’s algorithm operates by doing this sweep one vertex at a time. Our algorithm does the sweep for all vertices simultaneously.
We use the fact (given in the lecture on arrangements) that this angular sort can be performed for all vertices in O(n2)time. If we build the entire arrangement, this sorting algorithm will involveO(n2)space. However it can be implemented inO(n)space using an algorithm calledtopological plane sweep. Topological plane sweep
provides a way to sweep an arrangement of lines using a “flexible” sweeping line. Because events do not need to sorted, we can avoid theO(logn)factor, which would otherwise be needed to maintain the priority queue.
Here is a high-level intuitive view of the algorithm. First, recall the algorithm for computing trapezoidal maps.
We shoot a bullet up and down from every vertex until it hits its first line segment. This implicitly gives us the vertical visibility relationships between vertices and segments. Now, we imagine that angleθcontinuously sweeps out all slopes from−∞to+∞. Imagine that all the bullet lines attached to all the vertices begin to turn slowly counterclockwise. If we play the mind experiment of visualizing the rotation of these bullet paths, the question is what are the significant event points, and what happens with each event? As the sweep proceeds, we will eventually determine everything that is visible from every vertex in every direction. Thus, it should be an easy matter to piece together the edges of the visibility graph as we go.
Figure 77: Visibility graph by multiple angular sweep.
Let us consider this “multiple angular sweep” in greater detail.
It is useful to view the problem both in its primal and dual form. For each of the 2nsegment endpointsv = (va, vb), we consider its dual linev∗:y=vax−vb. Observe that a significant event occurs whenever a bullet path in the primal plane jumps from one line segment to another. This occurs whenθreaches the slope of the line joining two visible endpointsvandw. Unfortunately, it is somewhat complicated to keep track of which endpoints are visible and which are not (although if we could do so it would lead to a more efficient algorithm).
Instead we will take events to beallanglesθbetween two endpoints, whether they are visible or not. By duality, the slope of such an event will correspond to thea-coordinate of the intersection of dual linesv∗andw∗in the dual arrangement. (Convince yourself of this.) Thus, by sweeping the arrangement of the2ndual lines from left-to-right, we will enumerate all the slope events in angular order.
Next let’s consider what happens at each event point. Consider the state of the angular sweep algorithm for some slopeθ. For each vertexv, there are two bullet paths emanating fromvalong the line with slopeθ. Call one theforward bullet pathand the other thebackward bullet path. Letf(v)andb(v)denote the line segments that these bullet paths hit, respectively. If either path does not hit any segment then we store a special null value. Asθvaries the following events can occur. Assuming (through symbolic perturbation) that each slope is determined by exactly two lines, whenever we arrive at an events slopeθthere are exactly two verticesvandw that are involved. Here are the possible scenarios:
Same segment: Ifvandware endpoints of the same segment, then they are visible, and we add the edge(v, w) to the visibility graph.
v
w v
w
f(v) v w f(v) (new)
f(v)(old)
entry
v w f(v) (new)
same invisible exit
f(v)(old)
Figure 78: Possible events.
Invisible: Consider the distance fromvtow. First, determine whetherwlies on the same side asf(v)orb(v).
For the remainder, assume that it isf(v). (The case ofb(v)is symmetrical).
Compute the contact point of the bullet path shot fromvin directionθwith segmentf(v). If this path hits f(v)strictly beforew, then we know thatwis not visible tov, and so this is a “non-event”.
Segment entry: Consider the segment that is incident tow. Either the sweep is just about to enter this segment or is just leaving it. If we are entering the segment, then we setf(v)to this segment.
Segment exit: If we are just leaving this segment, then the bullet path will need to shoot out and find the next segment that it hits. Normally this would require some searching. (In particular, this is one of the reasons that the text’s algorithm has the extraO(logn)factor—to perform this search.) However, we claim that the answer is available to us inO(1)time.
In particular, since we are sweeping overwat the same time that we are sweeping overv. Thus we know that the bullet extension fromwhitsf(w). All we need to do is to setf(v) =f(w).
This is a pretty simple algorithm (although there are a number of cases). The only information that we need to keep track of is (1) a priority queue for the events, and (2) thef(v)andb(v)pointers for each vertexv. The priority queue is not stored explicitly. Instead it is available from the line arrangement of the duals of the line segment vertices. By performing a topological sweep of the arrangement, we can process all of these events in O(n2)time.