Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
@R.M the EdgeList doc page says "Edges in EdgeList are given in the same order they are entered for Graph" – Sjoerd C. de Vries Apr 13 '12 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]" – belisarius has settled Apr 14 '12 at 14:24
@belisarius Ha! You can clearly tell I don't work much with graph objects ;) I'm removing my comment – R. M. Apr 14 '12 at 15:08
@R.M Neither do I. But I was bitten a few times by these doubts :) – belisarius has settled Apr 14 '12 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. – Ralph Dratman Jul 15 '12 at 2:20

3 Answers 3

up vote 3 down vote accepted

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}]

enter image description here

share|improve this answer
g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4}];
AbsoluteOptions[g, EdgeWeight] /. HoldPattern[EdgeWeight -> x_] -> x
share|improve this answer
+1 How does HoldPattern work here? Any reason for not using AbsoluteOptions[g, EdgeWeight][[1, 2]] or /. (EdgeWeight -> x_) :> x ? – David Carraher Apr 13 '12 at 23:46
@David Nahhh just an old vice – belisarius has settled Apr 14 '12 at 4:50

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

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

both give:

enter image description here

Similarly, use

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

to get a list:

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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