Source: https://www.cril.univ-artois.fr/~delima/files/teaching/cm-algo1.pdf