Source: https://www.lmd.jussieu.fr/~lguez/Teaching/algorithmics_handout.pdf