Suppose I have a list of edges,
{1 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 3}
How do I get the 'invert' of the list,
{2 \[UndirectedEdge] 1, 3 \[UndirectedEdge] 1}
I'm aware that both these lists will yield the same graph.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityYou can use
UndirectedEdge[#2, #1, ##3] & @@@ edgelist
Reverse
should not be applied to an UndirectedEdge
as it may have more than two arguments in EdgeTaggedGraph
.
If you're not working on EdgeTaggedGraph
, then you can directly map Reverse
like below:
Reverse /@ edgelist
Or with the second argument of Reverse
(which is faster than mapping and uses less memory):
Reverse[edgelist, 2]
Or re-order the columns manually:
edgelist[[All, {2, 1}]]
In the case of EdgeTaggedGraph
where all the edges are tagged, you can use:
edgelist[[All, {2, 1, 3}]]
Or generally:
SubsetMap[Reverse, ;; 2] /@ edgelist
But it's slower (~45 times) and uses more memory in comparison with Szabolcs's answer.
Reverse
likeReverse /@ list
$\endgroup$SubsetMap[Reverse, ;; 2]
$\endgroup$