问题发生在我继续使用图论进行实验时,作为输入我还会添加图表的图片:
但我也在几个数组中描述了这个图: 首先,包含所有连接的数组:
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']]
我认为它包含一些额外的信息并添加了这样的一行:
arrAllConnections = [list(i) for i in set(map(tuple, arrAllConnections))]
还有一些节点我想连接到其他节点组。节点组是循环,这是循环列表:
arrWithWhatToConnect = [['A', 'B', 'C', 'D', 'E'], ['Z', 'P', 'L', 'Y']]
要连接的节点主要是循环外的节点,但它们可能位于内部。 (例如:' E'节点):
arrWhatToConnect = ['F', 'G', 'N', 'X2', 'X3', 'K3', 'K1', 'K2', 'E']
如果您查看图像,您可能会注意到以下几点:
X2
已连接到X3
,该X8
已连接到arrWhatToConnect
,但由于后者不在列表E
中,因此必须停止该过程; E
节点已经在循环中,因此只要在循环中找到F
,该过程就会停止; if F not in cycle/arrWithWhatToConnect, but in arrWhatToConnect -> check next node and repeat check for cycle/arrWithWhatToConnect or/|| arrWhatToConnect
需要递归,简而言之,它应该是这样的:for 'E' node -> ['A', 'B', 'C', 'D', 'E']
for 'F' node -> ['F', 'G', 'N', 'A', 'B', 'C', 'D', 'E']
etc
。最后,这是我想要的:
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 connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, olderItems):
arrAllConDel = arrAllConnections
print('CONTENTS OF TRUNCATED ARR CONNECTIONS', arrAllConDel)
arrConnected = []
for cycle in arrWithWhatToConnect:
if item in cycle:
arrConnected.extend(cycle)
arrConnected.extend(olderItems)
print('My item in cycle', item, cycle, arrConnected)
else:
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 in arrWhatToConnect:
del arrAllConDel[arrAllConnections.index(connection)]
connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item2, olderItems)
elif item == item2:
if item1 in arrWhatToConnect:
del arrAllConDel[arrAllConnections.index(connection)]
connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item1, olderItems)
return arrConnected
for item in arrWhatToConnect:
print(item)
print(connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, nullFirstConnect))
这些是包含所选节点和周期之间最短路径的列表。
我的代码:
net::ERR_INCOMPLETE_CHUNKED_ENCODING
这是我第一次尝试编写任何递归(或者如果我不记得它是一个已经实现的写递归的尝试;))。一切似乎都或多或少都清晰,甚至有效。但是没有足够的返回语句,这会对内存产生影响。以及我似乎应该添加visitedNodes数组。如果您可以告诉我如何改进我的代码,请执行此操作。谢谢。最好在没有任何模块(如nx等)的python中,我想了解内部发生了什么。