Data structure & algorithm implementation in PHP