NetworkX:如何创建加权图的关联矩阵? [英] NetworkX: how to create an incidence matrix of a weighted graph?

查看:753
本文介绍了NetworkX:如何创建加权图的关联矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建了一个像这样的网格网络: $ _

  from __future__ import division 
import networkx as nx
from pylab import *
import matplotlib.pyplot as plt
%pylab inline

ncols = 10

N = 10#每边节点$ b ()()()中的i,j)(b(g,n,grid_2d_graph,N,N)
nx.relabel_nodes(G,labels,False)
inds = labels.keys()
vals = labels.values()
inds = [(Nj-1,Ni- 1)for i,j in inds]
pos2 = dict(zip(vals,inds))



  

每个边缘都有一个与其长度相对应的权重(在这个微不足道的例子中,所有长度都等于1) #权重
从数学导入sqrt

权重= dict()
在源代码中,目标在G.edges()中:
x1,y1 = pos2 [source]
x2,y2 = pos2 [target]
weights [(source,target)] = round((math.sqrt((x2-x1)** 2 +(y2-y1)** 2) ),3)

for e在G.edges()中:
G [e [0]] [e [1]] =权重s [e]#将权重分配给G.edges()

这就是我的 G.edges()看起来像:(startnode ID,endnode ID,weight)

  [(0,1,1.0),
(0,10,1.0),
(1,11,1.0),
(1,2,1.0),...]平凡的情况:所有权重都是单一的

如何创建考虑权重的关联矩阵刚刚被定义?我想使用 nx.incidence_matrix(G,nodelist = None,edgelist = None,oriented = False,weight = None),但是正确的值权重在这种情况下? docs 表示 weight 是表示用于提供矩阵中每个值的边缘数据关键字的字符串,但它是什么意思特别?我也没有找到相关的例子。



有什么想法吗?

解决方案

下面是一个简单的例子,展示了如何正确设置边缘属性以及如何生成加权关联矩阵。

  import networkx as nx 
从数学导入sqrt

G = nx.grid_2d_graph(3,3)
for s,t in G.edges():
x1,y1 = s
x2,y2 = t
G [s] [t] ['weight'] = sqrt((x2-x1)** 2 +(y2-y1)** 2)* 42

print(nx.incidence_matrix(G,weight ='weight')。todense())

OUTPUT

  [[42. 42. 42. 0.0.0.0。 0。0。0] b $ b [0。0。42。42。42。0。0。0。0。0。0] 
[42.0。0。 $ 0.42,0.0,0.0]
[0.0.0.0.0.0.42.42.42.0]
[0.42.4.2,0.0,0.42.4.22.4]
[0.0.0.0.0。 0.42 0. 0. 0. 42]
[0.0 0. 0. 0. 0. 0. 0. 42. 0. 0.]
[0。 0. 0. 0. 0. 42. 0. 0. 0. 42. 42.]
[0.0.42 0.42.0.0.0.0.0。 0.]]

如果您想要矩阵中的节点和边的特定排序,请使用nodelist =和edgelist = networkx.indicence_matrix()的可选参数。


Having created a grid network like this:

from __future__ import division
import networkx as nx
from pylab import *
import matplotlib.pyplot as plt
%pylab inline

ncols=10 

N=10 #Nodes per side
G=nx.grid_2d_graph(N,N)
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=[(N-j-1,N-i-1) for i,j in inds]
pos2=dict(zip(vals,inds))

And having assigned each edge a weight corresponding to its length (in this trivial case, all lenghts=1):

#Weights
from math import sqrt

weights = dict()
for source, target in G.edges():
    x1, y1 = pos2[source]
    x2, y2 = pos2[target]
    weights[(source, target)] = round((math.sqrt((x2-x1)**2 + (y2-y1)**2)),3)

for e in G.edges():
    G[e[0]][e[1]] = weights[e] #Assigning weights to G.edges()

This is what my G.edges() looks like: (startnode ID, endnode ID, weight)

[(0, 1, 1.0),
 (0, 10, 1.0),
 (1, 11, 1.0),
 (1, 2, 1.0),... ] #Trivial case: all weights are unitary

How can I create an incidence matrix that takes into account the weights that have just been defined? I want to use nx.incidence_matrix(G, nodelist=None, edgelist=None, oriented=False, weight=None), but what is the correct value for weight in this case?

The docs say that weight is a string representing "the edge data key used to provide each value in the matrix", but what does it mean specifically? I have also failed to find relevant examples.

Any ideas?

解决方案

Here is a simple example showing how to properly set edge attributes and how to generate a weighted incidence matrix.

import networkx as nx
from math import sqrt

G = nx.grid_2d_graph(3,3)
for s, t in G.edges():
    x1, y1 = s
    x2, y2 = t
    G[s][t]['weight']=sqrt((x2-x1)**2 + (y2-y1)**2)*42

print(nx.incidence_matrix(G,weight='weight').todense())

OUTPUT

[[ 42.  42.  42.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.  42.  42.  42.   0.   0.   0.   0.   0.   0.]
 [ 42.   0.   0.   0.   0.   0.  42.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.  42.  42.  42.   0.   0.]
 [  0.  42.   0.  42.   0.   0.   0.   0.  42.   0.  42.   0.]
 [  0.   0.   0.   0.   0.   0.   0.  42.   0.   0.   0.  42.]
 [  0.   0.   0.   0.   0.  42.   0.   0.   0.  42.   0.   0.]
 [  0.   0.   0.   0.   0.   0.  42.   0.   0.   0.  42.  42.]
 [  0.   0.  42.   0.  42.   0.   0.   0.   0.   0.   0.   0.]]

If you want a particular ordering of the nodes and edges in the matrix use the nodelist= and edgelist= optional parameters to networkx.indicence_matrix().

这篇关于NetworkX:如何创建加权图的关联矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆