Skip to content

Commit

Permalink
Override equals, hash and clone
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Kelly committed Apr 28, 2021
1 parent db9215a commit 2b1fecb
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 90 deletions.
30 changes: 20 additions & 10 deletions src/main/io/github/ethankelly/Equations.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ public static int getUpperBound(Graph g, char[] states, boolean closures) {

public static int getLowerBound(int numVertices, char[] states, boolean closures) {
//TODO this is true iff there are no cut vertices
Graph g = GraphGenerator.tree(numVertices);
assert g.isMinimallyConnected() : "Lower bound graph is not minimally connected";
Graph g = GraphGenerator.cycle(numVertices);
assert g.getCutVertices().isEmpty() : "Lower bound graph should have no cut vertices";
assert g.getConnectedComponents().size() == 1 : "Lower bound graph should be connected";
Tuple tuples = new Tuple(g, states, closures);
return tuples.getTuples().size();
}
Expand All @@ -110,18 +111,27 @@ public static int getLowerBound(Graph g, char[] states, boolean closures) {

public static void main(String[] args) {
char[] SIR = new char[]{'S', 'I', 'R'};
char[] SIRP = new char[]{'S', 'I', 'R','P'};
System.out.println("4-cycle");
System.out.println("Upper bound with closures: " + getUpperBound(4, SIR, true));
System.out.println("Lower bound with closures: " + getLowerBound(4, SIR, true));
System.out.println("Upper bound without closures: " + getUpperBound(4, SIR, false));
System.out.println("Lower bound without closures: " + getLowerBound(4, SIR, false));

Graph g = GraphGenerator.getLollipop();
System.out.println(g);
System.out.print("\nCut vertices: ");
g.getCutVertices().forEach(System.out::print);
System.out.println("\nUB with closures:" + getUpperBound(g, SIR, true));
System.out.println("LB with closures:" + getLowerBound(g, SIR, true));
System.out.println("\nUB with closures: " + getUpperBound(g.getNumVertices(), SIR, true));
System.out.println("LB with closures: " + getLowerBound(g.getNumVertices(), SIR, true));
List<Graph> subGraphs = g.splice();
for (Graph subGraph : subGraphs) {
System.out.println(subGraph);
}

Graph h = GraphGenerator.getBowTie();
System.out.println(h);
System.out.print("\nCut vertices: ");
h.getCutVertices().forEach(System.out::print);
System.out.println("\nUB with closures: " + getUpperBound(h.getNumVertices(), SIR, true));
System.out.println("LB with closures: " + getLowerBound(h.getNumVertices(), SIR, true));
List<Graph> subGraphsH = h.splice();
for (Graph subGraph : subGraphsH) {
System.out.println(subGraph);
}
}
}
96 changes: 62 additions & 34 deletions src/main/io/github/ethankelly/Graph.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package main.io.github.ethankelly;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -25,7 +23,7 @@
*
* @author <a href="mailto:[email protected]">Ethan Kelly</a>
*/
public class Graph {
public class Graph implements Cloneable {
private static final int NIL = -1;
private String name;
private int numVertices;
Expand All @@ -38,19 +36,22 @@ public class Graph {
* Class constructor.
*
* @param numVertices the number of vertices to create in the graph.
* @param adjMatrix
* @param transmissionMatrix
*/
public Graph(int numVertices, String name) {
public Graph(int numVertices, String name, boolean[][] adjMatrix, int[][] transmissionMatrix) {
this.numVertices = numVertices;
this.adjMatrix = new boolean[numVertices][numVertices];
this.transmissionMatrix = new int[numVertices][numVertices];
this.name = name;
this.adjMatrix = adjMatrix;
this.transmissionMatrix = transmissionMatrix;

}

public static void main(String[] args) {
// testMethods();

// Create a graph given in the above diagram
Graph g = new Graph(5, "Test"); // 5 vertices numbered from 0 to 4
Graph g = new Graph(5, "Test", new boolean[5][5], new int[5][5]); // 5 vertices numbered from 0 to 4
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
Expand Down Expand Up @@ -95,7 +96,7 @@ private static void testMethods() {
g3.getCutVertices() +
"\nTime: " + g3.time);

Graph g4 = new Graph(17, "Fig. 7");
Graph g4 = new Graph(17, "Fig. 7", new boolean[17][17], new int[17][17]);
g4.addEdge(0, 1);
g4.addEdge(0, 3);
g4.addEdge(1, 2);
Expand Down Expand Up @@ -218,8 +219,8 @@ public List<List<Integer>> getConnectedComponents() {
}

private Graph makeSubGraph(List<Integer> vertices) {

Graph subGraph = new Graph(vertices.size(), this.getName() + " Subgraph");
//TODO Add cut-vertex back into sub-graph along with edges!
Graph subGraph = new Graph(vertices.size(), this.getName() + " Subgraph", new boolean[numVertices][numVertices], new int[numVertices][numVertices]);
for (int v = 0; v < vertices.size(); v++) {
for (int w = 0; w < this.getNumVertices(); w++) {
if (this.isEdge(vertices.get(v), w) && vertices.contains(w)) subGraph.addEdge(v, vertices.indexOf(w));
Expand Down Expand Up @@ -300,17 +301,19 @@ public void removeEdge(int i, int j) {
}

public void removeBridges(int i) {
int numConnectedComponents = getConnectedComponents().size();
for (int j = 0; j < getNumVertices(); j++) {
int numConnectedComponents = getConnectedComponents().size();
if (isEdge(i, j)) {
removeEdge(i, j);
// Ensure any edges we remove are bridges; preserve other edges
if (getConnectedComponents().size() == numConnectedComponents) addEdge(i, j);
else for (List<Integer> cc : getConnectedComponents()) if (cc.size() == 1) addEdge(i, j);

// if (getConnectedComponents().size() != numConnectedComponents + 1) addEdge(i, j);
// else break;
}
}
}

@SuppressWarnings("unused")
public boolean isMinimallyConnected() {
boolean minConnected = true;
// Check if graph is connected at all
Expand All @@ -323,6 +326,7 @@ public boolean isMinimallyConnected() {
}
}
}
assert !minConnected || (this.getNumEdges() - 1 == this.getNumVertices()) : "There are n-1 edges in a minimally connected graph.";
return minConnected;
}

Expand Down Expand Up @@ -375,26 +379,6 @@ public boolean isEdge(int i, int j) {
return getAdjMatrix()[i][j] || getAdjMatrix()[j][i];
}

/**
* Returns a textual representation of a graph as an adjacency matrix to be printed to the standard output.
*
* @return a string representation of the graph.
*/
@Override
public String toString() {
int n = getNumVertices();
boolean[][] matrix = getAdjMatrix();
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(i).append(": ");
for (boolean j : matrix[i]) {
s.append(j ? 1 : 0).append(" ");
}
s.append("\n");
}
return s.toString();
}

@SuppressWarnings("unchecked")
// Helper - converts a boolean adjacency matrix to a linked list
private LinkedList<Integer>[] getAdjacencyList() {
Expand Down Expand Up @@ -449,4 +433,48 @@ public Graph setName(String name) {
return this;
}

/**
* Returns a textual representation of a graph as an adjacency matrix to be printed to the standard output.
*
* @return a string representation of the graph.
*/
@Override
public String toString() {
int n = getNumVertices();
boolean[][] matrix = getAdjMatrix();
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(i).append(": ");
for (boolean j : matrix[i]) {
s.append(j ? 1 : 0).append(" ");
}
s.append("\n");
}
return s.toString();
}

@Override
public Graph clone() {
try {
return (Graph) super.clone();
} catch (CloneNotSupportedException e) {
return new Graph(this.getNumVertices(), this.getName(), this.getAdjMatrix(), this.getTransmissionMatrix());
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Graph graph = (Graph) o;
return getNumVertices() == graph.getNumVertices() && getNumEdges() == graph.getNumEdges() && Objects.equals(getName(), graph.getName()) && Arrays.equals(getAdjMatrix(), graph.getAdjMatrix()) && Arrays.equals(getTransmissionMatrix(), graph.getTransmissionMatrix());
}

@Override
public int hashCode() {
int result = Objects.hash(getName(), getNumVertices(), getNumEdges());
result = 31 * result + Arrays.deepHashCode(getAdjMatrix());
result = 31 * result + Arrays.deepHashCode(getTransmissionMatrix());
return result;
}
}
Loading

0 comments on commit 2b1fecb

Please sign in to comment.