# Rearrange graph

I have to rearrange graph described by

j = {1 -> 2, 2 -> 3, 3 -> 1};


appling some function to all vertices on the right so rearrange[j] output should give me

{1 -> f[2], 2 -> f[3], 3 -> f[1]}


I guess I can use ReplaceAll, but can't guess how.

-
Related, more general: mathematica.stackexchange.com/questions/5740/… – Pinguin Dirk Oct 15 '13 at 10:25

You can use MapAt function to map function on specific part of the expression

MapAt[f, {1 -> 2, 2 -> 3, 3 -> 1}, {All, 2}]
(* ==> {1 -> f[2], 2 -> f[3], 3 -> f[1]} *)


or use replacement rule

{1 -> 2, 2 -> 3, 3 -> 1} /. Rule[a_, b_] :> Rule[a, f[b]]

-

Anotner way,

#1 -> f[#2]& @@@ {1 -> 2, 2 -> 3, 3 -> 1}


yields:

{1 -> f[2], 2 -> f[3], 3 -> f[1]}

-
I think that brackets are unnecessary. Just #1 -> f[#2] & @@@. – ybeltukov Oct 15 '13 at 10:15
Yes! sorry...silly mistake...have corrected – ubpdqn Oct 15 '13 at 11:10

Here is another way:

j={1->2,2->3,3->1};
j[[All,2]]=f/@j[[All,2]];
j


{1 -> f[2], 2 -> f[3], 3 -> f[1]}

-

A little bit more compact method:

MapAt[f, #, 2] & /@ {1 -> 2, 2 -> 3, 3 -> 1}

{1 -> f[2], 2 -> f[3], 3 -> f[1]}

-