0
$\begingroup$

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!

$\endgroup$
4
$\begingroup$

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:

pairs={{a1,a2},{b1,b2},{a2,a3},{c1,c2},{a3,a1},{b2,b1}};
FindCycle[Rule@@@pairs,Length@pairs,All]

{{b1[DirectedEdge]b2,b2[DirectedEdge]b1},{a1[DirectedEdge]a2,a2[DirectedEdge]a3,a3[DirectedEdge]a1}}

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

pairs={{a1,a2},{b1,b2},{a2,a3},{c1,c2},{a3,a1},{b2,b1},{c2,c1}};
cycles = FindPermutation[pairs[[All,2]], pairs[[All,1]]]

Cycles[{{1,3,5},{2,6},{4,7}}]

Which produces the cycles:

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

{{a1,a2,a3},{b1,b2},{c1,c2}}

$\endgroup$

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.