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

Greedy algorithms on intervals \( [ a_i , b_i ) \) of the line. More...

Files

file  ADS/optimization/Intervals.h
 Greedy algorithms on intervals \( [ a_i , b_i ) \) of the line:

Functions

std::vector< std::size_t > ADS::independentSet (const std::vector< std::pair< long, long > > &I)
 the places of a largest set of disjoint intervals \( [ a , b ) \), in increasing order
std::vector< int > ADS::intervalColoring (const std::vector< std::pair< long, long > > &I)
 the colors, from 0, of the intervals \( [ a , b ) \), the fewest possible
std::vector< double > ADS::towers (std::vector< double > k, double d)
 the fewest towers such that each house is within \( d \) of one (the torri() of the book)

Detailed description

Greedy algorithms on intervals \( [ a_i , b_i ) \) of the line.

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

Function documentation

◆ independentSet()

std::vector< std::size_t > ADS::independentSet ( const std::vector< std::pair< long, long > > & I)
inline

the places of a largest set of disjoint intervals \( [ a , b ) \), in increasing order

In pseudocode (section 14.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{independentSet}( I ) \cr 2 & \quad S \gets \emptyset;\ e \gets -\infty\quad \triangleright\ e \text{ the right end of the last interval taken} \cr 3 & \quad \textbf{foreach}\ [ a , b ) \in I \text{, by increasing } b \ \textbf{do}\ \textbf{if}\ e \le a\ \textbf{then}\ S \gets S \cup \{ [ a , b ) \};\ e \gets b \cr 4 & \quad \textbf{return}\ S\quad \triangleright \ \text{the earliest end first leaves the most room} \end{array}} \]

Precondition
\( a < b \) for each interval
Postcondition
the intervals chosen are disjoint (audit)

◆ intervalColoring()

std::vector< int > ADS::intervalColoring ( const std::vector< std::pair< long, long > > & I)
inline

the colors, from 0, of the intervals \( [ a , b ) \), the fewest possible

In pseudocode (exercise 14.6), the free colors on a stack \( F \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intervalColoring}( I )\quad \triangleright\ \mathit{free} \text{ the colors released, } \mathit{used} \text{ those made} \cr 2 & \quad \mathit{free} \gets \text{an empty stack};\ \mathit{used} \gets 0 \cr 3 & \quad \textbf{foreach}\ \text{end of an interval } i \text{, by position, a right end before a left one}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \text{it is the right end}\ \textbf{then} \ \mathrm{push}( \mathit{free} , \mathit{color}[ i ] )\quad \triangleright\ i \text{ is over} \cr 5 & \qquad \textbf{else if}\ \mathit{free} = \emptyset\ \textbf{then} \ \mathit{color}[ i ] \gets \mathit{used};\ \mathit{used} \gets \mathit{used} + 1 \cr 6 & \qquad \textbf{else}\ \mathit{color}[ i ] \gets \mathrm{pop}( \mathit{free} ) \cr 7 & \quad \textbf{return}\ \mathit{color} \end{array}} \]

Precondition
\( a < b \) for each interval
Postcondition
intersecting intervals have different colors (audit)

◆ towers()

std::vector< double > ADS::towers ( std::vector< double > k,
double d )
inline

the fewest towers such that each house is within \( d \) of one (the torri() of the book)

In pseudocode (exercise 14.7):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{towers}( k , d )\quad \triangleright \ \text{a tower covers } d \text{ on each side} \cr 2 & \quad S \gets () \cr 3 & \quad \textbf{foreach}\ x \in k \text{, by increasing value} \ \textbf{do}\quad \triangleright \ \text{the leftmost point still uncovered} \cr 4 & \qquad \textbf{if}\ S = ()\ \textbf{or}\ \text{the last tower of } S + d < x\ \textbf{then}\ \text{append } x + d \text{ to } S \cr 5 & \quad \textbf{return}\ S \end{array}} \]

Precondition
\( d \ge 0 \)
Postcondition
each house within \( d \) of a tower (audit)