Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.04; 'from:addr:yahoo.co.uk': 0.04; 'element': 0.07; 'tom': 0.07; 'idea?': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'itself.': 0.14; 'language.': 0.14; 'collections': 0.16; 'defaultdict': 0.16; 'element,': 0.16; 'levels,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'set()': 0.16; 'subject: \n ': 0.16; 'sure.': 0.16; 'thankfully,': 0.16; 'elements': 0.16; 'language': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'skip:{ 20': 0.24; 'second': 0.26; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'sets': 0.30; "i'm": 0.30; 'about.': 0.31; 'large.': 0.31; 'this.': 0.32; 'figure': 0.32; 'could': 0.34; 'common': 0.35; 'possible.': 0.35; 'something': 0.35; 'anybody': 0.35; 'point.': 0.35; 'but': 0.35; 'described': 0.36; 'should': 0.36; 'follows:': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'our': 0.64; 'charset:windows-1252': 0.65; 'between': 0.67; 'therefore': 0.72; 'algorithm,': 0.84; 'common,': 0.84; 'subject:Sets': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Algorithm for Creating Supersets of Smaller Sets Based on Common Elements Date: Sat, 21 Feb 2015 22:40:48 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-24-222-48.ppp.as43234.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424558476 news.xs4all.nl 2940 [2001:888:2000:d::a6]:46676 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86071 On 21/02/2015 19:46, TommyVee wrote: > Start off with sets of elements as follows: > > 1. A,B,E,F > 2. G,H,L,P,Q > 3. C,D,E,F > 4. E,X,Z > 5. L,M,R > 6. O,M,Y > > Note that sets 1, 3 and 4 all have the element 'E' in common, therefore > they are "related" and form the following superset: > > A,B,C,D,E,F,X,Z > > Likewise, sets 2 and 5 have the element 'L' in common, then set 5 and 6 > have element 'M' in common, therefore they form the following superset: > > G,H,L,M,O,P,Q,R,Y > > I think you get the point. As long as sets have at least 1 common > element, they combine to form a superset. Also "links" (common > elements) between sets may go down multiple levels, as described in the > second case above (2->5->6). Cycles thankfully, are not possible. > > BTW, the number of individual sets (and resultant supersets) will be > very large. > > I don't know where to start with this. I thought about some type of > recursive algorithm, but I'm not sure. I could figure out the Python > implementation easy enough, I'm just stumped on the algorithm itself. > > Anybody have an idea? > > Thanks, Tom A naive approach but should give you something to think about. from collections import defaultdict sets = ({'A','B','E','F'}, {'G','H','L','P','Q'}, {'C','D','E','F'}, {'E','X','Z'}, {'L','M','R'}, {'O','M','Y'}) d = defaultdict(list) for i, aSet in enumerate(sets): for a in aSet: d[a].append(i) superSets = [] for k in d: if len(d[k]) > 1: superSet = set() for i in d[k]: superSet |= sets[i] superSets.append(superSet) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence