implemented the algorithm in objective c