

Что пытаюсь сделать: установить кратчайший путь между вершиной и циклическим графом, при условии, что эта вершина либо уже в циклическом графе, либо соединена с ним напрямую, то есть через вершину, принадлежащую циклу, либо через другие вершины, которые таким же образом могут быть связаны с графом и находятся в списке вершин, которые с графом связываться могут.
По-моему, я неверно расставила return. Что не так? Как правильно?
Вот мой код, версия в который раз исправленная и дополненная:
arrWhatToConnect = ['F', 'G', 'N', 'X2', 'X3', 'K3', 'K1', 'K2', 'E'] #THERE IS NO X8!!!!! arrWithWhatToConnect = [['A', 'B', 'C', 'D', 'E'], ['Z', 'P', 'L', 'Y']] arrAllConnections = [['F', 'G'], ['G', 'F'], ['G', 'N'], ['N', 'G'], ['N', 'E'], ['E', 'N'], ['E', 'D'], ['D', 'E'], ['D', 'C'], ['C', 'D'], ['C', 'B'], ['B', 'C'], ['B', 'A'], ['A', 'B'], ['A', 'E'], ['E', 'A'], ['B', 'X8'], ['X8', 'B'], ['X8', 'X3'], ['X3', 'X8'], ['X3', 'X2'], ['X2', 'X3'], ['C', 'T'], ['T', 'C'], ['T', 'T1'], ['T1', 'T'], ['T1', 'Y'], ['Y', 'T1'], ['Y', 'L'], ['L', 'Y'],['L', 'P'], ['P', 'L'], ['P', 'Z'], ['Z', 'P'], ['Z', 'Y'], ['Y', 'Z'], ['L', 'K3'], ['K3', 'L'], ['Z', 'K1'], ['K1', 'Z'], ['K1', 'K2'], ['K2', 'K1']] nullFirstConnect = [] #arrAllConnections = (sorted(item for item in arrAllConnections)) arrAllConnections = [list(i) for i in set(map(tuple, arrAllConnections))] print(arrAllConnections, 'REMOVED?') def visited(itemNext, arrConnect): return itemNext in arrConnect def connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, olderItems, mainItemOfConnection): arrAllConDel = arrAllConnections #print('CONTENTS OF TRUNCATED ARR CONNECTIONS', arrAllConDel) arrConnected = [] arrVisited = [] for cycle in arrWithWhatToConnect: print('ARRCONNECTED', arrConnected) if item in cycle: arrConnected.extend(cycle) arrConnected.extend(olderItems) print('My item in cycle', item, cycle, arrConnected, mainItemOfConnection) return set(arrConnected) #print('My item in cycle', item, cycle, arrConnected, mainItemOfConnection) #return set(arrConnected) else: if item in arrWhatToConnect: #print('Not in cycle', item) #print('GOING TO CHECK ELSEWHERE!') olderItems.extend(item) olderItems.extend(olderItems) for connection in arrAllConnections: item1 = connection[0] item2 = connection[1] if item in connection: if item == item1: #print('connection is', connection, 'item', item, 'item1', item1, 'item2', item2) if item2 not in arrConnected: del arrAllConDel[arrAllConnections.index(connection)] connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item2, olderItems, mainItemOfConnection) return arrConnected #how to send previous item, so to let program know that it should be not just ring, but ring + item? elif item == item2: if item1 not in arrConnected: del arrAllConDel[arrAllConnections.index(connection)] connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item1, olderItems, mainItemOfConnection) return arrConnected for item in arrWhatToConnect: print(item) print('CONNECTED CONNECTED CONNECTED', connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, nullFirstConnect, item))