diff --git a/algorithms/pom.xml b/algorithms/pom.xml new file mode 100644 index 0000000000..0c85a19534 --- /dev/null +++ b/algorithms/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + com.baeldung + algorithms + 0.0.1-SNAPSHOT + + + 4.12 + 3.6.0 + 1.5.0 + + + + + junit + junit + ${junit.version} + test + + + + + install + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + + + + \ No newline at end of file diff --git a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Dijkstra.java b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Dijkstra.java new file mode 100644 index 0000000000..cab550e4b7 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Dijkstra.java @@ -0,0 +1,59 @@ +package com.baeldung.algorithms.dijkstra; + + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map.Entry; +import java.util.Set; + +public class Dijkstra { + + public static Graph calculateShortestPathFromSource(Graph graph, Node source) { + + source.setDistance(0); + + 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()) + { + Node adjacentNode = adjacencyPair.getKey(); + Integer edgeWeigh = adjacencyPair.getValue(); + + if (!settledNodes.contains(adjacentNode)) { + CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode); + unsettledNodes.add(adjacentNode); + } + } + settledNodes.add(currentNode); + } + return graph; + } + + private static void CalculateMinimumDistance(Node evaluationNode, Integer edgeWeigh, Node sourceNode) { + Integer sourceDistance = sourceNode.getDistance(); + if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) { + evaluationNode.setDistance(sourceDistance + edgeWeigh); + LinkedList shortestPath = new LinkedList(sourceNode.getShortestPath()); + shortestPath.add(sourceNode); + evaluationNode.setShortestPath(shortestPath); + } + } + + private static Node getLowestDistanceNode(Set unsettledNodes) { + Node lowestDistanceNode = null; + int lowestDistance = Integer.MAX_VALUE; + for (Node node : unsettledNodes) { + int nodeDistance = node.getDistance(); + if (nodeDistance < lowestDistance) { + lowestDistance = nodeDistance; + lowestDistanceNode = node; + } + } + return lowestDistanceNode; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Graph.java b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Graph.java new file mode 100644 index 0000000000..e72e28a43c --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Graph.java @@ -0,0 +1,21 @@ +package com.baeldung.algorithms.dijkstra; + +import java.util.HashSet; +import java.util.Set; + +public class Graph { + + private Set nodes = new HashSet(); + + public void addNode(Node nodeA) { + nodes.add(nodeA); + } + + public Set getNodes() { + return nodes; + } + + public void setNodes(Set nodes) { + this.nodes = nodes; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Main.java b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Main.java new file mode 100644 index 0000000000..97f9765d3d --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Main.java @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000000..7e95bf5d27 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Node.java @@ -0,0 +1,58 @@ +package com.baeldung.algorithms.dijkstra; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class Node { + + private String name; + + private LinkedList shortestPath = new LinkedList(); + + private Integer distance = Integer.MAX_VALUE; + + 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; + } + + public Integer getDistance() { + return distance; + } + + public void setDistance(Integer distance) { + this.distance = distance; + } + + public List getShortestPath() { + return shortestPath; + } + + public void setShortestPath(LinkedList shortestPath) { + this.shortestPath = shortestPath; + } + +} diff --git a/algorithms/src/test/java/algorithms/DijkstraAlgorithmTest.java b/algorithms/src/test/java/algorithms/DijkstraAlgorithmTest.java new file mode 100644 index 0000000000..408da01e02 --- /dev/null +++ b/algorithms/src/test/java/algorithms/DijkstraAlgorithmTest.java @@ -0,0 +1,76 @@ +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; + +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 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.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()) { + switch (node.getName()) { + case "B": + assertTrue(node.getShortestPath().equals(shortestPathForNodeB)); + break; + case "C": + assertTrue(node.getShortestPath().equals(shortestPathForNodeC)); + break; + case "D": + assertTrue(node.getShortestPath().equals(shortestPathForNodeD)); + break; + case "E": + assertTrue(node.getShortestPath().equals(shortestPathForNodeE)); + break; + case "F": + assertTrue(node.getShortestPath().equals(shortestPathForNodeF)); + break; + } + } + } +} diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index 9bb3c12945..da26d8abe9 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -357,18 +357,4 @@ - - - - org.codehaus.mojo - findbugs-maven-plugin - ${findbugs-maven-plugin.version} - - Max - FindDeadLocalStores,FindNullDeref - - - - -