[Neo4j系列十五]neo4j扩展包APOC的图算法

neo4j 3月前 ⋅ 454 阅读

apoc扩展包提供了一些图算法,包括社区检测、路径扩展、中心点算法、PageRank算法,其中社区检测、中心点算法、PageRank算法与algo的有些重合,如果用到这几个算法,建议直接上algo,路径扩展实用性挺强,可以仔细研究一下。

1.Community Detection via Label Propagation标签传播的社区检测
CALL apoc.algo.community(25,null,'partition','X','OUTGOING','weight',10000)

第一个参数即’25’代表算法扫描所有节点的次数
第二个参数即null代表标签名的列表或筛选出来的节点列表
第三个参数即’partition’代表通过这个算法中权重计算设定一个属性值
第四个参数即’X’代表边的类型或type
第五个参数即’OUTGOING’代表边的方向
第六个参数即’weight’计算权重时选取的属性,如果没有默认设为1.0

2.Expand paths扩展路径
apoc.path.expand()查找某个节点开始的相关路径

apoc.path.expand(startNode|Node, relationshipFilter, labelFilter, minLevel, maxLevel )

match (tom:Person {name :"Tom Hanks"})
call apoc.path.expand(tom,"ACTED_IN>|PRODUCED<|FOLLOWS<","+Movie|Person",0,3) yield path as pp
return pp;
apoc.path.expandConfig()通过更多灵活配置查找相关路径

apoc.path.expandConfig(startNodeNode/list, {config}) yield path
{minLevel: -1|number,
maxLevel: -1|number,
relationshipFilter: ‘[<]RELATIONSHIP_TYPE1[>]|[<]RELATIONSHIP_TYPE2[>], [<]RELATIONSHIP_TYPE3[>]|[<]RELATIONSHIP_TYPE4[>]’,
labelFilter: ‘[+-/>]LABEL1|LABEL2|,[+-/>]LABEL1|LABEL2|,…’,
uniqueness: RELATIONSHIP_PATH|NONE|NODE_GLOBAL|NODE_LEVEL|NODE_PATH|NODE_RECENT|
            RELATIONSHIP_GLOBAL|RELATIONSHIP_LEVEL|RELATIONSHIP_RECENT,
bfs: true|false,
filterStartNode: true|false,
limit: -1|number,
optional: true|false,
endNodes: [nodes],
terminatorNodes: [nodes],
beginSequenceAtStart: true|false}
参数注释:
   minLevel最小度
   maxLevel最大度
   relationshipFilter关系过滤,具有方向性’<’代表INCOMING,’>’代表OUTGOING,没有指定方向就是双向的
    labelFilter节点标签过滤,’+’标签白名单,’-‘标签黑名单,’/‘终止节点标签,’>’结束节点标签
   Uniqueness独特性,RELATIONSHIP_PATH返回起始节点的每一条路径,默认的,NODE_GLOBAL,一个节点不能经过多于1次等
   filterStartNode是否过滤初始节点
   endNodes结束节点
   terminatorNodes终止节点
   limit返回路径数目,默认-1没有限制
   bfs是否为广度优先搜索

match (k:Person {name:'Keanu Reeves'})
call apoc.path.expandConfig(k, {relationshipFilter:'ACTED_IN|PRODUCED|DIRECTED', labelFilter:'/Western', uniqueness: 'NODE_GLOBAL'}) yield path
return path
apoc.path.subgraphNodes()获得起始节点的子图

apoc.path.subgraphNodes(startNodeNode/list, {maxLevel, relationshipFilter, labelFilter, bfs:true, filterStartNode:true, limit:-1, optional:false}) yield node

MATCH (user:User) WHERE user.id = 460
CALL apoc.path.subgraphNodes(user, {relationshipFilter:'FRIEND'}) YIELD node
RETURN node;
apoc.path.subgraphAll()

apoc.path.subgraphAll(startNodeNode/list, {maxLevel, relationshipFilter, labelFilter, bfs:true, filterStartNode:true, limit:-1}) yield nodes, relationships

MATCH (user:User) WHERE user.id = 460
CALL apoc.path.subgraphAll(user, {maxLevel:4}) YIELD nodes, relationships
RETURN nodes, relationships;
apoc.path.spanningTree()生成树

apoc.path.spanningTree(startNodeNode/list, {maxLevel, relationshipFilter, labelFilter, bfs:true, filterStartNode:true, limit:-1, optional:false}) yield path

MATCH (user:User) WHERE user.id = 460
CALL apoc.path.spanningTree(user, {labelFilter:'+User'}) YIELD path
RETURN path;
3.Centrality Algorithms
// create 100 nodes
FOREACH (id IN range(0,1000) | CREATE (:Node {id:id}))
// over the cross product (1M) create 100.000 relationships
MATCH (n1:Node),(n2:Node) WITH n1,n2 LIMIT 1000000 WHERE rand() < 0.1
CREATE (n1)-[:TYPE]->(n2)
Closeness Centrality Procedure亲密度中心点
apoc.algo.closeness()
MATCH (node:Node)
WHERE node.id %2 = 0
WITH collect(node) AS nodes
CALL apoc.algo.closeness(['TYPE'],nodes,'INCOMING') YIELD node, score
RETURN node, score
ORDER BY score DESC
Betweenness Centrality Procedure中介中心点
apoc.algo.betweenness()apoc.algo.betweenness()
MATCH (node:Node)
WHERE node.id %2 = 0
WITH collect(node) AS nodes
CALL apoc.algo.betweenness(['TYPE'],nodes,'BOTH') YIELD node, score
RETURN node, score
ORDER BY score DESC
4.PageRank Algorithm
apoc.algo.pageRank()
MATCH (node:Node)
WHERE node.id %2 = 0
WITH collect(node) AS nodes
// compute over relationships of all types
CALL apoc.algo.pageRank(nodes) YIELD node, score
RETURN node, score
ORDER BY score DESC
apoc.algo.pageRankWithConfig()可以配置的pageRank算法
MATCH (node:Node)
WHERE node.id %2 = 0
WITH collect(node) AS nodes
// peroform 10 page rank iterations, computing only over relationships of type TYPE_1
CALL apoc.algo.pageRankWithConfig(nodes,{iterations:10,types:'TYPE_1'}) YIELD node, score
RETURN node, score
ORDER BY score DESC

参考链接:https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_algorithm_procedures

 
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: