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

Two problems of dynamic programming on a matrix of \( m \) rows and \( n \) columns, each cell solved from its neighbors above and to the left. More...

Files

file  ADS/optimization/GridPaths.h
 Two problems of dynamic programming on a matrix of \( m \) rows and \( n \) columns, each cell solved from its neighbors above and to the left:

Classes

struct  ADS::GridSquare
 a square of a matrix: its side and its upper left cell More...

Functions

template<class T>
GridSquare ADS::largestSquare (const std::vector< std::vector< T > > &A)
 the largest square of nonzero cells of \( A \), the first in the order of the rows of its lower right cell
template<class C = std::uint64_t, class T>
C ADS::countLatticePaths (const std::vector< std::vector< T > > &B)
 the number of the paths from the upper left to the lower right cell of \( B \), each step right or down, through nonzero cells only; 0 for \( B \) empty

Detailed description

Two problems of dynamic programming on a matrix of \( m \) rows and \( n \) columns, each cell solved from its neighbors above and to the left.

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

Function documentation

◆ countLatticePaths()

template<class C = std::uint64_t, class T>
C ADS::countLatticePaths ( const std::vector< std::vector< T > > & B)

the number of the paths from the upper left to the lower right cell of \( B \), each step right or down, through nonzero cells only; 0 for \( B \) empty

In pseudocode (exercise B3 of 07/01/2013, Conta percorsi, of the Esercizi Programmazione dinamica of the book), one row \( N[ 0 .. n - 1 ] \) of the table kept, each entry adding the one on its left:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{countLatticePaths}( B )\quad \triangleright\ N[ j ] \text{ the paths to } ( i , j ) \text{, one row at a time} \cr 2 & \quad N \gets ( 1 , 0 , \ldots , 0 ) \cr 3 & \quad \textbf{foreach}\ \text{cell } ( i , j ) \text{, by rows} \ \textbf{do} \cr 4 & \qquad N[ j ] \gets 0 \text{ if } B[ i ][ j ] = 0 \text{ (a blocked cell), else } N[ j ] + ( N[ j - 1 ] \text{ if } j > 0 ) \cr 5 & \quad \textbf{return}\ N[ n - 1 ] \end{array}} \]

Precondition
\( B \) is a rectangle

◆ largestSquare()

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

the largest square of nonzero cells of \( A \), the first in the order of the rows of its lower right cell

In pseudocode (exercise B3 of 10/09/2012, Max square, of the Esercizi vari of the book), one row \( S[ 1 .. n ] \) of the table kept, \( \mathit{diag} \) the entry above and to the left:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{largestSquare}( A )\quad \triangleright \ S[ j + 1 ] \text{ the side of the largest square ending at } ( i , j ) \cr 2 & \quad S[ 0 .. n ] \gets 0;\ \mathit{best} \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ m - 1\ \textbf{do} \cr 4 & \qquad \mathit{diag} \gets 0\quad \triangleright\ \text{the old } S[ j ] \text{, the cell up and left} \cr 5 & \qquad \textbf{for}\ j \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 6 & \qquad\quad ( u , S[ j + 1 ] ) \gets ( S[ j + 1 ] , 0 \text{ if } A[ i ][ j ] = 0 \text{, else } 1 + \min\{ S[ j ] , S[ j + 1 ] , \mathit{diag} \} ) \cr 7 & \qquad\quad \mathit{diag} \gets u;\ \textbf{if}\ \mathit{best} < S[ j + 1 ]\ \textbf{then}\ \text{record } \mathit{best} \gets S[ j + 1 ] \text{ at } ( i , j ) \cr 8 & \quad \textbf{return}\ \mathit{best} \text{ and the upper left cell of its square} \end{array}} \]

Precondition
\( A \) is a rectangle
Postcondition
all the cells of the square inside \( A \) and nonzero (audit)