Improve the speed of the bfs-spanning-forest algorithm