Graph algorithm with reduced memory footprint.