|| apoc.algo.aStarConfig - APOC 核心文档 - Neo4j 文档

apoc.algo.aStarConfig

详情

语法

apoc.algo.aStarConfig(startNode, endNode, relTypesAndDirections, config) :: (path, weight)

描述

运行 A* 搜索算法,使用给定 RELATIONSHIP 属性名称作为成本函数,查找两个 NODE 值之间的最优路径。此过程在配置中查找权重、纬度和经度属性。

输入参数

名称

类型

描述

startNode

NODE

搜索的起始节点。

endNode

NODE

搜索的结束节点。

relTypesAndDirections

STRING

用于限制算法的关系类型。关系类型使用 APOC 的关系方向模式语法表示;[<]RELATIONSHIP_TYPE1[>]|[<]RELATIONSHIP_TYPE2[>]|…​

config

MAP

{ weight = 'distance' :: STRING, default = Double.MAX_VALUE :: FLOAT, y = 'latitude' :: STRING, x = 'longitude' :: STRING, pointPropName :: STRING }

返回参数

名称

类型

描述

path

PATH

路径结果。

weight

FLOAT

给定路径的权重。

示例

给定此数据集

CREATE (b:City {name:'Berlin', coords: point({latitude:52.52464,longitude:13.40514}), lat:52.52464,lon:13.40514})
CREATE (m:City {name:'München', coords: point({latitude:48.1374,longitude:11.5755}), lat:48.1374,lon:11.5755})
CREATE (f:City {name:'Frankfurt',coords: point({latitude:50.1167,longitude:8.68333}), lat:50.1167,lon:8.68333})
CREATE (h:City {name:'Hamburg', coords: point({latitude:53.554423,longitude:9.994583}), lat:53.554423,lon:9.994583})
CREATE (b)-[:DIRECT {dist:255.64*1000}]->(h)
CREATE (b)-[:DIRECT {dist:504.47*1000}]->(m)
CREATE (b)-[:DIRECT {dist:424.12*1000}]->(f)
CREATE (f)-[:DIRECT {dist:304.28*1000}]->(m)
CREATE (f)-[:DIRECT {dist:393.15*1000}]->(h)

可以执行以下查询(利用作为数字的 latlon 节点属性,以及 dist 关系属性,默认成本为 100)

MATCH (from:City {name:'München'}), (to:City {name:'Hamburg'})
CALL apoc.algo.aStarConfig(from, to, 'DIRECT', {weight:'dist',y:'lat', x:'lon',default:100})
YIELD weight, path
RETURN weight, path
结果
weight path

697430.0

{
  "start": {
"identity": 1520006,
"labels": [
      "City"
    ],
"properties": {
"name": "München",
"lon": 11.5755,
"lat": 48.1374,
"coords": point({srid:4326, x:11.5755, y:48.1374})
    }
  },
  "end": {
"identity": 1520008,
"labels": [
      "City"
    ],
"properties": {
"name": "Hamburg",
"lon": 9.994583,
"lat": 53.554423,
"coords": point({srid:4326, x:9.994583, y:53.554423})
    }
  },
  "segments": [
    {
      "start": {
"identity": 1520006,
"labels": [
          "City"
        ],
"properties": {
"name": "München",
"lon": 11.5755,
"lat": 48.1374,
"coords": point({srid:4326, x:11.5755, y:48.1374})
        }
      },
      "relationship": {
"identity": 3,
"start": 1520007,
"end": 1520006,
"type": "DIRECT",
"properties": {
"dist": 304280.0
        }
      },
      "end": {
"identity": 1520007,
"labels": [
          "City"
        ],
"properties": {
"name": "Frankfurt",
"lon": 8.68333,
"lat": 50.1167,
"coords": point({srid:4326, x:8.68333, y:50.1167})
        }
      }
    },
    {
      "start": {
"identity": 1520007,
"labels": [
          "City"
        ],
"properties": {
"name": "Frankfurt",
"lon": 8.68333,
"lat": 50.1167,
"coords": point({srid:4326, x:8.68333, y:50.1167})
        }
      },
      "relationship": {
"identity": 4,
"start": 1520007,
"end": 1520008,
"type": "DIRECT",
"properties": {
"dist": 393150.0
        }
      },
      "end": {
"identity": 1520008,
"labels": [
          "City"
        ],
"properties": {
"name": "Hamburg",
"lon": 9.994583,
"lat": 53.554423,
"coords": point({srid:4326, x:9.994583, y:53.554423})
        }
      }
    }
  ],
  "length": 2.0
}

或者等效地,使用相同的配置,利用作为 Point 类型的 coords 节点属性,获得相同的结果。请注意,在 3D 坐标的情况下,该过程将只选择 x 和 y 或经度和纬度值。

MATCH (from:City {name:'München'}), (to:City {name:'Hamburg'})
CALL apoc.algo.aStarConfig(from, to, 'DIRECT', {pointPropName:'coords', weight:'dist', default:100})
YIELD weight, path
RETURN weight, path
© . This site is unofficial and not affiliated with Neo4j, Inc.