刚才例会听不知道谁说了节点以一定概率向邻居节点发送激活信号,挺感兴趣,写了demo
函数jihuo以递归方式输送信号,这里没有卡递归层数,如果网络太大会崩溃,但是每次概率p会自乘,意味初始概率是0.5的话,下一层传播概率是0.25,越来越小,目前跑代码还没有崩溃,但是层数太高真的会崩。若随机数在范围内则进行激活下个邻居节点,代码如下:
import os
import networkx as nx
import random
from util.graphDealer import load_graph_gml
def jihuo(G, node, percent, res_dic):
percent *= percent
res_dic[node] += 1
for nei in nx.neighbors(G, node):
if random.random() < percent:
jihuo(G, nei, percent, res_dic)
if __name__ == '__main__':
dataSetList = os.listdir('../network/gml/')
dataSetDict = {}
dataSetId = 1
for dataSet in dataSetList:
dataSetDict[dataSetId] = dataSet
dataSetId += 1
print(dataSetDict)
for item in dataSetDict:
print("%s: %s" % (str(item), dataSetDict[item]))
gId = input("输入划分名单序号:")
G = load_graph_gml('../network/gml/%s' % dataSetDict[int(gId)])
G.graph['title'] = dataSetDict[int(gId)].split('.')[0]
res = {}
for i in G.nodes():
res[i] = 0
p = 0.5
for n in G.nodes():
jihuo(G, n, p, res)
sorted(res.items(), key=lambda x: x[1], reverse=True)
for r in res:
print(r, res[r])
对网络中所有节点进行随机激活可以得到如下数据,这里针对冰与火之歌网络仅进行了一次实验,没有代表性
book1 | book2 | book3 | book4 | book5 | book45 | all | ||||||||
一 | Eddard-Stark | 33 | Joffrey-Baratheon | 27 | Joffrey-Baratheon | 34 | Cersei-Lannister | 23 | Daenerys-Targaryen | 27 | Jon-Snow | 33 | Jaime-Lannister | 68 |
次 | Tyrion-Lannister | 31 | Tyrion-Lannister | 24 | Tyrion-Lannister | 27 | Jaime-Lannister | 22 | Jon-Snow | 21 | Daenerys-Targaryen | 29 | Tyrion-Lannister | 67 |
随 | Cersei-Lannister | 31 | Cersei-Lannister | 21 | Jon-Snow | 25 | Tyrion-Lannister | 19 | Tyrion-Lannister | 18 | Cersei-Lannister | 28 | Stannis-Baratheon | 66 |
机 | Robert-Baratheon | 30 | Catelyn-Stark | 21 | Catelyn-Stark | 24 | Sansa-Stark | 14 | Stannis-Baratheon | 17 | Sansa-Stark | 21 | Jon-Snow | 63 |
实 | Joffrey-Baratheon | 22 | Renly-Baratheon | 21 | Sansa-Stark | 22 | Tommen-Baratheon | 14 | Theon-Greyjoy | 15 | Stannis-Baratheon | 21 | Cersei-Lannister | 63 |
验 | Robb-Stark | 22 | Stannis-Baratheon | 20 | Tywin-Lannister | 21 | Margaery-Tyrell | 11 | Cersei-Lannister | 14 | Tyrion-Lannister | 21 | Joffrey-Baratheon | 59 |
排 | Jon-Snow | 21 | Sansa-Stark | 18 | Arya-Stark | 20 | Gregor-Clegane | 11 | Hizdahr-zo-Loraq | 12 | Theon-Greyjoy | 20 | Robert-Baratheon | 57 |
名 | Petyr-Baelish | 20 | Eddard-Stark | 17 | Robert-Baratheon | 19 | Stannis-Baratheon | 11 | Tywin-Lannister | 12 | Jaime-Lannister | 19 | Catelyn-Stark | 54 |
前 | Benjen-Stark | 18 | Robb-Stark | 17 | Cersei-Lannister | 18 | Samwell-Tarly | 10 | Selyse-Florent | 12 | Tommen-Baratheon | 18 | Sansa-Stark | 53 |
十 | Catelyn-Stark | 18 | Arya-Stark | 16 | Robb-Stark | 18 | Brienne-of-Tarth | 10 | Aegon-Targaryen | 11 | Irri | 16 | Robb-Stark | 50 |
分析数据可以看出来,在纯随机情况下,一些重要人物的排名的确高于其他角色
前两部robb还没凉的时候排名还可以,四五robb凉了就没了,心疼