neo4实现 Degree Centrality算法

文章目录


创建一个社交网络属性图(这是一个有向图)

CREATE (alice:User {name: 'Alice'}),
       (bridget:User {name: 'Bridget'}),
       (charles:User {name: 'Charles'}),
       (doug:User {name: 'Doug'}),
       (mark:User {name: 'Mark'}),
       (michael:User {name: 'Michael'}),
       (alice)-[:FOLLOWS]->(doug),
       (alice)-[:FOLLOWS]->(bridget),
       (alice)-[:FOLLOWS]->(charles),
       (mark)-[:FOLLOWS]->(doug),
       (mark)-[:FOLLOWS]->(michael),
       (bridget)-[:FOLLOWS]->(doug),
       (charles)-[:FOLLOWS]->(doug),
       (michael)-[:FOLLOWS]->(doug)

一、属性图如下

neo4实现 Degree Centrality算法

二、实现算法(unweight)

1.stream模式计算每个点的入度

CALL gds.alpha.degree.stream({
  nodeProjection: 'User',
  relationshipProjection: {
    FOLLOWS: {
      type: 'FOLLOWS',
      orientation: 'REVERSE'
    }
  }
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score AS followers
ORDER BY followers DESC

这句话orientation: ‘REVERSE’;表示节点关系的计算方向逆转,该算法默认计算出度,现在逆转计算入度

2.各节点入度计算结果如下

neo4实现 Degree Centrality算法

3.stream模式计算每个点的出度

CALL gds.alpha.degree.stream({
  nodeProjection: 'User',
  relationshipProjection: 'FOLLOWS'
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score AS followers
ORDER BY followers DESC

4.各节点出度计算结果如下

neo4实现 Degree Centrality算法

4.结论

We can see that Doug is the most popular user in our imaginary Twitter graph, with 5 followers - all other users follow him, but he doesn’t follow anybody back. In the real Twitter network celebrities have very high follower counts but tend to follow very few back people. We could therefore consider Doug a celebrity!
我们可以得出Doug是个名人,因为关注他的多,他关注的少,所以他是一个广受欢迎的“点”,可以被推荐给他人关注或者需要重点关注他。

三、实现算法(weight)

1.带权重的属性图如下

neo4实现 Degree Centrality算法

2.stream模式计算每个点的入度

CALL gds.alpha.degree.stream({
   nodeProjection: 'User',
   relationshipProjection: {
       FOLLOWS: {
           type: 'FOLLOWS',
           orientation: 'REVERSE',
           properties: 'score'
       }
   },
   relationshipWeightProperty: 'score'
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score AS weightedFollowers
ORDER BY weightedFollowers DESC

3.各节点入度计算结果如下

neo4实现 Degree Centrality算法

4.各节点出度计算结果如下

CALL gds.alpha.degree.stream({
   nodeProjection: 'User',
   relationshipProjection: {
       FOLLOWS: {
           type: 'FOLLOWS',         
           properties: 'score'
       }
   },
   relationshipWeightProperty: 'score'
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score AS weightedFollowers
ORDER BY weightedFollowers DESC

5.各节点出度计算结果如下

neo4实现 Degree Centrality算法

Doug still remains our most popular user, but there isn’t such a big gap to the next person. Charles and Michael both only have one follower, but those relationships have a high relationship weight.
增加权重之后,我们发现Charies和Michael的分数并不比Doug逊色,说明在真实现实世界,考虑权重影响因素还是很有必要的。

上一篇:2020/11/12 刘一辰的JAVA随笔


下一篇:数据结构与算法(Python版)二十二:递归可视化(谢尔宾斯基三角形)