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

Monte Carlo algorithms of chapter 17, quick and wrong with a bounded probability, which \( K \) independent runs lower to its \( K \)-th power. More...

Files

file  ADS/algorithm/MonteCarlo.h
 Monte Carlo algorithms of chapter 17, quick and wrong with a bounded probability, which \( K \) independent runs lower to its \( K \)-th power:

Functions

template<class F, class R>
bool ADS::zeroPolynomial (F &&p, std::size_t n, long d, int K, R &rng)
 whether \( p \), of \( n \) variables and total degree \( d \), is identically 0, by \( K \) random points
template<class T, class R>
bool ADS::freivalds (const std::vector< std::vector< T > > &A, const std::vector< std::vector< T > > &B, const std::vector< std::vector< T > > &C, int K, R &rng)
 whether \( A B = C \), by \( K \) runs of \( O( n^2 ) \)
template<class F, class R>
bool ADS::halfSatisfiable (F &&f, std::size_t n, int K, R &rng)
 whether the formula \( f \) of \( n \) variables is true on more than half of the assignments, from \( K \) random ones

Detailed description

Monte Carlo algorithms of chapter 17, quick and wrong with a bounded probability, which \( K \) independent runs lower to its \( K \)-th power.

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

Function documentation

◆ freivalds()

template<class T, class R>
bool ADS::freivalds ( const std::vector< std::vector< T > > & A,
const std::vector< std::vector< T > > & B,
const std::vector< std::vector< T > > & C,
int K,
R & rng )

whether \( A B = C \), by \( K \) runs of \( O( n^2 ) \)

In pseudocode (exercise 17.4, the algorithm of Freivalds):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{freivalds}( A , B , C , K )\quad \triangleright\ \text{whether } A B = C \text{, in } O( K n^2 ) \cr 2 & \quad \textbf{for}\ t \gets 1\ \textbf{to}\ K\ \textbf{do} \cr 3 & \qquad x \gets \text{a vector of } n \text{ signs } \pm 1 \text{ drawn at random} \cr 4 & \qquad \textbf{if}\ C x \ne A ( B x ) \ \textbf{then return false}\quad \triangleright \ \text{a certain answer} \cr 5 & \quad \textbf{return true} \end{array}} \]

Precondition
\( A \), \( B \) and \( C \) are \( n \times n \), and \( K \ge 0 \)

◆ halfSatisfiable()

template<class F, class R>
bool ADS::halfSatisfiable ( F && f,
std::size_t n,
int K,
R & rng )

whether the formula \( f \) of \( n \) variables is true on more than half of the assignments, from \( K \) random ones

In pseudocode (exercise 17.5), a majority of \( K \) samples:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{halfSatisfiable}( f , n , K ) \cr 2 & \quad y \gets \text{the number of the } K \text{ assignments of } n \text{ random bits that satisfy } f \cr 3 & \quad \textbf{return}\ y > K / 2 \end{array}} \]

Precondition
\( K \ge 0 \)

◆ zeroPolynomial()

template<class F, class R>
bool ADS::zeroPolynomial ( F && p,
std::size_t n,
long d,
int K,
R & rng )

whether \( p \), of \( n \) variables and total degree \( d \), is identically 0, by \( K \) random points

In pseudocode (chapter 17, the lemma of Schwartz and Zippel), one-sided: a point where \( p \ne 0 \) proves that \( p \) is not zero:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{zeroPolynomial}( p , n , d , K )\quad \triangleright\ \text{a wrong true with probability } \le 2^{-K} \cr 2 & \quad \textbf{for}\ t \gets 1\ \textbf{to}\ K\ \textbf{do} \cr 3 & \qquad \textbf{if}\ p( x ) \ne 0 \text{ for a point } x \text{ drawn from } \{ 1 , \ldots , 2 \max( d , 1 ) \}^n \ \textbf{then return false} \cr 4 & \quad \textbf{return true} \end{array}} \]

Precondition
\( K \ge 0 \) and \( 2 d \) is a long, i.e., \( d \le \mathrm{LONG\_MAX} / 2 \)