user30
user30

Reputation: 57

calculation of degree from a weighted adjacency matrix in networkx

I have an adjacency matrix with the non zero elements indicating the weights of the link.The weights are decimals. I would like to obtain the node strength of each of the nodes i.e., the sum of the edge weights adjacent to the node and also the weight distribution. I tried the following code:

import networkx as nx
G=nx. Graph(a)  # a is the adjacency matrix.
w=G.degree()

But i get the degree of each node as the answer and not the sum of the weights of the links connected to the node. Any help in this regard will be highly appreciable. I am new to networkx.

Upvotes: 0

Views: 2542

Answers (1)

J...S
J...S

Reputation: 5207

If the vertex sum of whose edges' weights need be found is vertex and the NetworkX graph is G, you could do something like

s=0
for neighbor in G[vertex]:
    s+=G[vertex][neighbor]['weight']
print(s)

G[vertex] will give all the details of all the vertices connected to the vertex vertex and G[vertex][neighbor] gives the details about the edge between vertex and neighbor vertices from which the weight information is taken using G[vertex][neighbor]['weight'].

Upvotes: 1

Related Questions