From a5c3599d7bc7a9ef5583ad2d50a55975f030fbea Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Tue, 29 Apr 2025 06:03:56 +0200 Subject: declare variables final when possible --- .../com/example/portfolio3/GraphAlgorithms.java | 38 +++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java') 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 edges){ + public static int pathLength(final Collection 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 edges){ + public static boolean isPath(final List edges){ for(int i=1;i path){ + public static Integer pathLength(final Graph g, final List path){ int length=0; for(int i=1;i - static List sortEdges(Collection edges){ + static List sortEdges(final Collection edges){ ArrayList 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 - static List sortEdgesFrom(Collection edges){ + static List sortEdgesFrom(final Collection edges){ ArrayList 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 - static List sortEdgesTo(Collection edges){ + static List sortEdgesTo(final Collection edges){ ArrayList 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 - public static List sortVertex(Collection vertices){ + public static List sortVertex(final Collection vertices){ ArrayList 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 - public static Set visitBreadthFirst(Graph g,Vertex v){ + public static Set visitBreadthFirst(final Graph g, final Vertex v){ HashSet thisLevel=new HashSet<>(); HashSet nextLevel=new HashSet<>(); HashSet visited=new HashSet<>(); @@ -176,7 +176,7 @@ public class GraphAlgorithms { /// @param g foo /// @param v foo /// @return Set - public static Set visitDepthFirst(Graph g,Vertex v){ + public static Set visitDepthFirst(final Graph g, final Vertex v){ HashSet 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 visited){ + private static void visitDepthFirst(final Graph g, final Vertex v, final Set 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 - public static Set minimumSpanningTree(Graph g){ + public static Set minimumSpanningTree(final Graph g){ Collection edges=g.edges(); HashSet mst=new HashSet<>(); HashSet frontier=new HashSet<>(); @@ -225,7 +225,7 @@ public class GraphAlgorithms { /// @param g foo /// @param start foo /// @return Set - public static Set dijkstra(Graph g, Vertex start){ + public static Set dijkstra(final Graph g, final Vertex start){ // create table for done, prev and weight from start int maxint =Integer.MAX_VALUE; HashSet 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 list,String f){ + public static void storeStrings(final List 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 loadStrings(String f){ + public static ArrayList loadStrings(final String f){ ArrayList list=new ArrayList<>(); try{ BufferedReader in=new BufferedReader(new FileReader(f)); -- cgit v1.2.3