For homework 6 in Algorithms