Skip to content

Commit 26cf50f

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 1257711 + e984ba6 commit 26cf50f

File tree

1,798 files changed

+13619
-396300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,798 files changed

+13619
-396300
lines changed

algorithms-miscellaneous-1/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
77

88
- [Validating Input With Finite Automata in Java](https://www.baeldung.com/java-finite-automata)
99
- [Example of Hill Climbing Algorithm](https://www.baeldung.com/java-hill-climbing-algorithm)
10-
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](https://www.baeldung.com/java-monte-carlo-tree-search)
11-
- [Binary Search Algorithm in Java](https://www.baeldung.com/java-binary-search)
1210
- [Introduction to Minimax Algorithm](https://www.baeldung.com/java-minimax-algorithm)
1311
- [How to Calculate Levenshtein Distance in Java?](https://www.baeldung.com/java-levenshtein-distance)
1412
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.algorithms.astar;
2+
3+
import java.util.Map;
4+
import java.util.Set;
5+
import java.util.stream.Collectors;
6+
7+
8+
public class Graph<T extends GraphNode> {
9+
private final Set<T> nodes;
10+
11+
private final Map<String, Set<String>> connections;
12+
13+
public Graph(Set<T> nodes, Map<String, Set<String>> connections) {
14+
this.nodes = nodes;
15+
this.connections = connections;
16+
}
17+
18+
public T getNode(String id) {
19+
return nodes.stream()
20+
.filter(node -> node.getId().equals(id))
21+
.findFirst()
22+
.orElseThrow(() -> new IllegalArgumentException("No node found with ID"));
23+
}
24+
25+
public Set<T> getConnections(T node) {
26+
return connections.get(node.getId()).stream()
27+
.map(this::getNode)
28+
.collect(Collectors.toSet());
29+
}
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.algorithms.astar;
2+
3+
public interface GraphNode {
4+
String getId();
5+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.algorithms.astar;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.PriorityQueue;
8+
import java.util.Queue;
9+
import java.util.stream.Collectors;
10+
11+
public class RouteFinder<T extends GraphNode> {
12+
private final Graph<T> graph;
13+
private final Scorer<T> nextNodeScorer;
14+
private final Scorer<T> targetScorer;
15+
16+
public RouteFinder(Graph<T> graph, Scorer<T> nextNodeScorer, Scorer<T> targetScorer) {
17+
this.graph = graph;
18+
this.nextNodeScorer = nextNodeScorer;
19+
this.targetScorer = targetScorer;
20+
}
21+
22+
public List<T> findRoute(T from, T to) {
23+
Map<T, RouteNode<T>> allNodes = new HashMap<>();
24+
Queue<RouteNode> openSet = new PriorityQueue<>();
25+
26+
RouteNode<T> start = new RouteNode<>(from, null, 0d, targetScorer.computeCost(from, to));
27+
allNodes.put(from, start);
28+
openSet.add(start);
29+
30+
while (!openSet.isEmpty()) {
31+
System.out.println("Open Set contains: " + openSet.stream().map(RouteNode::getCurrent).collect(Collectors.toSet()));
32+
RouteNode<T> next = openSet.poll();
33+
System.out.println("Looking at node: " + next);
34+
if (next.getCurrent().equals(to)) {
35+
System.out.println("Found our destination!");
36+
37+
List<T> route = new ArrayList<>();
38+
RouteNode<T> current = next;
39+
do {
40+
route.add(0, current.getCurrent());
41+
current = allNodes.get(current.getPrevious());
42+
} while (current != null);
43+
44+
System.out.println("Route: " + route);
45+
return route;
46+
}
47+
48+
graph.getConnections(next.getCurrent()).forEach(connection -> {
49+
double newScore = next.getRouteScore() + nextNodeScorer.computeCost(next.getCurrent(), connection);
50+
RouteNode<T> nextNode = allNodes.getOrDefault(connection, new RouteNode<>(connection));
51+
allNodes.put(connection, nextNode);
52+
53+
if (nextNode.getRouteScore() > newScore) {
54+
nextNode.setPrevious(next.getCurrent());
55+
nextNode.setRouteScore(newScore);
56+
nextNode.setEstimatedScore(newScore + targetScorer.computeCost(connection, to));
57+
openSet.add(nextNode);
58+
System.out.println("Found a better route to node: " + nextNode);
59+
}
60+
});
61+
}
62+
63+
throw new IllegalStateException("No route found");
64+
}
65+
66+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.algorithms.astar;
2+
3+
import java.util.StringJoiner;
4+
5+
class RouteNode<T extends GraphNode> implements Comparable<RouteNode> {
6+
private final T current;
7+
private T previous;
8+
private double routeScore;
9+
private double estimatedScore;
10+
11+
RouteNode(T current) {
12+
this(current, null, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
13+
}
14+
15+
RouteNode(T current, T previous, double routeScore, double estimatedScore) {
16+
this.current = current;
17+
this.previous = previous;
18+
this.routeScore = routeScore;
19+
this.estimatedScore = estimatedScore;
20+
}
21+
22+
T getCurrent() {
23+
return current;
24+
}
25+
26+
T getPrevious() {
27+
return previous;
28+
}
29+
30+
double getRouteScore() {
31+
return routeScore;
32+
}
33+
34+
double getEstimatedScore() {
35+
return estimatedScore;
36+
}
37+
38+
void setPrevious(T previous) {
39+
this.previous = previous;
40+
}
41+
42+
void setRouteScore(double routeScore) {
43+
this.routeScore = routeScore;
44+
}
45+
46+
void setEstimatedScore(double estimatedScore) {
47+
this.estimatedScore = estimatedScore;
48+
}
49+
50+
@Override
51+
public int compareTo(RouteNode other) {
52+
if (this.estimatedScore > other.estimatedScore) {
53+
return 1;
54+
} else if (this.estimatedScore < other.estimatedScore) {
55+
return -1;
56+
} else {
57+
return 0;
58+
}
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return new StringJoiner(", ", RouteNode.class.getSimpleName() + "[", "]").add("current=" + current)
64+
.add("previous=" + previous).add("routeScore=" + routeScore).add("estimatedScore=" + estimatedScore)
65+
.toString();
66+
}
67+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.algorithms.astar;
2+
3+
public interface Scorer<T extends GraphNode> {
4+
double computeCost(T from, T to);
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.algorithms.astar.underground;
2+
3+
import com.baeldung.algorithms.astar.Scorer;
4+
5+
public class HaversineScorer implements Scorer<Station> {
6+
@Override
7+
public double computeCost(Station from, Station to) {
8+
double R = 6372.8; // In kilometers
9+
10+
double dLat = Math.toRadians(to.getLatitude() - from.getLatitude());
11+
double dLon = Math.toRadians(to.getLongitude() - from.getLongitude());
12+
double lat1 = Math.toRadians(from.getLatitude());
13+
double lat2 = Math.toRadians(to.getLatitude());
14+
15+
double a = Math.pow(Math.sin(dLat / 2),2) + Math.pow(Math.sin(dLon / 2),2) * Math.cos(lat1) * Math.cos(lat2);
16+
double c = 2 * Math.asin(Math.sqrt(a));
17+
return R * c;
18+
}
19+
}

0 commit comments

Comments
 (0)