[R] sign(<permutation>) in R ?

Martin Maechler maechler at stat.math.ethz.ch
Tue Apr 15 18:54:05 CEST 2008


Ok,
thanks a lot, everyone!

Yes, I should rather have started thinking a bit more myself,
before going the easy route to R-help....

Anyway, the most obvious algorithm,
just putting things into place by swapping elements,
and counting how many times you have to swap, is easy and
quite efficient.

I'll post R code later, being busy for the next few hours.
Martin


>>>>> "MM" == Martin Maechler <maechler at stat.math.ethz.ch>
>>>>>     on Tue, 15 Apr 2008 18:13:43 +0200 writes:

    MM> I am looking for an algorithm (written in R (preferably) or C,
    MM> but even pseudo-code in a text book maybe fine)
    MM> to determine the sign of a permutation.

    MM> What is that?  Well, a permutation is either even or odd, the
    MM> sign is +1 or -1, respectively, see, e.g.,
    MM> http://en.wikipedia.org/wiki/Signature_of_a_permutation
    MM> which also says
    >>> In practice, in order to determine whether a given permutation
    >>> is even or odd, one writes the permutation as a product of
    >>> disjoint cycles. The permutation is odd if and only if this
    >>> factorization contains an odd number of even-length cycles.

    MM> but I would not know how to algorithmically
    MM> "write the permutation as a product of disjoint cycles"

    MM> If you start looking at R code, 
    MM> let's assume the permutation {\pi(i)}_{i=1..n} is simply given
    MM> as the (integer) vector (\pi(1), \pi(2), ..., \pi(n))
    MM> {or equivalently, a random permutation is simply found by  'sample(n)'}

    MM> Thank you in advance for further pointers,
    MM> or even working R code.

    MM> Best regards,
    MM> Martin Maechler, ETH Zurich

    MM> ______________________________________________
    MM> R-help at r-project.org mailing list
    MM> https://stat.ethz.ch/mailman/listinfo/r-help
    MM> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    MM> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list