summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/graph
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/graph')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java55
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java58
2 files changed, 59 insertions, 54 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 247ee31..a6e8eef 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java
@@ -3,16 +3,15 @@ package bjc.utils.graph;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.InputMismatchException;
-import java.util.Map;
-import java.util.Map.Entry;
import java.util.Scanner;
-import java.util.Set;
import java.util.stream.IntStream;
import bjc.utils.data.GenHolder;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.FunctionalMap;
+import bjc.utils.funcdata.IFunctionalList;
+import bjc.utils.funcdata.IFunctionalMap;
/**
* An adjacency map representing a graph
@@ -65,7 +64,7 @@ public class AdjacencyMap<T> {
"The number of vertices must be greater than 0");
}
- Set<Integer> vertices = new HashSet<>();
+ IFunctionalList<Integer> vertices = new FunctionalList<>();
IntStream.range(0, numVertices)
.forEach(element -> vertices.add(element));
@@ -91,8 +90,9 @@ public class AdjacencyMap<T> {
try {
columnWeight = Integer.parseInt(part);
} catch (NumberFormatException nfex) {
- InputMismatchException imex = new InputMismatchException(
- "" + part + " is not a valid weight.");
+ InputMismatchException imex =
+ new InputMismatchException("" + part
+ + " is not a valid weight.");
imex.initCause(nfex);
@@ -119,7 +119,8 @@ public class AdjacencyMap<T> {
/**
* The backing storage of the map
*/
- private Map<T, Map<T, Integer>> adjacencyMap = new HashMap<>();
+ private IFunctionalMap<T, IFunctionalMap<T, Integer>> adjacencyMap =
+ new FunctionalMap<>();
/**
* Create a new map from a set of vertices
@@ -127,13 +128,13 @@ public class AdjacencyMap<T> {
* @param vertices
* The set of vertices to create a map from
*/
- public AdjacencyMap(Set<T> vertices) {
+ public AdjacencyMap(IFunctionalList<T> vertices) {
if (vertices == null) {
throw new NullPointerException("Vertices must not be null");
}
vertices.forEach(vertex -> {
- Map<T, Integer> vertexRow = new HashMap<>();
+ IFunctionalMap<T, Integer> vertexRow = new FunctionalMap<>();
vertices.forEach(
targetVertex -> vertexRow.put(targetVertex, 0));
@@ -150,16 +151,11 @@ public class AdjacencyMap<T> {
public boolean isDirected() {
GenHolder<Boolean> result = new GenHolder<>(true);
- adjacencyMap.entrySet().forEach(mapEntry -> {
- Set<Entry<T, Integer>> entryVertices = mapEntry.getValue()
- .entrySet();
+ adjacencyMap.forEach((key, value) -> {
+ value.forEach((targetKey, targetValue) -> {
+ int inverseValue = adjacencyMap.get(targetKey).get(key);
- entryVertices.forEach(targetVertex -> {
- int leftValue = targetVertex.getValue();
- int rightValue = adjacencyMap.get(targetVertex.getKey())
- .get(mapEntry.getKey());
-
- if (leftValue != rightValue) {
+ if (targetValue != inverseValue) {
result.transform((bool) -> false);
}
});
@@ -206,11 +202,11 @@ public class AdjacencyMap<T> {
public Graph<T> toGraph() {
Graph<T> returnedGraph = new Graph<>();
- adjacencyMap.entrySet().forEach(sourceVertex -> sourceVertex
- .getValue().entrySet()
- .forEach(targetVertex -> returnedGraph.addEdge(
- sourceVertex.getKey(), targetVertex.getKey(),
- targetVertex.getValue())));
+ adjacencyMap.forEach((key, value) -> {
+ value.forEach((targetKey, targetValue) -> {
+ returnedGraph.addEdge(key, targetKey, targetValue, true);
+ });
+ });
return returnedGraph;
}
@@ -229,10 +225,11 @@ public class AdjacencyMap<T> {
PrintStream outputPrinter = new PrintStream(outputSink);
- adjacencyMap.entrySet().forEach(sourceVertex -> {
- sourceVertex.getValue().entrySet()
- .forEach(targetVertex -> outputPrinter.printf("%d ",
- targetVertex.getValue()));
+ adjacencyMap.forEach((key, value) -> {
+ value.forEach((targetKey, targetValue) -> {
+ outputPrinter.printf("%d", targetValue);
+ });
+
outputPrinter.println();
});
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 f6bfc85..8041604 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java
@@ -1,11 +1,8 @@
package bjc.utils.graph;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
@@ -15,6 +12,9 @@ import java.util.function.BiPredicate;
import bjc.utils.data.GenHolder;
import bjc.utils.data.IHolder;
+import bjc.utils.funcdata.FunctionalMap;
+import bjc.utils.funcdata.IFunctionalList;
+import bjc.utils.funcdata.IFunctionalMap;
/**
* A directed weighted graph, where the vertices have some arbitrary label
@@ -29,13 +29,13 @@ public class Graph<T> {
/**
* The backing representation of the graph
*/
- private final Map<T, Map<T, Integer>> backingGraph;
+ private final IFunctionalMap<T, IFunctionalMap<T, Integer>> backingGraph;
/**
* Create a new graph
*/
public Graph() {
- backingGraph = new HashMap<>();
+ backingGraph = new FunctionalMap<>();
}
/**
@@ -47,8 +47,11 @@ public class Graph<T> {
* The target vertex for this edge
* @param distance
* The distance from the source vertex to the target vertex
+ * @param directed
+ * Whether or not
*/
- public void addEdge(T sourceVertex, T targetVertex, int distance) {
+ public void addEdge(T sourceVertex, T targetVertex, int distance,
+ boolean directed) {
// Can't add edges with a null source or target
if (sourceVertex == null) {
throw new NullPointerException(
@@ -60,17 +63,22 @@ public class Graph<T> {
// Initialize adjacency list for vertices if necessary
if (!backingGraph.containsKey(sourceVertex)) {
- backingGraph.put(sourceVertex, new HashMap<T, Integer>());
- }
- if (!backingGraph.containsKey(targetVertex)) {
- backingGraph.put(targetVertex, new HashMap<T, Integer>());
+ backingGraph.put(sourceVertex,
+ new FunctionalMap<T, Integer>());
}
// Add the edge to the graph
backingGraph.get(sourceVertex).put(targetVertex, distance);
- // Uncomment this to make the graph undirected
- // graph.get(target).put(source, distance);
+ // Handle possible directed edges
+ if (!directed) {
+ if (!backingGraph.containsKey(targetVertex)) {
+ backingGraph.put(targetVertex,
+ new FunctionalMap<T, Integer>());
+ }
+
+ backingGraph.get(targetVertex).put(sourceVertex, distance);
+ }
}
/**
@@ -107,7 +115,7 @@ public class Graph<T> {
* The vertex to use as a source
* @return All of the edges with the specified vertex as a source
*/
- public Map<T, Integer> getEdges(T source) {
+ public IFunctionalMap<T, Integer> getEdges(T source) {
// Can't find edges for a null source
if (source == null) {
throw new NullPointerException("The source cannot be null.");
@@ -116,7 +124,7 @@ public class Graph<T> {
"Vertex " + source + " is not in graph");
}
- return Collections.unmodifiableMap(backingGraph.get(source));
+ return backingGraph.get(source);
}
/**
@@ -125,7 +133,7 @@ public class Graph<T> {
* @return The initial vertex of the graph
*/
public T getInitial() {
- return backingGraph.keySet().iterator().next();
+ return backingGraph.keyList().first();
}
/**
@@ -195,7 +203,7 @@ public class Graph<T> {
* @return A count of the vertices in this graph
*/
public int getVertexCount() {
- return backingGraph.size();
+ return backingGraph.getSize();
}
/**
@@ -203,8 +211,8 @@ public class Graph<T> {
*
* @return A unmodifiable set of all the vertices in the graph.
*/
- public Set<T> getVertices() {
- return Collections.unmodifiableSet(backingGraph.keySet());
+ public IFunctionalList<T> getVertices() {
+ return backingGraph.keyList();
}
/**
@@ -249,13 +257,13 @@ public class Graph<T> {
*/
public AdjacencyMap<T> toAdjacencyMap() {
AdjacencyMap<T> adjacencyMap =
- new AdjacencyMap<>(backingGraph.keySet());
+ new AdjacencyMap<>(backingGraph.keyList());
- backingGraph.entrySet().forEach(sourceVertex -> sourceVertex
- .getValue()
- .forEach((targetVertex, vertexWeight) -> adjacencyMap
- .setWeight(sourceVertex.getKey(), targetVertex,
- vertexWeight)));
+ backingGraph.forEach((key, value) -> {
+ value.forEach((targetKey, targetValue) -> {
+ adjacencyMap.setWeight(key, targetKey, targetValue);
+ });
+ });
return adjacencyMap;
}
@@ -274,7 +282,7 @@ public class Graph<T> {
Graph<E> g = new Graph<>();
edges.forEach(edge -> g.addEdge(edge.getSource(), edge.getTarget(),
- edge.getDistance()));
+ edge.getDistance(), true));
return g;
}