The algorithm used right now for identifying the endpoints to be connected traverses the whole graph to find the ones to connect. This method is really slow. One idea is to use python dictionaries for storing the nodes to be connected while they are being created.