Path: csiph.com!feeder.erje.net!2.us.feeder.erje.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!panix.com!forkosh From: JohnF Newsgroups: sci.math Subject: Re: Algorithm involving binary strings. Date: Thu, 31 Dec 2015 06:53:23 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 48 Message-ID: References: <4f1a0e21-4563-4697-8b6e-b4c41f8381ae@googlegroups.com> NNTP-Posting-Host: panix3.panix.com X-Trace: reader1.panix.com 1451544803 21199 166.84.1.3 (31 Dec 2015 06:53:23 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Thu, 31 Dec 2015 06:53:23 +0000 (UTC) User-Agent: tin/1.6.2-20030910 ("Pabbay") (UNIX) (NetBSD/6.1.5 (i386)) Xref: csiph.com sci.math:325283 Paul wrote: > Suppose we are given a collection of binary strings all of > the same length. The problem is to find the largest number n > such that there exist distinct strings s1, s2 such that n positions > are set to 1 in both s1 and s2 simultaneously. Also, the task > is to find all pairs so that this maximum is realised. > > This is easy to do but difficult to do efficiently. Could someone > help me, perhaps by referencing the right techniques to look at? > > No, this isn't a homework problem. No, I'm not enrolled in any > courses. I'm trying to learn some computer science. > Thanks, Paul (a) just curious: what suggested this problem to you? To me, as far as I can tell, it seems pretty useless, application-wise. And if you're "trying to learn some computer science", one of the first things to learn is recognizing useful or theoretically important problems. And, unless I'm wrong, this ain't one. But please correct me if I'm wrong about that -- then I'll learn something:). (b) there's efficient wrt computational complexity, and efficient wrt making efficient use of hardware architecture. I can't say about the first, but you can make use of 8-bit byte addressability of virtually all architectures, as follows. First, if n isn't a multiple of 8, concat some 0 bits to each string so that it is. Now munge through each string byte-by-byte. And have a 256-int table with nOnes[i]=the number of 1's for value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8. Then just sum up nOnes for each byte of a string. That'll reduce your loop by a factor of 8. (And I leave you to answer why munging through your string 32-bit-int by 32-bit-int isn't optimal.) And once you know the number of 1's in each string, the rest is easy ... except for uniqueness. If your collection of strings may contain duplicates, you'll also want to accumulate some md5-like hash of each string as you munge through them. (c) you just talking about learning, or you going to show us some code? Here's the function prototype in C, int nones ( unsigned char *s, int len ) /* s = ptr to bit string (beginning on byte boundary) */ /* len = total #bits in s */ /* returns #bits in s that are 1's */ Please follow up with that function... -- John Forkosh ( mailto: j@f.com where j=john and f=forkosh )