# How to extract edgeweights from a graph

I have a graph with weighted edges, and I'd like to obtain the edge weights of the edges in the graph. Is there a nicer way of doing this than getting the WeightedAdjacencyMatrix of the graph? A list of weights that's 1-1 with the EdgeList of the graph would be great.

• @R.M the EdgeList doc page says "Edges in EdgeList are given in the same order they are entered for Graph" Apr 13, 2012 at 22:27
• @R.M Exactly the same problem exists with WeightedAdjacencyMatrix[]. How do you know which vertex is in which rol/col? Well, you know because the help states that "The vertices Subscript[v, i] are assumed to be in the order given by VertexList[g]" Apr 14, 2012 at 14:24
• @belisarius Ha! You can clearly tell I don't work much with graph objects ;) I'm removing my comment
– rm -rf
Apr 14, 2012 at 15:08
• @R.M Neither do I. But I was bitten a few times by these doubts :) Apr 14, 2012 at 15:26
• I have spent far too much time trying to understand the role played by a vertex's list position as opposed to the vertex's "name." If one's vertex "names" are consecutive integers, everything works pretty much as expected. The difficulty starts for me when I use MorphologicalGraph, which returns a graph having non-consecutive integer "names" assigned to the vertices. In fact, for MorphologicalGraph, the vertex names constitute a permutation of Range[n]. Hey, what's so confusing about that? Cheer up -- there are only (n! - 1) ways you can be wrong. Hint: use VertexIndex to get back to reality. Jul 15, 2012 at 2:20

You can still use WeightedAdjacencyMatrix and massage the output to a nicer form —

g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4}];
{x_, y_} :> UndirectedEdge[x, y], {2}]


Using PropertyValue[g,property] and Thread or Map:

    g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4}];
Map[# -> PropertyValue[{g, #}, EdgeWeight] &, EdgeList[g]]


both give:

Similarly, use

   Thread[{EdgeList[g], PropertyValue[g, EdgeWeight]}]
Map[{#, PropertyValue[{g, #}, EdgeWeight]} &, EdgeList[g]]


to get a list:

• The best solution. Using the weighted adjacency matrix does not allow to easily associative edges with weights if graph vertexes are not integers. Nov 15, 2022 at 17:04
g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4}];
AbsoluteOptions[g, EdgeWeight] /. HoldPattern[EdgeWeight -> x_] -> x

• +1 How does HoldPattern work here? Any reason for not using AbsoluteOptions[g, EdgeWeight][[1, 2]] or /. (EdgeWeight -> x_) :> x ? Apr 13, 2012 at 23:46
• @David Nahhh just an old vice Apr 14, 2012 at 4:50