difference algorithm