Skip to content

Commit b6d9e5a

Browse files
committed
Refactor Algorithms module
1 parent 49c7cc8 commit b6d9e5a

6 files changed

Lines changed: 55 additions & 54 deletions

File tree

algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Dijkstra.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.algorithms.dijkstra;
22

3-
43
import java.util.HashSet;
54
import java.util.LinkedList;
65
import java.util.Map.Entry;
@@ -12,18 +11,19 @@ public static Graph calculateShortestPathFromSource(Graph graph, Node source) {
1211

1312
source.setDistance(0);
1413

15-
Set<Node> settledNodes = new HashSet<Node>();
16-
Set<Node> unsettledNodes = new HashSet<Node>();
14+
Set<Node> settledNodes = new HashSet<>();
15+
Set<Node> unsettledNodes = new HashSet<>();
1716
unsettledNodes.add(source);
1817

1918
while (unsettledNodes.size() != 0) {
2019
Node currentNode = getLowestDistanceNode(unsettledNodes);
2120
unsettledNodes.remove(currentNode);
22-
for (Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet())
23-
{
21+
for (Entry<Node, Integer> adjacencyPair : currentNode
22+
.getAdjacentNodes()
23+
.entrySet()) {
2424
Node adjacentNode = adjacencyPair.getKey();
2525
Integer edgeWeigh = adjacencyPair.getValue();
26-
26+
2727
if (!settledNodes.contains(adjacentNode)) {
2828
CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode);
2929
unsettledNodes.add(adjacentNode);
@@ -38,7 +38,7 @@ private static void CalculateMinimumDistance(Node evaluationNode, Integer edgeWe
3838
Integer sourceDistance = sourceNode.getDistance();
3939
if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) {
4040
evaluationNode.setDistance(sourceDistance + edgeWeigh);
41-
LinkedList<Node> shortestPath = new LinkedList<Node>(sourceNode.getShortestPath());
41+
LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath());
4242
shortestPath.add(sourceNode);
4343
evaluationNode.setShortestPath(shortestPath);
4444
}

algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Graph.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
public class Graph {
77

8-
private Set<Node> nodes = new HashSet<Node>();
9-
8+
private Set<Node> nodes = new HashSet<>();
9+
1010
public void addNode(Node nodeA) {
1111
nodes.add(nodeA);
1212
}

algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Main.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

algorithms/src/main/java/com/baeldung/algorithms/dijkstra/Node.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@
66
import java.util.Map;
77

88
public class Node {
9-
9+
1010
private String name;
11-
12-
private LinkedList<Node> shortestPath = new LinkedList<Node>();
13-
11+
12+
private LinkedList<Node> shortestPath = new LinkedList<>();
13+
1414
private Integer distance = Integer.MAX_VALUE;
15-
16-
Map<Node, Integer> adjacentNodes = new HashMap<Node, Integer>();
17-
15+
16+
private Map<Node, Integer> adjacentNodes = new HashMap<>();
17+
1818
public Node(String name) {
1919
this.name = name;
2020
}
21-
21+
2222
public void addDestination(Node destination, int distance) {
2323
adjacentNodes.put(destination, distance);
2424
}
25-
25+
2626
public String getName() {
2727
return name;
2828
}
29-
29+
3030
public void setName(String name) {
3131
this.name = name;
3232
}
33-
33+
3434
public Map<Node, Integer> getAdjacentNodes() {
3535
return adjacentNodes;
3636
}
37-
37+
3838
public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) {
3939
this.adjacentNodes = adjacentNodes;
4040
}

algorithms/src/test/java/algorithms/DijkstraAlgorithmTest.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,83 @@
11
package algorithms;
22

3-
import static org.junit.Assert.*;
3+
import com.baeldung.algorithms.dijkstra.Dijkstra;
4+
import com.baeldung.algorithms.dijkstra.Graph;
5+
import com.baeldung.algorithms.dijkstra.Node;
6+
import org.junit.Test;
47

58
import java.util.Arrays;
69
import java.util.List;
710

8-
import org.junit.Test;
9-
10-
import com.baeldung.algorithms.dijkstra.Dijkstra;
11-
import com.baeldung.algorithms.dijkstra.Graph;
12-
import com.baeldung.algorithms.dijkstra.Node;
11+
import static org.junit.Assert.assertTrue;
1312

1413
public class DijkstraAlgorithmTest {
1514

1615
@Test
1716
public void whenSPPSolved_thenCorrect() {
18-
17+
1918
Node nodeA = new Node("A");
2019
Node nodeB = new Node("B");
2120
Node nodeC = new Node("C");
22-
Node nodeD = new Node("D");
21+
Node nodeD = new Node("D");
2322
Node nodeE = new Node("E");
2423
Node nodeF = new Node("F");
25-
24+
2625
nodeA.addDestination(nodeB, 10);
2726
nodeA.addDestination(nodeC, 15);
28-
27+
2928
nodeB.addDestination(nodeD, 12);
3029
nodeB.addDestination(nodeF, 15);
31-
30+
3231
nodeC.addDestination(nodeE, 10);
33-
32+
3433
nodeD.addDestination(nodeE, 2);
3534
nodeD.addDestination(nodeF, 1);
36-
35+
3736
nodeF.addDestination(nodeE, 5);
38-
39-
Graph graph = new Graph();
40-
37+
38+
Graph graph = new Graph();
39+
4140
graph.addNode(nodeA);
4241
graph.addNode(nodeB);
4342
graph.addNode(nodeC);
4443
graph.addNode(nodeD);
4544
graph.addNode(nodeE);
4645
graph.addNode(nodeF);
47-
46+
4847
graph = Dijkstra.calculateShortestPathFromSource(graph, nodeA);
49-
48+
5049
List<Node> shortestPathForNodeB = Arrays.asList(nodeA);
5150
List<Node> shortestPathForNodeC = Arrays.asList(nodeA);
5251
List<Node> shortestPathForNodeD = Arrays.asList(nodeA, nodeB);
5352
List<Node> shortestPathForNodeE = Arrays.asList(nodeA, nodeB, nodeD);
5453
List<Node> shortestPathForNodeF = Arrays.asList(nodeA, nodeB, nodeD);
5554

56-
for(Node node : graph.getNodes()) {
55+
for (Node node : graph.getNodes()) {
5756
switch (node.getName()) {
5857
case "B":
59-
assertTrue(node.getShortestPath().equals(shortestPathForNodeB));
58+
assertTrue(node
59+
.getShortestPath()
60+
.equals(shortestPathForNodeB));
6061
break;
6162
case "C":
62-
assertTrue(node.getShortestPath().equals(shortestPathForNodeC));
63+
assertTrue(node
64+
.getShortestPath()
65+
.equals(shortestPathForNodeC));
6366
break;
6467
case "D":
65-
assertTrue(node.getShortestPath().equals(shortestPathForNodeD));
68+
assertTrue(node
69+
.getShortestPath()
70+
.equals(shortestPathForNodeD));
6671
break;
6772
case "E":
68-
assertTrue(node.getShortestPath().equals(shortestPathForNodeE));
73+
assertTrue(node
74+
.getShortestPath()
75+
.equals(shortestPathForNodeE));
6976
break;
7077
case "F":
71-
assertTrue(node.getShortestPath().equals(shortestPathForNodeF));
78+
assertTrue(node
79+
.getShortestPath()
80+
.equals(shortestPathForNodeF));
7281
break;
7382
}
7483
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</properties>
1616

1717
<modules>
18+
<module>algorithms</module>
1819
<module>annotations</module>
1920
<module>apache-cxf</module>
2021
<module>apache-fop</module>

0 commit comments

Comments
 (0)