1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Neo4J A* search</title>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Neo4J A* search</title>
<link rel="stylesheet" href="/assets/css/main.css">
<link rel="stylesheet" href="/assets/css/skeleton.css">
</head>
</head>
<body>
<div id="nav-container" class="container">
<ul id="navlist" class="left">
<li >
<a href="/" class="link-decor-none">hme</a>
</li>
<li class="active">
<a href="/archive/" class="link-decor-none">blg</a>
</li>
<li >
<a href="/projects/" class="link-decor-none">poc</a>
</li>
<li >
<a href="/about/" class="link-decor-none">abt</a>
</li>
<li><a href="/feed.xml" class="link-decor-none">rss</a></li>
</ul>
</div>
<main>
<div class="container">
<h2 class="center" id="title">NEO4J A* SEARCH</h2>
<h6 class="center">14 SEPTEMBER 2025</h5>
<br>
<div class="twocol justify"><p>Back in 2018, we used <a href="https://neo4j.com/" class="external" target="_blank" rel="noopener noreferrer">Neo4J</a> graph database to track the
movement of marine vessels. We were interested in the shortest path a ship
could take through a network of about 13,000 route points. Algorithms based on
graph theory, such as A* search, provide an optimal solution to this
problem. Therefore, it was useful to model the set of route points as a graph.</p>
<p>A graph is a finite set of vertices, and a subset of vertex pairs (edges).
Edges can have weights. In the case of vessel tracking, the route points form
the vertices of a graph; the routes between them, the edges; and the distances
between them are the weights. For different reasons, people are interested in
minimizing (or maximizing) the weight of a path through a set of vertices. For
instance, we may want to find the shortest path between two ports.</p>
<p>Given such a graph, an algorithm like Dijkstra’s search could compute the
shortest path between two vertices. In fact, this was the algorithm the Neo4J
project shipped with at the time. One drawback of Dijkstra’s algorithm is that
it computes all the shortest paths from the source to all other vertices before
terminating at the destination vertex. The exhaustive nature of this search
limited our search to about 4,000 route points.</p>
<p>The following enhancement to Dijkstra’s search, also known as the A* search,
employs a heuristic to steer the search in the direction of the destination
more quickly. In the case of our network of vessels, which are on the earth’s
surface, spherical distance is a good candidate for a heuristic:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>package org.neo4j.graphalgo.impl;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.neo4j.graphalgo.api.Graph;
import org.neo4j.graphalgo.core.utils.ProgressLogger;
import org.neo4j.graphalgo.core.utils.queue.IntPriorityQueue;
import org.neo4j.graphalgo.core.utils.queue.SharedIntPriorityQueue;
import org.neo4j.graphalgo.core.utils.traverse.SimpleBitSet;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import com.carrotsearch.hppc.IntArrayDeque;
import com.carrotsearch.hppc.IntDoubleMap;
import com.carrotsearch.hppc.IntDoubleScatterMap;
import com.carrotsearch.hppc.IntIntMap;
import com.carrotsearch.hppc.IntIntScatterMap;
public class ShortestPathAStar extends Algorithm<ShortestPathAStar> {
private final GraphDatabaseAPI dbService;
private static final int PATH_END = -1;
private Graph graph;
private final int nodeCount;
private IntDoubleMap gCosts;
private IntDoubleMap fCosts;
private double totalCost;
private IntPriorityQueue openNodes;
private IntIntMap path;
private IntArrayDeque shortestPath;
private SimpleBitSet closedNodes;
private final ProgressLogger progressLogger;
public static final double NO_PATH_FOUND = -1.0;
public ShortestPathAStar(
final Graph graph,
final GraphDatabaseAPI dbService) {
this.graph = graph;
this.dbService = dbService;
nodeCount = Math.toIntExact(graph.nodeCount());
gCosts = new IntDoubleScatterMap(nodeCount);
fCosts = new IntDoubleScatterMap(nodeCount);
openNodes = SharedIntPriorityQueue.min(
nodeCount,
fCosts,
Double.MAX_VALUE);
path = new IntIntScatterMap(nodeCount);
closedNodes = new SimpleBitSet(nodeCount);
shortestPath = new IntArrayDeque();
progressLogger = getProgressLogger();
}
public ShortestPathAStar compute(
final long startNode,
final long goalNode,
final String propertyKeyLat,
final String propertyKeyLon,
final Direction direction) {
reset();
final int startNodeInternal =
graph.toMappedNodeId(startNode);
final double startNodeLat =
getNodeCoordinate(startNodeInternal, propertyKeyLat);
final double startNodeLon =
getNodeCoordinate(startNodeInternal, propertyKeyLon);
final int goalNodeInternal =
graph.toMappedNodeId(goalNode);
final double goalNodeLat =
getNodeCoordinate(goalNodeInternal, propertyKeyLat);
final double goalNodeLon =
getNodeCoordinate(goalNodeInternal, propertyKeyLon);
final double initialHeuristic =
computeHeuristic(startNodeLat,
startNodeLon,
goalNodeLat,
goalNodeLon);
gCosts.put(startNodeInternal, 0.0);
fCosts.put(startNodeInternal, initialHeuristic);
openNodes.add(startNodeInternal, 0.0);
run(goalNodeInternal,
propertyKeyLat,
propertyKeyLon,
direction);
if (path.containsKey(goalNodeInternal)) {
totalCost = gCosts.get(goalNodeInternal);
int node = goalNodeInternal;
while (node != PATH_END) {
shortestPath.addFirst(node);
node = path.getOrDefault(node, PATH_END);
}
}
return this;
}
private void run(
final int goalNodeId,
final String propertyKeyLat,
final String propertyKeyLon,
final Direction direction) {
final double goalLat =
getNodeCoordinate(goalNodeId, propertyKeyLat);
final double goalLon =
getNodeCoordinate(goalNodeId, propertyKeyLon);
while (!openNodes.isEmpty() && running()) {
int currentNodeId = openNodes.pop();
if (currentNodeId == goalNodeId) {
return;
}
closedNodes.put(currentNodeId);
double currentNodeCost =
this.gCosts.getOrDefault(
currentNodeId,
Double.MAX_VALUE);
graph.forEachRelationship(
currentNodeId,
direction,
(source, target, relationshipId, weight) -> {
double neighbourLat =
getNodeCoordinate(target, propertyKeyLat);
double neighbourLon =
getNodeCoordinate(target, propertyKeyLon);
double heuristic =
computeHeuristic(
neighbourLat,
neighbourLon,
goalLat,
goalLon);
updateCosts(
source,
target,
weight + currentNodeCost,
heuristic);
if (!closedNodes.contains(target)) {
openNodes.add(target, 0);
}
return true;
});
progressLogger.logProgress(
(double) currentNodeId / (nodeCount - 1));
}
}
private double computeHeuristic(
final double lat1,
final double lon1,
final double lat2,
final double lon2) {
final int earthRadius = 6371;
final double kmToNM = 0.539957;
final double latDistance = Math.toRadians(lat2 - lat1);
final double lonDistance = Math.toRadians(lon2 - lon1);
final double a = Math.sin(latDistance / 2)
* Math.sin(latDistance / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2))
* Math.sin(lonDistance / 2)
* Math.sin(lonDistance / 2);
final double c = 2
* Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
final double distance = earthRadius * c * kmToNM;
return distance;
}
private double getNodeCoordinate(
final int nodeId,
final String coordinateType) {
final long neo4jId = graph.toOriginalNodeId(nodeId);
final Node node = dbService.getNodeById(neo4jId);
return (double) node.getProperty(coordinateType);
}
private void updateCosts(
final int source,
final int target,
final double newCost,
final double heuristic) {
final double oldCost =
gCosts.getOrDefault(target, Double.MAX_VALUE);
if (newCost < oldCost) {
gCosts.put(target, newCost);
fCosts.put(target, newCost + heuristic);
path.put(target, source);
}
}
private void reset() {
closedNodes.clear();
openNodes.clear();
gCosts.clear();
fCosts.clear();
path.clear();
shortestPath.clear();
totalCost = NO_PATH_FOUND;
}
public Stream<Result> resultStream() {
return StreamSupport.stream(
shortestPath.spliterator(), false)
.map(cursor -> new Result(
graph.toOriginalNodeId(cursor.value),
gCosts.get(cursor.value)));
}
public IntArrayDeque getFinalPath() {
return shortestPath;
}
public double getTotalCost() {
return totalCost;
}
public int getPathLength() {
return shortestPath.size();
}
@Override
public ShortestPathAStar me() {
return this;
}
@Override
public ShortestPathAStar release() {
graph = null;
gCosts = null;
fCosts = null;
openNodes = null;
path = null;
shortestPath = null;
closedNodes = null;
return this;
}
public static class Result {
/**
* the neo4j node id
*/
public final Long nodeId;
/**
* cost to reach the node from startNode
*/
public final Double cost;
public Result(Long nodeId, Double cost) {
this.nodeId = nodeId;
this.cost = cost;
}
}
}
</code></pre></div></div>
<p>The heuristic function is domain-specific. If chosen wisely, it can
significantly speed up the search. In our case, we achieved a 300x speedup,
enabling us to expand our search from 4,000 to 13,000 route points. The <a href="https://github.com/neo4j-contrib/neo4j-graph-algorithms/releases/tag/3.4.0.0" class="external" target="_blank" rel="noopener noreferrer">v3.4.0</a> of the
Neo4J graph algorithms shipped with the A* search algorithm.</p>
</div>
<p class="post-author right">by Wickramage Don Sadeep Madurange</p>
</div>
</main>
</body>
</html>
|