# How to change arguments of UndirectedEdge

There is a list of UndirectedEdge:

list = {1 <-> 2, 2 <-> 3, 4 <-> 5}


How to change it to:

{101 <-> 102, 102 <-> 103,  104 <-> 105}


?

I tried :

list /. UndirectedEdge[x_, y_] /; x :> x + 100;
list /. x_ <-> y_ /; x + 100 <-> y + 100;
list /. x_UndirectedEdge /; First[x] <-> Last[x]
list /. x_UndirectedEdge /; x :> First[x] <-> Last[x];


but they are not working correctly.

• You nearly had it! You're second attempt is very close, you have the correct pattern to match x_ \[UndirectedEdge] y_, and the correct replacement x + 100 \[UndirectedEdge] y + 100 the only minor mistake is that you used Condition, \;, which should be RuleDelayed, :>. Therefore try: list /. x_ \[UndirectedEdge] y_ :> x + 100 \[UndirectedEdge] y + 100; Oct 26, 2016 at 7:52
• list /. x_Integer :> 100 + x
– kglr
Oct 26, 2016 at 20:20

In such cases Casesis your friend ;-). You can use it to identify and modify arguments in the following way:

Cases[list,
UndirectedEdge[x_, y_] :> UndirectedEdge[100 + x, 100 + y]]
(*{101 <-> 102, 102 <-> 103, 104 <-> 105}*)

list = {1 <-> 2, 2 <-> 3,  4 <-> 5};

list /. x_Integer :> 100 + x
Map[100 + # &, list, {-1}]
Replace[list, x_ :> 100 + x, {2}]
MapAt[100 + # &, list, {{All, 1}, {All, 2}}]
UndirectedEdge @@@ (100 + {##} & @@@ list)
EdgeList @ VertexReplace[list, v_ :> 100 + v]


all give

{101 <-> 102, 102 <-> 103, 104 <-> 105}