4

有谁知道解决集团覆盖问题的库或一些代码(最好是Java)?

我找到了一个 OCaml版本,但我想使用一些我可以更容易集成的东西。

我还找到了 Java代码和 C 代码来查找图中的最大团,但我不知道如何利用此代码找到团覆盖(例如,迭代删除最大团,直到没有节点留下不会产生最佳解决方案)。

4

1 回答 1

0

确定图中是否存在 3-clique 的 Java 程序:

问题定义:

https://en.wikipedia.org/wiki/Clique_problem

输入格式:

被调用方法的输入three_clique是一个字符串编码,表示一个无向图,表示如下:

(1,2,3)((1,2);(2,3);(3,1))

这种编码表示具有三个节点的无向​​图:1,2,3。1 和 2、2 和 3 以及 3 和 1 之间有边。它看起来像一个三角形。显然,这个无向图包含一个 3 集团。

这种无向图的编码不包含 3-clique:

(1,2,3)((1,2);(3,4))

编码:

import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import java.util.AbstractMap;
import java.util.ArrayList;

public class Main{
    public static boolean three_clique(String encoding){
        if (encoding.length() == 0){
            return false;
        }
        String[] elements = encoding.substring(1, encoding.indexOf(")")).split(",");
        encoding = encoding.substring(encoding.indexOf(")")+2); 
        encoding = encoding.substring(0, encoding.length()-1);
        ArrayList<Map.Entry<Integer, Integer>> arr = new ArrayList<Map.Entry<Integer, Integer>>();
        String[] pairs = encoding.split(";");
        if (pairs.length == 1){
            return false;
        } 
        for(int x = 0; x < pairs.length; x++){
            String str = pairs[x].substring(1, pairs[x].length()-1);
            String[] items = str.split(",");
            int left = Integer.parseInt(items[0]);
            int right = Integer.parseInt(items[1]);
            arr.add(new AbstractMap.SimpleEntry(left, right));
        }
        for(int x = 0; x < elements.length; x++){
            for(int y = 0; y < elements.length; y++){
                for(int z = 0; z < elements.length; z++){
                    if (x != y && y != z && z != x){
                        int one = Integer.parseInt(elements[x]);
                        int two = Integer.parseInt(elements[y]);
                        int three = Integer.parseInt(elements[z]);
                        if (is_connected(arr, one, two) &&
                            is_connected(arr, two, three) &&
                            is_connected(arr, three, one)){
                                return true;
                        }
                    }
                }
            }
        }
        return false;
    }
    public static boolean is_connected(ArrayList<Map.Entry<Integer, Integer>> arr, int left, int right){
        for(int x = 0; x < arr.size(); x++){
            if (left == arr.get(x).getKey() && arr.get(x).getValue() == right){
                return true;
            }
            if (right == arr.get(x).getKey() && arr.get(x).getValue() == left){
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args){
        tests();
    }

    public static void tests(){
        String encoding = "";
        boolean expected;
        String msg = "";

        encoding = "";
        expected = false;
        msg = "expected '" + encoding + "' encoding to be false";
        doTest(encoding, expected, msg);

        encoding = "(1)()";
        expected = false;
        msg = "expected '" + encoding + "' encoding to be " + expected;
        doTest(encoding, expected, msg);

        encoding = "(1,2)((1,2))";
        expected = false;
        msg = "expected '" + encoding + "' encoding to be " + expected;
        doTest(encoding, expected, msg);

        encoding = "(1,2,3)((1,2);(2,3);(3,1))";
        expected = true;
        msg = "expected '" + encoding + "' encoding to be " + expected;
        doTest(encoding, expected, msg);

        encoding = "(1,2,3)((1,2);(3,4))";
        expected = false;
        msg = "expected '" + encoding + "' encoding to be " + expected;
        doTest(encoding, expected, msg);

        encoding = "(1,2,3,4)((1,2);(2,3);(3,1);(1,4))";
        expected = true;
        msg = "expected '" + encoding + "' encoding to be " + expected;
        doTest(encoding, expected, msg);

        encoding = "(1,2,3)((1,2);(2,3);(1,3))";
        expected = true;
        msg = "expected '" + encoding + "' encoding to be " + expected;
        doTest(encoding, expected, msg);
    }
    public static void doTest(String encoding, boolean expected, String msg){
        boolean result = three_clique(encoding);
        if (result == expected){
            System.out.print(".");
        }
        else{
            System.out.println("\n" + msg);
        }
    }
}

输出

程序在屏幕上输出一系列七个点,这意味着所有单元测试都通过了。为了证明它有效,请为像这样的大型无向图添加更多单元测试用例:(1,2,3,4,5)((1,5);(1,4);(1,3);(1,2);(1,1);)并查看它是否返回 false。

运行时复杂度:

具体而言,计算复杂度是多项式O(n^3)的。所以它的效率非常低,而且肯定不是这个问题的最佳算法。但它展示了如何处理和解决 Java 中的集团问题的起点。

于 2015-09-26T23:05:33.650 回答