当前位置:   article > 正文

影响力最大化算法——degreediscount以及python实现代码_degreediscount算法

degreediscount算法

一、简介

          degreediscount算法即度折扣算法,是一个基于节点度的启发式算法

         度折扣算法的基本思想是:假设节点j是节点i的邻居,如果j已被选为种子节点,那么在基于度中心性指标考虑节点i是否作为种子节点时,应该对连边(i,j)打折扣,因为i对j不能产生额外的影响。假设所有边的激活概率都相同,均为β。当节点i的邻居中有si个激活种子时,被激活的概率为1-(1-β)的si的次方,此时i节点能被邻居节点激活,其期望影响力与直接将i节点选为种子节点的期望影响力相同,即此时选择节点i作为种子节点不增加额外的影响力 (对期望影响力的贡献为 0)。由于节点 i没有被激活的概率为(1-β)的si的次方,当节点i被选为种子时,其能激活的节点数为1+(di+si)β,其中“1” 表示节点 被激活,“ (di+si)β”表示被激活的邻居数。因此考虑节点i选为种子时,其产生的期望影响力为:

         当节点i邻居中没有种子节点时,i作为种子节点产生的期望影响力为:。设γ是对邻居中每个种子节点的度折扣,则 ,可以得到,因此,当节点i有si个种子邻居时,它的度折扣值定义为:

         度折扣算法的基本步骤为:第一轮中没有种子节点,所有节点的度都没有被折扣,所以直接选择网络中度最大的节点作为第一个种子;接下来每一轮根据上式计算每个未被激活节点的度折扣值,并选择最大的一个节点加入种子集;循环更新计算直到选出k个种子节点加入种子集 。

二、代码

  1. def degreeDiscountIC(G, k, p=.01):
  2. ''' Finds initial set of nodes to propagate in Independent Cascade model (without priority queue)
  3. Input: G -- networkx graph object
  4. k -- number of nodes needed
  5. p -- propagation probability
  6. Output:
  7. S -- chosen k nodes
  8. Note: the routine runs twice slower than using PQ. Implemented to verify results
  9. '''
  10. d = dict()
  11. dd = dict() # degree discount
  12. t = dict() # number of selected neighbors
  13. S = [] # selected set of nodes
  14. for u in G:
  15. d[u] = sum([G[u][v]['weight'] for v in G[u]]) # each edge adds degree 1
  16. # d[u] = len(G[u]) # each neighbor adds degree 1
  17. dd[u] = d[u]
  18. t[u] = 0
  19. for i in range(k):
  20. # dd saves tuples, max function of a tuple compares the first value in the tuple, if it the same then compare the second,
  21. # we want to compare only the second, so x being a tuple with x[1] we select the second value of the tuple
  22. u, ddv = max(dd.items(), key=lambda x: x[1])
  23. # u, ddv = max(dd.items(), key=lambda (k,v): v)
  24. dd.pop(u)
  25. S.append(u)
  26. for v in G[u]:
  27. if v not in S:
  28. t[v] += G[u][v]['weight'] # increase number of selected neighbors
  29. dd[v] = d[v] - 2 * t[v] - (d[v] - t[v]) * t[v] * p
  30. return S
  31. '''
  32. if __name__ == '__main__':
  33. import time
  34. import networkx as nx
  35. start = time.time()
  36. from YHSF import ICModel
  37. S=[]
  38. address = 'E:/新建文件夹/DPSO_demo-master/twitter.txt'
  39. def read_raw_data(raw_file_dir):
  40. g = nx.MultiDiGraph()
  41. for line in open(raw_file_dir):
  42. str_list = line.split()
  43. n1 = int(str_list[0])
  44. n2 = int(str_list[1])
  45. weight = float(1)
  46. # try:
  47. # weight = float(str_list[2])
  48. # except:
  49. # weight = float(1)
  50. g.add_weighted_edges_from([(n1, n2, weight)]) # G.add_edges_from([(n1, n2)])
  51. # g.add_edges_from([(n1, n2)])
  52. # g.add_edges_from([(n1, n2, {'weight': weight})])
  53. # g.add_edges_from([(n1, n2, {'weight': weight, 'timestamp': timestamp})])
  54. return g
  55. def read_raw_data1(raw_file_dir):
  56. g = nx.MultiDiGraph()
  57. for line in open(raw_file_dir):
  58. str_list = line.split()
  59. n1 = int(str_list[0])
  60. n2 = int(str_list[1])
  61. weight = float(1)
  62. # try:
  63. # weight = float(str_list[2])
  64. # except:
  65. # weight = float(1)
  66. # g.add_weighted_edges_from([(n1, n2, weight)]) # G.add_edges_from([(n1, n2)])
  67. g.add_edges_from([(n1, n2)])
  68. # g.add_edges_from([(n1, n2, {'weight': weight})])
  69. # g.add_edges_from([(n1, n2, {'weight': weight, 'timestamp': timestamp})])
  70. return g
  71. def multidir2simpledir(multidir_graph):
  72. # 输出所有有向边,包括权重
  73. # print("-" * 10) # print(list(G.edges(data=True)))
  74. # for e in multidir_graph.edges.data('weight'):
  75. # print(e)
  76. print("raw:", multidir_graph.number_of_nodes(), multidir_graph.number_of_edges(),
  77. nx.number_of_selfloops(multidir_graph))
  78. c = Counter(multidir_graph.edges())
  79. simpledir_graph = nx.DiGraph()
  80. for n1, n2, w in multidir_graph.edges.data('weight'):
  81. # avoid repeating edges and self-loops
  82. if not simpledir_graph.has_edge(n1, n2) and n1 != n2:
  83. simpledir_graph.add_edge(n1, n2, weight=c[n1, n2])
  84. if n1 == n2: # 没有loop的节点属性为None,有loop为loop个数
  85. if not simpledir_graph.has_node(n1): # 新节点要先添加
  86. simpledir_graph.add_node(n1, loops=c[n1, n2])
  87. else: # 已有的节点,直接设置属性
  88. simpledir_graph.nodes[n1]["loops"] = c[n1, n2] # 报错原因是n1节点尚未添加到simpledir_graph
  89. print("processed:", simpledir_graph.number_of_nodes(), simpledir_graph.number_of_edges(),
  90. nx.number_of_selfloops(simpledir_graph))
  91. return simpledir_graph
  92. # 根据有向单边图的节点loop数以及边频数,重新计算边影响力
  93. def edgeimpact(simpledir_graph):
  94. graph = nx.DiGraph()
  95. N1insum = dict(simpledir_graph.in_degree(weight='weight'))
  96. for v, u, w in simpledir_graph.edges.data('weight'):
  97. impactv2u = float(w) / (N1insum[u] + 0) # simpledir_graph.nodes[u]["loops"]
  98. graph.add_edge(v, u, weight=impactv2u)
  99. flag = os.path.exists(address)
  100. if not flag: file = open(address, 'a')
  101. # print("^" * 10) # 输出归一化边权重
  102. for e in graph.edges.data('weight'):
  103. if not flag: s = str(e[0]) + " " + str(e[1]) + " " + str(e[2]) + '\n'
  104. if not flag: file.write(s)
  105. # print(e)
  106. if not flag: file.close()
  107. print("normalized:", graph.number_of_nodes(), graph.number_of_edges(), nx.number_of_selfloops(graph))
  108. # print(graph.get_edge_data(6,1),graph.get_edge_data(1,2))#graph.edges[1,2]['weight'],
  109. # print(graph.degree[1],graph.degree)
  110. # print(graph[4])#,graph.neighbors(4)
  111. return graph
  112. simpledir_graph = multidir2simpledir(read_raw_data(address))
  113. # 归一化边权重
  114. graph = edgeimpact(simpledir_graph)
  115. print(time.time() - start)
  116. #for k in range(5, 55, 5):
  117. print(S)
  118. Q=[]
  119. W=[]
  120. for k in range(5, 55, 5):
  121. start_time = time.time()
  122. S = degreeDiscountIC(graph, k, p=.01)
  123. #print('A:%.2f MB' % (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024))
  124. activenodes = LTModel.simulate(graph, S, 0.25)
  125. Q.append(activenodes)
  126. end_time = time.time()
  127. runningtime1 = end_time - start_time
  128. W.append(runningtime1)
  129. print(Q)
  130. print("总时间:", W)

 算法缺点:虽然度折扣算法具有快速、高效的特性,但该算法还有许多需要改进的地方,这些不足之处是制约该算法性能进一步提升的关键因素。首先,度折扣算法在计算节点的期望影响力时没有考虑邻居的差异性,而是简单地认为每个未激活的邻居节点对该节点期望影响力的贡献是相同的,导致计算期望影响力的公式不够精确。其次,度折扣算法没有考虑节点之间共同邻居数的影响,不能充分降低传播的冗余性,比如节点 和 之间有许多共同邻居,如果节点已被选择为种子节点,则节点被感染的可能性也很高,因为他们之间有多条可能的传播路径,此时再选择节点作为种子节点会导致传播的冗余效应。

算法介绍和内容引用:[1]夏欣, 马闯, 张海峰. 基于改进的度折扣方法研究社交网络影响力最大化问题[J]. 电子科技大学学报, 2021, 50(3):9.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/773896
推荐阅读
相关标签
  

闽ICP备14008679号