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

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

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <limits>
#include <random>
#include <vector>
#include "ADS/algorithm/impl/MonteCarlo.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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:

  • zeroPolynomial(), whether a polynomial of total degree \( d \), given as a function of its \( n \) variables, is identically 0: by the lemma of Schwartz and Zippel a random point of \( \{ 1 , \ldots , 2 d \}^n \) is a root of a nonzero one with probability at most \( 1 / 2 \); the answer zero may be wrong, the answer not zero never;
  • freivalds(), whether \( A B = C \) for \( n \times n \) matrices: \( A ( B x ) \) against \( C x \) for a random \( x \) of \( \pm 1 \), \( O( n^2 ) \) per run instead of the product, wrong with probability at most \( 1 / 2 \) when \( A B \ne C \) (exercise 17.4);
  • halfSatisfiable(), whether a boolean formula of \( n \) variables is true on more than half of the assignments, from the fraction of the true ones among \( K \) random assignments (exercise 17.5): by the bound of Hoeffding it errs with probability at most \( e^{-2 K \epsilon^2} \) when the true fraction is \( 1 / 2 + \epsilon \) or \( 1 / 2 - \epsilon \).

The contracts [see Contract.h]: \( K \ge 0 \) repetitions, zeroPolynomial() needs a degree \( d \) whose \( 2 d \) is a long, and freivalds() needs three \( n \times n \) matrices. The functions are defined in MonteCarlo.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 17 and exercises 17.4 and 17.5, https://algoritmicamente.it/materiali/libro/capitoli/cap17.pdf
J. T. Schwartz, Fast probabilistic algorithms for verification of polynomial identities, Journal of the ACM 27(4), 701-717, 1980, doi:10.1145/322217.322225
R. Zippel, Probabilistic algorithms for sparse polynomials, EUROSAM 1979, LNCS 72, 216-226, 1979, doi:10.1007/3-540-09519-5_73
R. Freivalds, Probabilistic machines can use less running time, IFIP Congress 1977, 839-842
W. Hoeffding, Probability inequalities for sums of bounded random variables, Journal of the American Statistical Association 58(301), 13-30, 1963, doi:10.1080/01621459.1963.10500830
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa