Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Faster ways to unionize intersecting sets?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70118] Faster ways to unionize intersecting sets?
  • From: "lsha" <lsha at earthlink.net>
  • Date: Wed, 4 Oct 2006 05:59:26 -0400 (EDT)

Hi,

I need to search a list of sets and unionize any sets that intersect. The
following functions seems to work but may be there are faster ways?

Thanks in advance.



intersectQ[s1_, s2_] := If[Intersection[s1, s2] != {}, True, False]

mergeSets[s_List] := Module[
{h, r, singles, club, cnt},
cnt = Length[s];
If[cnt < 2, Return[s]];
singles = {};
club = s;

While[cnt >= 2,
h = club[[1]];
r = Rest[club];
hit = 0;
club = If[intersectQ[h, #], hit = 1; Union[h, #], #] & /@ r;
If[hit == 0, singles = Append[singles, h]];
--cnt;
];
Join[singles, club]
]



  • Prev by Date: Easy way to 're-order' the axes in Plot3D
  • Next by Date: Re: Bessel K expansion, large argument?
  • Previous by thread: Re: Easy way to 're-order' the axes in Plot3D
  • Next by thread: Re: Faster ways to unionize intersecting sets?