summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:41:54 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:41:54 -0500
commiteb30c23bb03579bf839189ab0d2ad172d5b07766 (patch)
tree332c7f409a98e5863bcaf27705f4527e05e85c19 /BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java
parent99e79bca7c1fe3c43ca2ec67e36b8e323c5f7107 (diff)
Commenting of various things and minor refactoring
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java118
1 files changed, 85 insertions, 33 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);