子集和 TI 基本编程
我正在尝试对 TI-83 进行编程以进行子集和搜索。因此,给定一个长度为 N 的列表,我想找到给定长度为 L 的所有列表,其总和为给定值 V。
这与常规子集总和问题有点不同,因为我只搜索给定长度的子集,不是所有长度,并且递归不一定是首选,因为我无法调用我正在使用的程序。
我可以使用嵌套循环轻松完成任务,但是对于大于 L 的值来说,这变得很麻烦5. 我正在尝试动态解决方案,但一无所获。
实际上,此时,我只是想让列表引用正确,所以这就是我正在查看的内容。让我们举个例子:
L1={p,q,r,s,t,u}
让
N=6
我们寻找长度为 3 的所有子集以使其相对较短,因此 L = 3(6c3 = 20 个总输出)。
理想情况下,要搜索的列表引用是:
{1,2,3}
{1,2,4}
{1,2,5}
{1,2,6}
{1,3,4}
{1,3,5}
{1,3,6}
{1,4,5}
{1,4,6}
{1,5,6}
{2,3,4}
{2,3,5}
{2,3,6}
{2,4,5}
{2,4,6}
{2,5,6}
{3,4,5}
{3,4,6}
{3,5,6}
{4,5,6}
显然是通过以下方式完成的:
FOR A,1,N-2
FOR B,A+1,N-1
FOR C,B+1,N
display {A,B,C}
END
END
END
我最初对 N 的数据进行降序排序,这使我能够搜索缩短搜索的条件,并且当我增加循环内 A、B 和 C 的值。
我也在寻找更好的动态解决方案。我在网上做了一些研究,但我似乎无法根据我的具体情况调整那里的内容。
任何帮助将不胜感激。我尽量保持简短,以免写小说,而是解释我想要表达的意思。我可以根据需要提供更多详细信息。
I'm trying to program my TI-83 to do a subset sum search. So, given a list of length N, I want to find all lists of given length L, that sum to a given value V.
This is a little bit different than the regular subset sum problem because I am only searching for subsets of given lengths, not all lengths, and recursion is not necessarily the first choice because I can't call the program I'm working in.
I am able to easily accomplish the task with nested loops, but that is becoming cumbersome for values of L greater than 5. I'm trying for dynamic solutions, but am not getting anywhere.
Really, at this point, I am just trying to get the list references correct, so that's what I'm looking at. Let's go with an example:
L1={p,q,r,s,t,u}
so
N=6
let's look for all subsets of length 3 to keep it relatively short, so L = 3 (6c3 = 20 total outputs).
Ideally the list references that would be searched are:
{1,2,3}
{1,2,4}
{1,2,5}
{1,2,6}
{1,3,4}
{1,3,5}
{1,3,6}
{1,4,5}
{1,4,6}
{1,5,6}
{2,3,4}
{2,3,5}
{2,3,6}
{2,4,5}
{2,4,6}
{2,5,6}
{3,4,5}
{3,4,6}
{3,5,6}
{4,5,6}
Obviously accomplished by:
FOR A,1,N-2
FOR B,A+1,N-1
FOR C,B+1,N
display {A,B,C}
END
END
END
I initially sort the data of N descending which allows me to search for criteria that shorten the search, and using FOR loops screws it up a little at different places when I increment the values of A, B and C within the loops.
I am also looking for better dynamic solutions. I've done some research on the web, but I can't seem to adapt what is out there to my particular situation.
Any help would be appreciated. I am trying to keep it brief enough as to not write a novel but explain what I am trying to get at. I can provide more details as needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了优化,您只需跳过搜索的那些子树,它们现在已经超过值 V。递归是可行的方法,但是,由于您已经排除了这种情况,因此您将最好对允许的深度设置上限。
我会选择这样的东西(深度为 3):
现在这相当复杂,但它基本上在每个阶段都会检查您是否已经超过了所需的值,并且拒绝检查较低的子树作为效率衡量标准。它还保留当前级别的运行总计,以便在检查较低级别时不必进行大量添加。这是针对 Z 的数组值的加法和减法。
当您修改它以处理更多深度时(通过使用从
D
到的变量,它会变得更加复杂。 >K
11 个级别(如果您愿意将N
和L
向下移动到W
和X,则可以更多
或者如果 TI BASIC 允许变量名中包含多个字符),我能想到的唯一另一种非递归方法是使用一组值组来模拟迭代递归,这看起来很简单。只是稍微少了一些麻烦(尽管代码应该更少嵌套)。
For optimisation, you simply want to skip those sub-trees of the search where you already now they'll exceed the value V. Recursion is the way to go but, since you've already ruled that out, you're going to be best off setting an upper limit on the allowed depths.
I'd go for something like this (for a depth of 3):
Now that's pretty convoluted but it basically check at every stage whether you've already exceed the desired value and refuses to check lower sub-trees as an efficiency measure. It also keeps a running total for the current level so that it doesn't have to do a large number of additions when checking at lower levels. That's the adding and subtracting of the array values against Z.
It's going to get even more complicated when you modify it to handle more depth (by using variables from
D
toK
for 11 levels (more if you're willing to moveN
andL
down toW
andX
or if TI BASIC allows more than one character in a variable name).The only other non-recursive way I can think of doing that is to use an array of value groups to emulate recursion with iteration, and that will look only slightly less hairy (although the code should be less nested).