I have a list of transformations like this:
list = {"A" -> "B", "B" -> "A", "C" -> "D"}
As this is used to plot an undirected graph with GraphPlot, I don't want to have an Edge between the vertices A-B and B-A. I just want one of them.
How do I remove either A -> B or B -> A from this list? In the end, I want the list to look like this:
{"A" -> "B", "C" -> "D"}
I've tried using DeleteDuplicates, but I don't think I understand the testing part of that function (I should add that I'm a Mathematica beginner ... )
I made a function that can compare two transformations:
CmpTrans[x_,y_] := (x[[1]]/.x) == y[[1]]
It returns True for CmpTrans[A->B, B->A], but I can't seem to use this is the testing part of DeleteDuplicates.
Answer
This seems to do what you want:
rules = {"A" -> "B", "B" -> "A", "C" -> "D"};
Rule @@@ Union[Composition[Sort, List] @@@ rules]
(* {"A" -> "B", "C" -> "D"} *)
Comments
Post a Comment