pattern recognition algo