简体   繁体   English

Networkx图形类型

[英]Networkx Graph Type

I want to find a way of getting a "cluster" only if each component in the cluster is connected with every other component (directionally or non-) 我想找到一种仅在群集中的每个组件都与其他每个组件(定向或非连接)连接时获取“群集”的方法

This means that for a non-directional graph to get a cluster of A,B,C,DI need to specify 6 connections: 这意味着,要使非方向图获得A,B,C,DI的簇,需要指定6个连接:

(A,C)(A,B)(A,D)(C,B)(C,D) (B,D) (A,C)(A,B)(A,D)(C,B)(C,D) (B,D)

在此处输入图片说明

However, if I specify 但是,如果我指定

(A,C)(A,B)(A,D)(C,B)(C,D) (A,C)(A,B)(A,D)(C,B)(C,D)

在此处输入图片说明

I want this to only give me a cluster of A,B,C and then D. Because A,B,C are all fully connected whereas D is not. 我希望这只给我A,B,C和D的群集。因为A,B,C都完全连接,而D没有。

I can't really get this to work with networkx; 我真的不能将它与networkx一起使用; my code is below: 我的代码如下:

from pylab import *
import networkx as nx        
data_edges = [
    ('A','C'), 
    ('A','B'),
    ('A','D'),
    ('C','B'),
    ('C','D')
]

# Add edges
G=nx.Graph()
G.add_edges_from(data_edges)

# Cluster    
connections_nx = nx.biconnected_components(G)
print("Bi-connected")
for con in connections_nx:
    print(con)

#Draw
pos=nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos,node_color='r')
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G,pos,font_size=16)
plt.axis('off')
show()
plt.clf()

Because this outputs: 因为这输出:

{'D', 'A', 'B', 'C'} {'D','A','B','C'}

Instead of: 代替:

{'A', 'B', 'C'} {'A', 'C', 'D'} {'A','B','C'} {'A','C','D'}

Ah, silly me. 啊,愚蠢的我。 The term I was looking for were 'cliques' in networkx: 我要寻找的术语是networkx中的“ cliques”:

from pylab import *
import networkx as nx        
data_edges = [
    ('A','C'),
    ('A','B'),
    ('A','D'),
    ('A','E'),
    ('B','C'),
    ('B','E'),
    ('D','E'),
    ('C','F'),
    ('C','G'),
    ('F','G'),
    ('Y','X')

]

# Add edges
G=nx.Graph()
G.add_edges_from(data_edges)

cliq = nx.find_cliques(G)
in_cliq = set()
for c in cliq:
    for x in c:
        in_cliq.add(x)

print(in_cliq)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM