问题描述
我刚刚发现了一个算法寻找发电机组。我用Google搜索的解决方案后,却发现没有一个工作什么好,所以我想通了一个自己。但我不知道它的算法是,因为我无法找到它在网络上或任何书籍。我的意思是,它有名字吗?我几乎觉得我是它的发明者。但比起我发现在一些网站计算发电机组的算法,我觉得我的是更好的,不知道为什么没有人使用它?
I just discovered an algorithm for finding the power set. I googled after solutions, but found none that worked any good, so I figured out one myself. But I wonder what algorithm it is, because I cannot find it on the net or in any books. I mean, does it have a name? I hardly think I'm the inventor of it. But compared to the algorithms I found on some sites for calculating the power set, I think mine is far better and wonder why no one uses it?
这是算法:
R <- []
L <- [ e1, e2 ... en ]
c <- 0
function: powerSet(L, c)
R <- R union L
for e in L starting at c
powerSet(L\{e}, c)
end
return R
end
在这里,它是用Java实现的:
And here it is implemented in Java:
public static void powerSet(List<String> list, int count)
{
result.add(list);
for(int i = count; i < list.size(); i++)
{
List<String> temp = new ArrayList<String>(list);
temp.remove(i);
powerSet(temp, i);
}
}
有没有人认识它,还是我的发明?
Does anyone recognize it, or am I the inventor?
推荐答案
主要有两个原因:
- 在它使用的全球变量;
- 这是递归的,虽然这其实并不重要太多,因为它是一个
O(2 ^ n)的
算法。
- It uses global variables;
- It is recursive, although this doesn't really matter much because it's an
O(2^n)
algorithm.
这篇关于算法计算发电机组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!