aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio3
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-04-29 08:09:28 +0200
committerJonas Smedegaard <dr@jones.dk>2025-04-29 08:09:28 +0200
commit3738c1fc8b2fa92b819b1ff948b3b39de60757b7 (patch)
treec5db7a878770da6c8180cc468184712d585b0d88 /src/com.example.portfolio3
parent794be4694f1a1fa36d87543aab840331c6d39745 (diff)
always use brace in for- and if-construct
Diffstat (limited to 'src/com.example.portfolio3')
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java3
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java16
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java15
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java11
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java74
-rw-r--r--src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java14
6 files changed, 90 insertions, 43 deletions
diff --git a/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java
index 793260a..dbd4aa8 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java
@@ -22,8 +22,9 @@ public abstract class AbstractGraph implements Graph {
/// @param s foo
/// @return Vertex
public Vertex vertex(final String s) {
- if (vertexMap.containsKey(s))
+ if (vertexMap.containsKey(s)) {
return vertexMap.get(s);
+ }
Vertex v = new Vertex(s);
vertexMap.put(s, v);
vertexSet.add(v);
diff --git a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java
index 88e54d6..f90172e 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java
@@ -20,23 +20,27 @@ public class AdjListGraph extends AbstractGraph {
/// foo
public void insertEdge(final Vertex v1, final Vertex v2, final int w) {
Edge e = new Edge(v1, v2, w);
- if (!outEdge.containsKey(e.from()))
+ if (!outEdge.containsKey(e.from())) {
outEdge.put(e.from(), new HashSet<Edge>());
+ }
outEdge.get(e.from()).add(e);
}
/// foo
public Collection<Edge> edges() {
Set<Edge> edges = new HashSet<>();
- for (Vertex v: outEdge.keySet())edges.addAll(outEdge.get(v));
+ for (Vertex v: outEdge.keySet()) {
+ edges.addAll(outEdge.get(v));
+ }
return edges;
}
/// foo
public Collection<Edge> outEdge(final Vertex v) {
- if (!outEdge.containsKey(v))
+ if (!outEdge.containsKey(v)) {
return new HashSet<Edge>();
+ }
return outEdge.get(v);
}
@@ -45,11 +49,13 @@ public class AdjListGraph extends AbstractGraph {
public Integer getWeight(final Vertex v1, final Vertex v2) {
// linear in number of outedges from vertices
- if (!outEdge.containsKey(v1))
+ if (!outEdge.containsKey(v1)) {
return null;
+ }
for (Edge e: outEdge.get(v1)) {
- if (e.to() == v2)
+ if (e.to() == v2) {
return e.weight();
+ }
}
return null;
diff --git a/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java
index e85db6c..b538f24 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java
@@ -20,17 +20,20 @@ class AdjMapGraph extends AbstractGraph {
/// foo
public void insertEdge(final Vertex v1, final Vertex v2, final int w) {
Edge e = new Edge(v1,v2, w);
- if (!outEdge.containsKey(e.from()))
+ if (!outEdge.containsKey(e.from())) {
outEdge.put(e.from(), new HashMap<Vertex, Edge>());
+ }
outEdge.get(e.from()).put(e.to(), e);
}
/// foo
public Collection<Edge> edges() {
Set<Edge> edges = new HashSet<>();
- for (Vertex v : outEdge.keySet())
- for (Vertex w : outEdge.get(v).keySet())
+ for (Vertex v : outEdge.keySet()) {
+ for (Vertex w : outEdge.get(v).keySet()) {
edges.add(outEdge.get(v).get(w));
+ }
+ }
return edges;
}
@@ -43,10 +46,12 @@ class AdjMapGraph extends AbstractGraph {
/// foo
public Integer getWeight(final Vertex v1, final Vertex v2) {
// constant time operation
- if (!outEdge.containsKey(v1))
+ if (!outEdge.containsKey(v1)) {
return null;
- if (!outEdge.get(v1).containsKey(v2))
+ }
+ if (!outEdge.get(v1).containsKey(v2)) {
return null;
+ }
return outEdge.get(v1).get(v2).weight();
}
diff --git a/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java b/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
index 8bd9259..2f3b036 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java
@@ -29,9 +29,11 @@ class EdgeGraph extends AbstractGraph {
/// foo
public Collection<Edge> outEdge(final Vertex v) {
ArrayList<Edge> outEdge = new ArrayList<>();
- for (Edge e: edges)
- if (e.from() == v)
+ for (Edge e: edges) {
+ if (e.from() == v) {
outEdge.add(e);
+ }
+ }
return outEdge;
}
@@ -40,9 +42,10 @@ class EdgeGraph extends AbstractGraph {
public Integer getWeight(final Vertex v1, final Vertex v2) {
// linear in number of edges in the graph
- for (Edge e:edges) {
- if (e.from() == v1 && e.to() == v2)
+ for (Edge e: edges) {
+ if (e.from() == v1 && e.to() == v2) {
return e.weight();
+ }
}
return null;
diff --git a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java
index 6b47bd2..5c0dd80 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java
@@ -40,8 +40,9 @@ 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;
+ }
}
return true;
@@ -58,8 +59,9 @@ public class GraphAlgorithms {
int length = 0;
for (int i = 1; i < path.size(); i++) {
Integer w = g.getWeight(path.get(i - 1),path.get(i));
- if (w == null)
+ if (w == null) {
return null;
+ }
length += w;
}
@@ -79,10 +81,12 @@ public class GraphAlgorithms {
/// @return int
static int cmpEdgeWeight(final Edge e1, final Edge e2) {
int w1 = e1.weight(), w2 = e2.weight();
- if (w1 != w2)
+ if (w1 != w2) {
return w1 - w2;
- if (e1.from() != e2.from())
+ }
+ if (e1.from() != e2.from()) {
return e1.from().name().compareTo(e2.from().name());
+ }
return e1.to().name().compareTo(e2.to().name());
}
@@ -95,11 +99,13 @@ public class GraphAlgorithms {
/// @param e2 foo
/// @return int
static int cmpEdgeFrom(final Edge e1, final Edge e2) {
- if (e1.from() != e2.from())
+ if (e1.from() != e2.from()) {
return e1.from().name().compareTo(e2.from().name());
+ }
int w1 = e1.weight(), w2 = e2.weight();
- if (w1 != w2)
+ if (w1 != w2) {
return w1 - w2;
+ }
return e1.to().name().compareTo(e2.to().name());
}
@@ -112,10 +118,12 @@ public class GraphAlgorithms {
/// @param e2 foo
/// @return int
static int cmpEdgeTo(final Edge e1, final Edge e2) {
- if (e1.to() != e2.to())
+ if (e1.to() != e2.to()) {
return e1.to().name().compareTo(e2.to().name());
- if (e1.from() != e2.from())
+ }
+ if (e1.from() != e2.from()) {
return e1.from().name().compareTo(e2.from().name());
+ }
int w1 = e1.weight(), w2 = e2.weight();
return w1 - w2;
@@ -186,13 +194,16 @@ public class GraphAlgorithms {
//System.out.println("visited " + w);
visited.add(w);
Collection<Edge> outedge = g.outEdge(w);
- if (outedge == null)
+ if (outedge == null) {
continue;
+ }
for (Edge e: outedge) {
- if (visited.contains(e.to()))
+ if (visited.contains(e.to())) {
continue;
- if (thisLevel.contains(e.to()))
+ }
+ if (thisLevel.contains(e.to())) {
continue;
+ }
nextLevel.add(e.to());
}
}
@@ -222,12 +233,14 @@ public class GraphAlgorithms {
/// @param v foo
/// @param visited foo
private static void visitDepthFirst(final Graph g, final Vertex v, final Set<Vertex> visited) {
- if (visited.contains(v))
+ if (visited.contains(v)) {
return;
+ }
//System.out.println("visited "+v);
visited.add(v);
- for (Edge e: g.outEdge(v))
+ for (Edge e: g.outEdge(v)) {
visitDepthFirst(g, e.to(), visited);
+ }
}
/// an implementation of Prim's algorithm
@@ -247,15 +260,19 @@ public class GraphAlgorithms {
while (true) {
Edge nearest = null;
for (Edge e: edges) {
- if (!frontier.contains(e.from()))
+ if (!frontier.contains(e.from())) {
continue;
- if (frontier.contains(e.to()))
+ }
+ if (frontier.contains(e.to())) {
continue;
- if (nearest == null || nearest.weight() > e.weight())
+ }
+ if (nearest == null || nearest.weight() > e.weight()) {
nearest = e;
+ }
}
- if (nearest == null)
+ if (nearest == null) {
break;
+ }
mst.add(nearest);
frontier.add(nearest.to());
}
@@ -278,8 +295,9 @@ public class GraphAlgorithms {
HashSet<Vertex> done = new HashSet<>();
HashMap<Vertex,Edge> prev = new HashMap<>();
HashMap<Vertex,Integer> weight = new HashMap<>();
- for (Vertex w: g.vertices())
+ for (Vertex w: g.vertices()) {
weight.put(w, maxint);
+ }
// start node is done, distance 0 from start
weight.put(start, 0);
@@ -294,7 +312,9 @@ public class GraphAlgorithms {
for (Vertex w1:done) {
for (Edge e :g.outEdge(w1)) {
Vertex w2 = e.to();
- if (done.contains(w2)) continue;
+ if (done.contains(w2)) {
+ continue;
+ }
if ((weight.get(w1) + e.weight()) < neardist) {
nearest = e.to();
neardist = weight.get(w1) + e.weight();
@@ -305,7 +325,9 @@ public class GraphAlgorithms {
// System.out.println("find nearest "+done2near);
// if no more, then we are done
- if (nearest == null) break;
+ if (nearest == null) {
+ break;
+ }
// update distance from this node to other nodes
for (Edge e1 : g.outEdge(nearest)) {
@@ -338,11 +360,13 @@ public class GraphAlgorithms {
try {
BufferedReader in = new BufferedReader(new FileReader(file));
for (String line = in.readLine(); line != null; line = in.readLine()) {
- if (line.length() == 0)
+ if (line.length() == 0) {
continue;
+ }
String[] arr = line.split(",");
- if (arr.length != 3)
+ if (arr.length != 3) {
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()));
}
@@ -358,8 +382,9 @@ public class GraphAlgorithms {
public static void printGraph(final Graph g) {
for (Vertex v: sortVertex(g.vertices())) {
System.out.println(v.toString());
- for (Edge e: sortEdgesTo(g.outEdge(v)))
+ for (Edge e: sortEdgesTo(g.outEdge(v))) {
System.out.println(" " + e.toString());
+ }
}
}
@@ -389,8 +414,9 @@ public class GraphAlgorithms {
BufferedReader in = new BufferedReader(new FileReader(f));
while (true) {
String s = in.readLine();
- if (s == null)
+ if (s == null) {
break;
+ }
list.add(s);
}
in.close();
diff --git a/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java b/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
index 5a1f327..67c095e 100644
--- a/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
+++ b/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
@@ -37,11 +37,13 @@ public class MatrixGraph extends AbstractGraph {
/// @param v vertex
/// @return int
private int getIndex(final Vertex v) {
- if (vertex2index.containsKey(v))
+ if (vertex2index.containsKey(v)) {
return vertex2index.get(v);
+ }
int index = vertex2index.size();
- if (index >= index2vertex.length)
+ if (index >= index2vertex.length) {
throw new RuntimeException("Too many vertices in graph");
+ }
vertex2index.put(v, index);
index2vertex[index] = v;
@@ -59,7 +61,9 @@ public class MatrixGraph extends AbstractGraph {
for (int i = 0; i < numVertex; i++) {
for (int j = 0; j < numVertex; j++) {
Integer weight = matrix[i][j]; // may be null
- if (weight == null) continue;
+ if (weight == null) {
+ continue;
+ }
edges.add(new Edge(index2vertex[i],index2vertex[j],weight));
}
}
@@ -73,7 +77,9 @@ public class MatrixGraph extends AbstractGraph {
int i = vertex2index.get(v1);
for (int j = 0; j < numVertex; j++) {
Integer weight = matrix[i][j]; // may be null
- if (weight == null) continue;
+ if (weight == null) {
+ continue;
+ }
edges.add(new Edge(v1,index2vertex[j],weight));
}