Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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_Networkx - Fatal编程技术网

Python 如何连接两个具有中间边的子图?

Python 如何连接两个具有中间边的子图?,python,networkx,Python,Networkx,设G1、G2、G3为三个图。每个节点都有N[i]节点 让3x3矩阵A表示不相交图G1、G2、G3节点之间的接触概率 因此,A_ij表示位于Gi的节点与位于Gj的节点具有边的概率 Aii将是在Gi节点之间具有边缘的概率 我需要一些帮助来完成这项工作,并以这样的方式命名节点:在联合图中,我将能够看到哪些节点来自哪个Gi import networkx as nx import numpy as np A = np.array([[0.2, 0.4, 0.2], [0.4, 0.1, 0.5], [

G1、G2、G3
为三个图。每个节点都有
N[i]
节点

3x3
矩阵
A
表示不相交图
G1、G2、G3
节点之间的接触概率

因此,
A_ij
表示位于
Gi
的节点与位于
Gj的节点具有边的概率

Aii
将是在
Gi
节点之间具有边缘的概率

我需要一些帮助来完成这项工作,并以这样的方式命名节点:在联合图中,我将能够看到哪些节点来自哪个
Gi

import networkx as nx
import numpy as np

A = np.array([[0.2, 0.4, 0.2], [0.4, 0.1, 0.5], [0.2, 0.5, 0.3]])
N = [20,30,40]

def get_number_edges(A,N,n):
    return int(A[n-1,n-1]*N[n-1])

G1=nx.dense_gnm_random_graph(N[0],get_number_edges(A,N,1))
G2=nx.dense_gnm_random_graph(N[1],get_number_edges(A,N,2))
G3=nx.dense_gnm_random_graph(N[2],get_number_edges(A,N,3))

C=nx.disjoint_union(nx.disjoint_union(G1,G2),G3)
  • 我不知道如何将节点从
    Gi
    连接到
    Gj
    where
    I=j
  • 我不知道如何用一个标签来标注
    C
    处的节点,该标签指示了从哪个图形
    Gi
    产生的节点
    我将首先创建完整的图
    C
    ,然后根据需要创建子图

    import itertools
    import networkx as nx
    import numpy as np
    import matplotlib.pyplot as plt
    
    A = np.array([[0.2, 0.4, 0.2], [0.4, 0.1, 0.5], [0.2, 0.5, 0.3]])
    N = [20,30,40]
    
    # create a square probability matrix with dimensions (total nodes, total nodes)
    # and populate it with the values given in A
    indices = []
    ctr = 0
    for n in N:
        indices.append(list(range(ctr, ctr+n)))
        ctr += n
    
    # TODO: 
    # remove this horrible triple loop; 
    # have a cold and am not smart enough to do it properly now
    total_nodes = sum(N)
    probability = np.zeros((total_nodes, total_nodes), dtype=np.float)
    for ii, sources in enumerate(indices):
        for jj, targets in enumerate(indices):
            for source, target in itertools.product(sources, targets):
                probability[source, target] = A[ii, jj]
    plt.imshow(probability, cmap='gray'); plt.show()
    
    # convert probability matrix into an adjacency matrix
    adjacency = probability >= np.random.rand(*probability.shape)
    plt.imshow(adjacency, cmap='gray'); plt.show()
    
    # create networkx graph object
    C = nx.from_numpy_array(adjacency)
    
    # to get any subgraph, use the indices
    G1 = C.subgraph(indices[0])
    # etc
    

    有没有办法避免这个循环,或者使用networkx API来模拟这个循环?@0x90我不知道。