I want to define a function that takes a list of pairs as input and groups cycles, e.g. {{a1,a2},{b1,b2},{a2,a3},{c1,c2},{a3,a1},{b2,b1}} becomes {{a1,a2,a3,a1},{b1,b2,b1},{c1,c2}}.

I'm wondering if there is an obvious function for doing this which can simply be /@ to the list. As far as I can tell, Cycles does nothing to a list of this type. Is the only way to look at individual characters and manually group items?

Thank you!


In your example, there is no c2->c1 (or {c2, c1}) pair, so I don't think c1->c2 is a cycle. At any rate, one idea is to use FindCycle:



If you do include {c2, c1}, then you have a permutation, and you can use FindPermutation:

cycles = FindPermutation[pairs[[All,2]], pairs[[All,1]]]


Which produces the cycles:

pairs[[#, 1]]& /@ cycles[[1]]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.