Algorithm 深度优先组合算法
假设我有以下数组:Algorithm 深度优先组合算法,algorithm,combinations,combinatorics,Algorithm,Combinations,Combinatorics,假设我有以下数组: A = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h'], ['i'], ['j', 'k', 'l'] ] 我想找到每个数组元素与其他数组元素的所有可能组合(即“adgij”是一种可能性,但不是“abcde”) 我可以强制执行它,然后像这样循环所有内容(javascript): 这个方法的问题是它是广度优先的,所以如果我想在n次迭代后停止,我会有不完整的组合 有没有一种方法可以使用深度优先方法获得所有可能的组
A = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h'],
['i'],
['j', 'k', 'l']
]
我想找到每个数组元素与其他数组元素的所有可能组合(即“adgij”是一种可能性,但不是“abcde”)
我可以强制执行它,然后像这样循环所有内容(javascript):
这个方法的问题是它是广度优先的,所以如果我想在n次迭代后停止,我会有不完整的组合
有没有一种方法可以使用深度优先方法获得所有可能的组合?伪代码中的一个简单递归函数 每个递归步骤从当前索引的数组中选取一个元素,并为下一个索引调用函数
current
只能是一个列表
printAllCombinations(A,{},0)
打印所有组合(A、当前、索引)
如果索引==A.length
打印电流
返回
对于[索引]中的每个元素e
当前地址:addToBack(e)
打印所有组合(A、当前、索引+1)
当前的删除速度(e)
我基本上创建了一个映射(例如,[0,0,0,0,0]将首先选择列表中的所有成员,而[2,2,1,0,2]将选择所有最后一个成员),然后转换回列表。这有点棘手,但我希望我是对的:
#!/usr/bin/env python
import itertools
def map_good_opt(good_opt, A):
return [i[1][i[0]] for i in zip(good_opt, A)]
if "__main__" == __name__:
# your list of lists
A = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h'],
['i'],
['j', 'k', 'l']
]
# this part generates all options (a bit more actually...)
m = max(len(a) for a in A)
print "m : %d" % m
nums = range(m)
print "nums: %r" % str(nums)
opts = itertools.product(nums, repeat=len(A))
# now we have all number 00000 - 33333
# we don't want 33333 or anything higher than len(A[i]) for each list in A
opts = itertools.product(nums, repeat=len(A))
# this removes all bad options... (I hope :D)
good_opts = [opt for opt in opts if len([i for i in range(len(A)) if (opt[i] < len(A[i]))]) == len(A)]
# and we're left with the good options
for opts in good_opts:
print str(opt)
print "GO: %d" % len(good_opts)
for g in good_opts:
print str("OPTIONS: " + str(g))
print str("MAPPED TO: " + str(map_good_opt(g,A)))
print "done."
#/usr/bin/env python
进口itertools
def映射良好选项(良好选项,A):
返回[i[1][i[0]]以获取zip中的i(良好选项,A)]
如果“\uuuu main\uuuuuu”==\uuuuuuuu name\uuuuuuu:
#你的名单
A=[
[a',b',c'],
[d'、[e'、[f'],
['g','h'],
['i'],
['j'、'k'、'l']
]
#此部分生成所有选项(实际上稍微多一些…)
m=最大值(a中a的长度(a))
打印“m:%d”%m
nums=范围(m)
打印“nums:%r”%str(nums)
opts=itertools.product(nums,repeat=len(A))
#现在我们都有00000-33333
#我们不希望一个列表中的每个列表都有33333或任何高于len(A[i])的值
opts=itertools.product(nums,repeat=len(A))
#这将删除所有不好的选项。。。(我希望是D)
good_opts=[opt for opt in opts if len([i for i in range(len(A))if(opt[i]
我这样做只是为了学习,我最近在Stackoverflow中学到了这一点,你的问题看起来很有趣,可以在上面进行测试:)
祝你好运。如果我理解正确,你应该搜索“深度优先笛卡尔积”。以下是我找到的一个链接:这可能是以下链接的副本:
#!/usr/bin/env python
import itertools
def map_good_opt(good_opt, A):
return [i[1][i[0]] for i in zip(good_opt, A)]
if "__main__" == __name__:
# your list of lists
A = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h'],
['i'],
['j', 'k', 'l']
]
# this part generates all options (a bit more actually...)
m = max(len(a) for a in A)
print "m : %d" % m
nums = range(m)
print "nums: %r" % str(nums)
opts = itertools.product(nums, repeat=len(A))
# now we have all number 00000 - 33333
# we don't want 33333 or anything higher than len(A[i]) for each list in A
opts = itertools.product(nums, repeat=len(A))
# this removes all bad options... (I hope :D)
good_opts = [opt for opt in opts if len([i for i in range(len(A)) if (opt[i] < len(A[i]))]) == len(A)]
# and we're left with the good options
for opts in good_opts:
print str(opt)
print "GO: %d" % len(good_opts)
for g in good_opts:
print str("OPTIONS: " + str(g))
print str("MAPPED TO: " + str(map_good_opt(g,A)))
print "done."