Java 独立集的算法计数,始终减少1
我在编写计算树中独立集数量的算法时遇到困难。(独立集是指任意两个节点之间没有边。) 下面是我的ListNode java类:Java 独立集的算法计数,始终减少1,java,algorithm,Java,Algorithm,我在编写计算树中独立集数量的算法时遇到困难。(独立集是指任意两个节点之间没有边。) 下面是我的ListNode java类: 1public class ListNode 2{ 3 private Object data; 4 private ListNode next; 5 6 public ListNode(Object data, ListNode next) 7 { 8 this.data = data; 9 this
1public class ListNode
2{
3 private Object data;
4 private ListNode next;
5
6 public ListNode(Object data, ListNode next)
7 {
8 this.data = data;
9 this.next = next;
10 }
11
12 public Object getData() {return data;}
13 public ListNode getNext(){return next;}
14 public void setNext(ListNode n){next = n;}
15 public void setData(Object d){data = d;}
16 public boolean search(ListNode l, Object o)
17 {
18 while (l != null){
19 if (l.getData().equals(o))
20 return true;
21 l = l.getNext();
22 }
23 return false;
24 }
25 public static ListNode rev(ListNode curr)
26 {
27 ListNode rev = null;
28 while (curr != null){
29 rev = new ListNode(curr.getData(), rev);
30 curr = curr.getNext();
31 }
32 return rev;}}
以及我的TreeNode java类:
1public class TreeNode
2{ ListNode children = null;
3 public void addChild(TreeNode t)
4 {
5 if (children == null)
6 children = new ListNode(t, null);
7 else{
8 ListNode curr = children;
9 while (curr.getNext() != null)
10 curr = curr.getNext();
11 curr.setNext(new ListNode(t, null));
12 }}
13 public void setChildren(ListNode t){this.children = t;}
14 public int numStableSet()
15 {
16
17 if (children == null || children.getNext() == null)
18 return 2;
19 else{
20 int count = 2;
21 setChildren(children.getNext());
22 count *= numStableSet();
23 return count;
24 }
25 }
numStableSet方法就是我需要一些编码帮助的方法。现在设置好后,打印出的结果比正确答案少1个。我还没有弄清楚每个节点本身可能是一棵树的情况
感谢您的帮助我不相信您的算法总是会出错一次。让我们考虑几个例子,从最简单的例子开始。
- 无节点=>1个独立集,空集
- 一个节点=>2个独立集,为空,一个节点
- 一个父级及其唯一子级=>3个独立集,为空且任意一个节点
private int combinationsWithoutCurrent() {
int num = 1;
for (ListNode iter = children; iter != null; iter = iter.getNext())
num *= ((TreeNode)iter.getData()).numStableSets();
return num;
}
private int combinationsWithCurrent() {
int num = 1;
for (ListNode iter = children; iter != null; iter = iter.getNext())
num *= ((TreeNode)iter.getData()).combinationsWithoutCurrent();
return num;
}
public int numStableSet() {
return combinationsWithCurrent() + combinationsWithoutCurrent();
}
现在让我们考虑递归的情况,找到正确的算法。您当前正在访问给定节点。您可以决定不将该节点包括在稳定集中,然后访问其所有子节点并为这些子节点选择任意稳定集。或者,您可以决定包括当前节点,但前提是不包括其自己的父节点,并且在递归到子节点时,您必须确保不计算这些节点。跟踪所有可能的方法来组合这些选择,你就有了自己的计数。在Python伪代码中:
def组合开关断开(当前):
num=1
对于当前版本中的儿童:
num*=稳定集(子)
返回数
def组合开关电流(当前):
num=1
对于当前版本中的儿童:
num*=组合开关输出电流(子级)
返回数
def稳定集(当前):
返回(组合开关电流(电流)+
组合开关输出电流(电流))
由于您更喜欢Java和手工制作的容器类,这里有一些Java代码,我猜您的数据结构是如何使用的。由于在树遍历中从不调用getData
,因此我看不到代码中有任何实际的递归。所以我的猜测可能是错的
private int combinationsWithoutCurrent() {
int num = 1;
for (ListNode iter = children; iter != null; iter = iter.getNext())
num *= ((TreeNode)iter.getData()).numStableSets();
return num;
}
private int combinationsWithCurrent() {
int num = 1;
for (ListNode iter = children; iter != null; iter = iter.getNext())
num *= ((TreeNode)iter.getData()).combinationsWithoutCurrent();
return num;
}
public int numStableSet() {
return combinationsWithCurrent() + combinationsWithoutCurrent();
}
也许答案很明显,但你能不能直接返回/输出你得到的任何
+1
?@Link Ya,我考虑过了,但我不确定把+1放在哪里,因为算法需要是递归的。将+1放在错误的位置会导致超出正确答案的范围。我怀疑您可以/应该将其放在最终结果即将返回的位置?我不完全确定我是否理解您的代码。。。这与树而不仅仅是LinkedList有什么相似之处?为什么有一个ListNode和一个TreeNode?难道你不想有一个(树)节点类,它有一个列表子节点。