-4

I need to sum the total weights of a neighbors node. For example, I have a node (1) where its neighbors are (2) and (4). Its edges has a weight: (1-2 , weight = 3) and (1-4, weight = 5). Then, I need the result of sum weights (8).

I tried:

sum_tot = []
for neigh in G.neighbors(1):
    sum_tot.append(G[1][neigh]['weight'])
sum(sum_tot)

But results 0 Any ideas for python programming?

1 Answers1

1

I am very new to Python, but it seems that your problem should be represented by a nxn matrix for n nodes. Let each entry in the matrix represent the weight between each node in the column to each node in the row. So you would represent the distance between nodes 1 to 2 with a 3 in that location. Now, Python does not have arrays, but you could do this with lists.

After initializing this list to 0, you could assign something like this:

network[1][2]=3
network[2][1]=3
network[1][4]=5
network[4][1]=5

write your row and column operations to add the weights by traversing the matrix, starting at the node row, then switch node row with the column, and trying again. If you can NOT find a connection other than where you were on the previous iteration, then you are at an end, and will need to start at a new node and repeat the process. I would do an iteration on each row, and print out a table of nodes and weight sums. Hope that this gives you some ideas.

Hope I answered your question. I am a mathematician, but an amateur with Python.

JWoods4
  • 13
  • 3
  • Python library NetworkX is a standard tool for working with graphs, there is no need to reinvent it by dealing with adjacency matrix directly. –  Apr 30 '18 at 02:28
  • @bro - Thanks for that, I will keep that in mind in future answers. – JWoods4 Apr 30 '18 at 03:27