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

The multiplication of a chain of matrices of section 13.3. More...

Files

file  ADS/numeric/MatrixChain.h
 The multiplication of a chain of matrices of section 13.3: \( A_i \) of size \( c[ i ] \times c[ i + 1 ] \), \( 0 \le i < n \), and the order of the products with the fewest scalar multiplications.

Functions

std::pair< long, std::vector< std::vector< std::size_t > > > ADS::matrixChain (const std::vector< long > &c)
 the least cost \( \mathit{DP}[ 0 ][ n - 1 ] \) and the table \( S \) of the splits, for the \( n \) matrices of sizes \( c[ 0 .. n ] \) (the parentesizzazione() of the book)
std::string ADS::parenthesization (const std::vector< std::vector< std::size_t > > &S, std::size_t i, std::size_t j)
 the parenthesization of \( A_i \cdots A_j \) by the splits \( S \), e.g., ((A0·A1)·A2) (the stampaPar() of the book)

Detailed description

The multiplication of a chain of matrices of section 13.3.

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

Function documentation

◆ matrixChain()

std::pair< long, std::vector< std::vector< std::size_t > > > ADS::matrixChain ( const std::vector< long > & c)
inline

the least cost \( \mathit{DP}[ 0 ][ n - 1 ] \) and the table \( S \) of the splits, for the \( n \) matrices of sizes \( c[ 0 .. n ] \) (the parentesizzazione() of the book)

In pseudocode (section 13.3), by increasing length of the chain:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{matrixChain}( c )\quad \triangleright\ A_i \text{ is } c[ i ] \times c[ i + 1 ] \text{, } n = |c| - 1 \cr 2 & \quad \mathit{DP}[ i ][ i ] \gets 0 \text{ for every } i \cr 3 & \quad \textbf{for}\ h \gets 2\ \textbf{to}\ n\ \textbf{do}\quad \triangleright\ \text{the chains of } h \text{ matrices} \cr 4 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ n - h\ \textbf{do} \cr 5 & \qquad\quad j \gets i + h - 1 \cr 6 & \qquad\quad S[ i ][ j ] \gets \text{the } k \in [ i , j ) \text{ that minimizes } t_k = \mathit{DP}[ i ][ k ] + \mathit{DP}[ k + 1 ][ j ] + c[ i ] c[ k + 1 ] c[ j + 1 ] \cr & \qquad\qquad \text{(the first one on a tie)};\ \mathit{DP}[ i ][ j ] \gets t_{S[ i ][ j ]} \cr 7 & \quad \textbf{return}\ ( \mathit{DP}[ 0 ][ n - 1 ] , S ) \end{array}} \]

Precondition
\( c \) is not empty and its items are positive (audit)

◆ parenthesization()

std::string ADS::parenthesization ( const std::vector< std::vector< std::size_t > > & S,
std::size_t i,
std::size_t j )
inline

the parenthesization of \( A_i \cdots A_j \) by the splits \( S \), e.g., ((A0·A1)·A2) (the stampaPar() of the book)

In pseudocode (section 13.3):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{parenthesization}( S , i , j ) \cr 2 & \quad \textbf{if}\ i < j\ \textbf{then return}\ ( \mathrm{parenthesization}( S , i , S[ i ][ j ] ) \cdot \mathrm{parenthesization}( S , S[ i ][ j ] + 1 , j ) ) \cr 3 & \quad \textbf{return}\ \text{the name } A_i \end{array}} \]

Precondition
\( i \le j < n \)