10
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ @R.M the EdgeList doc page says "Edges in EdgeList are given in the same order they are entered for Graph" $\endgroup$ Apr 13, 2012 at 22:27
  • $\begingroup$ @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]" $\endgroup$ Apr 14, 2012 at 14:24
  • $\begingroup$ @belisarius Ha! You can clearly tell I don't work much with graph objects ;) I'm removing my comment $\endgroup$
    – rm -rf
    Apr 14, 2012 at 15:08
  • $\begingroup$ @R.M Neither do I. But I was bitten a few times by these doubts :) $\endgroup$ Apr 14, 2012 at 15:26
  • 1
    $\begingroup$ 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. $\endgroup$ Jul 15, 2012 at 2:20

3 Answers 3

4
$\begingroup$

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}];
Replace[Most@ArrayRules@UpperTriangularize@WeightedAdjacencyMatrix[g], 
    {x_, y_} :> UndirectedEdge[x, y], {2}]

enter image description here

$\endgroup$
7
$\begingroup$

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

$\endgroup$
5
$\begingroup$
g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4}];
AbsoluteOptions[g, EdgeWeight] /. HoldPattern[EdgeWeight -> x_] -> x
$\endgroup$
2
  • 2
    $\begingroup$ +1 How does HoldPattern work here? Any reason for not using AbsoluteOptions[g, EdgeWeight][[1, 2]] or /. (EdgeWeight -> x_) :> x ? $\endgroup$
    – DavidC
    Apr 13, 2012 at 23:46
  • $\begingroup$ @David Nahhh just an old vice $\endgroup$ Apr 14, 2012 at 4:50

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.