The Python implementation for this algorithm