solve step(9) in algorithm6.1 more efficiently