Re: lists of pairs
- To: mathgroup at smc.vnet.net
- Subject: [mg8511] Re: [mg8486] lists of pairs
- From: jpk at max.mpae.gwdg.de
- Date: Thu, 4 Sep 1997 02:20:00 -0400
- Sender: owner-wri-mathgroup at wolfram.com
> Hi all, I have a problem in discarding duplicates from a list of integer
> pairs, where if pairs of the form {a, b} and {b,a} exist in the list, I
> wish to discard (or select) only one, it doesn't matter which. The method
> I use below works, but is slow.
>
-- shnipp shnipp ---
> Suggestions? It is desired to minimize the time it takes to do this.
>
> Russell Towle
> Giant Gap Press: books on California history, digital topographic maps
> P.O. Box 141
> Dutch Flat, California 95714
> ------------------------------
> Voice: (916) 389-2872
> e-mail: rustybel at foothill.net
> ------------------------------
>
>
>
Hi Russel,
here is the timing of Your commands, on my machine:
In[15]:=
Timing[
c=Table[
Position[edges, x_ /; x == edges[[i]] || x == Reverse[edges[[i]]] ],
{i,Length[edges]}];
f=Flatten[Map[Take[#,1]&, Union[c]],2]
]
Out[15]=
{1.69 Second,{1,2,3,4,5,7,8,9,10,12,13,14,17,18,19,22,23,24,28,29,32,33,34,38,
39,43,44,48,49,53}}
And here the timing of my version:
In[14]:=
Timing[
sedges=Sort /@ edges; (* make a sorted copy of edges *)
dbls={}; (* here we will collect the double pairs *)
sedges //. {a___,i_,b___} /;
MemberQ[{a,b},i] :> (
AppendTo[dbls,Position[sedges,i]];
{a,b}
);
Flatten[First/@ dbls,2]
]
Out[14]=
{0.09 Second,{1,2,3,4,5,7,8,9,10,12,13,14,17,18,19,22,23,24,28,29,32,33,34,38,
39,43,44,48,49,53}}
Hope that speed increasment is ok for You.
Jens