### 6.5.5. A*

This section describes the A* algorithm in the Neo4j Graph Data Science library.

The A* (pronounced “A-star”) algorithm improves on the classic Dijkstra algorithm. It is based upon the observation that some searches are informed, and that by being informed we can make better choices over which paths to take through the graph.

This algorithm is in the alpha tier. For more information on algorithm tiers, see Chapter 6, Algorithms.

This section includes:

#### 6.5.5.1. History and explanation

The A* algorithm was first described in 1968 by Peter Hart, Nils Nilsson, and Bertram Raphael. For more information, see A Formal Basis for the Heuristic Determination of Minimum Cost Paths.

In A*, we split the path cost into two parts:

`g(n)`
This is the cost of the path from the starting point to some node n.
`h(n)`
This represents the estimated cost of the path from the node n to the destination node, as computed by a heuristic (an intelligent guess).

The A* algorithm balances `g(n)` and `h(n)` as it iterates the graph, thereby ensuring that at each iteration it chooses the node with the lowest overall cost `f(n) = g(n) + h(n)`.

In our implementation, geospatial distance is used as heurestic.

#### 6.5.5.2. Use-cases - when to use the A* algorithm

• The A* algorithm can be used to find shortest paths between single pairs of locations, where GPS coordinates are known.

#### 6.5.5.3. Syntax

The following will run the algorithm and stream results:

``````CALL gds.alpha.shortestPath.astar.stream(configuration:Map)
YIELD nodeId, cost``````

Table 6.399. Configuration
Name Type Default Optional Description

startNode

Node

null

no

The start node

endNode

Node

null

no

The end node

relationshipWeightProperty

String

null

yes

The projected property name that contains weight

propertyKeyLat

String

null

no

The projected property name that contains latitude coordinate

propertyKeyLon

String

null

no

The projected property name that contains longitude coordinate

nodeQuery

String

null

yes

The label to load from the graph. If null, load all nodes

relationshipQuery

String

null

yes

The relationship type to load from the graph. If null, load all nodes

defaultValue

Float

null

yes

The default value of the weight in case it is missing or invalid

Table 6.400. Results
Name Type Description

nodeId

Integer

Node ID

cost

Integer

The cost it takes to get from start node to specific node

#### 6.5.5.4. A* algorithm sample

The following will create a sample graph:

``````CREATE (a:Station {name: 'King\'s Cross St. Pancras', latitude: 51.5308, longitude: -0.1238}),
(b:Station {name: 'Euston',                    latitude: 51.5282, longitude: -0.1337}),
(c:Station {name: 'Camden Town',               latitude: 51.5392, longitude: -0.1426}),
(d:Station {name: 'Mornington Crescent',       latitude: 51.5342, longitude: -0.1387}),
(e:Station {name: 'Kentish Town',              latitude: 51.5507, longitude: -0.1402}),
(a)-[:CONNECTION {time: 2}]->(b),
(b)-[:CONNECTION {time: 3}]->(c),
(b)-[:CONNECTION {time: 2}]->(d),
(d)-[:CONNECTION {time: 2}]->(c),
(c)-[:CONNECTION {time: 2}]->(e)``````

The following will run the algorithm and stream results:

``````MATCH (start:Station {name: 'King\'s Cross St. Pancras'}), (end:Station {name: 'Kentish Town'})
CALL gds.alpha.shortestPath.astar.stream({
nodeProjection: {
Station: {
properties: ['longitude', 'latitude']
}
},
relationshipProjection: {
CONNECTION: {
type: 'CONNECTION',
orientation: 'UNDIRECTED',
properties: 'time'
}
},
startNode: start,
endNode: end,
propertyKeyLat: 'latitude',
propertyKeyLon: 'longitude'
})
YIELD nodeId, cost
RETURN gds.util.asNode(nodeId).name AS station, cost``````

Table 6.401. Results
station cost

"King’s Cross St. Pancras"

0

"Euston"

2

"Camden Town"

5

"Kentish Town"

7

#### 6.5.5.5. Cypher projection

If node label and relationship type are not selective enough to create the graph projection to run the algorithm on, you can use Cypher queries to project your graph. This can also be used to run algorithms on a virtual graph. You can learn more in the Section 4.3, “Cypher projection” section of the manual.

``````MATCH (start:Station {name: "King's Cross St. Pancras"}), (end:Station {name: "Kentish Town"})
CALL gds.alpha.shortestPath.astar.stream({
nodeQuery: 'MATCH (p:Station) RETURN id(p) AS id, p.latitude AS latitude, p.longitude AS longitude',
relationshipQuery: 'MATCH (p1:Station)-[r:CONNECTION]->(p2:Station) RETURN id(p1) AS source, id(p2) AS target, r.time AS weight',
startNode: start,
endNode: end,
relationshipWeightProperty: 'weight',
propertyKeyLat: 'latitude',
propertyKeyLat: 'longitude'
})
YIELD nodeId, cost
RETURN gds.util.asNode(nodeId).name AS station, cost``````
Table 6.402. Results
station cost

"King’s Cross St. Pancras"

0

"Euston"

2

"Camden Town"

5

"Kentish Town"

7