homework 2 solution of Algorithm 1 in Coursera by Tim Roughgarden