The code for the book Algorithm version 4