import java.util.*; import java.io.*; public class MaxCliquePP extends MaxClique { private MySet V; // set of vertices private ArrayStack DeleteStack; // used throughout private ArrayList L; // used throughout private Vertex[][] vertex; private DegreeComparator compareDegree; private BitSet[] N; // neighbourhood private BitSet[] invN; // not the neighbourhood public MaxCliquePP (String fname) throws IOException { super(fname); V = new MySet(n); vertex = new Vertex[n][n]; compareDegree = new DegreeComparator(); for (int i=0;i(n*n); L = new ArrayList(n); for (int i=0;i 0 && (System.currentTimeMillis() - cpuTime)/1000 >= timeLimit) return; nodes++; Vertex[] sortedVertex = new Vertex[P.size()]; int k = 0; for (int u : P) sortedVertex[k++] = vertex[depth][u]; Arrays.sort(sortedVertex,compareDegree); Stack S = numberSort(sortedVertex); //Stack S = BBColour(sortedVertex); while (!S.isEmpty()){ Vertex v = S.pop(); if (C.size() + v.getColour() <= maxSize) return; for (int u : P) vertex[depth+1][u].setDegree(vertex[depth][u].getDegree()); P.push(); C.push(); if (select(depth+1,v.getIndex(),C,P)) maxClique(depth+1,C,P); P.pop(); C.pop(); reject(depth,v.getIndex(),C,P); } } // // NOTE 1: it is possible that a vertex can be on the stack but has been deleted from P // this happens due to aggressive filtering in reject(...) removing vertices // of low degree as vertices are removed and maxSize (globally) increases as search // proceeds. Therefore redundant calls could be cut! However, so far any vertex // that satisfies this condition appears to be rejected because its colour is too low // private boolean select(int depth,int v,MySet C,MySet P){ C.add((Integer)v); if (C.size() > maxSize) saveSolution(C); DeleteStack.clear(); for (int i=0;i numberSort(Vertex[] v_in){ int colours = 0; int m = v_in.length; ArrayList[] colourClass = new ArrayList[m]; for (Vertex v : v_in){ int k = 0; while (colourClass[k] != null && conflicts(v,colourClass[k])) k++; if (colourClass[k] == null) colourClass[k] = new ArrayList(m); colourClass[k].add(v); v.setColour(k+1); colours = Math.max(colours,k+1); } Stack S = new Stack(); for (int k=0;k)colourClass[k]) S.push(v); return S; } private Stack BBColour(Vertex[] v_in){ BitSet P = new BitSet(n); for (Vertex v : v_in){P.set(v.getIndex(),true); v.setFlag(true);} Stack S = new Stack(); int maxColour = 0; while (P.cardinality() != 0){ maxColour++; BitSet Q = (BitSet)P.clone(); while (Q.cardinality() != 0){ Vertex w = null; for (Vertex v : v_in) if (v.getFlag()){w = v; break;} P.set(w.getIndex(),false); Q.set(w.getIndex(),false); Q.and(invN[w.getIndex()]); S.push(w); w.setColour(maxColour); w.setFlag(false); } } return S; } private boolean conflicts(Vertex v,ArrayList V){ for (Vertex u : V) if (A[u.getIndex()][v.getIndex()] == 1) return true; return false; } private void saveSolution(MySet C){ for (int i=0;i 1) mc.setTimeLimit((long)Integer.parseInt(args[1])); if (args.length > 2) mc.setStyle(Integer.parseInt(args[2])); //if (args.length > 3) mc.setTrace(); long cpuTime = System.currentTimeMillis(); mc.search(); cpuTime = System.currentTimeMillis() - cpuTime; System.out.println(mc.getMaxSize() +" "+ mc.getNodes() +" "+ cpuTime +" true"); System.out.println(mc.getSolution()); } }