From b6d9e5a7c04c284e6fcdfce319d5482aaca60cca Mon Sep 17 00:00:00 2001 From: pivovarit Date: Tue, 3 Jan 2017 18:50:56 +0100 Subject: [PATCH] Refactor Algorithms module --- .../algorithms/dijkstra/Dijkstra.java | 14 ++--- .../baeldung/algorithms/dijkstra/Graph.java | 4 +- .../baeldung/algorithms/dijkstra/Main.java | 9 --- .../baeldung/algorithms/dijkstra/Node.java | 24 ++++---- .../algorithms/DijkstraAlgorithmTest.java | 59 +++++++++++-------- pom.xml | 1 + 6 files changed, 56 insertions(+), 55 deletions(-) delete mode 100644 algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Main.java diff --git a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Dijkstra.java b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Dijkstra.java index cab550e4b7..b82eedbc3f 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Dijkstra.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Dijkstra.java @@ -1,6 +1,5 @@ package com.baeldung.algorithms.dijkstra; - import java.util.HashSet; import java.util.LinkedList; import java.util.Map.Entry; @@ -12,18 +11,19 @@ public class Dijkstra { source.setDistance(0); - Set settledNodes = new HashSet(); - Set unsettledNodes = new HashSet(); + Set settledNodes = new HashSet<>(); + Set unsettledNodes = new HashSet<>(); unsettledNodes.add(source); while (unsettledNodes.size() != 0) { Node currentNode = getLowestDistanceNode(unsettledNodes); unsettledNodes.remove(currentNode); - for (Entry adjacencyPair : currentNode.getAdjacentNodes().entrySet()) - { + for (Entry adjacencyPair : currentNode + .getAdjacentNodes() + .entrySet()) { Node adjacentNode = adjacencyPair.getKey(); Integer edgeWeigh = adjacencyPair.getValue(); - + if (!settledNodes.contains(adjacentNode)) { CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode); unsettledNodes.add(adjacentNode); @@ -38,7 +38,7 @@ public class Dijkstra { Integer sourceDistance = sourceNode.getDistance(); if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) { evaluationNode.setDistance(sourceDistance + edgeWeigh); - LinkedList shortestPath = new LinkedList(sourceNode.getShortestPath()); + LinkedList shortestPath = new LinkedList<>(sourceNode.getShortestPath()); shortestPath.add(sourceNode); evaluationNode.setShortestPath(shortestPath); } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Graph.java b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Graph.java index e72e28a43c..f24d6ae60e 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Graph.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Graph.java @@ -5,8 +5,8 @@ import java.util.Set; public class Graph { - private Set nodes = new HashSet(); - + private Set nodes = new HashSet<>(); + public void addNode(Node nodeA) { nodes.add(nodeA); } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Main.java b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Main.java deleted file mode 100644 index 97f9765d3d..0000000000 --- a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Main.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.algorithms.dijkstra; - -public class Main { - - public static void main(String[] args) { - - - } -} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Node.java b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Node.java index 7e95bf5d27..b00127a259 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Node.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Node.java @@ -6,35 +6,35 @@ import java.util.List; import java.util.Map; public class Node { - + private String name; - - private LinkedList shortestPath = new LinkedList(); - + + private LinkedList shortestPath = new LinkedList<>(); + private Integer distance = Integer.MAX_VALUE; - - Map adjacentNodes = new HashMap(); - + + private Map adjacentNodes = new HashMap<>(); + public Node(String name) { this.name = name; } - + public void addDestination(Node destination, int distance) { adjacentNodes.put(destination, distance); } - + public String getName() { return name; } - + public void setName(String name) { this.name = name; } - + public Map getAdjacentNodes() { return adjacentNodes; } - + public void setAdjacentNodes(Map adjacentNodes) { this.adjacentNodes = adjacentNodes; } diff --git a/algorithms/src/test/java/algorithms/DijkstraAlgorithmTest.java b/algorithms/src/test/java/algorithms/DijkstraAlgorithmTest.java index 408da01e02..86ee62c827 100644 --- a/algorithms/src/test/java/algorithms/DijkstraAlgorithmTest.java +++ b/algorithms/src/test/java/algorithms/DijkstraAlgorithmTest.java @@ -1,74 +1,83 @@ package algorithms; -import static org.junit.Assert.*; - -import java.util.Arrays; -import java.util.List; - -import org.junit.Test; - import com.baeldung.algorithms.dijkstra.Dijkstra; import com.baeldung.algorithms.dijkstra.Graph; import com.baeldung.algorithms.dijkstra.Node; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertTrue; public class DijkstraAlgorithmTest { @Test public void whenSPPSolved_thenCorrect() { - + Node nodeA = new Node("A"); Node nodeB = new Node("B"); Node nodeC = new Node("C"); - Node nodeD = new Node("D"); + Node nodeD = new Node("D"); Node nodeE = new Node("E"); Node nodeF = new Node("F"); - + nodeA.addDestination(nodeB, 10); nodeA.addDestination(nodeC, 15); - + nodeB.addDestination(nodeD, 12); nodeB.addDestination(nodeF, 15); - + nodeC.addDestination(nodeE, 10); - + nodeD.addDestination(nodeE, 2); nodeD.addDestination(nodeF, 1); - + nodeF.addDestination(nodeE, 5); - - Graph graph = new Graph(); - + + Graph graph = new Graph(); + graph.addNode(nodeA); graph.addNode(nodeB); graph.addNode(nodeC); graph.addNode(nodeD); graph.addNode(nodeE); graph.addNode(nodeF); - + graph = Dijkstra.calculateShortestPathFromSource(graph, nodeA); - + List shortestPathForNodeB = Arrays.asList(nodeA); List shortestPathForNodeC = Arrays.asList(nodeA); List shortestPathForNodeD = Arrays.asList(nodeA, nodeB); List shortestPathForNodeE = Arrays.asList(nodeA, nodeB, nodeD); List shortestPathForNodeF = Arrays.asList(nodeA, nodeB, nodeD); - for(Node node : graph.getNodes()) { + for (Node node : graph.getNodes()) { switch (node.getName()) { case "B": - assertTrue(node.getShortestPath().equals(shortestPathForNodeB)); + assertTrue(node + .getShortestPath() + .equals(shortestPathForNodeB)); break; case "C": - assertTrue(node.getShortestPath().equals(shortestPathForNodeC)); + assertTrue(node + .getShortestPath() + .equals(shortestPathForNodeC)); break; case "D": - assertTrue(node.getShortestPath().equals(shortestPathForNodeD)); + assertTrue(node + .getShortestPath() + .equals(shortestPathForNodeD)); break; case "E": - assertTrue(node.getShortestPath().equals(shortestPathForNodeE)); + assertTrue(node + .getShortestPath() + .equals(shortestPathForNodeE)); break; case "F": - assertTrue(node.getShortestPath().equals(shortestPathForNodeF)); + assertTrue(node + .getShortestPath() + .equals(shortestPathForNodeF)); break; } } diff --git a/pom.xml b/pom.xml index 19bec5bf81..4b10a76951 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ + algorithms annotations apache-cxf apache-fop