• 大小: 4KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-05-30
  • 语言: Python
  • 标签: SIR  Python  

资源简介

本资源针对一种边权重存在重尾分布复杂网络,改进原本的SIR模型对TopK重要节点进行性能评估。并将传播过程绘制成可视化图。本资源使用networkx工具包。

资源截图

代码片段和文件信息

import networkx as nx
import numpy as np
import os.path as osp
import matplotlib.pyplot as plt
import random 

def spread_model_smooth(DGtopkmax_iter_numinfect_rate=0.8remove_rate=0.2):
    CG = DG.to_directed()
    #max_weight = max([e[2][‘weight‘] for e in CG.edges(data=True)])
    N = CG.number_of_nodes()
    node_state={node:0 for node in CG}
    nx.set_node_attributes(CG node_state “state“)#为节点添加属性
    for n in topk:
        CG.node[n][‘state‘] = 1
    random.seed(150)
    for e in CG.edges():
        if(CG[e[0]][e[1]][‘weight‘]==1):
            CG[e[0]][e[1]][‘prob‘] = random.uniform(01)
        else:
            CG[e[0]][e[1]][‘prob‘] = 1
    
    all_infect_nodes = []#累计受感染节点
    all_infect_nodes.extend(topk)
    all_count_infect = [len(all_infect_nodes)]#记录每一次受感染的节点总数
    
    infected_digraph = nx.DiGraph()
    infected_digraph.add_nodes_from(topktime=0)
    
    all_remove_nodes = []#累计治愈节点
    all_count_remove = [0]#记录每一次免疫的总数
    
    count_iter_infect = [len(topk)]#记录每次新增的感染节点数量
    count_iter_remove = [0]#记录每次新增的免疫节点数量
    all_count_suscep = [N-len(topk)]#记录每次易感染节点数量
    for i in range(max_iter_num):
        new_infect = []
        new_remove = []
        #t1 = ‘%s time‘ % i + ‘ %s nodes‘ % len(all_infect_nodes)
        #print(t1) # 当前有多少个节点被感染

        for v in all_infect_nodes:
            
            if (i!=0 and random.uniform(01)                CG.remove_node(v)#该节点具有免疫能力,不再传播,应该从原始图中去除
                #all_infect_nodes.remove(v)#治愈之后,将不会出现该节点,节点失效
                #infected_di

评论

共有 条评论