algorithmic problem...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

algorithmic problem...

 
0
  #1
Aug 25th, 2008
okay so i've got n and an array of n numbers, i have to go through all the three's of numbers like a[i], a[j], a[k] so that i < j < k and take the maximum of each such numbers (max of a[i], a[j], a[k]). my first guess is three loops but that seems to be to slow:
  1. for( int i = 0; i < n-2; ++i )
  2. for( int j = i+1; j < n-1; ++j )
  3. for( int k = j+1; k < n; ++k )
  4. s += max( a[i], max( a[j], a[k] ) );
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: algorithmic problem...

 
0
  #2
Aug 25th, 2008
Does n have to be a factor of 3, or is n just some arbitrary number?
Last edited by Alex Edwards; Aug 25th, 2008 at 9:08 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: algorithmic problem...

 
0
  #3
Aug 25th, 2008
Please explain it more clearly. I don't seem to understand what are you trying to do.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: algorithmic problem...

 
0
  #4
Aug 25th, 2008
hmm... well ok
you get n numbers, where n is in N...
you gotta find all sets of 3 numbers so that the index of first is less than the second and the second is less than the third... you gotta sum the max elements of the sets of three.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: algorithmic problem...

 
0
  #5
Aug 25th, 2008
Hey, I dont seem to see any other method than this one. Is there anything wrong with the solution ?
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: algorithmic problem...

 
0
  #6
Aug 25th, 2008
umm, it's slow
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: algorithmic problem...

 
1
  #7
Aug 25th, 2008
You know, I tried to make up for my dumb response earlier by developing a meta-program for this algorithm to see if it would speed it up some.

I spent the last 2.5 hours doing tests on the std max, a macro max and my own definition of max. None worked so I thought that resorting to a templatized enumerated result would be more efficient but I can't spend more time on this at the moment.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: algorithmic problem...

 
0
  #8
Aug 25th, 2008
any 3-combination of the N-set would consist of numbers at 3 different positions in the array; you could choose positions i,j,k out of these three such that i<j<k holds.

so doesn't the problem reduce to generating all unique 3-combinations of an N-set.

several algorithms to do this efficiently exist.
Chase's twiddle or Knuth's (volume4) algorithms come to mind.

additional info: http://www.theory.csc.uvic.ca/~cos/i...tionsInfo.html
Last edited by vijayan121; Aug 25th, 2008 at 2:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,822
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: algorithmic problem...

 
3
  #9
Aug 25th, 2008
I don't know how to use the math BBCode, so this post won't look as good as it could, but focus on the concept. I'm going to just use (n choose m) to denote the statistical combinations

Consider a list of 5 numbers: 3 5 7 8 9
So n = 5 here. We're dealing with groups of 3 here, so let's make a varaible called m to denote that. m = 3. Notice the numbers are in order here. If your a matrix is something like this:

7 5 3 9 8
sort it so it's in order, and remember n = 5 and m = 3 (denoting number of elements and the fact that we're dealing in groups of 3).

3 5 7 8 9
Consider that in a group of three numbers, 9 will be the maximum of any triplet it is in. How many triplets is 9 in? Answer: The number of ways you can pick the two numbers from the remaining 4 elements after you've picked 9, or (n - 1 choose m - 1) or (4 choose 2). So the sum of the maximums when 9 is one of the elements in the triplet is:

9 * (4 choose 2) = 9 * 6 = 54
Now look at the triplets where 9 isn't one of the members. We have 4 numbers left:

3 5 7 8
8 is the maximum of any triple it is in here. How many times is 8 in a triplet here? Pick 8 and you have to pick 2 numbers from the 3 remaining numbers (3 5 7). How many ways can you do that? (3 choose 2). So the sum of the remaining maximums where 8 is a member of the triplet is:

8 * (3 choose 2) = 8 * 3 = 24
Finally, let's look at the triplets where neither 8 nor 9 is a member of the triplet. the remaining numbers are (3 5 7). How many triplets can you make from this? Answer: (3 choose 3), or 1. What's the maximum of (3 5 7)? 7. So you get:

  1. 7 * (3 choose 3) = 7 * 1 = 7

So you end up with 54 + 24 + 7 = 85, which is the same as you get when you do it with you program:

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. int s = 0;
  8. int a[] = {3, 5, 7, 8, 9};
  9. int n = 5;
  10.  
  11. for( int i = 0; i < n-2; ++i )
  12. for( int j = i+1; j < n-1; ++j )
  13. for( int k = j+1; k < n; ++k )
  14. s += max( a[i], max( a[j], a[k] ) );
  15.  
  16. cout << s << endl;
  17. return 0;
  18. }

It'll be faster because you don't have as many for-loops and comparisons. Sort it first, then just do a single loop counting down from n to k (5 to 3 in this case).

Hope this helps.

EDIT - Whoops! I mean count down from n to m. m represents the number of elements in the group (3 in this case), not k. I forgot which variable I had defined.
Last edited by VernonDozier; Aug 25th, 2008 at 2:49 pm. Reason: clarification
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: algorithmic problem...

 
0
  #10
Aug 25th, 2008
wow, thanks for help all, and i was thinking about getting Knuth's books, i've already got Sedgewick's but don't know should i buy those too. i've seen 3 volumes selling in a set, i checked it out, seems to be full of high-level math, anyone knows if i should get those?
i was thinking about Cormen's introduction to algorithms too...cause Sedgewick's too lazy to gief me strings.
Last edited by gregorynoob; Aug 25th, 2008 at 8:44 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC