我试图模拟一个加权的、定向的、无标度的网络,如本文第3节所述。由于没有LaTeX排版,我尝试在下图中总结我对该过程的理解:

My understanding of the procedure

请记住,这是我对所述程序的解释,因此我理解中的错误可能是错误的根源。到目前为止,我尝试的解决方案并没有导致幂律度分布。我的代码如下:

import numpy as np
import networkx as nx

N = 1000   # Final no. of nodes
d = 80     # Parameter for edge-weight update (called delta in paper)
m0 = 10    # Initial nodes
w0 = 100   # Initial weights

m1_in  = 1 # Number of new edges pointing TOWARDS the new node
m1_out = 1 # Number of new edges pointing AWAY from the new node
m2 = 0     # Number of new edges between previously existing nodes

# Create fully connected networks
G = nx.complete_graph(n=m0, create_using=nx.DiGraph)

# Set initial weights equal to w0
for e in G.edges():
    G[e[0]][e[1]]['Transaction'] = w0

# At each timestep t
t = m0 #0

while t <= N:
    
    # Calculate in/out strength of existing nodes
    s_in  = dict(G.in_degree(weight='Transaction'))
    s_out = dict(G.out_degree(weight='Transaction'))
    
    # Calculate totals
    s_in_sum  = sum(s_in.values(), 0.0)
    s_out_sum = sum(s_out.values(), 0.0)
    
    # Probability of linking joining new edges to existing nodes
    p_in  = {k: v / s_in_sum for k, v in s_in.items()}
    p_out = {k: v / s_out_sum for k, v in s_out.items()}
    
    # ----------------------------------------------------------
    # Prob. of existing node RECEIVING new edge is given by in_limit
    # In-limit
    in_list  = [i for i in p_in.values()]     # Stores in-probabilities as a list
    in_limit = [0.0]                          # Initializes value at 0
    for i, p_i in enumerate(in_list):
        in_limit.append(p_i + in_limit[i])
    
    # Select m1_out existing nodes that will RECEIVE new edges
    in_neigh = select_neigh(lim_list=in_limit, num_neigh=m1_out)
    
    # CALL WEIGHT UPDATE
    # Before adding new neighbour, update strength of existing neighbours
    # After updating, add link (weight w0) to new node. New node is SOURCE of edge
    update_weights(G=G, neighbours=in_neigh, delta=d, s_dict=s_in, case_1a=False, case_1b=True)
    
    # ----------------------------------------------------------
    # Prob. of existing node being the SOURCE of a new edge is given by out_limit
    # Out-limit
    out_list  = [i for i in p_out.values()]   # Stores out-probabilities as a list
    out_limit = [0.0]                         # Initializes value at 0
    for i, p_o in enumerate(out_list):
        out_limit.append(p_o + out_limit[i])
    
    # CALL SELECT OUT-NEIGHBOUR
    # Select m1_in existing nodes that will be SOURCES of new edges
    out_neigh = select_neigh(lim_list=out_limit, num_neigh=m1_in)
    
    # CALL WEIGHT UPDATE
    # Before adding new neighbour, update strength of existing neighbours
    # After updating, add link (weight w0) to new node. New node is DESTINATION of edge
    update_weights(G=G, neighbours=out_neigh, delta=d, s_dict=s_out, case_1a=True, case_1b=False)
    
    # -----------------------------------------------------------
    # Add new node - new node is DESTINATION of new edge
    G.add_node(t)
    # Add paths to selected nodes
    for n in in_neigh:
        G.add_edge(n, t, Transaction=w0)
    
    # Add new node - new node is SOURCE of new edge
    for n in out_neigh:
        G.add_edge(t, n, Transaction=w0)
    
    # Case 2 - New edges appear between previously existing nodes
    # To do
    
    # Next step
    t += 1

我定义的功能如下:

# Select neighbours
def select_neigh(lim_list, num_neigh):
    '''
    Args
    ------------------------------------
    lim_list: list - Probability range for each node
    num_neigh: int - Number of incoming nodes to assign to the new node.
                     m1_out in the paper for INCOMING edges to a new node
                     m1_in in the paper for OUTGOING edges to a new node
    ------------------------------------
    Returns: list of nodes selected for the new edge
    '''
    
    # List to store the neighbours
    neighbours = []
    
    # Flag to see if node is already a neighbour
    already_neighbour = False
    
    # Iterate through EXISTING NODES
    i = 0
    while i < num_neigh:
        rnd = np.random.random() # random number between 0 and 1
        # compare the random number to the limits and add node accordingly
        for j in range(len(lim_list) - 1):
            if rnd >= lim_list[j] and rnd < lim_list[j+1]:
                # if j is already a neighbor
                if j in neighbours:
                    # Raise the flag
                    already_neighbour =True
                else:
                    # if j is not already a neighbor add it to the neighbors list
                    neighbours.append(j)    
        # if the alread_neighbor flag is true, decrement i to redo the choice randomly and reset flag               
        if already_neighbour == True:
            already_neighbour = False
            i -= 1 # To repeat the choice of the node
        i+=1
    
    # Return result
    return neighbours

# Update weights
def update_weights(G, neighbours, delta, s_dict, case_1a = False, case_1b=False):
    '''
    Args
    ------------------------------------
    G: nx.DiGraph object - NetworkX network before being updated
    neighbours: list - list of neighbours to generate links into/from
    delta: float - delta parameter in the paper. Updates weights
    s_dict: Strength dictionary of every node.
            In/Out depends on incoming/outgoing edges
    case_1a: bool - Flag that determines if new edges are INCOMING towards existing nodes
    case_1b: bool - Flag that determines if new edges are OUTGOING from existing nodes
    '''
    if case_1b:
        # Update weights of existing edges
        for n in neighbours:
            for e in G.in_edges(n, data=True):
                w_in = G[e[0]][n]['Transaction'] # Value of edges incoming to n
                d_w = (delta*w_in)/s_dict[e[0]]  # Value of shift
                # Update weight
                G[e[0]][n]['Transaction'] += d_w
    
    if case_1a:
        # Update weights of existing edges
        for n in neighbours:
            for e in G.out_edges(n, data=True):
                w_nj = G[n][e[1]]['Transaction'] # Value of edges incoming to n
                d_w = (delta*w_nj)/s_dict[e[1]]  # Value of shift
                # Update weight
                G[n][e[1]]['Transaction'] += d_w

模拟加权、定向、无标度网络的更多相关文章

  1. python NetworkX库生成并绘制带权无向图

    这篇文章主要为大家介绍了python NetworkX库生成并绘制带权无向图的实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  2. Python利用networkx画图绘制Les Misérables人物关系

    这篇文章主要为大家介绍了Python利用networkx画图处理绘制Les Misérables悲惨世界里的人物关系图,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  3. networkx库绘制带权图给无权图加权重输出

    这篇文章主要为大家介绍了Python networkx库绘制带权图给无权图加权重并输出权重的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. 模拟加权、定向、无标度网络

    我试图模拟一个加权的、定向的、无标度的网络,如本文第3节所述。由于没有LaTeX排版,我尝试在下图中总结我对该过程的理解:请记住,这是我对所述程序的解释,因此我理解中的错误可能是错误的根源。到目前为止,我尝试的解决方案并没有导致幂律度分布。

  5. centos安装python-pip和networkx包

    2.利用pip安装networkx安装networkx前需要先安装矩阵处理包numpy和图形绘制包matplotlib.安装命令如下:安装成功!

  6. c – Python中的最大重量/最小成本二部分匹配代码

    我在二分图中搜索最大重量/最小成本匹配的Python代码.我一直在使用NetworkX中的一般情况下最大重量匹配代码,但是我发现我的需求太慢了.这可能是由于通用算法较慢的事实,以及NetworkX解决方案完全在Python中实现的事实.理想情况下,我想找到一些Python代码,用于包含一些C/C++代码的二分法匹配问题,但现在,比NetworkX实现更快的事情将会有所帮助.解决方法经过进一步的调查

  7. python – 使用networkx的节点标签

    我正在由curveSeq持有的给定的Y值序列创建一个图形.(X值自动枚举:0,1,2…

  8. python – Pygraphviz / networkx设置节点级别或层

    我有一个代表一种家谱树的数据集.每个节点有2个父母(第一代除外,他们没有父母).对于给定节点,其父节点可以来自任何前一代节点.例如,生成n中的节点可以具有n-1中的父节点和n-5中的另一个父节点.节点可以是其他几个节点的父节点.基本上,对于每个节点,我都知道它的生成及其父节点.我试图表示这个图表保持同一行中的同一代的节点.每一代都有10个节点,第一代除外.到目前为止,我正在尝试“点”布局.当我只输

随机推荐

  1. 如何扩展ATmega324PB微控制器的以下宏寄存器?

    我目前正在学习嵌入式,我有以下练习:展开以下宏寄存器:如果有人解决了这个问题,我将不胜感激,以便将来参考

  2. Python将ONNX运行时设置为返回张量而不是numpy数组

    在python中,我正在加载预定义的模型:然后我加载一些数据并运行它:到目前为止,它仍在正常工作,但我希望它默认返回Tensor列表,而不是numpy数组。我对ONNX和PyTorch都是新手,我觉得这是我在这里缺少的基本内容。这将使转换中的一些开销相同。

  3. 在macOS上的终端中使用Shell查找文件中的单词

    我有一个文本文件,其中有一行:我需要找到ID并将其提取到变量中。我想出了一个RexEx模式:但它似乎对我尝试过的任何东西都不起作用:grep、sed——不管怎样。我的一个尝试是:我为这样一个看似愚蠢的问题感到抱歉,但我在互联网上找不到任何东西:我在SO和SE上读了几十个类似的问题,并在谷歌上搜索了几个教程,但仍然无法找到答案。欢迎提供任何指导!

  4. react-chartjs-2甜甜圈图中只有标题未更新

    我正在使用react-chartjs-2在我的网站中实现甜甜圈图。下面是我用来呈现图表的代码。我将甜甜圈图的详细信息从父组件传递到子组件,所有道具都正确传递。当我在beforeDraw函数外部记录props.title时,它会记录正确的值,但当我在beforeDraw函数内部记录props.title时,它将记录标题的前一个值,从而呈现标题的前值。我在这里做错了什么?

  5. 如何在tkinter中使用Python生成器函数?

    生成器函数承诺使某些代码更易于编写。但我并不总是知道如何使用它们。假设我有一个斐波那契生成器函数fib(),我想要一个显示第一个结果的tkinter应用程序。当我点击“下一步”按钮时,它会显示第二个数字,依此类推。我如何构建应用程序来实现这一点?我可能需要在线程中运行生成器。但如何将其连接回GUI?

  6. 如何为每次提交将存储库历史记录拆分为一行?

    我正在尝试获取存储库的历史记录,但结果仅以单行文本的形式返回给我。

  7. 尝试在颤振项目上初始化Firebase时出错

    当尝试在我的颤振项目上初始化firebase时,我收到了这个错误有人知道我能做什么吗?应用程序分级Gradle插件Gradle项目颤振相关性我已经将firebase设置为Google文档已经在另一个模拟器上尝试过,已经尝试过创建一个全新的模拟器,已经在不同的设备上尝试过了,已经尝试了特定版本的firebase,已经尝试添加但没有任何效果,已经在youtube上看到了关于它的每一个视频,该应用程序在android和iOS两个平台上都抛出了这个错误

  8. 在unix中基于当前日期添加新列

    我试图在unix中基于时间戳列在最后一个单元格中添加一个状态列。我不确定如何继续。

  9. 麦克斯·蒙特利。我一直得到UncaughtReferenceError:当我在终端中写入node-v时,节点未定义

    如果这是您应该知道的,请确认:我已将所有shell更改为默认为zsh。当我在终端中写入node-v时,我一直收到“UncaughtReferenceError:nodeisnotdefined”。但它显示节点已安装。我是个新手,在这方面经验不足。

  10. 如何在前端单击按钮时调用后端中的函数?

    那么如何在后端添加一个新的端点,点击按钮调用这个函数。

返回
顶部