2013-03-13 65 views
0

集群连通图的最佳方式是什么?连通图的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]] 

感谢

+2

看看http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.connected_components.html 但你能解释一下为什么ex2的结果是[[0,1,3],[2,4,5]]? – HYRY 2013-03-13 13:35:02

+0

列,0,1,3连接,2,4,5也连接 – zedouard 2013-03-13 13:39:56

回答

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()