Algorithm 从未知数量的集合中选择元素
我想列出所有可能的组合,这些组合是从用户输入的多个(未知)集合中的每个集合中选择至少一个和atmost all元素而产生的。一个元素可能在多个集合中,但多次列出它并不是问题 例如:-如果用户输入3组作为Algorithm 从未知数量的集合中选择元素,algorithm,permutation,combinations,combinatorics,Algorithm,Permutation,Combinations,Combinatorics,我想列出所有可能的组合,这些组合是从用户输入的多个(未知)集合中的每个集合中选择至少一个和atmost all元素而产生的。一个元素可能在多个集合中,但多次列出它并不是问题 例如:-如果用户输入3组作为 {1,3,5} {2,4} {1} 输出 1,2,1 1,4,1 1,2,4,1 3,2,1 3,4,1 3,2,4,1 5,2,1 5,4,1 5,2,4,1 1,3,2,1 1,3,4,1 1,3,2,4,1 1,5,2,1 1,5,4,1 1,5,2,4,1 3,5,2,1 3,5,4
{1,3,5}
{2,4}
{1}
输出
1,2,1
1,4,1
1,2,4,1
3,2,1
3,4,1
3,2,4,1
5,2,1
5,4,1
5,2,4,1
1,3,2,1
1,3,4,1
1,3,2,4,1
1,5,2,1
1,5,4,1
1,5,2,4,1
3,5,2,1
3,5,4,1
3,5,2,4,1
1,3,5,2,1
1,3,5,4,1
1,3,5,2,4,1
C代码将更有帮助。谢谢。诸如此类的事情:
(集合不包含重复项)
def permutate(设置为perm,perm):
使用布尔值;
对于集合到perm中的元素集合:
used=false
对于perm中的元素\u perm
如果元素集=元素排列:
使用=正确;
打破
如果使用false:
如果长度(设置为perm)<长度(perm)
置换(将_设置为_perm,将元素_设置为u perm)
其他:
按打印
接受用户的输入;
set_to_perm=根据用户输入进行设置;
置换(将_设置为_perm,[]):
像这样的事情:
(集合不包含重复项)
def permutate(设置为perm,perm):
使用布尔值;
对于集合到perm中的元素集合:
used=false
对于perm中的元素\u perm
如果元素集=元素排列:
使用=正确;
打破
如果使用false:
如果长度(设置为perm)<长度(perm)
置换(将_设置为_perm,将元素_设置为u perm)
其他:
按打印
接受用户的输入;
set_to_perm=根据用户输入进行设置;
置换(将_设置为_perm,[]):
您可以使用递归算法枚举所需的所有集合:
current_set = { }
enumerate (list_of_sets):
if (list_of_sets is empty):
REPORT current_set
f = list_of_sets.front()
r = list_of_sets.tail() /* all sets except f */
n = f.size()
for (i = 0 .. n - 1):
current_set.insert(f[i])
rec (f, i + 1, r)
current_set.remove(f[i])
rec (set, index, remaining_sets):
if (index == set.size()):
enumerate(remaining_sets)
else:
current_set.insert(f[index])
rec(set, index + 1, remaining_sets)
current_set.remove(f[index])
rec(set, index + 1, remaining_sets)
可以使用递归算法枚举所需的所有集合:
current_set = { }
enumerate (list_of_sets):
if (list_of_sets is empty):
REPORT current_set
f = list_of_sets.front()
r = list_of_sets.tail() /* all sets except f */
n = f.size()
for (i = 0 .. n - 1):
current_set.insert(f[i])
rec (f, i + 1, r)
current_set.remove(f[i])
rec (set, index, remaining_sets):
if (index == set.size()):
enumerate(remaining_sets)
else:
current_set.insert(f[index])
rec(set, index + 1, remaining_sets)
current_set.remove(f[index])
rec(set, index + 1, remaining_sets)
由F#
执行示例:
> powerset_out [[1;3;5];[2;4];[1]] [];;
1,2,1
1,4,1
1,2,4,1
3,2,1
3,4,1
3,2,4,1
5,2,1
5,4,1
5,2,4,1
1,3,2,1
1,3,4,1
1,3,2,4,1
1,5,2,1
1,5,4,1
1,5,2,4,1
3,5,2,1
3,5,4,1
3,5,2,4,1
1,3,5,2,1
1,3,5,4,1
1,3,5,2,4,1
由F#
执行示例:
> powerset_out [[1;3;5];[2;4];[1]] [];;
1,2,1
1,4,1
1,2,4,1
3,2,1
3,4,1
3,2,4,1
5,2,1
5,4,1
5,2,4,1
1,3,2,1
1,3,4,1
1,3,2,4,1
1,5,2,1
1,5,4,1
1,5,2,4,1
3,5,2,1
3,5,4,1
3,5,2,4,1
1,3,5,2,1
1,3,5,4,1
1,3,5,2,4,1
看起来你想要输入集的幂集的笛卡尔积,但你并不想把空集包括在内,因为空集在形式上是任何集的幂集的一部分。我已经强调了两个术语,搜索SO将为这些操作生成算法,可能也是C代码。看起来你想要输入集的幂集的笛卡尔积,但你不想包括空集,它在形式上是任何集的幂集的一员。我已经强调了两个术语,搜索SO将为这些操作生成算法,可能也会生成C#代码。看起来你想要输入集的幂集的笛卡尔积,但有一点你不感兴趣,那就是包括空集,它在形式上是任何集的幂集的成员。我已经强调了两个术语,搜索SO将为这些操作生成算法,可能也是C代码。谢谢Mark。你的方法看起来很好,作为一个妓女,我已经把我的评论变成了一个答案,这样你就可以接受它——让我的其他同龄人要么欣赏我的智慧,要么咀嚼失望的苦涩杂草,然后对我投反对票。看起来你想要输入集的幂集的笛卡尔积,对于你不感兴趣的褶皱,包括空集,它在形式上是任何集的幂集的一个成员。我已经强调了两个术语,搜索SO将为这些操作生成算法,可能也是C代码。谢谢Mark。作为一个妓女,你的方法似乎很好。我已经把我的评论变成了一个答案,这样你就可以接受它——让我的其他同龄人要么钦佩我的智慧,要么咀嚼失望的苦涩杂草,然后投我一票。