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

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...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <utility>
#include <vector>
#include "ADS/optimization/impl/HouseProblems.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 largest sum of the items of \( A \), no two of them adjacent (the donations of houses no two of which are neighbors, the maximum weight independent set of a path): \( D[ i ] = \max( D[ i - 1 ] , D[ i - 2 ] + A_{i-1} ) \), \( D[ 0 ] = D[ -1 ] = 0 \); maxNonAdjacentSum() in \( \Theta( n ) \) time and \( O( 1 ) \) space, nonAdjacentSelection() with the places read back from the table, \( \Theta( n ) \) space. It is the weighted intervals of section 13.2 [see WeightedIntervals.h] with the unit intervals \( [ i , i + 2 ) \);
  • paintHouses(): the cheapest painting of the houses with \( k \) colors, \( \mathit{cost}[ h ][ c ] \) for the house \( h \) of color \( c \), two adjacent houses of different colors: \( D[ h ][ c ] = \mathit{cost}[ h ][ c ] + \min_{p \ne c} D[ h - 1 ][ p ] \), which is \( O( n k^2 ) \) as written; keeping of each row its least value, its color and its second least value, the least over \( p \ne c \) is one of the two, and the time is \( O( n k ) \), the colors read back from the table of \( O( n k ) \) space.

Both are shortest paths in the DAG of the states (house, choice), and generalize to a tree of houses by the same recurrence on the children.

The contracts [see Contract.h]: the rows of cost have the same number \( k \) of colors, \( k \ge 2 \) for two houses or more; the places chosen are increasing and not adjacent, the colors of adjacent houses differ, and both give the value found. The functions are defined in HouseProblems.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 13.2 and the Esercizi Programmazione dinamica, exercises B3 of 17/06/2013 and of 02/02/2026, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
J. Kleinberg, E. Tardos, Algorithm Design, Addison-Wesley, 2005, section 6.1
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa