Implemented Tarjan's algorithm