Papadimitriou's algorithm - Implemented whole algorithm.