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

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

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <limits>
#include <string>
#include <utility>
#include <vector>
#include "ADS/numeric/impl/MatrixChain.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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: \( 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.

The number of parenthesizations is the number of Catalan \( C_{n-1} \) [see Catalan.h], \( \Omega( 4^n / n^{3/2} ) \), but

\[ \mathit{DP}[ i ][ j ] = \min_{i \leq k < j} \{ \mathit{DP}[ i ][ k ] + \mathit{DP}[ k + 1 ][ j ] + c[ i ] c[ k + 1 ] c[ j + 1 ] \} , \qquad \mathit{DP}[ i ][ i ] = 0 , \]

filled by increasing length \( h = j - i + 1 \) in \( O( n^3 ) \) time, with \( S[ i ][ j ] \) the best \( k \), from which parenthesization() (the stampaPar() of the book) writes the parenthesization in \( O( n ) \).

The contracts [see Contract.h]: the vector of the sizes \( c_0 , \ldots , c_n \) has \( n + 1 \ge 1 \) positive items, and the parenthesization needs \( i \le j < n \). The functions are defined in MatrixChain.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 13.3, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
T. C. Hu, M. T. Shing, Computation of matrix chain products, part I, SIAM Journal on Computing 11(2), 362-373, 1982, doi:10.1137/0211028
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa