《图算法》中Graph Data Science代替Graph Algorithms (Neo4j)代码(第四章)

Neo4j 4.1将GraphAlgorithms用Graph Data Science Library代替。《图算法》一书中的示例需要改写,本文按照书中的示例,改写了代码,并实际运行,结果与书中一致。

第四章 GDS代码

首先要创建内存数据投影,Graph Data Science使用内存数据库。以下代码中的’myGraph’就是GDS中要求的graphName。

call gds.graph.create("myGraph","Place","EROAD") 
YIELD graphName, nodeCount, relationshipCount;
  • Shortest Path with Neo4j

原代码

MATCH (source:Place {id: "Amsterdam"}),
 (destination:Place {id: "London"})
CALL algo.shortestPath.stream(source, destination, null)
YIELD nodeId, cost
RETURN algo.getNodeById(nodeId).id AS place, cost

GDS代码

MATCH (source:Place {id: "Amsterdam"}),(destination:Place {id:"London"})
CALL gds.alpha.shortestPath.stream({nodeProjection: 'Place', 
relationshipProjection: 'EROAD',
startNode: source,
endNode: destination,
relationshipWeightProperty: null})
YIELD nodeId, cost
RETURN gds.util.asNode(nodeId).id AS name, cost
  • Shortest Path (Weighted) with Neo4j

原代码

MATCH (source:Place {id: "Amsterdam"}),
 (destination:Place {id: "London"})
CALL algo.shortestPath.stream(source, destination, "distance")
YIELD nodeId, cost
RETURN algo.getNodeById(nodeId).id AS place, cost

GDS代码

MATCH (source:Place {id: "Amsterdam"}),(destination:Place {id: "London"})
CALL gds.alpha.shortestPath.stream({nodeProjection: 'Place', 
relationshipProjection:{
EROAD:{
type: 'EROAD',
properties: 'distance',
orientation: 'UNDIRECTED'
}},
startNode: source,
endNode: destination,
relationshipWeightProperty: 'distance'
})
YIELD nodeId, cost
RETURN gds.util.asNode(nodeId).id AS name, cost
  • Shortest Path Variation: A*

原代码

MATCH (source:Place {id: "Den Haag"}),
 (destination:Place {id: "London"})
CALL algo.shortestPath.astar.stream(source,
 destination, "distance", "latitude", "longitude")
YIELD nodeId, cost
RETURN algo.getNodeById(nodeId).id AS place, cost

GDS代码

MATCH  (source:Place {id: "Den Haag"}),(destination:Place {id: "London"})
CALL gds.alpha.shortestPath.astar.stream({
nodeProjection: {Place: {
properties: ['longitude', 'latitude']}},
relationshipProjection: {EROAD: {type: 'EROAD',orientation: 'UNDIRECTED',properties: 'distance'}},
startNode: source,
endNode: destination,
relationshipWeightProperty: 'distance',
propertyKeyLat: 'latitude',
propertyKeyLon: 'longitude'})
YIELD nodeId, cost
RETURN gds.util.asNode(nodeId).id AS station, cost
  • Yen’s k-Shortest Paths

原代码

MATCH (start:Place {id:"Gouda"}),
 (end:Place {id:"Felixstowe"})
CALL algo.kShortestPaths.stream(start, end, 5, "distance")
YIELD index, nodeIds, path, costs
RETURN index,
 [node in algo.getNodesById(nodeIds[1..-1]) | node.id] AS via,
 reduce(acc=0.0, cost in costs | acc + cost) AS totalCost

GDS代码

MATCH (start:Place {id:"Gouda"}),(end:Place {id:"Felixstowe"})
CALL gds.alpha.kShortestPaths.stream({nodeProjection:{Place: {properties: ['longitude', 'latitude']}},
relationshipProjection: {EROAD: {type: 'EROAD',orientation: 'UNDIRECTED',properties: 'distance'}},
startNode:start,endNode:end,k:5,relationshipWeightProperty: 'distance'})
YIELD index, nodeIds, costs
RETURN index, [node IN gds.util.asNodes(nodeIds) | node.id] AS places, reduce(acc = 0.0, cost IN costs | acc + cost) AS totalCost
  • All Pairs Shortest Path

原代码

CALL algo.allShortestPaths.stream(null)
YIELD sourceNodeId, targetNodeId, distance
WHERE sourceNodeId < targetNodeId
RETURN algo.getNodeById(sourceNodeId).id AS source,
 algo.getNodeById(targetNodeId).id AS target,
 distance
ORDER BY distance DESC
LIMIT 10

GDS代码

CALL gds.alpha.allShortestPaths.stream({nodeProjection:{Place: {properties: ['longitude', 'latitude']}},
relationshipProjection: {EROAD: {type: 'EROAD',orientation: 'UNDIRECTED'}},
relationshipWeightProperty: null})
YIELD sourceNodeId, targetNodeId, distance
WHERE sourceNodeId < targetNodeId
RETURN gds.util.asNode(sourceNodeId).id AS source,
gds.util.asNode(targetNodeId).id AS target,distance
ORDER BY distance DESC
LIMIT 10
  • All Pairs Shortest Path(Weighted)

原代码

CALL algo.allShortestPaths.stream("distance")
YIELD sourceNodeId, targetNodeId, distance
WHERE sourceNodeId < targetNodeId
RETURN algo.getNodeById(sourceNodeId).id AS source,
 algo.getNodeById(targetNodeId).id AS target,
 distance
ORDER BY distance DESC
LIMIT 10

GDS代码

CALL gds.alpha.allShortestPaths.stream({nodeProjection:{Place: {properties: ['longitude', 'latitude']}},
relationshipProjection: {EROAD: {type: 'EROAD',orientation: 'UNDIRECTED',properties: 'distance'}},
relationshipWeightProperty: 'distance'})
YIELD sourceNodeId, targetNodeId, distance
WHERE sourceNodeId < targetNodeId
RETURN gds.util.asNode(sourceNodeId).id AS source,
gds.util.asNode(targetNodeId).id AS target,distance
ORDER BY distance DESC
LIMIT 10
  • Single Source Shortest Path

原代码

MATCH (n:Place {id:"London"})
CALL algo.shortestPath.deltaStepping.stream(n, "distance", 1.0)
YIELD nodeId, distance
WHERE algo.isFinite(distance)
RETURN algo.getNodeById(nodeId).id AS destination, distance
ORDER BY distance

GDS代码

MATCH (n:Place {id:"London"})
call gds.alpha.shortestPath.deltaStepping.stream({nodeProjection:{Place: {properties: ['longitude', 'latitude']}},
relationshipProjection: {EROAD: {type: 'EROAD',orientation: 'UNDIRECTED',properties: 'distance'}},
startNode: n,
relationshipWeightProperty: 'distance',
delta: 3.0})
YIELD nodeId, distance
RETURN gds.util.asNode(nodeId).id AS destination, distance
ORDER BY distance
  • Minimum Spanning Tree

原代码

MATCH (n:Place {id:"Amsterdam"})
CALL algo.spanningTree.minimum("Place", "EROAD", "distance", id(n),
 {write:true, writeProperty:"MINST"})
YIELD loadMillis, computeMillis, writeMillis, effectiveNodeCount
RETURN loadMillis, computeMillis, writeMillis, effectiveNodeCount


MATCH path = (n:Place {id:"Amsterdam"})-[:MINST*]-()
WITH relationships(path) AS rels
UNWIND rels AS rel
WITH DISTINCT rel AS rel
RETURN startNode(rel).id AS source, endNode(rel).id AS destination,
 rel.distance AS cost

GDS代码

MATCH (n:Place {id:"Amsterdam"})
CALL gds.alpha.spanningTree.minimum.write({nodeProjection:{Place: {properties: ['longitude', 'latitude']}},
relationshipProjection: {EROAD: {type: 'EROAD',orientation: 'UNDIRECTED',properties: 'distance'}},
startNodeId: id(n),
relationshipWeightProperty: 'distance',
writeProperty: 'MINST',
weightWriteProperty: 'writeCost'
})
YIELD createMillis, computeMillis, writeMillis, effectiveNodeCount
RETURN createMillis, computeMillis, writeMillis, effectiveNodeCount;

MATCH path = (n:Place {id:"Amsterdam"})-[:MINST*]-()
WITH relationships(path) AS rels
UNWIND rels AS rel
WITH DISTINCT rel AS rel
RETURN startNode(rel).id AS source, endNode(rel).id AS destination,
rel.writeCost AS cost
  • Random Walk

原代码

MATCH (source:Place {id: "London"})
CALL algo.randomWalk.stream(id(source), 5, 1)
YIELD nodeIds
UNWIND algo.getNodesById(nodeIds) AS place
RETURN place.id AS place

GDS代码

MATCH (source:Place {id: "London"})
CALL gds.alpha.randomWalk.stream({nodeProjection:'*',
relationshipProjection: {EROAD: {type: 'EROAD',orientation: 'UNDIRECTED'}},
start: id(source),
steps: 5,
walks: 1})
YIELD nodeIds
UNWIND nodeIds AS nodeId
RETURN gds.util.asNode(nodeId).id AS place
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值