3

I am working with Graphs in which I want to define a Graph with Symbols or Strings as EdgeLabels, and later replace these with functions of the symbols with symbols then replaced with numerical values. I have tried using Replace without success. I can replace the symbols with numerical values by setting the values of the symbols, but that does not allow for replacing the symbol by a function of the symbol first.

Below are my unsuccessful attempts:

g = Graph[{1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3}, 
  EdgeLabels -> {1 \[DirectedEdge] 2 -> x, 2 \[DirectedEdge] 3 -> y}]

g // FullForm

(replacements doesn't work)

values = {x -> 1, y -> 2};

g /. values

(a more complicated case)

g /. Rule[DirectedEdge[n1_, n2_], s_] -> Rule[DirectedEdge[n1, n2], 1/s]

(this does work, but doesn't fulfill the need for more complicated
replacements
)

x = z;

g

David Keith
  • 4,340
  • 1
  • 12
  • 28

1 Answers1

3

You can use

Graph[g, EdgeLabels -> PropertyValue[g, EdgeLabels] /. values]

or

SetProperty[g, EdgeLabels -> PropertyValue[g, EdgeLabels] /. values]

to get

enter image description here

Similarly, for the second example:

values2 = Rule[DirectedEdge[n1_, n2_], s_] :> 
   Rule[DirectedEdge[n1, n2], 1/s];

Graph[g, EdgeLabels -> PropertyValue[g, EdgeLabels] /. values2]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896