6
$\begingroup$

I have a very simple graph, a loop:

x = Graph[{1, 2, 3, 4}, {1 <-> 2, 3 <-> 2, 3 <-> 4, 1 <-> 4}, 
  EdgeWeight -> {1, 10, 30, 60}, VertexLabels -> "Name", 
  EdgeLabels -> "EdgeWeight"]

Now, I want to change the values of its EdgeWeights. First, let's make sure I can even fetch them:

In[105]:= PropertyValue[x, EdgeWeight]

Out[105]= {1, 10, 30, 60}

Yay! Now let's try and change them:

In[110]:= PropertyValue[x, EdgeWeight] = {20, 49, 87, 5}

Out[110]= {20, 49, 87, 5}

Cool, now let's check their values.

In[111]:= PropertyValue[x, EdgeWeight]

Out[111]= {1, 10, 30, 60}

Wait, what... That's strange, because in this answer, that syntax is working...

Okay, let's try SetProperty, that's what it's for.

SetProperty[x, EdgeWeight -> {20, 49, 87, 5}]
<<Returns graph of x, but with initial weights as the edge labels, but ok>>

Now let's check...

In[116]:= PropertyValue[x, EdgeWeight]

Out[116]= {1, 10, 30, 60}

Um...

Let's try checking just the first value?

In[117]:= PropertyValue[{x, 1}, EdgeWeight]

Out[117]= $Failed

What's going on here? EdgeWeight is in the PropertyList@x. Is this somehow a bug?

I'm sure I'm missing something but I just wanted to make it clear that I tried a few things. What am I missing?

For posterity, I'm using version 10.0.2.0 on Windows 8 (which would explain the bug).

$\endgroup$
  • 1
    $\begingroup$ If you use x = SetProperty[x, EdgeWeight -> {20, 49, 87, 5}], then PropertyValue[x, EdgeWeight] gives the expected result. The docs SetProperty >> Details says: SetProperty will produce a new graph with a modified property value. $\endgroup$ – kglr May 7 '15 at 2:16
  • $\begingroup$ You need to use PropertyValue[{x, 1 <-> 2}, EdgeWeight] instead of PropertyValue[{x, 1}, EdgeWeight]. In the latter is {x,1} interpreted as vertex 1 in graph x which does not have an EdgeWeight property. $\endgroup$ – kglr May 7 '15 at 2:22
  • $\begingroup$ In version 9.0.1.0 (windows 8, 64-bit) PropertyValue[x, EdgeWeight] = {20, 49, 87, 5};PropertyValue[x, EdgeWeight] returns ` {20, 49, 87, 5}` as expected. $\endgroup$ – kglr May 7 '15 at 2:25
  • $\begingroup$ Ahhh... I swear I tried that, but I'll make sure to try it tomorrow. But why isn't setting PropertyValue = somevalue working? $\endgroup$ – F dot Floss May 7 '15 at 2:32
  • $\begingroup$ @kguler, thanks again. Though I must say, the syntax of having to use a<->b as the identifier in PropertyValue is weird, because the EdgeWeights are just an ordered list identified by an integer index, like EdgeList. $\endgroup$ – YungHummmma May 7 '15 at 13:54
4
$\begingroup$

Version 9.0.1.0 (Windows 8 64-bit)

In version 9.0.1.0 (Windows 8 64-bit), both PropertyValue and SetProperty work as expected:

x = Graph[{1, 2, 3, 4}, {1 <-> 2, 3 <-> 2, 3 <-> 4, 1 <-> 4},
  EdgeWeight -> {1, 10, 30, 60}, VertexLabels -> "Name", 
  EdgeLabels -> "EdgeWeight", ImagePadding -> 20]

Mathematica graphics

PropertyValue

PropertyValue[x, EdgeWeight]
(* {1, 10, 30, 60} *)
PropertyValue[x, EdgeWeight] = {20, 49, 87, 5};
PropertyValue[x, EdgeWeight]
(* {20, 49, 87, 5} *)

SetProperty

The docs SetProperty >> Details says:

SetProperty will produce a new graph with a modified property value.

x = Graph[{1, 2, 3, 4}, {1 <-> 2, 3 <-> 2, 3 <-> 4, 1 <-> 4},
   EdgeWeight -> {1, 10, 30, 60}, VertexLabels -> "Name", 
   EdgeLabels -> "EdgeWeight", ImagePadding -> 20];
x2 = SetProperty[x, EdgeWeight -> {20, 49, 87, 5}]

Mathematica graphics

{PropertyValue[x, EdgeWeight], PropertyValue[x2, EdgeWeight]}
(* {{1, 10, 30, 60}, {20, 49, 87, 5}} *)

Finally, in PropertyValue[{x, 1}, EdgeWeight], the first argument {x,1} is interpreted as vertex 1 in graph x which does not have an EdgeWeight property. One should use PropertyValue[{x, 1<->2}, EdgeWeight] instead.

{PropertyValue[{x2, 1}, EdgeWeight], PropertyValue[{x2, 1 <-> 2}, EdgeWeight]}
(* {$Failed, 20} *)

Version 10.1.0 for Linux x86 (64-bit)

I can reproduce the issues on Version 10.1.0 (on Wolfram Programming Cloud). That is,

{ PropertyValue[x, EdgeWeight] = {20, 49, 87, 5}; x,  PropertyValue[x, EdgeWeight] }
(* {{20, 49, 87, 5}, {1, 10, 30, 60}} *)

and

{y = SetProperty[x, EdgeWeight -> {20, 49, 87, 5}],PropertyValue[y, EdgeWeight] }

both give

enter image description here

Work-arounds:

x = Graph[{1, 2, 3, 4}, {1 <-> 2, 3 <-> 2, 3 <-> 4, 1 <-> 4}, 
  EdgeWeight -> {1, 10, 30, 60}, VertexLabels -> "Name", 
  EdgeLabels -> "EdgeWeight"];

  {(PropertyValue[{x,#}, EdgeWeight] =#2)&@@@ Transpose[{EdgeList[x],{20, 49, 87, 5}}]; 
   x, PropertyValue[x,EdgeWeight]}

and

x = Graph[{1, 2, 3, 4}, {1 <-> 2, 3 <-> 2, 3 <-> 4, 1 <-> 4}, 
  EdgeWeight -> {1, 10, 30, 60}, VertexLabels -> "Name", 
  EdgeLabels -> "EdgeWeight"];

{Fold[SetProperty[{#,#2[[1]]},EdgeWeight ->#2[[2]]]&,x,
      Transpose[{EdgeList[x], {20, 49, 87, 5}}]], 
 PropertyValue[x, EdgeWeight]}

both give

enter image description here

$\endgroup$
  • 1
    $\begingroup$ That looks like a bug; do you agree? $\endgroup$ – Mr.Wizard May 7 '15 at 5:25
  • 1
    $\begingroup$ @Mr.Wizard, yes it does. $\endgroup$ – kglr May 7 '15 at 6:03
  • 1
    $\begingroup$ I've gotta say that after all my false "peter and the wolf" accusations (where it turns out I'm just stupid and it's not a bug), it's a little satisfying to finally find a bug. I was pulling out my hair last night for hours trying to figure out what I did wrong. Oh and I added my version number (10) to the OP. $\endgroup$ – YungHummmma May 7 '15 at 14:02
  • $\begingroup$ @kguler, also, I think there may be a typo in your 2nd workaround, if you run that code it throws an error because y2 is undefined. $\endgroup$ – YungHummmma May 7 '15 at 14:13
  • $\begingroup$ One more quick question if that's okay: Your 1st workaround worked great for me, so I tried making a function out of it: ChangeEdgeWeights[gin_, newEWs_] := (pair = {(PropertyValue[{gin, #}, EdgeWeight] = #2) & @@@ Transpose[{EdgeList[gin], newEWs}]; gin, PropertyValue[gin, EdgeWeight]}; ]; Return[First@pair];) But it's giving me an error saying that my input graph "is not a symbol" in the property value assignment. Actually, maybe it's a warning because it still returns the right values. What's the deal with that? Thanks! $\endgroup$ – YungHummmma May 7 '15 at 14:30

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.