NEO4J-链路预测算法03-优先链接算法(Preferential Attachment)应用场景简介

说明:使用neo4j算法库时需引入跟neo4j数据库对应的算法库插件或自定义算法库

1.简介

        优先链接时一种用于计算节点接近度的度量。基于他们的共享邻居

        它使用如下公式进行计算的:PN(x,y)=|N(x)|*|N(y)|,其中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

上一篇:NEO4J-链路预测算法02-公共邻居算法(adamicAdar)应用场景简介(抖音朋友推荐)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂攻城师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值