最近我一直在试图学习递归算法,并且我遇到了一个死胡同。给定一定数量的列表,其中每个列表表示从一个商店到所有其他商店所需的时间,以及包含时间间隔序列的列表,是否有一种方法可以找到使用递归的商店之间的所有可能路线?在python中查找约束列表中的所有组合
例如
list_of_shops = [shop1, shop2, shop3, shop4]
# Index for this list and the one below are the same
list_of_time_it_takes = [[0, 2, 1, 4], [2, 0, 1, 4], [2, 1, 0, 4], [1, 2, 3, 0]]
# the 0 indicates the index of the shop. It takes 0 minutes to reach the shop from itself.
list_of_time_intervals = [0, 2, 2]
商店只能访问一次。我们可以看到,3个店都以2个分钟的间隔被访问和可能的途径是:
shop4> SHOP2> shop1
shop3> shop1> SHOP2
所以我试图解决上面使用下面的代码所需的输出问题:
shops = [[0, 2, 1, 4, 9], [2, 0, 1, 4, 9], [2, 1, 0, 4, 9], [1, 2, 3, 0, 11], [3, 6, 16, 4, 0]]
times = [0, 2, 2, 4, 11]
list_of_shops = ['shop0', 'shop1', 'shop2', 'shop3', 'shop4']
index_dict = {}
def function(shops_input, times_input, i, index_list):
#print("given i = ", i)
#print("given shops = ", shops_input)
#print("given times = ", times_input)
shops_copy, times_copy = shops_input[:], times_input[:]
pop = times_copy.pop(0)
#print("obtained pop = ", pop)
if shops[i] in shops_copy:
index_list.append(shops.index(shops[i]))
shops_copy.pop(shops_copy.index(shops[i]))
if len(index_list) == len(times):
index_dict[list_of_shops[index_list[0]]] = index_list
print(index_list)
print(index_dict)
if len(times_copy):
try:
function(shops_copy, times_copy, shops[i].index(times_copy[0]), index_list)
except ValueError:
return
def main_function(shops, times):
for i in range(len(shops)):
function(shops, times, i, [])
print("---------end funct---------")
main_function(shops, times)
而且它在某些情况下工作,但肯定不是在所有情况下。它应该给我所有可能的路线基于给定的时间间隔,但是,它似乎并没有在很多情况下工作。
例如,如果我改变商店和时间:
shops = [[0,1,1,1],[1,0,1,1],[1,1,0,1],[1,1,1,0]]
times = [0, 1, 1]
它给出的2个possiblities的输出 - >开始从SHOP2 = [2,0,1]和 - >从shop3 =起始[3 ,0,1]。有什么办法可以让我的算法有效吗?
谢谢!我理解树结构的基础知识,但是我从未与他们合作过。有没有办法使用您提供的解决方案来获得我可以使用的可能路线的可重用表示?例如,一个包含起始点作为关键字的字典,以及作为键值的所有从该点开始的路线?我在问,因为这是需要使用这些值的稍大项目的一部分。 非常感谢! – 2015-04-03 05:53:58
ofcourse你可以,你只需要输出,并将其转换成你想要的。我会为你离开那个挑战:) – fceruti 2015-04-03 06:35:03
我就在那上面!顺便说一下,是否有一种简单的设置方法,以避免算法中出现重复?例如,如果我想排除shop1 - > shop2 - > shop1类型的路由。或者这样做效率不高? – 2015-04-03 06:38:52