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.
$\begingroup$
$\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$– Sjoerd C. de VriesApr 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$– Dr. belisariusApr 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$– Dr. belisariusApr 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$– Ralph DratmanJul 15, 2012 at 2:20
Add a comment
|
3 Answers
$\begingroup$
$\endgroup$
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}]
$\begingroup$
$\endgroup$
1
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:
Similarly, use
Thread[{EdgeList[g], PropertyValue[g, EdgeWeight]}]
Map[{#, PropertyValue[{g, #}, EdgeWeight]} &, EdgeList[g]]
to get a list:
-
$\begingroup$ The best solution. Using the weighted adjacency matrix does not allow to easily associative edges with weights if graph vertexes are not integers. $\endgroup$ Nov 15, 2022 at 17:04
$\begingroup$
$\endgroup$
2
g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4}];
AbsoluteOptions[g, EdgeWeight] /. HoldPattern[EdgeWeight -> x_] -> x
-
2$\begingroup$ +1 How does
HoldPattern
work here? Any reason for not usingAbsoluteOptions[g, EdgeWeight][[1, 2]]
or/. (EdgeWeight -> x_) :> x
? $\endgroup$– DavidCApr 13, 2012 at 23:46 -