Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图中有未连接的节点_Python_Graph_Networkx - Fatal编程技术网

Python 图中有未连接的节点

Python 图中有未连接的节点,python,graph,networkx,Python,Graph,Networkx,所以,我对python还是新手,但我编写这个函数是为了生成一个具有某些层次结构特征的图。但是,有时此函数会创建具有未连接节点的图。但这不应该发生,因为每个节点都应该与其“级别”下的其他节点完全连接。层次结构的特征遵循Seigel 2009的特征:“基础是由参数扩展率确定的层次结构,其中一个个体位于顶部,网络中的每个个体与他下方的若干个个体相连,其数量等于扩展率,持续到群体中不再剩下任何个体为止。因此,虽然在最后一个层次之前的每一个层次都包含相当于扩展率幂的个体数量,但如果总人口没有适当划分,最后

所以,我对python还是新手,但我编写这个函数是为了生成一个具有某些层次结构特征的图。但是,有时此函数会创建具有未连接节点的图。但这不应该发生,因为每个节点都应该与其“级别”下的其他节点完全连接。层次结构的特征遵循Seigel 2009的特征:“基础是由参数扩展率确定的层次结构,其中一个个体位于顶部,网络中的每个个体与他下方的若干个个体相连,其数量等于扩展率,持续到群体中不再剩下任何个体为止。因此,虽然在最后一个层次之前的每一个层次都包含相当于扩展率幂的个体数量,但如果总人口没有适当划分,最后一个层次的个体数量可能会少于这个数量。同一级别内的个人之间的每个潜在联系也有一个与被建立的级别连接相等的概率。”

def继承权图(n,e,l):
'''
n是图中的节点数,
e是扩展率。这是每个级别上的人数
如果n/e有一个余数,那么最后一级有那么多
里面的人。
l是水平连接。它是一个人与某人连接的概率
在他们所属的级别内。
'''
G=nx.Graph()
G.name=“继承权图(%s,%s,%s)”%(n,e,l)
r=(n-1)%e
s=(n-r-1)/e
h=s+1
#G=空图(n=0)
G.add_节点(0,level=int(0))
对于范围内的i:
列表=范围(1,(e+1))
A=nx.Graph()
#对于列表中的项目:
#使用属性level='i'创建e节点
A.从(列表,级别=int(i))添加节点
#以概率l在节点之间添加边
名称=A.节点()
对于名称中的名称:
B=非邻域(A,名称)
对于B中的u:
q=随机均匀(0,1)

如果q不能确定您在快速扫描时的错误,但为什么您有
q=random.uniform(0,1)如果q@Joel因为代码没有按预期工作,那么应该去的地方也是如此。不是代码审查。在代码审查时,代码应该按照作者所知的预期工作。这里不是这样。@Mast谢谢clarifying@Joel我真的希望总是能增加优势,这就是我想做的……我在cod还是新手因此,可能有更好的方法。只需
a.add_edge(u,name)
即可添加边。如果某些条件成立,则无需执行此操作。不确定快速扫描时是否会出错,但为什么会有
q=random.uniform(0,1)如果q@Joel因为代码没有按预期工作,那么应该去的地方也是如此。不是代码审查。在代码审查时,代码应该按照作者所知的预期工作。这里不是这样。@Mast谢谢clarifying@Joel我真的希望总是能增加优势,这就是我想做的……我在cod还是新手因此,可能有更好的方法。只需
a.add\u edge(u,name)
即可添加边。如果某些条件成立,则无需执行此操作。
def heirarchy_graph(n,e,l):
    '''
    n is number of nodes in graph,
    e is the Expansion Rate. This is the number of people on each level
       If n/e has a remainder then the last level has that many
       people in it.
    l is the Level connection. It is the probability that a person is connected to someone
      within the level they belong to.
    '''
    G = nx.Graph()
    G.name="heirarchy_graph(%s,%s,%s)"%(n,e,l)

    r = (n-1)%e
    s = (n-r-1)/e
    h = s + 1
    #G = empty_graph(n=0)

    G.add_node(0, level=int(0))

    for i in range(s):
        list = range(1,(e+1))
        A = nx.Graph()
        #for item in list:
            #create e nodes with attribute level='i'
        A.add_nodes_from(list,level=int(i))

        # add edges between nodes with probability l
        names = A.nodes()

        for name in names:
            B = non_neighbors(A,name)
            for u in B:
                q = random.uniform(0,1)
                if q <= l:
                    A.add_edge(u,name)
        #return A
        #print(A)
        G = nx.disjoint_union(G,A)


    if r != 0: 
        h = s+1
        list = range(1,(r+1))
        A = nx.Graph()
        #create e nodes with attribute level='i'
        A.add_nodes_from(list,level=int(h))
        # add edges between nodes with probability l
        names = A.nodes()
        for name in names:
            B = non_neighbors(A,name)
            for u in B:
                q = random.uniform(0,1)
                if q <= l:
                    A.add_edge(u,name)


        G = nx.disjoint_union(G,A)

       ## add edges between levels
    level = nx.get_node_attributes(G,'level')
    names = G.nodes()
    for name in names:
        levelname = level[name]
        B = non_neighbors(G,name)

        for u in B:
            levelneighbor = level[u]
            if levelname == (levelneighbor + 1):
                G.add_edge(u,name)

    return G