Intuitively, the algorithm works as follows: