summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java118
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java51
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/graph/GenHolder.java75
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java17
4 files changed, 148 insertions, 113 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java
index 7a79233..3b6d6ef 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java
@@ -10,9 +10,68 @@ import java.util.Scanner;
import java.util.Set;
import java.util.stream.IntStream;
+import bjc.utils.data.GenHolder;
+
+/**
+ * An adjacency map representing a graph
+ *
+ * @author ben
+ *
+ * @param <T>
+ * The type of the nodes in the graph
+ */
public class AdjacencyMap<T> {
+ /**
+ * Create an adjacency map from a stream of text
+ *
+ * @param stream
+ * The stream of text to read in
+ * @return An adjacency map defined by the text
+ */
+ public static AdjacencyMap<Integer> fromStream(InputStream stream) {
+ Scanner scn = new Scanner(stream);
+ scn.useDelimiter("\n");
+
+ // First, read in number of vertices
+ int numVertices = Integer.parseInt(scn.next());
+
+ Set<Integer> vertices = new HashSet<>();
+ IntStream.range(0, numVertices).forEach(e -> vertices.add(e));
+
+ // Create the adjacency map
+ AdjacencyMap<Integer> aMap = new AdjacencyMap<>(vertices);
+
+ GenHolder<Integer> row = new GenHolder<>(0);
+
+ scn.forEachRemaining((strang) -> {
+ String[] parts = strang.split(" ");
+ int col = 0;
+
+ for (String part : parts) {
+ aMap.setWeight(row.held, col, Integer.parseInt(part));
+
+ col++;
+ }
+
+ row.held++;
+ });
+
+ scn.close();
+
+ return aMap;
+ }
+
+ /**
+ * The backing storage of the map
+ */
private Map<T, Map<T, Integer>> adjMap;
+ /**
+ * Create a new map from a set of vertices
+ *
+ * @param vertices
+ * The set of vertices to create a map from
+ */
public AdjacencyMap(Set<T> vertices) {
vertices.forEach(src -> {
Map<T, Integer> srcRow = new HashMap<>();
@@ -23,6 +82,11 @@ public class AdjacencyMap<T> {
});
}
+ /**
+ * Check if the graph is directed
+ *
+ * @return Whether or not the graph is directed
+ */
public boolean isDirected() {
GenHolder<Boolean> res = new GenHolder<>(true);
@@ -39,10 +103,25 @@ public class AdjacencyMap<T> {
return res.held;
}
+ /**
+ * Set the weight of an edge
+ *
+ * @param src
+ * The source node of the edge
+ * @param tgt
+ * The target node of the edge
+ * @param weight
+ * The weight of the edge
+ */
public void setWeight(T src, T tgt, int weight) {
adjMap.get(src).put(tgt, weight);
}
+ /**
+ * Convert this to a different graph representation
+ *
+ * @return The new representation of this graph
+ */
public Graph<T> toGraph() {
Graph<T> ret = new Graph<>();
@@ -54,39 +133,12 @@ public class AdjacencyMap<T> {
return ret;
}
- public static AdjacencyMap<Integer> fromStream(InputStream stream) {
- Scanner scn = new Scanner(stream);
- scn.useDelimiter("\n");
-
- // First, read in number of vertices
- int numVertices = Integer.parseInt(scn.next());
-
- Set<Integer> vertices = new HashSet<>();
- IntStream.range(0, numVertices).forEach(e -> vertices.add(e));
-
- // Create the adjacency map
- AdjacencyMap<Integer> aMap = new AdjacencyMap<>(vertices);
-
- GenHolder<Integer> row = new GenHolder<>(0);
-
- scn.forEachRemaining((strang) -> {
- String[] parts = strang.split(" ");
- int col = 0;
-
- for (String part : parts) {
- aMap.setWeight(row.held, col, Integer.parseInt(part));
-
- col++;
- }
-
- row.held++;
- });
-
- scn.close();
-
- return aMap;
- }
-
+ /**
+ * Convert an adjacency map back into a stream
+ *
+ * @param os
+ * The stream to convert to
+ */
public void toStream(OutputStream os) {
PrintStream ps = new PrintStream(os);
diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java b/BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java
index 9fb20c0..cf0764b 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java
@@ -1,27 +1,68 @@
package bjc.utils.graph;
+/**
+ * An edge in a weighted graph
+ *
+ * @author ben
+ *
+ * @param <T>
+ * The type of the nodes in the graph
+ */
public class Edge<T> {
- private final T source, target;
+ /**
+ * The distance from initial to terminal node
+ */
private final int distance;
+ /**
+ * The initial and terminal nodes of this edge
+ */
+ private final T source, target;
+
+ /**
+ * Create a new edge with set parameters
+ *
+ * @param node1
+ * The initial node of the edge
+ * @param node2
+ * The terminal node of the edge
+ * @param distance
+ * The distance between initial and terminal edge
+ */
public Edge(T node1, T node2, int distance) {
this.source = node1;
this.target = node2;
this.distance = distance;
}
+ /**
+ * Get the distance in this edge
+ *
+ * @return The distance between the initial and terminal nodes of this
+ * edge
+ */
+ public int getDistance() {
+ return distance;
+ }
+
+ /**
+ * Get the initial node of an edge
+ *
+ * @return The initial node of this edge
+ */
public T getSource() {
return source;
}
+ /**
+ * Get the target node of an edge
+ *
+ * @return The target node of this edge
+ */
public T getTarget() {
return target;
}
- public int getDistance() {
- return distance;
- }
-
@Override
public String toString() {
return " first vertex " + source + " to vertex " + target
diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/graph/GenHolder.java
deleted file mode 100644
index df363fa..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/graph/GenHolder.java
+++ /dev/null
@@ -1,75 +0,0 @@
-package bjc.utils.graph;
-
-import java.util.function.Function;
-
-/**
- * Holds a single value of a specific type. This is used for indirect
- * references to data, and more specifically for accessing non-final
- * variables from a lambda. AKA the identity monad
- *
- * @author ben
- *
- * @param <T>
- * The type of the data being held
- */
-public class GenHolder<T> {
- /**
- * The state this holder is responsible for.
- */
- public T held;
-
- /**
- * Creates a new empty holder, with its state set to null
- */
- public GenHolder() {
- held = null;
- }
-
- /**
- * Creates a new holder, with its state initialized to the provided
- * value
- *
- * @param held
- * The state to initialize this holder to.
- */
- public GenHolder(T hld) {
- held = hld;
- }
-
- /**
- * Apply the given transformation to the held value. Returns the holder
- * for allowing chaining of transforms
- *
- * @param f
- * The transform to apply to the value
- * @return The holder
- */
- public GenHolder<T> transform(Function<T, T> f) {
- held = f.apply(held);
-
- return this;
- }
-
- /**
- * Return the result of applying the given transformation to the held
- * value Doesn't change the held value
- *
- * @param f
- * The transformation to apply
- * @return A holder with the transformed value
- */
- public <NewT> GenHolder<NewT> map(Function<T, NewT> f) {
- return new GenHolder<NewT>(f.apply(held));
- }
-
- /**
- * Returns a raw mapped value, not contained in a GenHolder
- *
- * @param f
- * The function to use for mapping the value
- * @return The mapped value outside of a GenHolder
- */
- public <E> E unwrap(Function<T, E> f) {
- return f.apply(held);
- }
-}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java
index 69e42e4..03bc0c2 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java
@@ -13,6 +13,8 @@ import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
+import bjc.utils.data.GenHolder;
+
/**
* A directed weighted graph, where the vertices have some arbitrary label
*
@@ -23,6 +25,9 @@ import java.util.function.BiPredicate;
*/
public class Graph<T> {
+ /**
+ * The backing representation of the graph
+ */
private final Map<T, Map<T, Integer>> graph;
/**
@@ -165,6 +170,11 @@ public class Graph<T> {
return minEdges;
}
+ /**
+ * Get the count of the vertices in this graph
+ *
+ * @return A count of the vertices in this graph
+ */
public int getVertexCount() {
return graph.size();
}
@@ -225,6 +235,13 @@ public class Graph<T> {
return aMap;
}
+ /**
+ * Create a graph from a list of edges
+ *
+ * @param edges
+ * The list of edges to build from
+ * @return A graph built from the provided edge-list
+ */
public static <E> Graph<E> fromEdgeList(List<Edge<E>> edges) {
Graph<E> g = new Graph<>();