节点的平均距离通过节点数的对数增加python

灯光

我想在随机图中显示节点的平均距离增加log N,其中N是节点数。这里的p是在一对节点之间具有边的概率。我试过的是:

import matplotlib.pyplot as plt
import networkx as nx
%matplotlib inline

def run_experiment(ns, p=0.45, iters=30):
    """
    p: probability of having an edge between a pair of nodes
    ns: sequence of `number of nodes(n)` to try
    iters: number of times to run for each `n`
    
    """
    G = {}
    shortestPath={}
    res = []
    for n in ns:
        print(n)
        for i in range(iters):
            G[i] = nx.gnp_random_graph(n, p)
            shortestPath[i] = nx.average_shortest_path_length(G[i], weight=None, method=None)
        means = array([shortestPath[k] for k in shortestPath]).mean()
        print(means)
        res.append(means)
    return np.array(res)

我尝试了一些n值:

ns = [1,10, 100, 1000]
res = run_experiment(ns)

然后我绘制它以显示对数曲线,但是我知道了:

dictionary = dict(zip(ns, res))
plt.plot(list(dictionary.keys()),list(dictionary.values()))
plt.yscale('log')
plt.xscale('log')

在此处输入图片说明

问题是什么?

死神触发

看来问题出在随机图的生成上(请参阅创建至少连接一个边的随机图)。通过建议的功能(请参见下面的代码)添加随机生成的图,结果是(该图实际上还有3000的附加ns-entry):

在此处输入图片说明

这似乎证实了您的结果。这里的代码:

from itertools import combinations, groupby
import random
from numpy import array
import matplotlib.pyplot as plt
import networkx as nx
%matplotlib inline

# from https://stackoverflow.com/questions/61958360/how-to-create-random-graph-where-each-node-has-at-least-1-edge-using-networkx
def gnp_random_connected_graph(n, p):
    """
    Generates a random undirected graph, similarly to an Erdős-Rényi 
    graph, but enforcing that the resulting graph is conneted
    """
    edges = combinations(range(n), 2)
    G = nx.Graph()
    G.add_nodes_from(range(n))
    if p <= 0:
        return G
    if p >= 1:
        return nx.complete_graph(n, create_using=G)
    for _, node_edges in groupby(edges, key=lambda x: x[0]):
        node_edges = list(node_edges)
        random_edge = random.choice(node_edges)
        G.add_edge(*random_edge)
        for e in node_edges:
            if random.random() < p:
                G.add_edge(*e)
    return G

def run_experiment(ns, p=0.45, iters=30):
    """
    p: probability of having an edge between a pair of nodes
    ns: sequence of `number of nodes(n)` to try
    iters: number of times to run for each `n`
    
    """
    G = {}
    shortestPath={}
    res = []
    for n in ns:
        print(n)
        for i in range(iters):
            #G[i] = nx.gnp_random_graph(n, p)
            G[i] = gnp_random_connected_graph(n, p)
            shortestPath[i] = nx.average_shortest_path_length(G[i], weight=None, method=None)
        means = array([shortestPath[k] for k in shortestPath]).mean()
        print(means)
        res.append(means)
    return np.array(res)

ns = [1,10, 100, 1000]
res = run_experiment(ns)

dictionary = dict(zip(ns, res))
plt.plot(list(dictionary.keys()),list(dictionary.values()))
plt.yscale('log')
plt.xscale('log')

请注意,我刚刚在您的代码中更改了一行,并引用了生成函数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过节点获取FTP上传进度

来自分类Dev

如何通过节点运行testcafe

来自分类Dev

通过节点 js 将响应从 python flask 转发到客户端

来自分类Dev

如何增加DiagrammeR中节点之间的距离

来自分类Dev

java - 如何通过节点在列表中的位置获取节点?(不是值,是整个节点)

来自分类Dev

BST不增加Java中的节点数

来自分类Dev

设置innerHTML会增加HTML节点数

来自分类Dev

通过节点中的原型访问变量的值

来自分类Dev

AWS Dynamodb无法通过节点js同步获取数据

来自分类Dev

如何使用令牌通过节点调用Google API并表达

来自分类Dev

VueJS通过节点插件返回循环数据值

来自分类Dev

通过节点服务器连接到MongoDB的警告

来自分类Dev

通过节点运行DOH测试-'未定义文档'

来自分类Dev

通过节点属性进行ArangoDB有效遍历

来自分类Dev

如何通过节点接收Redis过期事件?

来自分类Dev

如何通过节点ID获得真正的元素?反应性的

来自分类Dev

通过节点js调用jasper report rest api

来自分类Dev

XSLT 转换 - 在迭代期间通过节点集设置变量

来自分类Dev

是否可以通过节点运行source-cmd?

来自分类Dev

如何使此终端应用程序通过节点测试?

来自分类Dev

通过节点将数据从Excel导入到SQL

来自分类Dev

通过长度路径返回python文件中的节点数

来自分类Java

用sax跳过节点

来自分类Dev

libxml ++ TextReader; 跳过节点

来自分类Dev

如何通过节点js将子节点添加到现有json文件中?

来自分类Dev

R igraph:找到通过节点g的节点u和v之间最短路径的总数

来自分类Dev

群集内节点距离增加一倍

来自分类Dev

node.js-通过Apache的静态内容?通过节点找不到

来自分类Dev

Tensorflow:随着训练的进行,图中的节点数量不断增加

Related 相关文章

热门标签

归档