来自连接图的Numpy集群

时间:2013-03-13 13:15:49

标签: python graph numpy

群集连接图的最佳方法是什么?

ex1:

[[ 1 1 1 1 0 0]
 [ 1 1 1 1 0 0]
 [ 1 1 1 1 0 0]
 [ 1 1 1 1 0 0]
 [ 0 0 0 0 1 1]
 [ 0 0 0 0 1 1]]

结果:

==> [[0,1,2,3],[4,5]]

EX2

[[ 0 1 0 1 0 0]
 [ 1 1 0 1 0 0]
 [ 0 1 0 1 0 0]
 [ 1 0 0 0 0 0]
 [ 0 0 1 0 1 1]
 [ 0 0 0 0 1 1]]

结果:

==> [[0,1,3],[2,4,5]]

EX3

[[ 0 1 0 0 0 0]
 [ 1 1 0 0 0 0]
 [ 0 0 1 1 0 0]
 [ 0 0 0 1 0 0]
 [ 0 0 0 0 1 1]
 [ 0 0 0 0 1 1]]

结果:

==> [[0,1],[2,3],[4,5]]

感谢

1 个答案:

答案 0 :(得分:2)

在某些示例中,例如ex2,您已经给出了有向图或有向图A != A.T。在这种情况下,通过考虑strongly connected components可以找到更合理的定义。在这种情况下,拆分为[0,1,3],[4,5],[2]networkx可以帮助您找到这些:

import numpy as np
import networkx as nx

A = np.array([[0,1,0,1,0,0],
              [1,1,0,1,0,0],
              [0,1,0,1,0,0],
              [1,0,0,0,0,0],
              [0,0,1,0,1,1],
              [0,0,0,0,1,1]])

G = nx.from_numpy_matrix(A, create_using=nx.DiGraph()) 
for subg in nx.strongly_connected_component_subgraphs(G):
    print subg.nodes()