From 4a96d9cad446ea405b51dfeebb01a1b6d7f6fb2b Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 27 Sep 2022 19:21:16 -0400 Subject: Add some interesting new things Adds a number of things based off of some of the notes I've made over time, plus a few papers I've read. More details to come later, whenever I decide to actually get serious about documentation and examples and the like --- .../main/java/bjc/utils/graph/SimpleAlgGraph.java | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 base/src/main/java/bjc/utils/graph/SimpleAlgGraph.java (limited to 'base/src/main/java/bjc/utils/graph/SimpleAlgGraph.java') diff --git a/base/src/main/java/bjc/utils/graph/SimpleAlgGraph.java b/base/src/main/java/bjc/utils/graph/SimpleAlgGraph.java new file mode 100644 index 0000000..2c7ba08 --- /dev/null +++ b/base/src/main/java/bjc/utils/graph/SimpleAlgGraph.java @@ -0,0 +1,83 @@ +package bjc.utils.graph; + +import java.util.*; + +/** + * A basic implementation of a directed algebraic graph. + * + * @author bjcul + * + * @param The type of the vertices + */ +public class SimpleAlgGraph implements AlgGraph> { + // TODO: consider if some function for labeling edges would make sense + private Set vertices; + private Map edges; + + /** + * Create a new empty graph. + */ + public SimpleAlgGraph() { + vertices = new HashSet<>(); + edges = new HashMap<>(); + } + + /** + * Create a new graph with the given vertices, but no edges. + * + * @param vertexes The vertices for the graph. + */ + @SafeVarargs + public SimpleAlgGraph(Vertex... vertexes) { + this(); + + for (Vertex vertex : vertexes) + vertices.add(vertex); + } + + @Override + public SimpleAlgGraph empty() { + return new SimpleAlgGraph<>(); + } + + @Override + public SimpleAlgGraph vertex(B val) { + return new SimpleAlgGraph<>(); + } + + /** + * Overlay a graph onto this one, adding its vertices and edges. + * + * @param other The graph to overlay + */ + @Override + public void overlay(AlgGraph> other) { + SimpleAlgGraph graph = (SimpleAlgGraph) other; + + vertices.addAll(graph.vertices); + edges.putAll(graph.edges); + } + + /** + * Connect a graph to this one, adding its vertices and edges; as well as + * establishing an edge from each node in this graph, to each node in the other + * graph. + * + * @param other The graph to connect + */ + @Override + public void connect(AlgGraph> other) { + SimpleAlgGraph graph = (SimpleAlgGraph) other; + + // note: this could be inefficent for large graphs + for (Vertex left : vertices) { + for (Vertex right : graph.vertices) { + edges.put(left, right); + } + } + + overlay(other); + } + + +} \ No newline at end of file -- cgit v1.2.3