diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 06:03:56 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 06:11:41 +0200 |
commit | a5c3599d7bc7a9ef5583ad2d50a55975f030fbea (patch) | |
tree | db4ed74e48997d4b4450148116a9c990978a1641 /src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java | |
parent | 03d2d06e8f5bfadd79912efb94e8931e54296735 (diff) |
declare variables final when possible
Diffstat (limited to 'src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java')
-rw-r--r-- | src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java index 9337ac1..d2a20f2 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java +++ b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java @@ -27,7 +27,7 @@ public class GraphAlgorithms { /// does not require the edges to form a path /// @param edges foo /// @return int - public static int pathLength(Collection<Edge> edges){ + public static int pathLength(final Collection<Edge> edges){ return edges.stream().mapToInt(e-> e.weight()).sum(); } @@ -36,7 +36,7 @@ public class GraphAlgorithms { /// the to-vertex in one edge is the from-vertex of the next /// @param edges foo /// @return boolean - public static boolean isPath(List<Edge> edges){ + 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())return false; } @@ -49,7 +49,7 @@ public class GraphAlgorithms { /// @param g foo /// @param path foo /// @return Integer - public static Integer pathLength(Graph g,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)); @@ -69,7 +69,7 @@ public class GraphAlgorithms { /// @param e1 foo /// @param e2 foo /// @return int - static int cmpEdgeWeight(Edge e1,Edge e2) { + static int cmpEdgeWeight(final Edge e1, final Edge e2) { int w1=e1.weight(),w2=e2.weight(); if(w1!=w2)return w1-w2; if(e1.from()!=e2.from())return e1.from().name().compareTo(e2.from().name()); @@ -82,7 +82,7 @@ public class GraphAlgorithms { /// @param e1 foo /// @param e2 foo /// @return int - static int cmpEdgeFrom(Edge e1,Edge e2) { + static int cmpEdgeFrom(final Edge e1, final Edge e2) { if(e1.from()!=e2.from())return e1.from().name().compareTo(e2.from().name()); int w1=e1.weight(),w2=e2.weight(); if(w1!=w2)return w1-w2; @@ -95,7 +95,7 @@ public class GraphAlgorithms { /// @param e1 foo /// @param e2 foo /// @return int - static int cmpEdgeTo(Edge e1,Edge e2) { + static int cmpEdgeTo(final Edge e1, final Edge e2) { if(e1.to()!=e2.to())return e1.to().name().compareTo(e2.to().name()); if(e1.from()!=e2.from())return e1.from().name().compareTo(e2.from().name()); int w1=e1.weight(),w2=e2.weight(); @@ -105,7 +105,7 @@ public class GraphAlgorithms { /// sort a collection of edges based on their weights /// @param edges foo /// @return List<Edge> - static List<Edge> sortEdges(Collection<Edge> edges){ + static List<Edge> sortEdges(final Collection<Edge> edges){ ArrayList<Edge> list=new ArrayList<>(edges); Collections.sort(list,GraphAlgorithms::cmpEdgeWeight); return list; @@ -114,7 +114,7 @@ public class GraphAlgorithms { /// sort a collection of edges based on from-vertex /// @param edges foo /// @return List<Edge> - static List<Edge> sortEdgesFrom(Collection<Edge> edges){ + static List<Edge> sortEdgesFrom(final Collection<Edge> edges){ ArrayList<Edge> list=new ArrayList<>(edges); Collections.sort(list,GraphAlgorithms::cmpEdgeFrom); return list; @@ -123,7 +123,7 @@ public class GraphAlgorithms { /// sort a collection of edges based on to-vertex /// @param edges foo /// @return List<Edge> - static List<Edge> sortEdgesTo(Collection<Edge> edges){ + static List<Edge> sortEdgesTo(final Collection<Edge> edges){ ArrayList<Edge> list=new ArrayList<>(edges); Collections.sort(list,GraphAlgorithms::cmpEdgeTo); return list; @@ -132,7 +132,7 @@ public class GraphAlgorithms { /// sort a collection of vertices based on their name /// @param vertices foo /// @return List<Vertex> - public static List<Vertex> sortVertex(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())); return list; @@ -147,7 +147,7 @@ public class GraphAlgorithms { /// @param g foo /// @param v foo /// @return Set<Vertex> - public static Set<Vertex> visitBreadthFirst(Graph g,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<>(); @@ -176,7 +176,7 @@ public class GraphAlgorithms { /// @param g foo /// @param v foo /// @return Set<Vertex> - public static Set<Vertex> visitDepthFirst(Graph g,Vertex v){ + public static Set<Vertex> visitDepthFirst(final Graph g, final Vertex v){ HashSet<Vertex> visit=new HashSet<>(); visitDepthFirst(g, v,visit); return visit; @@ -186,7 +186,7 @@ public class GraphAlgorithms { /// @param g foo /// @param v foo /// @param visited foo - private static void visitDepthFirst(Graph g,Vertex v,Set<Vertex> visited){ + private static void visitDepthFirst(final Graph g, final Vertex v, final Set<Vertex> visited){ if(visited.contains(v))return; //System.out.println("visited "+v); visited.add(v); @@ -198,7 +198,7 @@ public class GraphAlgorithms { /// naive implementation without priorityqueue /// @param g foo /// @return Set<Edge> - public static Set<Edge> minimumSpanningTree(Graph g){ + public static Set<Edge> minimumSpanningTree(final Graph g){ Collection<Edge> edges=g.edges(); HashSet<Edge> mst=new HashSet<>(); HashSet<Vertex> frontier=new HashSet<>(); @@ -225,7 +225,7 @@ public class GraphAlgorithms { /// @param g foo /// @param start foo /// @return Set<Edge> - public static Set<Edge> dijkstra(Graph g, 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; HashSet<Vertex> done=new HashSet<>(); @@ -280,7 +280,7 @@ public class GraphAlgorithms { /// stores file as bidirectional graph /// @param g foo /// @param file foo - public static void readGraph(Graph g, String file) { + 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()) { @@ -298,7 +298,7 @@ public class GraphAlgorithms { /// foo /// @param g foo - public static void printGraph(Graph g) { + 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))) @@ -309,7 +309,7 @@ public class GraphAlgorithms { /// store a list of lines as a file /// @param list foo /// @param f foo - public static void storeStrings(List<String> list,String f){ + public static void storeStrings(final List<String> list, final String f){ try{ PrintWriter out=new PrintWriter(new FileWriter(f)); for(String s:list){ @@ -324,7 +324,7 @@ public class GraphAlgorithms { /// read a file a returns a list of lines /// @param f foo /// @return ArrayList - public static ArrayList<String> loadStrings(String f){ + public static ArrayList<String> loadStrings(final String f){ ArrayList<String> list=new ArrayList<>(); try{ BufferedReader in=new BufferedReader(new FileReader(f)); |