Pattern Recognition Algorithm