Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| 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 | John Forkosh <forkosh@panix.com> |
| Newsgroups | sci.math |
| Subject | Re: Algorithm involving binary strings. |
| Date | Fri, 1 Jan 2016 02:19:11 +0000 (UTC) |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Lines | 169 |
| Message-ID | <n64nmv$lrv$1@reader1.panix.com> (permalink) |
| References | <4f1a0e21-4563-4697-8b6e-b4c41f8381ae@googlegroups.com> <n62jd3$kmf$1@reader1.panix.com> <b5cc51c7-94b1-469b-ac7c-2276f4bc4633@googlegroups.com> <0685fc25-f598-47c8-b037-23ac17659e10@googlegroups.com> |
| NNTP-Posting-Host | panix3.panix.com |
| X-Trace | reader1.panix.com 1451614751 22399 166.84.1.3 (1 Jan 2016 02:19:11 GMT) |
| X-Complaints-To | abuse@panix.com |
| NNTP-Posting-Date | Fri, 1 Jan 2016 02:19:11 +0000 (UTC) |
| User-Agent | tin/1.6.2-20030910 ("Pabbay") (UNIX) (NetBSD/6.1.5 (i386)) |
| Xref | csiph.com sci.math:325309 |
Show key headers only | View raw
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 <pepstein5@gmail.com> 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 <pepstein5@gmail.com> 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<string> 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 )
Back to sci.math | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2015-12-30 09:41 -0800
Re: Algorithm involving binary strings. Virgil <VIRGIL@VIRGIL.com> - 2015-12-30 13:16 -0700
Re: Algorithm involving binary strings. quasi <quasi@null.set> - 2015-12-30 16:30 -0500
Re: Algorithm involving binary strings. James Waldby <not@valid.invalid> - 2015-12-31 01:18 +0000
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2015-12-31 06:53 +0000
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2015-12-31 06:46 -0800
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2015-12-31 07:38 -0800
Re: Algorithm involving binary strings. John Forkosh <forkosh@panix.com> - 2016-01-01 02:19 +0000
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2016-01-01 11:44 +0000
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2016-01-01 05:00 -0800
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2016-01-02 02:11 +0000
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2016-01-02 02:20 -0800
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2016-01-02 12:50 +0000
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2016-01-02 07:12 -0800
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2016-01-03 04:55 +0000
csiph-web