说明:使用neo4j算法库时需引入跟neo4j数据库对应的算法库插件或自定义算法库
1.简介
优先链接时一种用于计算节点接近度的度量。基于他们的共享邻居
它使用如下公式进行计算的:,其中N(x)是与节点x相邻的节点集,N(y)是与y节点相邻的节点集,PN(x,y)即为计算x和y节点的相邻节点数乘积
值0表示两个节点不接近,而较高的值表示节点相近。
2.使用场景
进入抖音app,常常会弹出一个框,可能认识,上面显示显示由多少个共同朋友,即使用公共邻居算法。
neo4j算法库中提供了algo.linkprediction.preferentialAttachment函数
3.源码解析
源码如下:
public double preferentialAttachment(@Name("node1") Node node1, @Name("node2") Node node2,
@Name(value = "config", defaultValue = "{}") Map<String, Object> config) {
if (node1 == null || node2 == null) {
throw new RuntimeException("Nodes must not be null");
}
ProcedureConfiguration configuration = ProcedureConfiguration.create(config);
RelationshipType relationshipType = configuration.getRelationship();
Direction direction = configuration.getDirection(Direction.BOTH);
return degree(node1, relationshipType, direction) * degree(node2, relationshipType, direction);
}
入参:node1,node2为两个节点数据,不能为空,config为条件参数,config中能识别relationshipQuery和direction,其中relationshipQuery为节点关系编码,direction为方向,默认为BOTH无方向,OUTING为node1->node2,INCOMING为node2->node1。
方法解析如下:
1.将传入的config参数放入ProcedureConfiguration的config中
ProcedureConfiguration configuration = ProcedureConfiguration.create(config);
2.获取ProcedureConfiguration类config中relationshipQuery参数(此参数为关系名称)
RelationshipType relationshipType = configuration.getRelationship();
3.获取config中direction参数,此为指定关系方向
Direction direction = configuration.getDirection(Direction.BOTH);
4.条件返回节点1的关系节点,并且过滤掉与节点2没有关系的节点(即取节点1和节点2同时有relationshipType关系的且方向为direction的节点)
Set<Node> neighbors = new NeighborsFinder(api).findCommonNeighbors(node1, node2, relationshipType, direction);
5.统计节点node1具有relationshipType关系,且方向为derection的节点数
degree(node1, relationshipType, direction):具体代码如下
private int degree(Node node, RelationshipType relationshipType, Direction direction) {
return relationshipType == null ? node.getDegree(direction) : node.getDegree(relationshipType, direction);
}
node.getDegree(relationshipType, direction)的源码如下:作用统计该节点关系为relationshipType,指向为direction的关系的数量
@Override
public int getDegree(RelationshipType relationshipType, Direction direction) {
return (int) Iterables.count(getRelationships(relationshipType,direction));
}
node.getDegree(direction)的源码如下:作用统计该节点关系数量
@Override
public int getDegree(Direction direction) {
return (int) Iterables.count(getRelationships(direction));
}
4.neo4j中公共邻居算法使用示例
1.初始化数据
MERGE (zhen:Person {name: "Zhen"})
MERGE (praveena:Person {name: "Praveena"})
MERGE (michael:Person {name: "Michael"})
MERGE (arya:Person {name: "Arya"})
MERGE (karin:Person {name: "Karin"})
MERGE (zhen)-[:FRIENDS]-(arya)
MERGE (zhen)-[:FRIENDS]-(praveena)
MERGE (praveena)-[:WORKS_WITH]-(karin)
MERGE (praveena)-[:FRIENDS]-(michael)
MERGE (michael)-[:WORKS_WITH]-(karin)
MERGE (arya)-[:FRIENDS]-(karin)
2.以下将返回 Michael 和 Karin 的 Preferential Attachment 分数
MATCH (p1:Person {name: 'Michael'})
MATCH (p2:Person {name: 'Karin'})
RETURN algo.linkprediction.preferentialAttachment(p1, p2) AS score
结果:6.0
3.我们还可以根据特定的关系类型计算一对节点的分数。以下将仅根据FRIENDS
关系返回 Michael 和 Karin 的 Preferential Attachment 分数
MATCH (p1:Person {name: 'Michael'})
MATCH (p2:Person {name: 'Karin'})
RETURN algo.linkprediction.preferentialAttachment(p1, p2, {relationshipQuery: "FRIENDS"}) AS score
结果:1.0