I need to find the total weight of an WeightedAdjacencyGraph. I have a graph in that form

a = WeightedAdjacencyGraph[{...},{...},{...}....etc]

How can I find its total weight?


You could just do this: if a graph is undirected:

Total[UpperTriangularize[WeightedAdjacencyMatrix[g]], 2]

if a graph is directed:

Total[WeightedAdjacencyMatrix[g], 2]

or by PropertyValue

Total[PropertyValue[g, EdgeWeight]]
| improve this answer | |
  • $\begingroup$ Don't forget the self loops! $\endgroup$ – Szabolcs Oct 12 '13 at 19:23
  • $\begingroup$ @Szabolcs thanks, I missed that. $\endgroup$ – halmir Oct 13 '13 at 4:39

This should deal with undirected, directed and graphs with loops (non-infinite non-zero diagonal elements):

totalweight=Total@(PropertyValue[{g, #}, EdgeWeight] & /@ EdgeList[g])

where g is graph of interest

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.