Skip to content

Commit 0464c9f

Browse files
Merge branch 'master' into pr/949-yasser-handler-mapping
2 parents d5f3eaa + 8c879fc commit 0464c9f

20 files changed

Lines changed: 503 additions & 168 deletions

File tree

‎algorithms/pom.xml‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>algorithms</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<properties>
9+
<junit.version>4.12</junit.version>
10+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
11+
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>${junit.version}</version>
19+
<scope>test</scope>
20+
</dependency>
21+
</dependencies>
22+
23+
<build>
24+
<defaultGoal>install</defaultGoal>
25+
<pluginManagement>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>${maven-compiler-plugin.version}</version>
31+
<configuration>
32+
<source>1.8</source>
33+
<target>1.8</target>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.codehaus.mojo</groupId>
38+
<artifactId>exec-maven-plugin</artifactId>
39+
<version>${exec-maven-plugin.version}</version>
40+
</plugin>
41+
</plugins>
42+
</pluginManagement>
43+
</build>
44+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.algorithms.dijkstra;
2+
3+
import java.util.HashSet;
4+
import java.util.LinkedList;
5+
import java.util.Map.Entry;
6+
import java.util.Set;
7+
8+
public class Dijkstra {
9+
10+
public static Graph calculateShortestPathFromSource(Graph graph, Node source) {
11+
12+
source.setDistance(0);
13+
14+
Set<Node> settledNodes = new HashSet<>();
15+
Set<Node> unsettledNodes = new HashSet<>();
16+
unsettledNodes.add(source);
17+
18+
while (unsettledNodes.size() != 0) {
19+
Node currentNode = getLowestDistanceNode(unsettledNodes);
20+
unsettledNodes.remove(currentNode);
21+
for (Entry<Node, Integer> adjacencyPair : currentNode
22+
.getAdjacentNodes()
23+
.entrySet()) {
24+
Node adjacentNode = adjacencyPair.getKey();
25+
Integer edgeWeigh = adjacencyPair.getValue();
26+
27+
if (!settledNodes.contains(adjacentNode)) {
28+
CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode);
29+
unsettledNodes.add(adjacentNode);
30+
}
31+
}
32+
settledNodes.add(currentNode);
33+
}
34+
return graph;
35+
}
36+
37+
private static void CalculateMinimumDistance(Node evaluationNode, Integer edgeWeigh, Node sourceNode) {
38+
Integer sourceDistance = sourceNode.getDistance();
39+
if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) {
40+
evaluationNode.setDistance(sourceDistance + edgeWeigh);
41+
LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath());
42+
shortestPath.add(sourceNode);
43+
evaluationNode.setShortestPath(shortestPath);
44+
}
45+
}
46+
47+
private static Node getLowestDistanceNode(Set<Node> unsettledNodes) {
48+
Node lowestDistanceNode = null;
49+
int lowestDistance = Integer.MAX_VALUE;
50+
for (Node node : unsettledNodes) {
51+
int nodeDistance = node.getDistance();
52+
if (nodeDistance < lowestDistance) {
53+
lowestDistance = nodeDistance;
54+
lowestDistanceNode = node;
55+
}
56+
}
57+
return lowestDistanceNode;
58+
}
59+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.algorithms.dijkstra;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class Graph {
7+
8+
private Set<Node> nodes = new HashSet<>();
9+
10+
public void addNode(Node nodeA) {
11+
nodes.add(nodeA);
12+
}
13+
14+
public Set<Node> getNodes() {
15+
return nodes;
16+
}
17+
18+
public void setNodes(Set<Node> nodes) {
19+
this.nodes = nodes;
20+
}
21+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.algorithms.dijkstra;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class Node {
9+
10+
private String name;
11+
12+
private LinkedList<Node> shortestPath = new LinkedList<>();
13+
14+
private Integer distance = Integer.MAX_VALUE;
15+
16+
private Map<Node, Integer> adjacentNodes = new HashMap<>();
17+
18+
public Node(String name) {
19+
this.name = name;
20+
}
21+
22+
public void addDestination(Node destination, int distance) {
23+
adjacentNodes.put(destination, distance);
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public Map<Node, Integer> getAdjacentNodes() {
35+
return adjacentNodes;
36+
}
37+
38+
public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) {
39+
this.adjacentNodes = adjacentNodes;
40+
}
41+
42+
public Integer getDistance() {
43+
return distance;
44+
}
45+
46+
public void setDistance(Integer distance) {
47+
this.distance = distance;
48+
}
49+
50+
public List<Node> getShortestPath() {
51+
return shortestPath;
52+
}
53+
54+
public void setShortestPath(LinkedList<Node> shortestPath) {
55+
this.shortestPath = shortestPath;
56+
}
57+
58+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package algorithms;
2+
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;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static org.junit.Assert.assertTrue;
12+
13+
public class DijkstraAlgorithmTest {
14+
15+
@Test
16+
public void whenSPPSolved_thenCorrect() {
17+
18+
Node nodeA = new Node("A");
19+
Node nodeB = new Node("B");
20+
Node nodeC = new Node("C");
21+
Node nodeD = new Node("D");
22+
Node nodeE = new Node("E");
23+
Node nodeF = new Node("F");
24+
25+
nodeA.addDestination(nodeB, 10);
26+
nodeA.addDestination(nodeC, 15);
27+
28+
nodeB.addDestination(nodeD, 12);
29+
nodeB.addDestination(nodeF, 15);
30+
31+
nodeC.addDestination(nodeE, 10);
32+
33+
nodeD.addDestination(nodeE, 2);
34+
nodeD.addDestination(nodeF, 1);
35+
36+
nodeF.addDestination(nodeE, 5);
37+
38+
Graph graph = new Graph();
39+
40+
graph.addNode(nodeA);
41+
graph.addNode(nodeB);
42+
graph.addNode(nodeC);
43+
graph.addNode(nodeD);
44+
graph.addNode(nodeE);
45+
graph.addNode(nodeF);
46+
47+
graph = Dijkstra.calculateShortestPathFromSource(graph, nodeA);
48+
49+
List<Node> shortestPathForNodeB = Arrays.asList(nodeA);
50+
List<Node> shortestPathForNodeC = Arrays.asList(nodeA);
51+
List<Node> shortestPathForNodeD = Arrays.asList(nodeA, nodeB);
52+
List<Node> shortestPathForNodeE = Arrays.asList(nodeA, nodeB, nodeD);
53+
List<Node> shortestPathForNodeF = Arrays.asList(nodeA, nodeB, nodeD);
54+
55+
for (Node node : graph.getNodes()) {
56+
switch (node.getName()) {
57+
case "B":
58+
assertTrue(node
59+
.getShortestPath()
60+
.equals(shortestPathForNodeB));
61+
break;
62+
case "C":
63+
assertTrue(node
64+
.getShortestPath()
65+
.equals(shortestPathForNodeC));
66+
break;
67+
case "D":
68+
assertTrue(node
69+
.getShortestPath()
70+
.equals(shortestPathForNodeD));
71+
break;
72+
case "E":
73+
assertTrue(node
74+
.getShortestPath()
75+
.equals(shortestPathForNodeE));
76+
break;
77+
case "F":
78+
assertTrue(node
79+
.getShortestPath()
80+
.equals(shortestPathForNodeF));
81+
break;
82+
}
83+
}
84+
}
85+
}

‎aws/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

aws-lambda/pom.xml renamed to aws/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.baeldung</groupId>
5-
<artifactId>aws-lambda</artifactId>
5+
<artifactId>aws</artifactId>
66
<version>0.1.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
8-
<name>aws-lambda</name>
8+
<name>aws</name>
99

1010
<dependencies>
1111
<dependency>

aws-lambda/src/main/java/com/baeldung/LambdaMethodHandler.java renamed to aws/src/main/java/com/baeldung/lambda/LambdaMethodHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung;
1+
package com.baeldung.lambda;
22

33
import com.amazonaws.services.lambda.runtime.Context;
44

aws-lambda/src/main/java/com/baeldung/LambdaRequestHandler.java renamed to aws/src/main/java/com/baeldung/lambda/LambdaRequestHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung;
1+
package com.baeldung.lambda;
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;

aws-lambda/src/main/java/com/baeldung/LambdaRequestStreamHandler.java renamed to aws/src/main/java/com/baeldung/lambda/LambdaRequestStreamHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package com.baeldung;
1+
package com.baeldung.lambda;
22

33
import com.amazonaws.services.lambda.runtime.Context;
4-
import com.amazonaws.services.lambda.runtime.RequestHandler;
54
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
65
import org.apache.commons.io.IOUtils;
76

0 commit comments

Comments
 (0)