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

Two problems of dynamic programming on a row of \( n \) houses, where the choice of each house depends only on the one of its neighbor. More...

Files

file  ADS/optimization/HouseProblems.h
 Two problems of dynamic programming on a row of \( n \) houses, where the choice of each house depends only on the one of its neighbor:

Functions

template<class T>
T ADS::maxNonAdjacentSum (const std::vector< T > &A)
 the largest sum of items of \( A \) no two of them adjacent, 0 for none
template<class T>
std::pair< T, std::vector< std::size_t > > ADS::nonAdjacentSelection (const std::vector< T > &A)
 the sum of maxNonAdjacentSum() and the places of its items, increasing; an item is taken only if it makes the sum larger
template<class T>
std::pair< T, std::vector< std::size_t > > ADS::paintHouses (const std::vector< std::vector< T > > &cost)
 the least cost of a painting of the houses, adjacent houses of different colors, \( \mathit{cost}[ h ][ c ] \) for the house \( h \) of the color \( c \), and the colors

Detailed description

Two problems of dynamic programming on a row of \( n \) houses, where the choice of each house depends only on the one of its neighbor.

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

Function documentation

◆ maxNonAdjacentSum()

template<class T>
T ADS::maxNonAdjacentSum ( const std::vector< T > & A)

the largest sum of items of \( A \) no two of them adjacent, 0 for none

In pseudocode (exercise B3 of 17/06/2013, Fundraising, of the Esercizi Programmazione dinamica of the book), the last two entries of the table kept:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxNonAdjacentSum}( A ) \cr 2 & \quad ( \mathit{prev} , \mathit{cur} ) \gets ( 0 , 0 )\quad \triangleright \ \text{the best of the prefixes shorter by two and by one} \cr 3 & \quad \textbf{foreach}\ x \in A\ \textbf{do}\ ( \mathit{prev} , \mathit{cur} ) \gets ( \mathit{cur} , \max\{ \mathit{prev} + x , \mathit{cur} \} ) \cr 4 & \quad \textbf{return}\ \mathit{cur} \end{array}} \]

Postcondition
the result is not negative

◆ nonAdjacentSelection()

template<class T>
std::pair< T, std::vector< std::size_t > > ADS::nonAdjacentSelection ( const std::vector< T > & A)

the sum of maxNonAdjacentSum() and the places of its items, increasing; an item is taken only if it makes the sum larger

In pseudocode (the same exercise), the table read back from its end:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{nonAdjacentSelection}( A[ 0 .. n - 1 ] )\quad \triangleright\ D[ i ] \text{ the best of } A[ 0 .. i - 1 ] \cr 2 & \quad D[ 0 ] \gets 0;\ D[ i ] \gets \max\{ D[ i - 2 ] + A[ i - 1 ] , D[ i - 1 ] \} \text{ for } i = 1 , \ldots , n\quad \triangleright\ D[ -1 ] = 0 \cr 3 & \quad S \gets \emptyset;\ i \gets n \cr 4 & \quad \textbf{while}\ i > 0\ \textbf{do}\quad \triangleright\ A[ i - 1 ] \text{ is taken iff it raised } D \cr 5 & \qquad \textbf{if}\ D[ i ] = D[ i - 1 ]\ \textbf{then}\ i \gets i - 1\ \textbf{else}\ S \gets S \cup \{ i - 1 \};\ i \gets \max\{ i - 2 , 0 \} \cr 6 & \quad \textbf{return}\ ( D[ n ] , S ) \end{array}} \]

Postcondition
no two places adjacent, the items of the sum found

◆ paintHouses()

template<class T>
std::pair< T, std::vector< std::size_t > > ADS::paintHouses ( const std::vector< std::vector< T > > & cost)

the least cost of a painting of the houses, adjacent houses of different colors, \( \mathit{cost}[ h ][ c ] \) for the house \( h \) of the color \( c \), and the colors

In pseudocode (exercise B3 of 02/02/2026, Hateville PNRR, of the Esercizi Programmazione dinamica of the book), \( b \) and \( s \) the colors of the least and of the second least entry of the row before:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{paintHouses}( \mathit{cost} )\quad \triangleright\ D[ h ][ c ] \text{ the best of the houses up to } h \text{, } h \text{ of color } c \cr 2 & \quad D[ 0 ] \gets \mathit{cost}[ 0 ] \cr 3 & \quad \textbf{for}\ h \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad b \gets \text{the color of the least entry of } D[ h - 1 ];\ s \gets \text{that of the second least} \cr 5 & \qquad D[ h ][ c ] \gets \mathit{cost}[ h ][ c ] + D[ h - 1 ][ s \text{ if } c = b \text{, else } b ] \text{ for every color } c\quad \triangleright\ O( k ) \text{ a house} \cr 6 & \quad c_{n-1} \gets \text{the color of the least entry of } D[ n - 1 ] \cr 7 & \quad c_{h-1} \gets \text{the color of the least entry of } D[ h - 1 ] \text{ other than } c_h \text{, for } h = n - 1 , \ldots , 1 \cr 8 & \quad \textbf{return}\ ( D[ n - 1 ][ c_{n-1} ] , c ) \end{array}} \]

Precondition
the rows of the same size \( k \), \( k \ge 2 \) if there are two rows or more, \( k \ge 1 \) if one
Postcondition
adjacent colors differ and give the cost found