gpt4 book ai didi

c# - 通过增加索引总和来生成有序组合的有效方法

转载 作者:可可西里 更新时间:2023-11-01 08:47:27 25 4
gpt4 key购买 nike

对于启发式算法,我需要一个接一个地评估特定集合的组合,直到达到停止标准。

因为它们很多,目前我使用以下内存高效迭代器 block 生成它们(受 python 的 itertools.combinations 启发):

public static IEnumerable<T[]> GetCombinations<T>(this IList<T> pool, int r)
{
int n = pool.Count;
if (r > n)
throw new ArgumentException("r cannot be greater than pool size");
int[] indices = Enumerable.Range(0, r).ToArray();
yield return indices.Select(idx => pool[idx]).ToArray();
while (true)
{
int i;
for (i = r - 1; i >= 0; i--)
if (indices[i] != i + n - r)
break;
if (i < 0)
break;
indices[i] += 1;
for (int j = i + 1; j < r; j++)
indices[j] = indices[j - 1] + 1;
yield return indices.Select(idx => pool[idx]).ToArray();
}
}

问题是,为了大大提高我的启发式效率,我需要生成这些组合,这些组合按它们索引的总和排序(换句话说,我需要首先生成包含集合的第一个元素的组合).

例如
考虑集合 S = {0,1,2,3,4,5}
(我选择这个集合是为了简单起见,因为元素和它们的索引是一致的)。
r=4 的所有可能组合从给定算法生成的数字是:

(0, 1, 2, 3)  SUM:  6
(0, 1, 2, 4) SUM: 7
(0, 1, 2, 5) SUM: 8
(0, 1, 3, 4) SUM: 8
(0, 1, 3, 5) SUM: 9
(0, 1, 4, 5) SUM: 10
(0, 2, 3, 4) SUM: 9
(0, 2, 3, 5) SUM: 10
(0, 2, 4, 5) SUM: 11
(0, 3, 4, 5) SUM: 12
(1, 2, 3, 4) SUM: 10
(1, 2, 3, 5) SUM: 11
(1, 2, 4, 5) SUM: 12
(1, 3, 4, 5) SUM: 13
(2, 3, 4, 5) SUM: 14

如您所见,组合并未严格按总和升序排序。

期望的结果是:
(总和相同的组合顺序不重要)

(0, 1, 2, 3)  SUM:  6
(0, 1, 2, 4) SUM: 7
(0, 1, 2, 5) SUM: 8
(0, 1, 3, 4) SUM: 8
(0, 1, 3, 5) SUM: 9
(0, 2, 3, 4) SUM: 9
(0, 1, 4, 5) SUM: 10
(0, 2, 3, 5) SUM: 10
(1, 2, 3, 4) SUM: 10
(0, 2, 4, 5) SUM: 11
(1, 2, 3, 5) SUM: 11
(0, 3, 4, 5) SUM: 12
(1, 2, 4, 5) SUM: 12
(1, 3, 4, 5) SUM: 13
(2, 3, 4, 5) SUM: 14

一个简单的解决方案是生成所有组合,然后根据它们的总和对它们进行排序;但这并不是真正有效/可行的,因为组合的数量变得很大 n成长。

我也快速浏览了组合格雷码,但我找不到适合这个问题的人。

您对如何实现这样的事情有想法吗?

编辑:

这个问题有一个替代的(不幸的是不简单)公式。
给定一个集合 S和一个数字 r ,所有可能的总和都很容易找到,因为它们只是第一个 r 总和的所有数字。 S 的元素到最后的总和 r S 的元素.

话虽这么说,如果,对于每个总和 T我们可以有效地¹找到总和为T的所有组合我们解决了原始问题,因为我们只是按升序生成它们。

¹ 有效地意味着我不想生成所有组合并丢弃具有不同总和的组合。

编辑 2:

根据@EricLippert 的建议,我创建了以下代码:

public static IEnumerable<T[]> 
GetCombinationsSortedByIndexSum<T>(this IList<T> pool, int r)
{
int n = pool.Count;
if (r > n)
throw new ArgumentException("r cannot be greater than pool size");
int minSum = ((r - 1) * r) / 2;
int maxSum = (n * (n + 1)) / 2 - ((n - r - 1) * (n - r)) / 2;

for (int sum = minSum; sum <= maxSum; sum++)
{
foreach (var indexes in AllMonotIncrSubseqOfLenMWhichSumToN(0, n - 1, r, sum))
yield return indexes.Select(x => pool[x]).ToArray();
}
}

static IEnumerable<IEnumerable<int>>
AllMonotIncrSubseqOfLenMWhichSumToN(int seqFirstElement, int seqLastElement, int m, int n)
{
for (int i = seqFirstElement; i <= seqLastElement - m + 1; i++)
{
if (m == 1)
{
if (i == n)
yield return new int[] { i };
}
else
{
foreach (var el in AllMonotIncrSubseqOfLenMWhichSumToN(i + 1, seqLastElement, m - 1, n - i))
yield return new int[] { i }.Concat(el);
}
}
}

这很好用(希望是 Eric 的意思 :P),但我仍然担心递归方法的复杂性。事实上,我们似乎正在为每个总和重新生成所有组合,丢弃那些总和未达到所需值的组合。

为了降低内部函数的复杂性,我找到了一种通过使用有效的上限和下限来限制迭代的方法(现在真的很难说这有什么复杂性)。

检查 my answer查看最终代码。

最佳答案

我想到的解决方案是:

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
// Preconditions:
// * items is a sequence of non-negative monotone increasing integers
// * n is the number of items to be in the subsequence
// * sum is the desired sum of that subsequence.
// Result:
// A sequence of subsequences of the original sequence where each
// subsequence has n items and the given sum.
static IEnumerable<IEnumerable<int>> M(IEnumerable<int> items, int sum, int n)
{
// Let's start by taking some easy outs. If the sum is negative
// then there is no solution. If the number of items in the
// subsequence is negative then there is no solution.

if (sum < 0 || n < 0)
yield break;

// If the number of items in the subsequence is zero then
// the only possible solution is if the sum is zero.

if (n == 0)
{
if (sum == 0)
yield return Enumerable.Empty<int>();
yield break;
}

// If the number of items is less than the required number of
// items, there is no solution.

if (items.Count() < n)
yield break;

// We have at least n items in the sequence, and
// and n is greater than zero, so First() is valid:

int first = items.First();

// We need n items from a monotone increasing subsequence
// that have a particular sum. We might already be too
// large to meet that requirement:

if (n * first > sum)
yield break;

// There might be some solutions that involve the first element.
// Find them all.

foreach(var subsequence in M(items.Skip(1), sum - first, n - 1))
yield return new[]{first}.Concat(subsequence);

// And there might be some solutions that do not involve the first element.
// Find them all.

foreach(var subsequence in M(items.Skip(1), sum, n))
yield return subsequence;
}
static void Main()
{
int[] x = {0, 1, 2, 3, 4, 5};
for (int i = 0; i <= 15; ++i)
foreach(var seq in M(x, i, 4))
Console.WriteLine("({0}) SUM {1}", string.Join(",", seq), i);
}
}

输出是你想要的输出。

我没有尝试对此进行优化。对它进行概要分析并查看大部分时间花在了哪里会很有趣。

更新:为了好玩,我编写了一个使用不可变堆栈而不是任意枚举的版本。享受吧!

using System;
using System.Collections.Generic;
using System.Linq;

abstract class ImmutableList<T> : IEnumerable<T>
{
public static readonly ImmutableList<T> Empty = new EmptyList();
private ImmutableList() {}
public abstract bool IsEmpty { get; }
public abstract T Head { get; }
public abstract ImmutableList<T> Tail { get; }
public ImmutableList<T> Push(T newHead)
{
return new List(newHead, this);
}

private sealed class EmptyList : ImmutableList<T>
{
public override bool IsEmpty { get { return true; } }
public override T Head { get { throw new InvalidOperationException(); } }
public override ImmutableList<T> Tail { get { throw new InvalidOperationException(); } }
}
private sealed class List : ImmutableList<T>
{
private readonly T head;
private readonly ImmutableList<T> tail;
public override bool IsEmpty { get { return false; } }
public override T Head { get { return head; } }
public override ImmutableList<T> Tail { get { return tail; } }
public List(T head, ImmutableList<T> tail)
{
this.head = head;
this.tail = tail;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
for (ImmutableList<T> current = this; !current.IsEmpty; current = current.Tail)
yield return current.Head;
}
}

class Program
{
// Preconditions:
// * items is a sequence of non-negative monotone increasing integers
// * n is the number of items to be in the subsequence
// * sum is the desired sum of that subsequence.
// Result:
// A sequence of subsequences of the original sequence where each
// subsequence has n items and the given sum.
static IEnumerable<ImmutableList<int>> M(ImmutableList<int> items, int sum, int n)
{
// Let's start by taking some easy outs. If the sum is negative
// then there is no solution. If the number of items in the
// subsequence is negative then there is no solution.

if (sum < 0 || n < 0)
yield break;

// If the number of items in the subsequence is zero then
// the only possible solution is if the sum is zero.
if (n == 0)
{
if (sum == 0)
yield return ImmutableList<int>.Empty;
yield break;
}

// If the number of items is less than the required number of
// items, there is no solution.

if (items.Count() < n)
yield break;

// We have at least n items in the sequence, and
// and n is greater than zero.
int first = items.Head;

// We need n items from a monotone increasing subsequence
// that have a particular sum. We might already be too
// large to meet that requirement:

if (n * first > sum)
yield break;

// There might be some solutions that involve the first element.
// Find them all.

foreach(var subsequence in M(items.Tail, sum - first, n - 1))
yield return subsequence.Push(first);

// And there might be some solutions that do not involve the first element.
// Find them all.
foreach(var subsequence in M(items.Tail, sum, n))
yield return subsequence;
}
static void Main()
{
ImmutableList<int> x = ImmutableList<int>.Empty.Push(5).
Push(4).Push(3).Push(2).Push(1).Push(0);
for (int i = 0; i <= 15; ++i)
foreach(var seq in M(x, i, 4))
Console.WriteLine("({0}) SUM {1}", string.Join(",", seq), i);
}
}

关于c# - 通过增加索引总和来生成有序组合的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16737068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com