|
ADS
Algorithms and Data Structures
|
The independent set of weighted intervals of section 13.2. More...
Files | |
| file | ADS/optimization/WeightedIntervals.h |
| The independent set of weighted intervals of section 13.2: among \( n \) intervals \( [ a_i , b_i ) \), open on the right, of weights \( w_i \), a set of disjoint ones of largest weight. | |
Classes | |
| struct | ADS::WeightedInterval< W > |
| an interval \( [ a , b ) \) of weight \( w \) More... | |
Functions | |
| template<class W> | |
| std::pair< std::vector< std::size_t >, W > | ADS::maxinterval (const std::vector< WeightedInterval< W > > &I) |
| the places of the intervals of a largest independent set, in increasing order, and its weight | |
The independent set of weighted intervals of section 13.2.
The definitions, the invariants and the costs of the area are in Theory of optimization.
| std::pair< std::vector< std::size_t >, W > ADS::maxinterval | ( | const std::vector< WeightedInterval< W > > & | I | ) |
the places of the intervals of a largest independent set, in increasing order, and its weight
In pseudocode (section 13.2), the intervals numbered from 1 by increasing right end:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxinterval}( I ) \cr 2 & \quad \text{number the intervals } 1 , \ldots , n \text{ by increasing right end};\ h \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}\ h \gets \max\{ h , i \}\ \textbf{else}\ \mathit{pred}[ i ] \gets h \cr 5 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do}\quad \triangleright\ \mathit{pred}[ i ] \text{ the last interval ending before } i \text{ starts} \cr 6 & \qquad \mathit{DP}[ i ] \gets \max\{ \mathit{DP}[ i - 1 ] , w_i + \mathit{DP}[ \mathit{pred}[ i ] ] \}\quad \triangleright \ \mathit{DP}[ 0 ] = 0 \cr 7 & \quad S \gets \emptyset;\ i \gets n \cr 8 & \quad \textbf{while}\ i > 0\ \textbf{do}\quad \triangleright\ i \text{ is taken iff skipping it is worse} \cr 9 & \qquad \textbf{if}\ w_i + \mathit{DP}[ \mathit{pred}[ i ] ] > \mathit{DP}[ i - 1 ]\ \textbf{then}\ S \gets S \cup \{ i \};\ i \gets \mathit{pred}[ i ]\ \textbf{else}\ i \gets i - 1 \cr 10 & \quad \textbf{return}\ ( S , \mathit{DP}[ n ] ) \end{array}} \]