Binary graph cuts, Algorithm 12.1 in Simon J.D. Prince's Algorithm Booklet