PHP计算网络中节点的中心性

时间:2017-01-12 16:14:55

标签: php json matrix graph dijkstra

我正在寻找一种方法来计算一组网络节点的接近度和中介性。

作为输入,我有一个带有起始节点端节点和边缘信息的json对象:

[{
    "publication": 4,
    "origin": 10,
    "destination": 11
},

 ....,

{
    "publication": 5,
    "origin": 10,
    "destination": 12
}, {
    "publication": 8,
    "origin": 12,
    "destination": 13
}]

由于使用邻居矩阵对于非常大的数据集来说是无效的,我正在寻找另一种计算中心性的方法。 Dijkstra的算法是否可以选择,因为我有一个无向/未加权的图形?我将如何实现它以使用此json作为输入?

1 个答案:

答案 0 :(得分:1)

为了帮助您入门,您可以执行以下操作:

NullPointer Exception

请注意,多边指示$edgeList = json_decode($thatJSONDataYouHaveInTheQuestion,true); $graph = []; foreach ($edgeList as $edgeData) { $graph[$edgeData["origin"]][$edgeData["destination"]] = isset($graph[$edgeData["origin"]][$edgeData["destination"]])?$graph[$edgeData["origin"]][$edgeData["destination"]]+1:1; //$graph[$edgeData["destination"]][$edgeData["origin"]] = isset($graph[$edgeData["destination"]][$edgeData["origin"]])?$graph[$edgeData["destination"]][$edgeData["origin"]]+1:1 //Uncomment for undirected graphs } 处的数字 现在你有一个非常简单的图形结构。你可以这样做:

$graph["sourceN"]["targetN"]

或基本上做任何你需要做的事情来实现PHP中的Dijkstra算法。

节点由function containsEdge($graph, $source, $target) { return isset($graph[$source]) && isset($graph[$source][$target]) && $graph[$source][$target] > 0; } 给出,或节点的所有相邻边由array_keys($graph)

给出
相关问题