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
@@ -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<Node> settledNodes = new HashSet<Node>();
Set<Node> unsettledNodes = new HashSet<Node>();
Set<Node> settledNodes = new HashSet<>();
Set<Node> unsettledNodes = new HashSet<>();
unsettledNodes.add(source);
while (unsettledNodes.size() != 0) {
Node currentNode = getLowestDistanceNode(unsettledNodes);
unsettledNodes.remove(currentNode);
for (Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet())
{
for (Entry<Node, Integer> 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<Node> shortestPath = new LinkedList<Node>(sourceNode.getShortestPath());
LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath());
shortestPath.add(sourceNode);
evaluationNode.setShortestPath(shortestPath);
}
@@ -5,8 +5,8 @@ import java.util.Set;
public class Graph {
private Set<Node> nodes = new HashSet<Node>();
private Set<Node> nodes = new HashSet<>();
public void addNode(Node nodeA) {
nodes.add(nodeA);
}
@@ -1,9 +0,0 @@
package com.baeldung.algorithms.dijkstra;
public class Main {
public static void main(String[] args) {
}
}
@@ -6,35 +6,35 @@ import java.util.List;
import java.util.Map;
public class Node {
private String name;
private LinkedList<Node> shortestPath = new LinkedList<Node>();
private LinkedList<Node> shortestPath = new LinkedList<>();
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) {
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<Node, Integer> getAdjacentNodes() {
return adjacentNodes;
}
public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) {
this.adjacentNodes = adjacentNodes;
}