ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
IntervalCover.h

The cover of a segment by intervals. More...

Files

file  ADS/optimization/IntervalCover.h
 The cover of a segment by intervals: among \( n \) closed intervals \( [ a_i , b_i ] \), the fewest whose union contains the segment \( [ \mathit{lo} , \mathit{hi} ] \), e.g., the sprinklers at \( D_i \) of radius \( R_i \), of intervals \( [ D_i - R_i , D_i + R_i ] \), that water a lawn \( [ 0 , L ] \).

Functions

template<class T>
std::optional< std::vector< std::size_t > > ADS::minimumIntervalCover (const std::vector< std::pair< T, T > > &I, T lo, T hi)
 the places of the fewest closed intervals \( I \) whose union contains \( [ \mathit{lo} , \mathit{hi} ] \), by increasing left end; nothing if there is none

Detailed description

The cover of a segment by intervals.

The definitions, the invariants and the costs of the area are in Theory of optimization.

Function documentation

◆ minimumIntervalCover()

template<class T>
std::optional< std::vector< std::size_t > > ADS::minimumIntervalCover ( const std::vector< std::pair< T, T > > & I,
T lo,
T hi )

the places of the fewest closed intervals \( I \) whose union contains \( [ \mathit{lo} , \mathit{hi} ] \), by increasing left end; nothing if there is none

In pseudocode (a greedy algorithm of chapter 14, beyond the book), \( x \) the end of the part covered so far:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minimumIntervalCover}( I , \mathit{lo} , \mathit{hi} ) \cr 2 & \quad \text{sort } I \text{ by left end};\ x \gets \mathit{lo};\ S \gets \text{the empty sequence} \cr 3 & \quad \textbf{while}\ x < \mathit{hi}\ \textbf{do} \cr 4 & \qquad [ a , b ] \gets \text{the interval with } a \le x \text{ that } \text{ends farthest, not taken before} \cr 5 & \qquad \textbf{if}\ \text{there is none}\ \textbf{or}\ b \le x \ \textbf{then return nil} \cr 6 & \qquad x \gets b;\ S \gets S \cdot ( [ a , b ] ) \cr 7 & \quad \textbf{return}\ S \end{array}} \]

Precondition
\( \mathit{lo} < \mathit{hi} \), and \( a \le b \) for each interval \( [ a , b ] \)
Postcondition
the intervals cover \( [ \mathit{lo} , \mathit{hi} ] \) (audit)