下面是负责创建图形及其边缘的代码片段,具体取决于边缘是否存在以及验证最短路径的条件:
for q in range(len(aaa_binary)):
if len(added)!=i+1:
g.add_nodes_from (aaa_binary[q])
t1 = (aaa_binary[q][0],aaa_binary[q][1])
t2 = (aaa_binary[q][1],aaa_binary[q][2])
t3 = (aaa_binary[q][2],aaa_binary[q][3])
if g.has_edge(*t1)==False and g.has_edge(*t2)==False and g.has_edge(*t3)==False:
g.add_edge(*t1)
g.add_edge(*t2)
g.add_edge(*t3)
added.append([aaa_binary[q],'p'+ str(i)])
for j in range(len(added)):
if nx.shortest_path(g, added[j][0][0], added[j][0][3])!=added[j][0] or nx.shortest_path(g, aaa_binary[q][0], aaa_binary[q][3])!=aaa_binary[q]:
g.remove_edge(*t1)
g.remove_edge(*t2)
g.remove_edge(*t3)
added.remove([aaa_binary[q],'p'+ str(i)])
break
if g.has_edge(*t1)==False and g.has_edge(*t2)==False and g.has_edge(*t3)==True:
g.add_edge(*t1)
g.add_edge(*t2)
added.append([aaa_binary[q],'p'+ str(i)])
for j in range(len(added)):
if nx.shortest_path(g, added[j][0][0], added[j][0][3])!=added[j][0] or nx.shortest_path(g, aaa_binary[q][0], aaa_binary[q][3])!=aaa_binary[q]:
g.remove_edge(*t1)
g.remove_edge(*t2)
added.remove([aaa_binary[q],'p'+ str(i)])
break
# ... and then the rest of the False and True possibilities combinations in the `if g.has_edge()'condition.
已添加[] - 表单中的当前有效路径列表[[[0, 2, 4, 6], 'p0'], [[0, 2, 4, 1], 'p1'],...]
aaa_binary[] - 要在表单中签入的路径组合列表[[0, 2, 4, 6], [0, 2, 6, 4], [0, 4, 2, 6],...]
循环操作:
该算法从aaa_binary列表中选择一个子列表,然后将节点添加到图形并创建边。然后,算法检查给定的边是否存在。如果不存在,则将其添加到图形中,如果存在,则不添加。然后,如果不满足最短路径的条件,则仅从图形中删除新添加的边。如此,直到您从aaa_binary列表中找到正确的路径。
正如您只能使用四元素子列表看到的那样,在aaa_binary列表中的条件if g.has_edge ()
中有 8 种不同的 False 和 True 组合,这已经造成了技术问题。但是,我想开发这个来检查,例如,八元素路径,然后组合将是 128!很明显,我不能以目前的方式做到这一点。 而且我关心循环必然只添加不存在的边,因为这样更容易控制最佳图的创建。
因此,我的问题是,是否有可能以不同的方式编写这样的循环并使其更加自动化?如有任何评论,我将不胜感激。
怎么样:
added_now = []
for edge in (t1,t2,t3):
if not g.has_edge(*edge):
g.add_edge(*edge)
added_now.append(edge)
added.append([aaa_binary[q],'p'+ str(i)])
for j in range(len(added)):
if nx.shortest_path(g, added[j][0][0], added[j][0][3])!=added[j][0] or nx.shortest_path(g, aaa_binary[q][0], aaa_binary[q][3])!=aaa_binary[q]:
for edge in added_now:
g.remove_edge(*edge)
added.remove([aaa_binary[q],'p'+ str(i)])
您只想对未添加的每个边执行相同的操作。
此解决方案适合您吗?
-
它不会阻止您访问 4-elem 路径。它根据当前
aaa_binary[q]
的镜头进行调整。如果要选择 n-elem 路径,它应该很容易修改。:) -
它没有一个无止境的 if 列表。
对于范围内的 Q(Len(aaa_binary)):
if len(added)!=i+1: g.add_nodes_from(aaa_binary[q]) # Instead of having hard-coded variable, make a list. tn = [] for idx in range(0, len(aaa_binary[q]) - 1): # Linking the current elem, to the next one. # The len() - 1 avoids the iteration on the last elem, # that will not have another elem after it. tn.append((aaa_binary[q][idx], aaa_binary[q][idx + 1])) # Instead of checking each and every case, try to make your # task 'general'. Here, you want to add the edge that doesn't exist. indexSaver = [] for index, item in enumerate(tn): if g.has_edge(*item): g.add_edge(*item) # This line is here to keep in mind which item we added, # Since we do not want to execute `.has_edge` multiple times. indexSaver.append(index) # This line is quite unclear as we do not know what is 'added', # neither 'i' in your code. So I will let it as is. added.append([aaa_binary[q], 'p' + str(i)]) # Now that non-existent edges have been added... # I don't understand this part. So we will just modify the [3] # index that was seemingly here to specify the last index. for j in range(len(added)): lastIndex = len(added) - 1 if nx.shortest_path(g, added[j][0][0], added[j][0][lastIndex])!=added[j][0] or nx.shortest_path(g, aaa_binary[q][0], aaa_binary[q][lastIndex])!=aaa_binary[q]: # On the same logic of adding edges, we delete them. for idx, item in enumerate(tn): if idx in indexSaver: g.remove_edge(*item) added.remove([aaa_binary[q], 'p' + str(i)]) break