Edge completion/linking algorithm in Python?