Add Algorithms Module (#936)
* Update pom.xml Add FindBugs reporting plugin * Add algorithms module Dijkstra implementation * Update pom.xml * Update pom.xml
This commit is contained in:
parent
99e86c5f19
commit
49c7cc84ed
44
algorithms/pom.xml
Normal file
44
algorithms/pom.xml
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>algorithms</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||||
|
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<defaultGoal>install</defaultGoal>
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>${exec-maven-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -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<Node> settledNodes = new HashSet<Node>();
|
||||||
|
Set<Node> unsettledNodes = new HashSet<Node>();
|
||||||
|
unsettledNodes.add(source);
|
||||||
|
|
||||||
|
while (unsettledNodes.size() != 0) {
|
||||||
|
Node currentNode = getLowestDistanceNode(unsettledNodes);
|
||||||
|
unsettledNodes.remove(currentNode);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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<Node> shortestPath = new LinkedList<Node>(sourceNode.getShortestPath());
|
||||||
|
shortestPath.add(sourceNode);
|
||||||
|
evaluationNode.setShortestPath(shortestPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Node getLowestDistanceNode(Set<Node> 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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.algorithms.dijkstra;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class Graph {
|
||||||
|
|
||||||
|
private Set<Node> nodes = new HashSet<Node>();
|
||||||
|
|
||||||
|
public void addNode(Node nodeA) {
|
||||||
|
nodes.add(nodeA);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Node> getNodes() {
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNodes(Set<Node> nodes) {
|
||||||
|
this.nodes = nodes;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.algorithms.dijkstra;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -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<Node> shortestPath = new LinkedList<Node>();
|
||||||
|
|
||||||
|
private Integer distance = Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
Map<Node, Integer> adjacentNodes = new HashMap<Node, Integer>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getDistance() {
|
||||||
|
return distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDistance(Integer distance) {
|
||||||
|
this.distance = distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Node> getShortestPath() {
|
||||||
|
return shortestPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setShortestPath(LinkedList<Node> shortestPath) {
|
||||||
|
this.shortestPath = shortestPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<Node> shortestPathForNodeB = Arrays.asList(nodeA);
|
||||||
|
List<Node> shortestPathForNodeC = Arrays.asList(nodeA);
|
||||||
|
List<Node> shortestPathForNodeD = Arrays.asList(nodeA, nodeB);
|
||||||
|
List<Node> shortestPathForNodeE = Arrays.asList(nodeA, nodeB, nodeD);
|
||||||
|
List<Node> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -357,18 +357,4 @@
|
|||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<reporting>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
|
||||||
<artifactId>findbugs-maven-plugin</artifactId>
|
|
||||||
<version>${findbugs-maven-plugin.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<effort>Max</effort>
|
|
||||||
<omitVisitors>FindDeadLocalStores,FindNullDeref</omitVisitors>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</reporting>
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user