Introduction to Algorithms, 2nd edition.