Path: csiph.com!goblin3!goblin.stu.neva.ru!panix!panix.com!forkosh From: JohnF Newsgroups: sci.math Subject: Re: Algorithm involving binary strings. Date: Sat, 2 Jan 2016 02:11:06 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 237 Message-ID: References: <4f1a0e21-4563-4697-8b6e-b4c41f8381ae@googlegroups.com> <0685fc25-f598-47c8-b037-23ac17659e10@googlegroups.com> <6e5fb971-139e-4155-936e-4a0f8e6191ab@googlegroups.com> NNTP-Posting-Host: panix3.panix.com X-Trace: reader1.panix.com 1451700666 22958 166.84.1.3 (2 Jan 2016 02:11:06 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Sat, 2 Jan 2016 02:11:06 +0000 (UTC) User-Agent: tin/1.6.2-20030910 ("Pabbay") (UNIX) (NetBSD/6.1.5 (i386)) Xref: csiph.com sci.math:325364 Paul wrote: > Thanks, John (and thanks also for your suggestions below). > Since we've started top-posting, we might as well continue with > this convention. Bitwise-or is the right concept. However, > the right tool for this is bitset. I tried to apply your advice > but to improve it by using bitset instead of several unsigned longs. > The for loop now reads: > > for(int i = 0; i < n; ++i) > for(int j = i + 1; j < n; ++j) { > int knownByPair = 0; > bitset<500> topicRep = bitset<500>(topic[i]) > | bitset<500>(topic[j]); > for(int k = 0; k < m; ++k) > knownByPair += topicRep[k]; > if(knownByPair > numTopics ) { > knowers = 1; > numTopics = knownByPair; } > else if(knownByPair == numTopics) > ++knowers; > } > > This has been checked on hackerrank's platform and satisfies their tests. > Is this what you have in mind, or are there still problems? No problems as far as I can tell, offhand (just by looking). You could arguably (though I might lose the argument) improve the k=0...m-1 loop by "counting by bytes" like I suggested in original follow-up. But I'm not sure how much improvement, if any, that would actually accomplish. You already accomplished the big improvement with the one-step 500-bit or, eliminating that loop entirely. Maybe changing <500> to the actual input might be helpful. I'd imagine that if M<=8 or 32 or 64, then cc -O3 might find trickier ways to generate the requisite code for those kinds of cases. > Thanks for your suggestion. I already have this book. However, > I tend to read books from cover to cover (with the possible exception > of some exercises) and I find the problem of finding chapters/pages > to omit very difficult. So I'm not sure such a huge book is a way > to optimise my progress. Accordingly, I'm reading Algorithms > Unlocked instead. Gee, reading an algorithms book cover-to-cover sounds like reading a cookbook cover-to-cover. I usually focus on the dinner (problem) at hand, and study the relevant recipes (algorithms). Of course, the best way for you is whatever way you find best. > Most famous books are available free online. However, getting > non-pirated versions from virus-free websites is harder. I tried > to research the website bayanbox.ir and couldn't find any info. > Do you recommend this website more generally? > Thanks, Paul Nope, never heard of that site before google coughed it up while I was looking for the publisher's url for the book, and found that pdf instead. I downloaded the pdf myself, which looked okay to me in several viewers. Didn't realize executable code could be embedded in pdf's. (P.S. the "probability generalization" in that other follow-up post is actually kind of silly, since there's a totally trivial closed-form solution that I overlooked: if p's the probability a bit is set, then 1-(1-p)^2 is the "or" probability.) > On Friday, January 1, 2016 at 2:19:21 AM UTC, John Forkosh wrote: >> Excuse the top-posting, but just a few comments... >> (a) right about your remark that you originally mis-stated the problem. >> The problem statement clarifies what you wanted. >> (b) here's a possibly better approach than the "brute force" approach >> you have below, though the 500-topic max makes it a bit messier than >> it might be: what you want is an (inclusive) "or" of all pairs of >> bit strings. Each pair's a putative team. Once you've "or"'ed a team's >> two bit strings, then just count the number of 1's in the "or", like >> we all originally discussed. And the rest is just simple house-keeping. >> If you can "or" a 64-bit unsigned long long, then that lets you >> simultaneously (with one operation) check 64 topics. So just >> declare unsigned long long people[500][8];, where 8*64=512 >> accommodates the 500 max topics. Then parse and interpret those >> input strings accordingly during initialization. That'll be a >> little harder than what you have below, but then the real work >> should be a lot quicker. >> (c) if you want to self-study algorithms, I think I can safely say >> one of the best books is Cormen, et al's (long line follows), >> http://bayanbox.ir/view/4177858657730907268/introduction-to-algorithms-3rd-edition.pdf >> I'm surprised it's downloadable for free. You can just wget the whole thing. >> >> Paul wrote: >> > On Thursday, December 31, 2015 at 2:46:55 PM UTC, Paul wrote: >> >> On Thursday, December 31, 2015 at 6:53:34 AM UTC, JohnF wrote: >> >> > 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... >> >> >> > Thanks to everyone for their comments. I'm doing challenge >> > problems on hackerrank to prepare for a software dev interview. >> > Unfortunately, I misunderstood the problem. I did solve it >> > correctly (according to hackerrank) but I'm concerned about >> > inefficiency. Here is the link: >> > https://www.hackerrank.com/challenges/acm-icpc-team >> > However, since it's https that may mean that passwords etc. >> > are necessary. Here then is a copy-paste of the question (note >> > that the question is different to what I said here previously.) >> > Many thanks for your feedback. >> > Paul >> > >> > ********* BEGIN PROBLEM STATEMENT >> > Problem Statement >> > >> > You are given a list of N people who are attending ACM-ICPC >> > World Finals. Each of them are either well versed in a topic >> > or they are not. Find out the maximum number of topics >> > a 2-person team can know. And also find out how many teams >> > can know that maximum number of topics. >> > >> > Note Suppose a, b, and c are three different people, >> > then (a,b) and (b,c) are counted as two different teams. >> > >> > Input Format >> > The first line contains two integers, N and M, separated >> > by a single space, where N represents the number of people, >> > and M represents the number of topics. N lines follow. >> > Each line contains a binary string of length M. If the ith line's >> > jth character is 1, then the ith person knows the jth topic; >> > otherwise, he doesn't know the topic. >> > >> > Constraints >> > 2<=N<=500 >> > 1<=M<=500 >> > >> > Output Format >> > On the first line, print the maximum number of topics a >> > 2-person team can know. On the second line, print the number >> > of 2-person teams that can know the maximum number of topics. >> > >> > Sample Input >> > 4 5 >> > 10101 >> > 11100 >> > 11010 >> > 00101 >> > >> > Sample Output >> > 5 >> > 2 >> > >> > Explanation >> > (1, 3) and (3, 4) know all the 5 topics. So the maximal topics >> > a 2-person team knows is 5, and only 2 teams can achieve this. >> > ***** END PROBLEM STATEMENT. >> > >> > Here is my solution with the #includes omitted. >> > int main() { >> > int n; >> > int m; >> > cin >> n >> m; >> > vector topic(n); >> > for(int topic_i = 0;topic_i < n;topic_i++) { >> > cin >> topic[topic_i]; } >> > int numTopics = 0; >> > int knowers = 0; >> > >> > for(int i = 0; i < n; ++i) >> > for(int j = i + 1; j < n; ++j) { >> > int knownByPair = 0; >> > for(int k = 0; k < m; ++k) { >> > if(topic[i][k] == '1' || topic[j][k] == '1') >> > ++knownByPair; } >> > if(knownByPair > numTopics ) { >> > knowers = 1; >> > numTopics = knownByPair; } >> > else if(knownByPair == numTopics) >> > ++knowers; >> > } >> > cout << numTopics << endl << knowers; >> > return 0; >> > } >> > >> > I've spotted an added efficiency myself. >> > for(int k = 0; k < m; ++k) { >> > if(topic[i][k] == '1' || topic[j][k] == '1') >> > ++knownByPair; } >> > We don't actually need to go all the way to m if we already >> > know that knownByPair will be less than numTopics. >> > Hence >> > for(int k = 0; knownByPair + m - k >= numTopics && k < m; ++k) { >> > // body unchanged. } >> > This also passes the hackerrank test but doesn't feel >> > like a very substantial improvement. >> > Paul >> -- >> John Forkosh ( mailto: j@f.com where j=john and f=forkosh ) -- John Forkosh ( mailto: j@f.com where j=john and f=forkosh )