Skip to content

Commit

Permalink
CFLT plugin very minor renaming & refactoring (#1601)
Browse files Browse the repository at this point in the history
Very minor refactoring and renaming to make the code a bit less terrible
* addition changed to suffix
* style checks fixed
* more styling fixes
* static const moved to utils file
* complication errors fixed
* PMD checks
* redundant comment removed
* comment added back in
  • Loading branch information
IgorWiecz committed May 16, 2024
1 parent bc51c09 commit 9342e87
Show file tree
Hide file tree
Showing 14 changed files with 584 additions and 621 deletions.
39 changes: 16 additions & 23 deletions workcraft/CfltPlugin/src/org/workcraft/plugins/cflt/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.workcraft.plugins.cflt.utils.GraphUtils.SPECIAL_CLONE_CHARACTER;

/**
* An undirected and unweighed graph as a set of edges
* An undirected and unweighted graph as a set of edges
*/
public class Graph {

private ArrayList<Edge> edges = new ArrayList<>();
private ArrayList<String> vertices = new ArrayList<>();
boolean[][] connections;

public Graph(ArrayList<Edge> edgeList, ArrayList<String> vertices, boolean[][] connections) {
this.setEdges(edgeList);
this.edges = edgeList;
this.connections = connections;
this.vertices = vertices;
}
Expand All @@ -34,15 +36,6 @@ public void initialiseConnections() {
}
}

public void printGraph() {
for (Edge edge : getEdges()) {
System.out.println("Edge: " + edge.getFirstVertex() + ' ' + edge.getSecondVertex());
}
for (String s : getVertices()) {
System.out.println("Vertex: " + s);
}
}

public void addVertex(String v) {
this.vertices.add(v);
}
Expand Down Expand Up @@ -110,16 +103,16 @@ public void setEdges(ArrayList<Edge> edges) {
}

public Graph cloneGraph(int counter) {
String addition = "$" + counter;
ArrayList<String> vClone = new ArrayList<>();
ArrayList<Edge> eClone = new ArrayList<>();
for (String v : this.getVertices()) {
vClone.add(v + addition);
}
for (Edge e : this.getEdges()) {
eClone.add(new Edge(e.getFirstVertex() + addition, e.getSecondVertex() + addition));
}
return new Graph(eClone, vClone, null);
}
String suffix = SPECIAL_CLONE_CHARACTER + counter;

ArrayList<String> vertices = this.vertices.stream().map(vertexName -> {
return vertexName + suffix;
}).collect(Collectors.toCollection(ArrayList::new));

ArrayList<Edge> edges = this.getEdges().stream().map(edgeName -> {
return new Edge(edgeName.getFirstVertex() + suffix, edgeName.getSecondVertex() + suffix);
}).collect(Collectors.toCollection(ArrayList::new));

return new Graph(edges, vertices, null);
}
}
27 changes: 13 additions & 14 deletions workcraft/CfltPlugin/src/org/workcraft/plugins/cflt/Node.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package org.workcraft.plugins.cflt;

public class Node {

private String left;
private String right;
private String leftChildName;
private String rightChildName;
private Operator operator;

public Node(String left, String right, Operator operator) {
this.left = left;
this.right = right;
public Node(String leftChildName, String rightChildName, Operator operator) {
this.leftChildName = leftChildName;
this.rightChildName = rightChildName;
this.operator = operator;
}

public String getLeft() {
return left;
public String getLeftChildName() {
return this.leftChildName;
}

public void setLEft(String left) {
this.left = left;
public void setLeftChildName(String leftChild) {
this.leftChildName = leftChild;
}

public String getRight() {
return right;
public String getRightChildName() {
return this.rightChildName;
}

public void setB(String right) {
this.right = right;
public void setRightChildName(String rightChild) {
this.rightChildName = rightChild;
}

public Operator getOperator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,51 @@
*/
public class ExhaustiveSearch {

public static ArrayList<ArrayList<String>> getEdgeCliqueCover(Graph initG, ArrayList<Edge> optionalEdges) {
// The solution, i.e. a list of the vertices contained in each final clique
ArrayList<ArrayList<String>> ecc = new ArrayList<>();
public static ArrayList<ArrayList<String>> getEdgeCliqueCover(Graph initialGraph, ArrayList<Edge> optionalEdges) {
ArrayList<ArrayList<String>> edgeCliqueCover = new ArrayList<>();
ArrayList<ArrayList<String>> allMaxCliques = null;

try {
allMaxCliques = MaxCliqueEnumerator.getAllMaxCliques(initG);
allMaxCliques = MaxCliqueEnumerator.getAllMaxCliques(initialGraph);
} catch (Exception e) {
e.printStackTrace();
}
//printSolution(allMaxCliques);
AdvancedGraph g = null;
if (!initG.getEdges().isEmpty()) {
// Max number of cliques to be used in the final ecc (i.e. the depth of the tree to be traversed)

AdvancedGraph graph = null;
if (!initialGraph.getEdges().isEmpty()) {
// Max number of cliques to be used in the final edge clique cover (i.e. the depth of the tree to be traversed)
int k = 0;
while (ecc.isEmpty()) {
g = new AdvancedGraph(initG, allMaxCliques);
ecc = branch(g, k, new ArrayList<>(), optionalEdges);
while (edgeCliqueCover.isEmpty()) {
graph = new AdvancedGraph(initialGraph, allMaxCliques);
edgeCliqueCover = branch(graph, k, new ArrayList<>(), optionalEdges);
k++;
}
}
return ecc;
return edgeCliqueCover;
}

@SuppressWarnings("unchecked")
private static ArrayList<ArrayList<String>> branch(AdvancedGraph g, int k,
ArrayList<ArrayList<String>> ecc, ArrayList<Edge> optionalEdges) {
private static ArrayList<ArrayList<String>> branch(AdvancedGraph graph, int k,
ArrayList<ArrayList<String>> edgeCliqueCover, ArrayList<Edge> optionalEdges) {

if (g.isCovered(ecc, optionalEdges)) {
return ecc;
if (graph.isCovered(edgeCliqueCover, optionalEdges)) {
return edgeCliqueCover;
}

if (k < 0) {
return new ArrayList<>();
}

Edge e = g.selectEdge();
if (e == null) {
return ecc;
Edge selectedEdge = graph.selectEdge();
if (selectedEdge == null) {
return edgeCliqueCover;
}

for (ArrayList<String> maxClique : g.getMaximalCliques(e)) {
ArrayList<ArrayList<String>> newEcc = (ArrayList<ArrayList<String>>) ecc.clone();
for (ArrayList<String> maxClique : graph.getMaximalCliques(selectedEdge)) {
ArrayList<ArrayList<String>> newEcc = (ArrayList<ArrayList<String>>) edgeCliqueCover.clone();
newEcc.add(maxClique);

ArrayList<ArrayList<String>> eccPrime = branch(g, k - 1, newEcc, optionalEdges);
if (!eccPrime.isEmpty()) {
return eccPrime;
ArrayList<ArrayList<String>> edgeCliqueCoverPrime = branch(graph, k - 1, newEcc, optionalEdges);
if (!edgeCliqueCoverPrime.isEmpty()) {
return edgeCliqueCoverPrime;
}
}
return new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@

public class MaxCliqueEnumerator {

int nodesCount;
int nodeCount;
ArrayList<Vertex> graph = new ArrayList<>();
ArrayList<ArrayList<String>> allMaxCliques = new ArrayList<>();
HashMap<String, Integer> vertexNameIndex = new HashMap<>();
HashMap<Integer, String> vertexIndexName = new HashMap<>();
HashMap<String, Integer> vertexNameToIndex = new HashMap<>();
HashMap<Integer, String> vertexIndexToName = new HashMap<>();

static class Vertex implements Comparable<Vertex> {
int x;

int degree;
ArrayList<Vertex> nbrs = new ArrayList<>();
ArrayList<Vertex> neighbours = new ArrayList<>();

public int getX() {
return x;
Expand All @@ -35,32 +34,31 @@ public void setDegree(int degree) {
this.degree = degree;
}

public ArrayList<Vertex> getNbrs() {
return nbrs;
public ArrayList<Vertex> getNeighbours() {
return neighbours;
}

public void setNbrs(ArrayList<Vertex> nbrs) {
this.nbrs = nbrs;
public void setNeighbours(ArrayList<Vertex> neighbours) {
this.neighbours = neighbours;
}

public void addNbr(Vertex y) {
this.nbrs.add(y);
if (!y.getNbrs().contains(y)) {
y.getNbrs().add(this);
y.degree++;
public void addNeighbour(Vertex vertex) {
this.neighbours.add(vertex);
if (!vertex.getNeighbours().contains(vertex)) {
vertex.getNeighbours().add(this);
vertex.degree++;
}
this.degree++;

}

public void removeNbr(Vertex y) {
this.nbrs.remove(y);
if (y.getNbrs().contains(y)) {
y.getNbrs().remove(this);
y.degree--;
public void removeNeighbour(Vertex vertex) {
this.neighbours.remove(vertex);
if (vertex.getNeighbours().contains(vertex)) {
vertex.getNeighbours().remove(this);
vertex.degree--;
}
this.degree--;

}

@Override
Expand All @@ -69,63 +67,51 @@ public int compareTo(Vertex o) {
}
}

void initGraph() {
public static ArrayList<ArrayList<String>> getAllMaxCliques(Graph graph) {
MaxCliqueEnumerator enumerator = new MaxCliqueEnumerator();

enumerator.initialiseMap(graph);
enumerator.readNextGraph(graph);
enumerator.bronKerboschPivotExecute();
return enumerator.allMaxCliques;
}

private void initGraph() {
graph.clear();
for (int i = 0; i < nodesCount; i++) {
for (int i = 0; i < nodeCount; i++) {
Vertex v = new Vertex();
v.setX(i);
graph.add(v);
}
}

void readNextGraph(Graph g) {
nodesCount = g.getVertices().size();
private void readNextGraph(Graph g) {
nodeCount = g.getVertices().size();
int edgesCount = g.getEdges().size();
initGraph();

for (int k = 0; k < edgesCount; k++) {
String[] strArr = new String[2];
strArr[0] = g.getEdges().get(k).getFirstVertex();
strArr[1] = g.getEdges().get(k).getSecondVertex();
int u = vertexNameIndex.get(strArr[0]);
int v = vertexNameIndex.get(strArr[1]);
Vertex vertU = graph.get(u);
Vertex vertv = graph.get(v);
vertU.addNbr(vertv);
int u = vertexNameToIndex.get(strArr[0]);
int v = vertexNameToIndex.get(strArr[1]);
Vertex vertexU = graph.get(u);
Vertex vertexV = graph.get(v);
vertexU.addNeighbour(vertexV);
}
}

// Finds nbr of vertex i
ArrayList<Vertex> getNbrs(Vertex v) {
private ArrayList<Vertex> getNeighbours(Vertex v) {
int i = v.getX();
return graph.get(i).nbrs;
return graph.get(i).neighbours;
}

// Intersection of two sets
ArrayList<Vertex> intersect(ArrayList<Vertex> arlFirst,
private ArrayList<Vertex> intersect(ArrayList<Vertex> arlFirst,
ArrayList<Vertex> arlSecond) {
ArrayList<Vertex> arlHold = new ArrayList<>(arlFirst);
arlHold.retainAll(arlSecond);
return arlHold;
}

// Union of two sets
ArrayList<Vertex> union(ArrayList<Vertex> arlFirst,
ArrayList<Vertex> arlSecond) {
ArrayList<Vertex> arlHold = new ArrayList<>(arlFirst);
arlHold.addAll(arlSecond);
return arlHold;
}

// removes the neighbours
ArrayList<Vertex> removeNbrs(ArrayList<Vertex> arlFirst, Vertex v) {
ArrayList<Vertex> arlHold = new ArrayList<>(arlFirst);
arlHold.removeAll(v.getNbrs());
return arlHold;
}

// Version without a pivot
void bronKerboschWithoutPivot(ArrayList<Vertex> r, ArrayList<Vertex> p, ArrayList<Vertex> x, String pre) {
private void bronKerboschWithoutPivot(ArrayList<Vertex> r, ArrayList<Vertex> p, ArrayList<Vertex> x, String pre) {
if ((p.isEmpty()) && (x.isEmpty())) {
saveClique(r);
return;
Expand All @@ -134,44 +120,30 @@ void bronKerboschWithoutPivot(ArrayList<Vertex> r, ArrayList<Vertex> p, ArrayLis
ArrayList<Vertex> p1 = new ArrayList<>(p);
for (Vertex v : p) {
r.add(v);
bronKerboschWithoutPivot(r, intersect(p1, getNbrs(v)),
intersect(x, getNbrs(v)), pre + "\t");
bronKerboschWithoutPivot(r, intersect(p1, getNeighbours(v)),
intersect(x, getNeighbours(v)), pre + "\t");
r.remove(v);
p1.remove(v);
x.add(v);
}
}

void bronKerboschPivotExecute() {
private void bronKerboschPivotExecute() {
ArrayList<Vertex> x = new ArrayList<>();
ArrayList<Vertex> r = new ArrayList<>();
ArrayList<Vertex> p = new ArrayList<>(graph);
bronKerboschWithoutPivot(r, p, x, "");
}

void saveClique(ArrayList<Vertex> r) {
private void saveClique(ArrayList<Vertex> r) {
ArrayList<String> maxClique = new ArrayList<>();
for (Vertex v : r) {
maxClique.add(vertexIndexName.get(v.getX()));
maxClique.add(vertexIndexToName.get(v.getX()));
}
allMaxCliques.add(maxClique);
}

private void initialiseMap(Graph g) {
for (int x = 0; x < g.getVertices().size(); x++) {
vertexNameIndex.put(g.getVertices().get(x), x);
vertexIndexName.put(x, g.getVertices().get(x));
private void initialiseMap(Graph graph) {
for (int x = 0; x < graph.getVertices().size(); x++) {
vertexNameToIndex.put(graph.getVertices().get(x), x);
vertexIndexToName.put(x, graph.getVertices().get(x));
}
}

public static ArrayList<ArrayList<String>> getAllMaxCliques(Graph g) {

MaxCliqueEnumerator ff = new MaxCliqueEnumerator();

ff.initialiseMap(g);
ff.readNextGraph(g);
ff.bronKerboschPivotExecute();
return ff.allMaxCliques;
}

}

0 comments on commit 9342e87

Please sign in to comment.