[英]NetworkX: Adjacency Matrix of partially failed graphs/networks
Recalling that the Adjacency Matrix gives us 1
if two nodes are connected and 0
otherwise, I wanted to compute one matrix for a regular graph with all its nodes active
, and one for the same graph where a few nodes have failed
. 回想一下,如果连接了两个节点,邻接矩阵给我们
1
否则给我们0
,我想为所有节点都active
的正则图计算一个矩阵,为其中几个节点发生failed
的同一图计算一个矩阵。
Let's consider a Lattice Network of 2x2
nodes. 让我们考虑一个
2x2
节点的莱迪思网络。 Its Adjacency Matrix (A) is: 其邻接矩阵(A)为:
0 1 1 0
1 0 0 1
1 0 0 1
0 1 1 0
resulting in this graph: 产生此图:
Now, let's remove node 0
: 现在,让我们删除节点
0
:
G.remove_node(0)
This is what the new Adjacency Matrix (A1) looks like: 这就是新的邻接矩阵(A1)的样子:
0 0 1
0 0 1
1 1 0
Now, the two Matrices are noticeably different in terms of size. 现在,两个矩阵的大小明显不同。
My question : how can I make sure that Matrix A1 has the same size of Matrix A ? 我的问题 :如何确定矩阵A1的大小与矩阵A相同? Which is to say, if node
0
is not going to be present because it has failed, I want a 0
to be placed in A1 in correspondence of the 0-th
row and column , so that the size of the matrix remains unaltered. 这就是说,如果节点
0
因为失败而不会出现,我希望将0
对应于第0-th
行和0-th
列放入A1 中 ,以便矩阵的大小保持不变。 I need to do this for purposes of comparison and computation. 为了比较和计算,我需要这样做。 But to do this I assume I need to access the function creating the adjacency matrix.
但是要做到这一点,我假设我需要访问创建邻接矩阵的函数。 Can I do it in a simpler way?
我可以用更简单的方式做到吗?
Example with node 0
failed: 节点
0
失败的示例:
0 0 0 0
0 0 0 1
0 0 0 1
0 1 1 0
This is how I create the 2x2
network and generate the Adjacency Matrix: 这就是我创建
2x2
网络并生成邻接矩阵的方式:
import networkx as nx
N=2
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i,j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
inds=labels.keys()
vals=labels.values()
inds.sort()
vals.sort()
pos2=dict(zip(vals,inds))
nx.draw_networkx(G, pos=pos2, with_labels=True, node_size = 200)
A=nx.adjacency_matrix(G)
A.toarray()
#G.remove_node(i) to remove node i
Try G.remove_edges_from(G.edges(0))
which will remove all of the edges of 0
rather than the entire node. 尝试使用
G.remove_edges_from(G.edges(0))
,它将删除所有0
的边而不是整个节点。 Then generate the adjacency matrix. 然后生成邻接矩阵。
Based on a little research and the advice of Joel, I've come up with this method. 根据一些研究和Joel的建议,我提出了这种方法。 I want to post it here so that anyone who has the will can propose improvements.
我想在这里发布它,以便任何有意愿的人都可以提出改进建议。
For a regular 3x3
network, this is how we can obtain the adjacency matrix in a righteous way: 对于常规的
3x3
网络,这是我们以公允的方式获取邻接矩阵的方式:
#Create the graph (see question above)
A=nx.adjacency_matrix(G, nodelist=range(N*N))
A=A.todense()
This yields a N^2xN^2
matrix where each row/column corresponds to a specific node (using nodelist
allows to have the rows/columns to be printed out sorted 0
to K
where K
is the total number of nodes): 这将产生一个
N^2xN^2
矩阵,其中每个行/列对应一个特定节点(使用nodelist
允许打印出要打印的行/列,排序为0
到K
,其中K
是节点总数):
[[0 1 0 1 0 0 0 0 0]
[1 0 1 0 1 0 0 0 0]
[0 1 0 0 0 1 0 0 0]
[1 0 0 0 1 0 1 0 0]
[0 1 0 1 0 1 0 1 0]
[0 0 1 0 1 0 0 0 1]
[0 0 0 1 0 0 0 1 0]
[0 0 0 0 1 0 1 0 1]
[0 0 0 0 0 1 0 1 0]]
If node 0
had failed, we would have to replace its connections ( 1
) with absent connections ( 0
), while preserving the size of the adjacency matrix. 如果节点
0
发生故障,我们将不得不用不存在的连接( 0
)替换其连接( 1
),同时保留邻接矩阵的大小。 In this case, row 0
and column 0
would be filled with 0
. 在这种情况下,行
0
和列0
将填入0
。 My solution then goes as follows: 然后,我的解决方案如下:
P=K #Total number of nodes before failures
def nodes_connected(i, j):
try:
if i in G.neighbors(j):
return 1
except nx.NetworkXError:
return False
A1=numpy.zeros((P*P,P*P))
for i in range(0,P*P,1):
for j in range(0,P*P,1):
if i not in G.nodes():
A1[i][:]=0
A1[:][i]=0
elif i in G.nodes():
A1[i][j]=nodes_connected(i,j)
A1[j][i]=A1[i][j]
for i in range(0,P*P,1):
for j in range(0,P*P,1):
if math.isnan(A1[i][j]):
A1[i][j]=0
print(A1)
This yields the following: 这将产生以下结果:
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 1. 0. 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 1. 0. 1. 0. 0.]
[ 0. 1. 0. 1. 0. 1. 0. 1. 0.]
[ 0. 0. 1. 0. 1. 0. 0. 0. 1.]
[ 0. 0. 0. 1. 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 1. 0. 1. 0. 1.]
[ 0. 0. 0. 0. 0. 1. 0. 1. 0.]]
Matrix A1 now tells us that node 0
has no connections whatsoever. 现在,矩阵A1告诉我们节点
0
没有任何连接。 It also tells us, similar to matrix A, that node 1
is connected to nodes 2
and 4
. 它还告诉我们,类似于矩阵A,节点
1
连接到节点2
和4
。
If anyone has corrections to propose, please feel welcome to do so. 如果有人有更正的建议,欢迎您提出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.