Refactor Algorithms module

This commit is contained in:
pivovarit 2017-01-03 18:50:56 +01:00
parent 49c7cc84ed
commit b6d9e5a7c0
6 changed files with 56 additions and 55 deletions

View File

@ -1,6 +1,5 @@
package com.baeldung.algorithms.dijkstra; package com.baeldung.algorithms.dijkstra;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -12,18 +11,19 @@ public class Dijkstra {
source.setDistance(0); source.setDistance(0);
Set<Node> settledNodes = new HashSet<Node>(); Set<Node> settledNodes = new HashSet<>();
Set<Node> unsettledNodes = new HashSet<Node>(); Set<Node> unsettledNodes = new HashSet<>();
unsettledNodes.add(source); unsettledNodes.add(source);
while (unsettledNodes.size() != 0) { while (unsettledNodes.size() != 0) {
Node currentNode = getLowestDistanceNode(unsettledNodes); Node currentNode = getLowestDistanceNode(unsettledNodes);
unsettledNodes.remove(currentNode); unsettledNodes.remove(currentNode);
for (Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet()) for (Entry<Node, Integer> adjacencyPair : currentNode
{ .getAdjacentNodes()
.entrySet()) {
Node adjacentNode = adjacencyPair.getKey(); Node adjacentNode = adjacencyPair.getKey();
Integer edgeWeigh = adjacencyPair.getValue(); Integer edgeWeigh = adjacencyPair.getValue();
if (!settledNodes.contains(adjacentNode)) { if (!settledNodes.contains(adjacentNode)) {
CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode); CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode);
unsettledNodes.add(adjacentNode); unsettledNodes.add(adjacentNode);
@ -38,7 +38,7 @@ public class Dijkstra {
Integer sourceDistance = sourceNode.getDistance(); Integer sourceDistance = sourceNode.getDistance();
if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) { if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) {
evaluationNode.setDistance(sourceDistance + edgeWeigh); evaluationNode.setDistance(sourceDistance + edgeWeigh);
LinkedList<Node> shortestPath = new LinkedList<Node>(sourceNode.getShortestPath()); LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath());
shortestPath.add(sourceNode); shortestPath.add(sourceNode);
evaluationNode.setShortestPath(shortestPath); evaluationNode.setShortestPath(shortestPath);
} }

View File

@ -5,8 +5,8 @@ import java.util.Set;
public class Graph { public class Graph {
private Set<Node> nodes = new HashSet<Node>(); private Set<Node> nodes = new HashSet<>();
public void addNode(Node nodeA) { public void addNode(Node nodeA) {
nodes.add(nodeA); nodes.add(nodeA);
} }

View File

@ -1,9 +0,0 @@
package com.baeldung.algorithms.dijkstra;
public class Main {
public static void main(String[] args) {
}
}

View File

@ -6,35 +6,35 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class Node { public class Node {
private String name; private String name;
private LinkedList<Node> shortestPath = new LinkedList<Node>(); private LinkedList<Node> shortestPath = new LinkedList<>();
private Integer distance = Integer.MAX_VALUE; private Integer distance = Integer.MAX_VALUE;
Map<Node, Integer> adjacentNodes = new HashMap<Node, Integer>(); private Map<Node, Integer> adjacentNodes = new HashMap<>();
public Node(String name) { public Node(String name) {
this.name = name; this.name = name;
} }
public void addDestination(Node destination, int distance) { public void addDestination(Node destination, int distance) {
adjacentNodes.put(destination, distance); adjacentNodes.put(destination, distance);
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Map<Node, Integer> getAdjacentNodes() { public Map<Node, Integer> getAdjacentNodes() {
return adjacentNodes; return adjacentNodes;
} }
public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) { public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) {
this.adjacentNodes = adjacentNodes; this.adjacentNodes = adjacentNodes;
} }

View File

@ -1,74 +1,83 @@
package algorithms; 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.Dijkstra;
import com.baeldung.algorithms.dijkstra.Graph; import com.baeldung.algorithms.dijkstra.Graph;
import com.baeldung.algorithms.dijkstra.Node; 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 { public class DijkstraAlgorithmTest {
@Test @Test
public void whenSPPSolved_thenCorrect() { public void whenSPPSolved_thenCorrect() {
Node nodeA = new Node("A"); Node nodeA = new Node("A");
Node nodeB = new Node("B"); Node nodeB = new Node("B");
Node nodeC = new Node("C"); Node nodeC = new Node("C");
Node nodeD = new Node("D"); Node nodeD = new Node("D");
Node nodeE = new Node("E"); Node nodeE = new Node("E");
Node nodeF = new Node("F"); Node nodeF = new Node("F");
nodeA.addDestination(nodeB, 10); nodeA.addDestination(nodeB, 10);
nodeA.addDestination(nodeC, 15); nodeA.addDestination(nodeC, 15);
nodeB.addDestination(nodeD, 12); nodeB.addDestination(nodeD, 12);
nodeB.addDestination(nodeF, 15); nodeB.addDestination(nodeF, 15);
nodeC.addDestination(nodeE, 10); nodeC.addDestination(nodeE, 10);
nodeD.addDestination(nodeE, 2); nodeD.addDestination(nodeE, 2);
nodeD.addDestination(nodeF, 1); nodeD.addDestination(nodeF, 1);
nodeF.addDestination(nodeE, 5); nodeF.addDestination(nodeE, 5);
Graph graph = new Graph(); Graph graph = new Graph();
graph.addNode(nodeA); graph.addNode(nodeA);
graph.addNode(nodeB); graph.addNode(nodeB);
graph.addNode(nodeC); graph.addNode(nodeC);
graph.addNode(nodeD); graph.addNode(nodeD);
graph.addNode(nodeE); graph.addNode(nodeE);
graph.addNode(nodeF); graph.addNode(nodeF);
graph = Dijkstra.calculateShortestPathFromSource(graph, nodeA); graph = Dijkstra.calculateShortestPathFromSource(graph, nodeA);
List<Node> shortestPathForNodeB = Arrays.asList(nodeA); List<Node> shortestPathForNodeB = Arrays.asList(nodeA);
List<Node> shortestPathForNodeC = Arrays.asList(nodeA); List<Node> shortestPathForNodeC = Arrays.asList(nodeA);
List<Node> shortestPathForNodeD = Arrays.asList(nodeA, nodeB); List<Node> shortestPathForNodeD = Arrays.asList(nodeA, nodeB);
List<Node> shortestPathForNodeE = Arrays.asList(nodeA, nodeB, nodeD); List<Node> shortestPathForNodeE = Arrays.asList(nodeA, nodeB, nodeD);
List<Node> shortestPathForNodeF = Arrays.asList(nodeA, nodeB, nodeD); List<Node> shortestPathForNodeF = Arrays.asList(nodeA, nodeB, nodeD);
for(Node node : graph.getNodes()) { for (Node node : graph.getNodes()) {
switch (node.getName()) { switch (node.getName()) {
case "B": case "B":
assertTrue(node.getShortestPath().equals(shortestPathForNodeB)); assertTrue(node
.getShortestPath()
.equals(shortestPathForNodeB));
break; break;
case "C": case "C":
assertTrue(node.getShortestPath().equals(shortestPathForNodeC)); assertTrue(node
.getShortestPath()
.equals(shortestPathForNodeC));
break; break;
case "D": case "D":
assertTrue(node.getShortestPath().equals(shortestPathForNodeD)); assertTrue(node
.getShortestPath()
.equals(shortestPathForNodeD));
break; break;
case "E": case "E":
assertTrue(node.getShortestPath().equals(shortestPathForNodeE)); assertTrue(node
.getShortestPath()
.equals(shortestPathForNodeE));
break; break;
case "F": case "F":
assertTrue(node.getShortestPath().equals(shortestPathForNodeF)); assertTrue(node
.getShortestPath()
.equals(shortestPathForNodeF));
break; break;
} }
} }

View File

@ -15,6 +15,7 @@
</properties> </properties>
<modules> <modules>
<module>algorithms</module>
<module>annotations</module> <module>annotations</module>
<module>apache-cxf</module> <module>apache-cxf</module>
<module>apache-fop</module> <module>apache-fop</module>