java - 如何避免不必要的计算寻找最大独立集?

标签 java algorithm graph set graph-theory

我编写了一个算法来查找图的最大独立集。根据定义,独立集是“一个集合 S,使得图的每条边至少有一个不在 S 中的端点,并且每个不在 S 中的顶点在 S 中至少有一个邻居”

该图是一个无向图,如下图:

节点:1,2,3,4,5,6,7 边:1-2,2-3,3-4,4-5,5-6,6-7,1-5

这是我的实现:

FindMIS fms= new FindMIS(network);


public class FindMIS {

INetwork network;

public FindMIS(INetwork network) {

    this.network = network;
    ArrayList<INode> nodes = new ArrayList<>();
    nodes.addAll(network.getNodesList());
    Iterator<INode> iter = nodes.iterator();
    ArrayList<INode> IS = new ArrayList<>();
    while (iter.hasNext()) {
        INode node=iter.next();
        visitNode(node, IS, nodes);

    }


}

private void visitNode(INode node, ArrayList<INode> previousIS, ArrayList<INode>   

previousCandidates) {


    ArrayList<INode> IS=new ArrayList<>();
     IS.addAll(previousIS);
    ArrayList<INode> candidates = new ArrayList<>();

    candidates.addAll(previousCandidates);
    //System.out.println(node);
    ArrayList<INode> neighbor = (ArrayList<INode>) network.getNeighborsof(node);
    for (INode n : previousCandidates) {
        if (neighbor.contains(n)) {
            candidates.remove(n);
        }

    }
    IS.add(node);

    candidates.remove(node);
    Iterator<INode> iter = candidates.iterator();
    while (iter.hasNext()) {
        visitNode(iter.next(), IS, candidates);
    }
    if (candidates.size()==0){
         Iterator<INode> iter2=IS.iterator();
    System.out.print("output:{" );
    while(iter2.hasNext()){
        System.out.print(iter2.next().getid());
    }
        System.out.println("}");
    }




   }
}  

输出如下:

output:{1 3 6 }
output:{1 4 6 }
output:{1 6 3 }
output:{1 6 4 }
output:{2 4 6 }
output:{2 4 7 }
output:{2 5 7 }
output:{2 6 4 }
output:{2 7 4 }
output:{2 7 5 }
output:{3 1 6 }
output:{3 5 7 }
output:{3 6 1 }
output:{3 7 5 }
output:{4 1 6 }
output:{4 2 6 }
output:{4 2 7 }
output:{4 6 1 }
output:{4 6 2 }
output:{4 7 2 }
output:{5 2 7 }
output:{5 3 7 }
output:{5 7 2 }
output:{5 7 3 }
output:{6 1 3 }
output:{6 1 4 }
output:{6 2 4 }
output:{6 3 1 }
output:{6 4 1 }
output:{6 4 2 }
output:{7 2 4 }
output:{7 2 5 }
output:{7 3 5 }
output:{7 4 2 }
output:{7 5 2 }
output:{7 5 3 }

You can realize that there are some redundant sets like {1,3,6} and {1,6,3}. The final result must be:

output:{1 3 6}
output:{1 4 6}
output:{2 4 6}
output:{2 4 7}
output:{2 5 7}
output:{3 5 7}

I am trying to figure out a way to avoid unnecessary computation. 
I appreciate for any idea.

更新:1:在 Darryl Gerrow 的回复之后,我更改了我的 visitNode 方法如下:它起作用了。我的算法仍然存在一些问题,以使其更具可读性和可移植性。每当我完成时,我都会发布最终版本。感谢所有社区。除了寻找所有节点之外,如果有人有更好的想法在图中找到最大独立集,我真的很感激阅读。

private void visitNode(INode node, ArrayList<INode> previousIS, ArrayList<INode>    

previousCandidates) {


    ArrayList<INode> IS=new ArrayList<>();
     IS.addAll(previousIS);
    ArrayList<INode> candidates = new ArrayList<>();

    candidates.addAll(previousCandidates);
    //System.out.println(node);
    ArrayList<INode> neighbor = (ArrayList<INode>) network.getNeighborsof(node);
    for (INode n : previousCandidates) {
        if (neighbor.contains(n)) {
            candidates.remove(n);
        }

    }
    IS.add(node);

    candidates.remove(node);


    Iterator<INode> iter = candidates.iterator();
    while (iter.hasNext()) {
       INode nextnode=iter.next();
       if (node.getid() < nextnode.getid())
        visitNode(nextnode, IS, candidates);
    }
    if (candidates.size()==0){
         Iterator<INode> iter2=IS.iterator();
    System.out.print("output:{" );
    while(iter2.hasNext()){
        System.out.print(iter2.next().getid() +" ");
    }
        System.out.println("}");
    }




}

最佳答案

看起来您正在进行 n 次方迭代,所以这就是您得到的原因 {1, 3, 6}, {1, 6, 3}, {3, 6, 1}, ....基本上是可接受集合的所有可能组合。

我认为您需要重写迭代,以便您只访问比当前节点更大的节点。

即。你会得到 {1, 3, 6},但是 {1, 6, 3} 是 Not Acceptable ,因为 6 > 3。

您可能不得不放弃 Iterator 并使用 ArrayList 的索引方法。

关于java - 如何避免不必要的计算寻找最大独立集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18619071/

相关文章:

java - 为什么我的面板没有出现在 JFrame 中

java - 节省实例变量

algorithm - 我可以使用 google 推荐 URL 并查看搜索的内容吗?

c++ - 从 CUDA 数组中删除元素

php - 在php中创建条形图的问题

java - 如何将 RapidMiner 集成到 PHP Web 应用程序中?

java - 特定年份一个月中不工作的天数

c - 在关联数组中查找此数据的最佳算法?

python - 智能计算图表刻度位置

c++ - 有向无环图的 C/C++ 实现