一.介绍:
  连接组件算法也可称为并查集算法,是解决动态连通性问题的一类非常高效的数据结构。
  在计算机科学中,并查集是一种树形的数据结构,用于处理不交集的合并及查询问题。
  并查集存在两个操作(1,union联合;2,find查找)和一个需要解答的问题(1.isConneced是否相互连           接;2,isSameSet是否在同一集合中)
二.neo4j算法
  在neo4j算法中,将并查集算法分为两种,一种是无权重的,一种是有权重的,语法分别如下:
  
无权重

CALL algo.unionFind.stream(label:String, relationship:String)
YIELD nodeId,setId

有权重

CALL algo.unionFind.stream(label:String, relationship:String, {weightProperty:'weight',
defaultValue:0.0, threshold:1.0, concurrency: 1})
YIELD nodeId,setId

三.实例:

创建节点及关系:

MERGE (nAlice:User {id:'Alice'})
MERGE (nBridget:User {id:'Bridget'})
MERGE (nCharles:User {id:'Charles'})
MERGE (nDoug:User {id:'Doug'})
MERGE (nMark:User {id:'Mark'})
MERGE (nMichael:User {id:'Michael'})
MERGE (nAlice)-[:FRIEND {weight:0.5}]->(nBridget)
MERGE (nAlice)-[:FRIEND {weight:4}]->(nCharles)
MERGE (nMark)-[:FRIEND {weight:1}]->(nDoug)
MERGE (nMark)-[:FRIEND {weight:2}]->(nMichael)

neo4j社区发现算法(Community detection algorithms)-3.The Connected Components algorithm
无权重:

CALL algo.unionFind.stream('User', 'FRIEND', {})
YIELD nodeId,setId
RETURN algo.getNodeById(nodeId).id AS user, setId

neo4j社区发现算法(Community detection algorithms)-3.The Connected Components algorithm有权重:

CALL algo.unionFind.stream('User', 'FRIEND', {weightProperty:'weight',
defaultValue:0.0, threshold:1.0, concurrency: 1})
YIELD nodeId,setId
RETURN algo.getNodeById(nodeId).id AS user, setId

neo4j社区发现算法(Community detection algorithms)-3.The Connected Components algorithm通过对比两个有无权重之间算法的差异,可以看到Bridget在有权重的算法中被单独分为了一个集合。

相关文章: