图的团覆盖的Java库
有没有人知道解决小团体封面问题的库或代码(最好是Java) 我发现了一个OCaml,但我想使用一些我可以更容易集成的东西 我还找到了Java和C代码来查找图中的最大团,但我不知道如何利用这些代码来查找团覆盖(例如,迭代删除最大团,直到没有剩余节点为止,不会产生最佳解决方案)。确定图中是否有3团的Java程序: 问题定义:图的团覆盖的Java库,java,graph-algorithm,clique,clique-problem,Java,Graph Algorithm,Clique,Clique Problem,有没有人知道解决小团体封面问题的库或代码(最好是Java) 我发现了一个OCaml,但我想使用一些我可以更容易集成的东西 我还找到了Java和C代码来查找图中的最大团,但我不知道如何利用这些代码来查找团覆盖(例如,迭代删除最大团,直到没有剩余节点为止,不会产生最佳解决方案)。确定图中是否有3团的Java程序: 问题定义: import java.util.AbstractMap.SimpleEntry; import java.util.Map; import java.util.Abstrac
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);
}
}
}
输入格式:
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);
}
}
}
名为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);
}
}
}
import java.util.AbstractMap.SimpleEntry;
导入java.util.Map;
导入java.util.AbstractMap;
导入java.util.ArrayList;
公共班机{
公共静态布尔三组(字符串编码){
if(encoding.length()==0){
返回false;
}
String[]elements=encoding.substring(1,encoding.indexOf(“)”).split(“,”);
encoding=encoding.substring(encoding.indexOf(“)”)+2);
encoding=encoding.substring(0,encoding.length()-1);
ArrayList arr=新的ArrayList();
String[]pairs=encoding.split(“;”);
if(pairs.length==1){
返回false;
}
对于(int x=0;x
输出
程序在屏幕上输出一系列七个点,这意味着所有单元测试都通过。为了证明它是有效的,可以为像这样的大型无向图添加更多的单元测试用例:(1,2,3,4,5)((1,5);(1,4);(1,3);(1,2);(1,1);)
,看看它是否返回false
运行时复杂性:
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);
}
}
}
具体来说,计算复杂性是O(n^3)。所以它的效率非常低,而且肯定不是解决这个问题的最佳算法。但它展示了如何在Java中处理和解决小团体问题的起点。除非OCaml版本有一些优化,否则通过生成所有分区和测试,用Java编写暴力版本应该很容易。感谢您的建议,我将保留此选项作为最后的手段。是的,OCAml版本似乎有很好的运行时性能。