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] ]