Implements the mincut subroutine and outer loop