Added improved algorithm in algo2.py