diffusion code

ic with networkx

def ic_with_networkx(G_networkx, S, target_nodes, p, mc):
    """
    Input:  graph object, set of seed nodes, propagation probability
            and the number of Monte-Carlo simulations
    Output: average number of nodes influenced by the seed nodes
    """
    # Loop over the Monte-Carlo Simulations
    spread = []
    for _ in range(mc):
        # Simulate propagation process
        new_active, A = list(S), list(S)
        while new_active:
            # For each newly active node, find its neighbors that become activated
            new_ones = []
            for node in new_active:
                # Determine neighbors that become infected
                for u in G_networkx.neighbors(node):
                    if random.random() < p:
                        new_ones.append(u)
            new_active = list(set(new_ones) - set(A))
            # Add newly activated nodes to the set of activated nodes
            A += new_active
        spread.append(len(A))
    return(np.mean(spread))

ic with target_nodes and networkx

def ic_with_networkx_target_nodes(G_networkx, S, target_nodes, p, mc):
    """
    Input:  graph object, set of seed nodes, propagation probability
            and the number of Monte-Carlo simulations
    Output: average number of nodes influenced by the seed nodes
    """
    # Loop over the Monte-Carlo Simulations
    spread = []
    for _ in range(mc):
        # Simulate propagation process
        new_active, A = list(S), list(S)
        while new_active:
            # For each newly active node, find its neighbors that become activated
            new_ones = []
            for node in new_active:
                # Determine neighbors that become infected
                for u in G_networkx.neighbors(node):
                    if random.random() < p:
                        new_ones.append(u)
            new_active = list(set(new_ones) - set(A))
            # Add newly activated nodes to the set of activated nodes
            A += new_active
        spread.append(len(list(set(A).intersection(target_nodes))))  
    return(np.mean(spread))
posted @ 2024-01-07 23:23  X1OO  阅读(4)  评论(0)    收藏  举报