问题定义:
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个点的团:
(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中解决团问题的起始点。