After locating a happy clique C in we need to search it for a node with a maximum number of unoriented neighbors. In this section we give an algorithm that performs this task.
Let be the intervals in Cordered in increasing left endpoint order. Clearly, . Thus the endpoints of the j vertices in C partition the line into 2j+1 disjoint intervals , where , Il=(L(l),L(l+1)] for , Ij=(L(j),R(1)], Il=(R(l-j),R(l-j+1)] for j < l < 2jand . The algorithm consists of the following three stages.
Stage 1:
Let e be an unoriented node
that has a non-empty intersection
with the interval
[L(1),R(j)]. Mark each of e's endpoints
with the index of the interval that contains it.
Stage 2:
Let o be an array of j counters, each corresponding
to a node in C.
The intention is to assign values to o such that
the sum
is the unoriented degree
of the node .
The counters are initialized to zero. For each unoriented node e that overlaps
with the interval
[L(1),R(j)] we change at most four of the
counters as follows.
Let Il and Ir be the intervals in which L(e) and R(e)occur, respectively. We may assume l < r as otherwise eis not adjacent to any node in C and we can ignore it.
We continue according to one
of the
following cases.
Case 1:
.
All the vertices from el+1 to
er are adjacent to e:
we increment o[l+1] and decrement o[r+1] (if r<j).
Case 2:
.
All the vertices from el-j+1 to er-jare adjacent to e:
we increment o[l-j+1] and decrement o[r-j+1] (if r < 2j).
Case 3:
l < j and j < r.
Let
.
If m > 0 then
all the vertices from e1 to em are adjacent to e:
we increment o[1] and decrement o[m+1].
Similarly let
.
If M < j then the vertices
from el+1 to ej are adjacent to e: we increment the counter
o[l+1].
Stage 3: Compute
.
Return ef.
The following theorem summarizes the result of this section.
The complexity of the algorithm is proportional to the size of C plus the number of unoriented nodes in , and hence, it is O(n).