Re: Re: Peculiar behaviour of Mathematica code
- To: mathgroup at smc.vnet.net
- Subject: [mg57983] Re: [mg57970] Re: Peculiar behaviour of Mathematica code
- From: Andrzej Kozlowski <andrzej at akikoz.net>
- Date: Wed, 15 Jun 2005 05:58:17 -0400 (EDT)
- References: <d8jlau$srs$1@smc.vnet.net> <200506140910.FAA13608@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 14 Jun 2005, at 18:10, Maxim wrote:
>
>
> It is strange that NextLexicographicSubset and NextGrayCodeSubset both
> break down on certain subsets and NextGrayCodeSubset is useless for
> incrementally generating the power set:
>
> In[1]:= <<discretemath`
>
> In[2]:= NextLexicographicSubset[{a, b, c}, {c}]
>
> Drop::drop: Cannot drop positions -2 through -1 in {c}.
>
> Drop::seqs: Sequence specification (+n, -n, {+n}, {-n}, {m, n}, or
> {m, n,
> s}) expected at position 3 in Drop[{c}, -2, a].
>
> Out[2]= Drop[{c}, -2, a]
>
> In[3]:= NextGrayCodeSubset[{a, b, c}, {c}]
>
> Part::partw: Part 4 of {a, b, c} does not exist.
>
> Part::partw: Part 4 of {a, b, c} does not exist.
>
> Out[3]= {c, {a, b, c}[[4]]}
>
> Maxim Rytin
> m.r at inbox.ru
>
>
The code for NextLexicographicSubset in the Combinatorica package is:
NextLexicographicSubset[l_List,s_List]:=Module[{elem},If[Last[
s]===Last[l],(elem=s[[Length[s]-1]];
Append[
Drop[s,-2],l[[Position[l,elem][[1,1]]+1]]]),Append[
s,l[[Position[l,Last[s]][[1,1]]+1]]]]]
This is obviously going to break down when the list s consists just
of the element Last[l]. The correct code should be:
NextLexicographicSubset[l_List, s_List] := Module[{elem}, If[
Last[s] === Last[l], (elem = s[[Length[s] - 1]];
If[Length[s] == 1, {}, Append[Drop[s, -2], l[[
Position[l, elem][[1, 1]] + 1]]]]), Append[s, l[[Position[l,
Last[s]][[
1, 1]] + 1]]]]]
It seems strange that this bug has survived so long!
I assume the other cases are of the same kind although I have not
checked this.
Andrzej Kozlowski
Chiba, Japan
- References:
- Re: Peculiar behaviour of Mathematica code
- From: Maxim <ab_def@prontomail.com>
- Re: Peculiar behaviour of Mathematica code