aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio3
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-04-29 09:34:35 +0200
committerJonas Smedegaard <dr@jones.dk>2025-04-29 09:34:35 +0200
commit95d3e8a088f033da51cabe34bcdbba3061760b5e (patch)
treeb484427c919d47809f4f39fa072a365cafeb23be /src/com.example.portfolio3
parentf0c7a1872b667858155ca30349b80b9ff240fb1d (diff)
wrap long lines
Diffstat (limited to 'src/com.example.portfolio3')
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java4
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java11
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java10
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java4
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java91
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java27
6 files changed, 105 insertions, 42 deletions
diff --git a/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java
index 514ac1e..9d356e8 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java
@@ -33,7 +33,9 @@ public abstract class AbstractGraph implements Graph {
}
/// foo
- public final void insertEdge(final String v, final String u, final int w) {
+ public final void insertEdge(
+ final String v, final String u, final int w
+ ) {
insertEdge(vertex(v), vertex(u), w);
}
diff --git a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java
index 8484d58..d9a5d19 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java
@@ -8,7 +8,8 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-/// Adjecency List Graph - A map from vertices to set of outedges from the vertex
+/// Adjecency List Graph - A map from vertices
+/// to set of outedges from the vertex
public class AdjListGraph extends AbstractGraph {
/// foo
@@ -18,7 +19,9 @@ public class AdjListGraph extends AbstractGraph {
private Map<Vertex, Set<Edge>> outEdge = new HashMap<>();
/// foo
- public final void insertEdge(final Vertex v1, final Vertex v2, final int w) {
+ public final void insertEdge(
+ final Vertex v1, final Vertex v2, final int w
+ ) {
Edge e = new Edge(v1, v2, w);
if (!outEdge.containsKey(e.from())) {
outEdge.put(e.from(), new HashSet<Edge>());
@@ -46,7 +49,9 @@ public class AdjListGraph extends AbstractGraph {
}
/// foo
- public final Integer getWeight(final Vertex v1, final Vertex v2) {
+ public final Integer getWeight(
+ final Vertex v1, final Vertex v2
+ ) {
// linear in number of outedges from vertices
if (!outEdge.containsKey(v1)) {
diff --git a/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java
index 8c2c139..d5749a9 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java
@@ -8,7 +8,8 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-/// Adjecency Map Graph - A map from vertices to map of target vertex to edge
+/// Adjecency Map Graph - A map from vertices
+/// to map of target vertex to edge
class AdjMapGraph extends AbstractGraph {
/// foo
@@ -18,10 +19,13 @@ class AdjMapGraph extends AbstractGraph {
private Map<Vertex, Map<Vertex, Edge>> outEdge = new HashMap<>();
/// foo
- public void insertEdge(final Vertex v1, final Vertex v2, final int w) {
+ public void insertEdge(
+ final Vertex v1, final Vertex v2, final int w
+ ) {
Edge e = new Edge(v1, v2, w);
if (!outEdge.containsKey(e.from())) {
- outEdge.put(e.from(), new HashMap<Vertex, Edge>());
+ outEdge.put(e.from(),
+ new HashMap<Vertex, Edge>());
}
outEdge.get(e.from()).put(e.to(), e);
}
diff --git a/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java b/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
index 2f3b036..4fe3930 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
@@ -17,7 +17,9 @@ class EdgeGraph extends AbstractGraph {
Set<Edge> edges = new HashSet<>();
/// foo
- public void insertEdge(final Vertex v1, final Vertex v2, final int w) {
+ public void insertEdge(
+ final Vertex v1, final Vertex v2, final int w
+ ) {
edges.add(new Edge(v1, v2, w));
}
diff --git a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java
index 4d8a8c2..61ef6c2 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java
@@ -21,7 +21,7 @@ public class GraphAlgorithms {
/// foo
GraphAlgorithms() { }
- /// Calculates the length of a path or any other collection of edes
+ /// calculate the length of a path or another collection of edges
///
/// does not require the edges to form a path
///
@@ -39,7 +39,8 @@ public class GraphAlgorithms {
/// @return boolean
public static boolean isPath(final List<Edge> edges) {
for (int i = 1; i < edges.size(); i++) {
- if (edges.get(i - 1).to() != edges.get(i).from()) {
+ if (edges.get(i - 1).to() != edges.get(i).from()
+ ) {
return false;
}
}
@@ -54,10 +55,13 @@ public class GraphAlgorithms {
/// @param g foo
/// @param path foo
/// @return Integer
- public static Integer pathLength(final Graph g, final List<Vertex> path) {
+ public static Integer pathLength(
+ final Graph g, final List<Vertex> path
+ ) {
int length = 0;
for (int i = 1; i < path.size(); i++) {
- Integer w = g.getWeight(path.get(i - 1), path.get(i));
+ Integer w = g.getWeight(
+ path.get(i - 1), path.get(i));
if (w == null) {
return null;
}
@@ -84,7 +88,8 @@ public class GraphAlgorithms {
return w1 - w2;
}
if (e1.from() != e2.from()) {
- return e1.from().name().compareTo(e2.from().name());
+ return e1.from().name().compareTo(
+ e2.from().name());
}
return e1.to().name().compareTo(e2.to().name());
@@ -99,7 +104,8 @@ public class GraphAlgorithms {
/// @return int
static int cmpEdgeFrom(final Edge e1, final Edge e2) {
if (e1.from() != e2.from()) {
- return e1.from().name().compareTo(e2.from().name());
+ return e1.from().name().compareTo(
+ e2.from().name());
}
int w1 = e1.weight(), w2 = e2.weight();
if (w1 != w2) {
@@ -118,10 +124,12 @@ public class GraphAlgorithms {
/// @return int
static int cmpEdgeTo(final Edge e1, final Edge e2) {
if (e1.to() != e2.to()) {
- return e1.to().name().compareTo(e2.to().name());
+ return e1.to().name().compareTo(
+ e2.to().name());
}
if (e1.from() != e2.from()) {
- return e1.from().name().compareTo(e2.from().name());
+ return e1.from().name().compareTo(
+ e2.from().name());
}
int w1 = e1.weight(), w2 = e2.weight();
@@ -165,9 +173,12 @@ public class GraphAlgorithms {
///
/// @param vertices foo
/// @return List<Vertex>
- public static List<Vertex> sortVertex(final Collection<Vertex> vertices) {
+ public static List<Vertex> sortVertex(
+ final Collection<Vertex> vertices
+ ) {
ArrayList<Vertex> list = new ArrayList<>(vertices);
- Collections.sort(list, (Vertex v1, Vertex v2) -> v1.name().compareTo(v2.name()));
+ Collections.sort(list, (Vertex v1, Vertex v2) ->
+ v1.name().compareTo(v2.name()));
return list;
}
@@ -182,7 +193,9 @@ public class GraphAlgorithms {
/// @param g foo
/// @param v foo
/// @return Set<Vertex>
- public static Set<Vertex> visitBreadthFirst(final Graph g, final Vertex v) {
+ public static Set<Vertex> visitBreadthFirst(
+ final Graph g, final Vertex v
+ ) {
HashSet<Vertex> thisLevel = new HashSet<>();
HashSet<Vertex> nextLevel = new HashSet<>();
HashSet<Vertex> visited = new HashSet<>();
@@ -219,7 +232,9 @@ public class GraphAlgorithms {
/// @param g foo
/// @param v foo
/// @return Set<Vertex>
- public static Set<Vertex> visitDepthFirst(final Graph g, final Vertex v) {
+ public static Set<Vertex> visitDepthFirst(
+ final Graph g, final Vertex v
+ ) {
HashSet<Vertex> visit = new HashSet<>();
visitDepthFirst(g, v, visit);
@@ -231,7 +246,9 @@ public class GraphAlgorithms {
/// @param g foo
/// @param v foo
/// @param visited foo
- private static void visitDepthFirst(final Graph g, final Vertex v, final Set<Vertex> visited) {
+ private static void visitDepthFirst(
+ final Graph g, final Vertex v, final Set<Vertex> visited
+ ) {
if (visited.contains(v)) {
return;
}
@@ -265,7 +282,9 @@ public class GraphAlgorithms {
if (frontier.contains(e.to())) {
continue;
}
- if (nearest == null || nearest.weight() > e.weight()) {
+ if (nearest == null
+ || nearest.weight() > e.weight()
+ ) {
nearest = e;
}
}
@@ -287,7 +306,9 @@ public class GraphAlgorithms {
/// @param g foo
/// @param start foo
/// @return Set<Edge>
- public static Set<Edge> dijkstra(final Graph g, final Vertex start) {
+ public static Set<Edge> dijkstra(
+ final Graph g, final Vertex start
+ ) {
// create table for done, prev and weight from start
int maxint = Integer.MAX_VALUE;
@@ -314,15 +335,19 @@ public class GraphAlgorithms {
if (done.contains(w2)) {
continue;
}
- if ((weight.get(w1) + e.weight()) < neardist) {
+ if ((weight.get(w1)
+ + e.weight())
+ < neardist
+ ) {
nearest = e.to();
- neardist = weight.get(w1) + e.weight();
+ neardist = weight.get(w1)
+ + e.weight();
done2near = e;
}
}
}
- // System.out.println("find nearest "+done2near);
+ //System.out.println("find nearest "+done2near);
// if no more, then we are done
if (nearest == null) {
break;
@@ -357,17 +382,27 @@ public class GraphAlgorithms {
/// @param file foo
public static void readGraph(final Graph g, final String file) {
try {
- BufferedReader in = new BufferedReader(new FileReader(file));
- for (String line = in.readLine(); line != null; line = in.readLine()) {
+ BufferedReader in = new BufferedReader(
+ new FileReader(file));
+ for (String line = in.readLine();
+ line != null;
+ line = in.readLine()
+ ) {
if (line.length() == 0) {
continue;
}
String[] arr = line.split(",");
if (arr.length != 3) {
- throw new RuntimeException("CSV file format error: " + line);
+ throw new RuntimeException(
+ "CSV file format error: "
+ + line);
}
- g.insertEdge(arr[0].trim(), arr[1].trim(), Integer.parseInt(arr[2].trim()));
- g.insertEdge(arr[1].trim(), arr[0].trim(), Integer.parseInt(arr[2].trim()));
+ g.insertEdge(arr[0].trim(),
+ arr[1].trim(),
+ Integer.parseInt(arr[2].trim()));
+ g.insertEdge(arr[1].trim(),
+ arr[0].trim(),
+ Integer.parseInt(arr[2].trim()));
}
in.close();
} catch (IOException e) {
@@ -391,9 +426,12 @@ public class GraphAlgorithms {
///
/// @param list foo
/// @param f foo
- public static void storeStrings(final List<String> list, final String f) {
+ public static void storeStrings(
+ final List<String> list, final String f
+ ) {
try {
- PrintWriter out = new PrintWriter(new FileWriter(f));
+ PrintWriter out = new PrintWriter(
+ new FileWriter(f));
for (String s: list) {
out.println(s);
}
@@ -410,7 +448,8 @@ public class GraphAlgorithms {
public static ArrayList<String> loadStrings(final String f) {
ArrayList<String> list = new ArrayList<>();
try {
- BufferedReader in = new BufferedReader(new FileReader(f));
+ BufferedReader in = new BufferedReader(
+ new FileReader(f));
while (true) {
String s = in.readLine();
if (s == null) {
diff --git a/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java b/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
index 41d5880..bd80c00 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
@@ -14,7 +14,8 @@ public class MatrixGraph extends AbstractGraph {
private Integer[][] matrix = null; // made in constructor
/// foo
- // We must be able to map vertices to index in matrix and back again
+ // We must be able to map vertices
+ // to index in matrix and back again
private Vertex[] index2vertex; // made in constructor
/// foo
@@ -42,7 +43,8 @@ public class MatrixGraph extends AbstractGraph {
}
int index = vertex2index.size();
if (index >= index2vertex.length) {
- throw new RuntimeException("Too many vertices in graph");
+ throw new RuntimeException(
+ "Too many vertices in graph");
}
vertex2index.put(v, index);
index2vertex[index] = v;
@@ -51,7 +53,9 @@ public class MatrixGraph extends AbstractGraph {
}
/// foo
- public final void insertEdge(final Vertex v1, final Vertex v2, final int w) {
+ public final void insertEdge(
+ final Vertex v1, final Vertex v2, final int w
+ ) {
matrix[getIndex(v1)][getIndex(v2)] = w;
}
@@ -60,11 +64,16 @@ public class MatrixGraph extends AbstractGraph {
HashSet<Edge> edges = new HashSet<>();
for (int i = 0; i < numVertex; i++) {
for (int j = 0; j < numVertex; j++) {
- Integer weight = matrix[i][j]; // may be null
+
+ // may be null
+ Integer weight = matrix[i][j];
if (weight == null) {
continue;
}
- edges.add(new Edge(index2vertex[i], index2vertex[j], weight));
+ edges.add(new Edge(
+ index2vertex[i],
+ index2vertex[j],
+ weight));
}
}
@@ -87,9 +96,11 @@ public class MatrixGraph extends AbstractGraph {
}
/// foo
- public final Integer getWeight(final Vertex v1, final Vertex v2) {
+ public final Integer getWeight(
+ final Vertex v1, final Vertex v2
+ ) {
- // constant time operation
- return matrix[vertex2index.get(v1)][vertex2index.get(v2)];
+ // constant time operation
+ return matrix[vertex2index.get(v1)][vertex2index.get(v2)];
}
}