摘要:主要讲解一些基本的信息传播模型,以及IC模型和SI模型的python实现及可视化。
author:xiao黄
缓慢而坚定的生长
影响力模型
传染模型
先上代码,这里我就不解释了,代码里面的注释我觉得很详细了,不懂可以评论或者私信。
import random
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
max_iter_num = 10 # 模拟的次数
G = nx.karate_club_graph() # 空手道俱乐部
for edge in G.edges:
G.add_edge(edge[0], edge[1], weight=random.uniform(0,1)) # 可不可以作为权值
for node in G:
G.add_node(node, state = 0) # 用state标识状态 state=0 未激活,state=1 激活
seed = 33 # 选定33作为初始激活节点
G.node[seed]['state'] = 1 # 表示33被激活
activated_graph = nx.Graph() # 被激活的图
activated_graph.add_node(seed)
all_active_nodes = [] # 所有被激活的节点放在这里
all_active_nodes.append(seed)
start_influence_nodes = [] # 刚被激活的节点 即有影响力去影响别人的节点
start_influence_nodes.append(seed)
for i in range(max_iter_num):
new_active = list()
t1 = '%s time' % i + ' %s nodes' % len(all_active_nodes)
print(t1) # 当前有多少个节点激活
# 画图
plt.title(t1)
nx.draw(activated_graph, with_labels=True)
plt.show()
for v in start_influence_nodes:
for nbr in G.neighbors(v):
if G.node[nbr]['state'] == 0: # 如果这个邻居没被激活
edge_data = G.get_edge_data(v, nbr)
if random.uniform(0, 1) < edge_data['weight']:
G.node[nbr]['state'] = 1
new_active.append(nbr)
activated_graph.add_edge(v, nbr) # 画图 添加边
start_influence_nodes.clear() # 将原先的有个影响力的清空
start_influence_nodes.extend(new_active) # 将新被激活的节点添加到有影响力
all_active_nodes.extend(new_active) # 将新被激活的节点添加到激活的列表中
print('all_active_nodes',all_active_nodes) # 打印
节点图,这里我就放了9张,第十张不放了,你们运行的时候会看到的
先上代码,这里我就不解释了,代码里面的注释我觉得很详细了,不懂可以评论或者私信。
import random
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
max_iter_num = 5 # 模拟的次数
G = nx.karate_club_graph() # 空手道俱乐部
for edge in G.edges:
G.add_edge(edge[0], edge[1], weight=random.uniform(0,1)) # 可不可以作为权值 病毒的感染能力
for node in G:
G.add_node(node, state = 0) # 用state标识状态 state=0 未激活,state=1 激活
seed = 33 # 选定33作为传染源
G.node[seed]['state'] = 1 # 表示33是感染的
all_infect_nodes = [] # 所有被感染的节点放在这里
all_infect_nodes.append(seed)
infected_graph = nx.Graph() # 被激活的图
infected_graph.add_node(seed)
for i in range(max_iter_num):
new_infect = list() # 新被感染的
t1 = '%s time' % i + ' %s nodes' % len(all_infect_nodes)
print(t1) # 当前有多少个节点被感染
# 画图
plt.title(t1)
nx.draw(infected_graph, with_labels=True)
plt.show()
# 感染的机会不止一次
for v in all_infect_nodes:
for nbr in G.neighbors(v):
if G.node[nbr]['state'] == 0: # 如果这个邻居节点没被感染
edge_data = G.get_edge_data(v, nbr)
if random.uniform(0, 1) < edge_data['weight']:
G.node[nbr]['state'] = 1
new_infect.append(nbr)
infected_graph.add_edge(v, nbr) # 画图 添加边
all_infect_nodes.extend(new_infect) # 将新感染的添加到
print('all_active_nodes:', all_infect_nodes)
import random
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
max_iter_num = 10 # 模拟的次数
G = nx.karate_club_graph() # 空手道俱乐部
for edge in G.edges:
G.add_edge(edge[0], edge[1], weight=random.uniform(0,1)) # 可不可以作为权值
for node in G:
G.add_node(node, state = 0) # 用state标识状态 state=0 未激活,state=1 激活
seed = 33 # 选定33作为初始激活节点
G.node[seed]['state'] = 1 # 表示33被激活
# activated_graph = nx.Graph() # 被激活的图
# activated_graph.add_node(seed)
all_active_nodes = [] # 所有被激活的节点放在这里
all_active_nodes.append(seed)
start_influence_nodes = [] # 刚被激活的节点 即有影响力去影响别人的节点
start_influence_nodes.append(seed)
color_list = ['brown','orange','r','g','b','y','m','gray','black','c','pink','brown','orange','r','g','b','y','m','gray','black','c','pink']
res = [[seed]]
for i in range(max_iter_num):
new_active = list()
t1 = '%s time' % i + ' %s nodes' % len(all_active_nodes)
print(t1) # 当前有多少个节点激活
# 画图
# plt.title(t1)
# nx.draw(activated_graph, with_labels=True,node_color=color_list[i])
# plt.show()
for v in start_influence_nodes:
for nbr in G.neighbors(v):
if G.node[nbr]['state'] == 0: # 如果这个邻居没被激活
edge_data = G.get_edge_data(v, nbr)
if random.uniform(0, 1) < edge_data['weight']:
G.node[nbr]['state'] = 1
new_active.append(nbr)
# activated_graph.add_edge(v, nbr) # 画图 添加边
print('激活',new_active)
start_influence_nodes.clear() # 将原先的有个影响力的清空
start_influence_nodes.extend(new_active) # 将新被激活的节点添加到有影响力
all_active_nodes.extend(new_active) # 将新被激活的节点添加到激活的列表中
res.append(new_active)
print('all_active_nodes',all_active_nodes) # 打印
# print(res)
res = [c for c in res if c]
pos = nx.spring_layout(G) # 节点的布局为spring型
nx.draw(G,pos, with_labels=True, node_color='w', node_shape = '.')
color_list = ['brown','orange','r','g','b','y','m','gray','black','c','pink','brown','orange','r','g','b','y','m','gray','black','c','pink']
for i in range(len(res)):
nx.draw_networkx_nodes(G, pos, with_labels=True, node_color=color_list[i], nodelist=res[i])
plt.show()
import random
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
max_iter_num = 5 # 模拟的次数
G = nx.karate_club_graph() # 空手道俱乐部
for edge in G.edges:
G.add_edge(edge[0], edge[1], weight=random.uniform(0,1)) # 可不可以作为权值 病毒的感染能力
for node in G:
G.add_node(node, state = 0) # 用state标识状态 state=0 未感染,state=1 激活
seed = 33 # 选定33作为传染源
G.node[seed]['state'] = 1 # 表示33是感染的
all_infect_nodes = [] # 所有被感染的节点放在这里
all_infect_nodes.append(seed)
res = [[seed]]
# infected_graph = nx.Graph() # 被激活的图
# infected_graph.add_node(seed)
for i in range(max_iter_num):
new_infect = list() # 新被感染的
t1 = '%s time' % i + ' %s nodes' % len(all_infect_nodes)
print(t1) # 当前有多少个节点被感染
# 画图
# plt.title(t1)
# nx.draw(infected_graph, with_labels=True)
# plt.show()
# 感染的机会不止一次
for v in all_infect_nodes:
for nbr in G.neighbors(v):
if G.node[nbr]['state'] == 0: # 如果这个邻居节点没被感染
edge_data = G.get_edge_data(v, nbr)
if random.uniform(0, 1) < edge_data['weight']:
G.node[nbr]['state'] = 1
new_infect.append(nbr)
# infected_graph.add_edge(v, nbr) # 画图 添加边
res.append(new_infect)
all_infect_nodes.extend(new_infect) # 将新感染的添加到
print('all_active_nodes:', all_infect_nodes)
res = [c for c in res if c]
pos = nx.spring_layout(G) # 节点的布局为spring型
nx.draw(G,pos, with_labels=True, node_color='w', node_shape = '.')
color_list = ['brown','orange','r','g','b','y','m','gray','black','c','pink','brown','orange','r','g','b','y','m','gray','black','c','pink']
for i in range(len(res)):
nx.draw_networkx_nodes(G, pos, with_labels=True, node_color=color_list[i], nodelist=res[i])
plt.show()