Velocity Reviews > Perl > Circular lists

# Circular lists

xhoster@gmail.com
Guest
Posts: n/a

 01-09-2009
Jim Gibson <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>, gamo
> <(E-Mail Removed)> wrote:
>
> > EXAMPLE:
> >
> > I have a set of @set = qw(a a b b b c c c c);
> >
> > In a loop...
> >
> > @list = shuffle(@set);
> >
> > and I want to know how many diferent circular lists could be generated.

>
> I think you want to know how many unique permutations may be generated
> that are invariant under rotation (moving the last element to the front
> of the list any number of times).
>
> You can use the advice given in 'perldoc -q permute' to generate all
> possible permutations. Because the elements in your example are not
> unique, you will generate some permutations which are also not unique.
> If this is actually the case, then you want an efficient way of
> ignoring redundant permutations.
>
> One way would be to attach a unique key to each member of your list,
> e.g. qw( a1 a2 b3 b4 b5 c6 c7 c8 c9 ). Then, you only accept a
> permutation that has each repeated element in order. For example ( a1,
> a2, ... ) would be accepted but ( a2, a1, ... ) would be rejected. You
> apply this test for all the a's, b's, and c's in the permutation. If
> any are out of order, reject the permutation. To use the list, strip
> off the keys.

It might be possible to integrate that technique into the Fischer-Kause
algorithm so that only the unique ones are generated, rather than having
to generate, test, and discard. Now that would be some subtle code.

>
> For the circularity problem, each permutation can be rotated to produce
> all of the equivalent cases. For N elements (9 in your sample case),
> there will be N equivalent cases. To avoid generating the redundant
> cases, select one element (e.g. a1 from your list), place it in the
> first location, and generate all possible permutations of the remaining
> N-1 elements, using the method described above if you have non-unique
> elements. The result should be all possible circular lists (if I

I think this is a good start, but it still produces redundant results.

For example, if @set = qw/A A C/; # and we number the elements as you did
above, then this method will return both:

A1 A2 C1
A1 C1 A2

Which are circularly the same once the digits are removed.

Xho

--
The costs of publication of this article were defrayed in part by the
this fact.

gamo
Guest
Posts: n/a

 01-10-2009
On Fri, 9 Jan 2009, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Jim Gibson <(E-Mail Removed)> wrote:
> >
> > I think you want to know how many unique permutations may be generated
> > that are invariant under rotation (moving the last element to the front
> > of the list any number of times).
> >
> > You can use the advice given in 'perldoc -q permute' to generate all
> > possible permutations. Because the elements in your example are not
> > unique, you will generate some permutations which are also not unique.
> > If this is actually the case, then you want an efficient way of
> > ignoring redundant permutations.
> >
> > One way would be to attach a unique key to each member of your list,
> > e.g. qw( a1 a2 b3 b4 b5 c6 c7 c8 c9 ). Then, you only accept a
> > permutation that has each repeated element in order. For example ( a1,
> > a2, ... ) would be accepted but ( a2, a1, ... ) would be rejected. You
> > apply this test for all the a's, b's, and c's in the permutation. If
> > any are out of order, reject the permutation. To use the list, strip
> > off the keys.

>
> It might be possible to integrate that technique into the Fischer-Kause
> algorithm so that only the unique ones are generated, rather than having
> to generate, test, and discard. Now that would be some subtle code.
>
> >
> > For the circularity problem, each permutation can be rotated to produce
> > all of the equivalent cases. For N elements (9 in your sample case),
> > there will be N equivalent cases. To avoid generating the redundant
> > cases, select one element (e.g. a1 from your list), place it in the
> > first location, and generate all possible permutations of the remaining
> > N-1 elements, using the method described above if you have non-unique
> > elements. The result should be all possible circular lists (if I
> > understand your definition correctly).

>
> I think this is a good start, but it still produces redundant results.
>
> For example, if @set = qw/A A C/; # and we number the elements as you did
> above, then this method will return both:
>
> A1 A2 C1
> A1 C1 A2
>
> Which are circularly the same once the digits are removed.
>
> Xho
>

Thanks to all the responders, but I think now that the problem is
unsolvable. Since you need to compare a new candidate with those of
the past, the problem is more or less O(n^2). No matter if the algo-
rithm is random or deterministic in the enumeration.

Best regards,

> --
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.
>

--
http://www.telecable.es/personales/gamo/
"Was it a car or a cat I saw?"
perl -E 'say 111_111_111**2;'

Jürgen Exner
Guest
Posts: n/a

 01-10-2009
gamo <(E-Mail Removed)> wrote:
>On Fri, 9 Jan 2009, RedGrittyBrick wrote:
>> gamo wrote:
>> > I want to learn an effient way of handle circular lists.

>>
>> What do you mean by a circular list? AFAIK Perl only knows about plain lists.
>>

>...
>> Given a set a,b,c - the for loop could be used to yield
>> a b c
>> b c a
>> c a b
>> This might be the sort of thing you are looking for. I exclude b,c,a because
>> of the way I have interpreted your mention of "circular list".

>
>If a,b,c is a list
>b,c,a is the same list rotated b,c,a,b,c,a (note the a,b,c)
>c,a,b is the same too, because c,a,b,c,a,b (note the a,b,c)
>
>a,c,b is another list because a,c,b,a,c,b is new
>
>I expect this clarifies the problem.

Are you confirming Red's understanding of the problem or are you
correcting his understanding? To me both versions, his and yours, seem
to be the same.

If they are, then generating all your "circular lists" is trivial, as is
computing their number.

Obviously there are exactly as many circular lists as there are elements
in the original list, because each element can become the first element
in a result list.

And you can generate them by using a running \$index (0..\$#list) and for
each result list concatenate the elements from \$index to \$#list with the
elements from 0 to \$index-1.

jue

sln@netherlands.com
Guest
Posts: n/a

 01-10-2009
On Sat, 10 Jan 2009 07:22:12 -0800, Jürgen Exner <(E-Mail Removed)> wrote:
>
>And you can generate them by using a running \$index (0..\$#list) and for
>each result list concatenate the elements from \$index to \$#list with the
>elements from 0 to \$index-1.
>
>jue

Haven't seen any code yet. I think this is easier for you to write the
manipulation of ring buffers in English than to actually do it.

Anybody can profess how it can be done, unless you actually do it of course.
That separates the men from the boys.

sln

sln@netherlands.com
Guest
Posts: n/a

 01-11-2009
On Sat, 10 Jan 2009 20:34:14 GMT, (E-Mail Removed) wrote:

>On Sat, 10 Jan 2009 07:22:12 -0800, Jürgen Exner <(E-Mail Removed)> wrote:
>>
>>And you can generate them by using a running \$index (0..\$#list) and for
>>each result list concatenate the elements from \$index to \$#list with the
>>elements from 0 to \$index-1.
>>
>>jue

>
>Haven't seen any code yet. I think this is easier for you to write the
>manipulation of ring buffers in English than to actually do it.
>
>Anybody can profess how it can be done, unless you actually do it of course.
>That separates the men from the boys.
>
>sln

No problem bro, i'm writing a class that does it all

sln

Tim Greer
Guest
Posts: n/a

 01-11-2009
(E-Mail Removed) wrote:

> On Sat, 10 Jan 2009 20:34:14 GMT, (E-Mail Removed) wrote:
>
>>On Sat, 10 Jan 2009 07:22:12 -0800, JÃ¼rgen Exner
>><(E-Mail Removed)> wrote:
>>>
>>>And you can generate them by using a running \$index (0..\$#list) and
>>>for each result list concatenate the elements from \$index to \$#list
>>>with the elements from 0 to \$index-1.
>>>
>>>jue

>>
>>Haven't seen any code yet. I think this is easier for you to write the
>>manipulation of ring buffers in English than to actually do it.
>>
>>Anybody can profess how it can be done, unless you actually do it of
>>course. That separates the men from the boys.
>>
>>sln

>
> No problem bro, i'm writing a class that does it all
>
> sln

You're replying to yourself again? Just so you know, you've replied to
your previous post saying you've not seen the code yet, and just
replied saying "no problem" and you're writing a class that does it
all?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Rom
Guest
Posts: n/a

 01-11-2009
Tim Greer wrote:
> (E-Mail Removed) wrote:
>
>> On Sat, 10 Jan 2009 20:34:14 GMT, (E-Mail Removed) wrote:
>>
>>> On Sat, 10 Jan 2009 07:22:12 -0800, Jürgen Exner
>>> <(E-Mail Removed)> wrote:
>>>>
>>>> And you can generate them by using a running \$index (0..\$#list) and
>>>> for each result list concatenate the elements from \$index to \$#list
>>>> with the elements from 0 to \$index-1.
>>>>
>>>> jue
>>>
>>> Haven't seen any code yet. I think this is easier for you to write
>>> the manipulation of ring buffers in English than to actually do it.
>>>
>>> Anybody can profess how it can be done, unless you actually do it of
>>> course. That separates the men from the boys.
>>>
>>> sln

>>
>> No problem bro, i'm writing a class that does it all
>>
>> sln

>
> You're replying to yourself again? Just so you know, you've replied
> to your previous post saying you've not seen the code yet, and just
> replied saying "no problem" and you're writing a class that does it
> all?

Maybe it's a multiple identify disorder? That may sound like a joke but it
is in fact a real human ailment. I mean all the trolls over the years in
various groups likely suffer from one mental illness or another. I must
admit, in all my years, I cannot recall seeing someone reply to themselves
in this way. Of course it is possible he just was tired and made an error
when replying to someone else; it happens.

--
Thanks, Rom.

Tim Greer
Guest
Posts: n/a

 01-11-2009
Rom wrote:

> Tim Greer wrote:
>> (E-Mail Removed) wrote:
>>
>>> On Sat, 10 Jan 2009 20:34:14 GMT, (E-Mail Removed) wrote:
>>>
>>>> On Sat, 10 Jan 2009 07:22:12 -0800, Jürgen Exner
>>>> <(E-Mail Removed)> wrote:
>>>>>
>>>>> And you can generate them by using a running \$index (0..\$#list)
>>>>> and for each result list concatenate the elements from \$index to
>>>>> \$#list with the elements from 0 to \$index-1.
>>>>>
>>>>> jue
>>>>
>>>> Haven't seen any code yet. I think this is easier for you to write
>>>> the manipulation of ring buffers in English than to actually do it.
>>>>
>>>> Anybody can profess how it can be done, unless you actually do it
>>>> of course. That separates the men from the boys.
>>>>
>>>> sln
>>>
>>> No problem bro, i'm writing a class that does it all
>>>
>>> sln

>>
>> You're replying to yourself again? Just so you know, you've replied
>> to your previous post saying you've not seen the code yet, and just
>> replied saying "no problem" and you're writing a class that does it
>> all?

>
> Maybe it's a multiple identify disorder? That may sound like a joke
> but it is in fact a real human ailment. I mean all the trolls over the
> years in various groups likely suffer from one mental illness or
> another. I must admit, in all my years, I cannot recall seeing someone
> reply to themselves in this way. Of course it is possible he just was
> tired and made an error when replying to someone else; it happens.
>

Actually, he replies to himself all of the time. I really don't mind
it, it just seems weird.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

xhoster@gmail.com
Guest
Posts: n/a

 01-11-2009
gamo <(E-Mail Removed)> wrote:
> > >
> > > I have a set of @set =3D qw(a a b b b c c c c);
> > >
> > > In a loop...
> > >
> > > @list =3D shuffle(@set);

> >=20
> > Is this the shuffle from List::Util? If so, why use a random method
> > as part of determining a non-random result? If you want to inspect
> > every permutation, don't do it randomly.

>
> I don't want to inspect every permutation because the number of
> permutations is n! =3D n*(n-1)*(n-2)...*1 and a problem of 20! is
> intractable.

There only that many permutations if your "set" has only unique letters,
which in your example it does not. Anyway, may gut feeling is that to get
an accurate count based on stochastic sampling, your will need to be about
as large as the underlying domain, anyway. But I could be wrong.

> Suppose that I have the huge list of permutations in memory.
> Wich are the circular rotations of another?=20

canonicalize! Or solve the problem analytically. If your set is such that
no permutation can have a self-rotation, then every circular list will
have exactly N linear representations.

> > How to make it more efficient is highly dependent on what you want to
> > do once you detect the sameness. You haven't really spelled that out.
> > It will also depend on the nature of @set, i.e. how redundant the
> > elements of
> > it are. Since the @set you show us isn't "large", it is hard to
> > extrapolate from your example up to your actual use case.
> >
> >
> > > or long loop.

> >
> > What loop is hypothetically long?
> >

>
> Take this as an example (not tested)
>
> #!/usr/local/bin/perl -w
>
> use List::Util qw(shuffle);
> @a =3D qw(a a a a a r r g g n);

I has able to solve this analytically, without any programming, because
of the uniqueness of 'n'. compute the permutations of 9 letters with
5,2,2 degeneracy, then treat gg as a single unit, computing perutations of
8 letters with 5,2,1 degeneracy, and subtract.

> for (1..10_000_000){
> @set =3D shuffle(@a);
> \$s =3D join '',@set;
> \$two =3D \$s . \$s;
> next if (\$two =3D~ /gg/);

Now here is a wrinkle you haven't shown us before. It will be devastating
to certain approaches to the problem.

snip of code which I don't understand the logic behind, and which doesn't
seem to work.

>
> __END__
> I know that the solution by a previous version is 588.

That's what I got analytically, too.

Xho

--
The costs of publication of this article were defrayed in part by the
this fact.

xhoster@gmail.com
Guest
Posts: n/a

 01-11-2009
gamo <(E-Mail Removed)> wrote:
> >
> > I think this is a good start, but it still produces redundant results.
> >
> > For example, if @set = qw/A A C/; # and we number the elements as you
> > did above, then this method will return both:
> >
> > A1 A2 C1
> > A1 C1 A2
> >
> > Which are circularly the same once the digits are removed.
> >
> > Xho
> >

>
> Thanks to all the responders, but I think now that the problem is
> unsolvable.

For some input sizes and structures, clearly it is intractable. But the
same is true for nearly all problems.

> Since you need to compare a new candidate with those of
> the past, the problem is more or less O(n^2).

Where n is what, the size of @set, or the factorial of that size?

Xho

--
The costs of publication of this article were defrayed in part by the
this fact.