Java库,用于图表的集团封面

时间:2012-08-02 18:09:40

标签: java graph-algorithm clique clique-problem

有没有人知道解决集团封面问题的库或某些代码(最好是Java)?

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

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

1 个答案:

答案 0 :(得分:0)

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

定义的问题:

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

输入格式:

名为three_clique的方法的输入是一个String编码,表示如下所示的无向图:

(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。

运行时复杂性:

计算复杂度为Polynomial,具体为O(n^3)。所以效率非常低,当然不是解决这个问题的最佳算法。但它展示了如何在Java中解决和解决集团问题的出发点。

相关问题