Processing an Offline Insertion-Query Sequence with Applications Danny Z. Chen and Haitao Wang Dept. of Computer Science and Engineering University of Notre Dame Indiana, USA
Outline Problem definitions Algorithms Applications
Problem Definitions A sorted number set S={a1,a2,…,an} (a1≤a2≤…≤an) in an interval [L,R]; let a0=L, an+1=R Min-gap: min0<i<n+2{ai-ai-1} Max-gap: max0<i<n+2{ai-ai-1} Max-gap L R Min-gap
Problem Definitions (cont.) Min-G-Ins (min-gap of insertions): Input: S, and S0={L,R}, and an offline sequence of insertion operations op1,op2,…,opn; each opj produces a set Sj from Sj-1 by inserting into Sj-1 a number from S\Sj-1 Output: the min-gapδi in Si after every insertion opi
Problem Definitions (cont.) Min-G-Dels (min-gap of deletions): Input: S0=S∪{L,R}, an offline sequence of deletions Max-G-Ins (max-gap of insertions): Output: max-gap of every Sj after each insertion Max-G-Dels (max-gap of deletions)
Problem Definitions (cont.) Ins-Query (insertions with queries): Input: S and X={x1,x2,…,xm} (x1≤x2≤…≤xm) (m=O(n)) in [L,R]; S0={L,R}; an offline mixed sequence of n insertions on S and m queries on X Output: For each query with query value xj, report its predecessor, pre(Si, xi), and its successor, suc(Si, xj), where Si is the set produced by all insertions preceding the query. Del-Query (deletions with queries): All insertions above are deletions; S0=S∪{L,R}.
Previous Work For all problems: O(nlog n) time, by balanced binary tree O(nloglog n) time, by integer data structure (because the universal set is S)
A Related Problem A problem of Aho, Hopcroft and Ullman: Input: S=Ø, and an offline sequence of insertion and extract-min operations, where each insertion inserts an integer i in [1,n] into S, and each extract-min finds the minimum value in S and remove it; each integer in [1,n] is inserted at most once. Output: The integers returned by all extract-min operations. Solution: Solvable in linear time. Different from our problem in the following sense: Our problems are not restricted to integers. The output in our problems is min-gap rather than the minimum number. There are queries in our problems. Use different algorithmic techniques.
Our Solutions Linear time for all problems By geometric modeling and the application of Gabow and Tarjan’s special case linear time union-find data structure
Min-G-ins (min-gap of insertions) Geometric modeling: If the j-th insertion is on ai, then create a point pi=(ai,j) in the plane For each point pi, create a line segment by connecting pi to the horizontal line y=n+1 y j x ai
An Example of Geometric Modeling y (a4,6) (a5,5) (a2,4) (a6,3) (a1,2) (a3,1) x
An Observation For each point pi, if we shoot a horizontal ray to its left, let the x-coordinate of the segment which is hit first by the ray, called hit segment, be li. If the j-th insertion is on ai, then the predecessor of ai in Sj-1, pre(Sj-1,ai), is li
An Example of the Observation y (a4,6) L (a5,5) (a2,4) R (a6,3) (a1,2) (a3,1) x
An Observation (cont.) For each point pi, if we shoot a ray to its right, let the x-coordinate of the hit segment be ri. If the j-th insertion is on ai, then the successor of ai in Sj-1, suc(Sj-1,ai), is ri.
An Example of the Observation (cont.) y (a4,6) L (a5,5) (a2,4) R (a6,3) (a1,2) (a3,1) x
An Observation (cont.) The insertion of ai produces two (smaller) gaps ai-pre(Sj-1,ai) and suc(Sj-1,ai)-ai Suppose we know δi-1; then the min-gap of Sj is min{δi-1 , ai-pre(Sj-1,ai), suc(Sj-1,ai)-ai} the insertion of ai pre(Sj-1,ai) suc(Sj-1,ai)
An Observation (cont.) Key issue: Compute li and ri for each ai Our approach: A simple sweeping from left to right using a stack.
The Algorithm Let bi be the length of the segment induced by pi, and let b0=bn+1=n+1. For each bi, 0<i<n+1, compute li=ak1 where k1=max{ t | t<i and bt>bi} and ri=ak2 where k2=min{ t | t>i and bt>bi} For each i, compute ci=min{ai-li,ri-ai} Set δ0=R-L. For j=1 to n, compute δj=min{δj-1,cg(j)}, where g(j) is the index of the number inserted in the j-th insertion
Ins-Query (Insertions with queries) Geometric modeling: If the j-th operation is on ai, then create a data point pi=(ai,j) in the plane If the j-th operation is on a query value xi, then create a query point qi=(xi,j) in the plane For each data point, create a vertical segment by connecting it to the horizontal line y=m+n+1
An Example of Geometric Modeling y (x2,6) (a3,5) (x1,4) (a4,3) (a1,2) (a2,1) x
An Observation For each query point qj, if we shoot a horizontal ray to its left, then the x-coordinate of the hit segment is the predecessor of xi; if we shoot a horizontal ray to its right, then the x-coordinate of the hit segment is the successor of xi; It is sufficient to solve the ray shooting problem.
An Example of the Ray Shooting
A Sweeping Algorithm Sweep the plane by a horizontal line from y=m+n+1 to y=0 while maintaining all segments intersecting the sweeping line. All points are events Data point event: remove the corresponding segment Query point event: search its hit segment Key issue: A dynamic data structure is needed to maintain all the segments intersecting the sweeping line and to search the hit segment for each query point
A Solution Maintaining the segments intersecting the sweeping line is equivalent to maintaining a set of sub-intervals on [L,R] Gabow and Tarjan’s linear time special case union-find data structure can be applied.
sweeping line moving downward Initially sweeping line moving downward y (a3,6) (a4,5) (x2,4) (a1,3) (x1,2) (a2,1) x Interval set: {[L,a1],[a1,a2] ,[a2,a3],[a3,a4],[a4,R]} x1 is in [a1,a2] and x2 is in [a3,a4]
A Data Point (a3,6) is Swept y (a3,6) (a4,5) (x2,4) (a1,3) (x1,2) (a2,1) x Interval set: {[L,a1],[a1,a2] ,[a2,a4],[a4,R]} x1 is in [a1,a2] and x2 is in [a2,a4]
A Data Point (a4,5) is Swept y (a3,6) (a4,5) (x2,4) (a1,3) (x1,2) (a2,1) x Interval set: {[L,a1],[a1,a2] ,[a2,R]} x1 is in [a1,a2] and x2 is in [a2,R]
A Data Point (x2,4) is Swept y (a3,6) (a4,5) (x2,4) (a1,3) (x1,2) (a2,1) x Interval set: {[L,a1],[a1,a2] ,[a2,R]} x2 is in [a2,R], so its predecessor is a2 and its successor is R
Other Problems The problems Min-G-Del, Max-G-Ins, Max-G-Del and Del-Query can all be solved in linear time by slight modifications on the previous two algorithms.
Applications Horizontal ray shooting Given a set S of n vertical line segments and a set P of n points in the plane, the segments and points are sorted in two lists by their x-coordinates and y-coordinates, respectively Shoot a horizontal ray from each point in P to its left and report the segment in S hit first by the ray Our solution: O(nlog k) time by applying our algorithm on Ins-Query problem and using interval tree data structure, where k is the minimum number of cliques of the corresponding interval graph for the input vertical segments.
Horizontal Ray Shooting
Applications (cont.) Shortest Arc Covering Given a set P of n positions on a circle in a cyclic order, and an offline sequence of insertions each of which inserts a point at an available position (the position then becomes unavailable) Report the shortest circular arc which covers all points on the circle after each insertion. Our solution: O(n) time by reducing the problem to the Max-G-Ins problem.
Shortest Arc Covering
Thank you!