The first code was: - first bridge, then connect, which cuses the problem - bridged edges are connected. But - first connect, then bridge. If the script can't connect, then it will try to bridge. If the script can connect, then it is impossible to bridge. Most important is to have the proper logic of how the things must…