go implements Algorithms 4