程序员的组合数学?
我开始编写一个 C# Silverlight 程序,尝试找到解决旅行推销员问题的强力解决方案。但一直在尝试找出所有可能的路线。
对于我的程序,我生成随机点并尝试找到可以将它们全部连接起来的最短线,而无需访问任何两次。
所以如果我有三个点 A,B, & CI 想要找到 A、B 和 A 的所有不同组合。 C,其中每个仅使用一次,并且该集合与颠倒时已找到的另一个集合不同。
例如: ABC ACB BAC
但是我怎样才能计算任意数量的点的所有组合呢?
我编写这个程序是为了好玩,现在我更感兴趣的是找到一个好的资源来学习如何解决编程中的组合问题。我为学习组合学找到的所有内容都告诉我如何找到可能的组合数量,但对于实际枚举所有可能的组合毫无用处。
I started writing a C# Silverlight program to try and find brute force solutions to the travelling sales man problems. But got stuck on trying to figure out all the possible routes.
For my program I am generating random dots and trying to find the shortest line that can join them all, without visiting any twice.
so if I have three dots A,B, & C I would want to find all the different combinations of A,B, & C, where each is only used once and the set is not the same as another set already found when reversed.
eg:
ABC
ACB
BAC
But how can I compute all the combinations for any number of dots?
I was writing this program for fun and I am now more interested in finding a good resource for learning about how to solve combinatorial problems in programming. Everything I have found for learning combinatorics tells me how to find to number of possible combinations and is useless for actually enumerating all the possible combinations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您对此类事情感兴趣,我建议您尝试一下 euler 项目上的一些问题,例如 http ://projecteuler.net/problem=15
在 python 的 itertools 模块中,它有一些带有示例代码的示例。
您可以将示例代码转换为您选择的编程语言。
http://docs.python.org/library/itertools.html
示例函数:
示例代码:
请注意,在上面的问题中,如果您允许一个人以直线距离从点 x1,y1 到点 x2,y2,那么这不是同一个问题。 (因为您可以对点进行排序并将它们放入空间数据结构中)。我认为在旅行推销员问题中,你应该有“风/丘陵道路”,这样即使两个点在 x 和 y 方面很接近,它们也可能有一个大的加权边缘连接它们。
If you're getting intertested in this sort of thing, i recommend you try out some of the problems on project euler, e.g. http://projecteuler.net/problem=15
In pythons itertools module it has some examples with example code.
You could convert the sample code to the programming language of your choice.
http://docs.python.org/library/itertools.html
sample functions:
sample code:
Note that in your above problem, if you are allowing one to go from point x1,y1 to point x2,y2 in straight line distance, then it isn't the same problem. (as you can sort the points and put them into a spatial datastructure). I Think in the traveling salesman problem, you're supposed to have "windy/hilly roads" so that even if two points are close together in terms of x and y, they may have a large weighted edge connecting them.
这是我的 C# 类,用于查找排列或组合:
用法:
Here's my C# class to find permutations or combinations:
Usage:
这里有一个Python的解决方案。第一个函数是一个递归函数,它生成与输入列表长度 n 相同的所有排列 P(n,n)。第二个函数运行第一个函数并过滤掉任何已存在反向排列的排列。
Here is a solution in Python. The first function is a recursive function that generates all permutations P(n,n) of the same length n as the input list. The second function runs the first one and filters out any permutation whose reverse already exists.