作者:张武科
概述
**紧密中心度(Closeness Centrality)**计量了一个节点到其他所有节点的紧密性,即该节点到其他节点的距离的倒数;节点对应的值越高表示紧密性越好,能够在图中传播信息的能力越强,可用以衡量信息流入或流出该节点的能力,多用与社交网络中关键节点发掘等场景。
算法介绍
对于图中一个给定节点,紧密性中心性是该节点到其他所有节点的最小距离和的倒数:
其中,u表示待计算紧密中心度的节点,d(u, v)表示节点u到节点v的最短路径距离;实际场景中,更多地使用归一化后的紧密中心度,即计算目标节点到其他节点的平均距离的倒数:
其中,n表示图中节点数。
算法实现
首先,基于AlgorithmUserFunction
接口实现ClosenessCentrality
,如下:
@Description(name = "closeness_centrality", description = "built-in udga for ClosenessCentrality")
public class ClosenessCentrality implements AlgorithmUserFunction<Long, Long> {
private AlgorithmRuntimeContext context;
private long sourceId;
@Override
public void init(AlgorithmRuntimeContext context, Object[] params) {
this.context = context;
if (params.length != 1) {
throw new IllegalArgumentException("Only support one arguments, usage: func(sourceId)");
}
this.sourceId = ((Number) params[0]).longValue();
}
@Override
public void process(RowVertex vertex, Iterator<Long> messages) {
List<RowEdge> edges = context.loadEdges(EdgeDirection.OUT);
if (context.getCurrentIterationId() == 1L) {
context.sendMessage(vertex.getId(), 1L);
context.sendMessage(sourceId, 1L);
} else if (context.getCurrentIterationId() == 2L) {
context.updateVertexValue(ObjectRow.