Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Look for recursive algorithm

Thread view: 
jacksu - 15 Jun 2006 23:40 GMT
Say i have int array, want to know all the sum combination, ignore
orders.

say [1, 2, 3]
have

[1, 2 ,3]
[3, 3]
[1, 5]
[4, 2]
[6]

How to write efficient java program to do so? The array size from 2 to
1000.

Thanks.
Rajah - 15 Jun 2006 23:53 GMT
Can you provide a little more info?

Based on the sample, I'm guessing that you want to sum the elements of
a vector, call the sum s, and then generate the set of all vectors of
non-negative integers that add up to s, where the number of elements is
less than or equal to the number of elements in the original vector. Is
that what you're thinking? (You didn't list [1, 1, 1, 1, 1, 1], for
example.) Also, are you doing this for a programming assignment?
jacksu - 16 Jun 2006 00:01 GMT
> Can you provide a little more info?
>
[quoted text clipped - 4 lines]
> that what you're thinking? (You didn't list [1, 1, 1, 1, 1, 1], for
> example.) Also, are you doing this for a programming assignment?

actually I need a set of sums of elements in the original array, [1, 1,
1, 1, 1, 1] may not be able to express what I want.  Let say the arr is

[37, 15, 12]

I expected to get 6 arrays back:
[64]
[49, 15]
[37, 27]
[52, 12]
[37, 15, 12]

Thanks.
Patricia Shanahan - 16 Jun 2006 00:04 GMT
>> Can you provide a little more info?
>>
[quoted text clipped - 18 lines]
>
> Thanks.

Are you required to use recursion?

Patricia
jacksu - 16 Jun 2006 00:07 GMT
> >> Can you provide a little more info?
> >>
[quoted text clipped - 22 lines]
>
> Patricia

no, any algorithm which is efficient and readable will be great.

Thanks
Rajah - 16 Jun 2006 00:14 GMT
Another thought... do you want to generate
1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 1
3, 1, 2
3, 2, 1

Or just the last triplet? (In other words, is the permutation of the
series important?)
jacksu - 16 Jun 2006 00:17 GMT
> Another thought... do you want to generate
> 1, 2, 3
[quoted text clipped - 6 lines]
> Or just the last triplet? (In other words, is the permutation of the
> series important?)

No, I don't need permutation. And I don't know two thing could link
together.
Daniel Dyer - 16 Jun 2006 00:00 GMT
> Say i have int array, want to know all the sum combination, ignore
> orders.
[quoted text clipped - 12 lines]
>
> Thanks.

Take a look at this thread:

http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/f4974
7b6b02e8ab1


Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Red Orchid - 16 Jun 2006 01:55 GMT
"jacksu" <jacksuyu@gmail.com> wrote or quoted in
Message-ID: <1150411248.371529.78560@y41g2000cwy.googlegroups.com>:

> Say i have int array, want to know all the sum combination, ignore
> orders.
[quoted text clipped - 7 lines]
> [4, 2]
> [6]

I think that the following computations is required for both
efficient and inefficient algorithms.

- nCk : Combination of different k of n things without repetition.
 (3C3, 3C2, 3C1 in the above case)
- Sum of each array.
- Remove duplicate value.

the codes may be like :

<java_pseudocode>
(untested)

int n = ...;

for (int k = 1; k <= n; k++) {

  int[] s = nSk(n, k);

  ...... // do something
}

   
   
/**
*  'nSk' returns the sum array of nCk.
*/   
int[] nSk(int n, int k) {
       
   int[][] c = nCk(n, k);
   int[] s = new int[c.length];
       
   for (int i = 0; i < c.length; i++) {
           
       s[i] = sum(c[i]);
   }
   return removeDuplicate(s);
}

   
int[][] nCk(int n, int k) {
       
   // select some proper algorithm in your environment.       
}

   
int sum(int[] a) {
       
   int s = 0;
       
   for (int i = 0; i < a.length; i++) {
           
       s += a[i];
   }
   return s;
}
   
   
int[] removeDuplicate(int[] a) {

   Arrays.sort(a);
       
   int[] t = new int[a.length];
   int idx = 0;
       
   t[idx++] = a[0];
       
   for (int i = 1; i < a.length; i++) {

       if (a[i - 1] != a[i]) {
               
           t[idx++] = a[i];
       }
   }
   int[] r = new int[idx];
   System.arraycopy(t, 0, r, 0, idx);
   return r;
}
</java_pseudocode>
 

 

> How to write efficient java program to do so? The array size from 2 to
> 1000.
>
> Thanks.
jacksu - 16 Jun 2006 05:39 GMT
Hmm, I think whatI need iswhat you mentioned nck, other part are pretty
striaght forward.any moresuggestion? Thanks

> "jacksu" <jacksuyu@gmail.com> wrote or quoted in
> Message-ID: <1150411248.371529.78560@y41g2000cwy.googlegroups.com>:
[quoted text clipped - 90 lines]
> >
> > Thanks.
Piotr Kobzda - 16 Jun 2006 13:47 GMT
> Say i have int array, want to know all the sum combination, ignore
> orders.
[quoted text clipped - 10 lines]
> How to write efficient java program to do so? The array size from 2 to
> 1000.

Assuming I understand your needs well, the expected result may consist of:
  i) each k-combination without repetitions of n-elements input array;
where k changes from 0 to n-2 (inclusive), with a single additional
element which is a sum of n-k elements not contained in combination; and
  ii) an input array itself.

See results (r) of applying this algorithm to your example array:

n: 3

// applying i):

k: 0
r: [] + (6) = [6]

k: 1
r: [1] + (5) = [1, 5]
r: [2] + (4) = [2, 4]
r: [3] + (3) = [3, 3]

// applying ii):

r: [1, 2, 3]

Notice that using above algorithm you will need iterative combination
series generator. This is because in expected case of 1000 elements
input array the result will consist of
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668068376
distinct arrays, which will probably not fit into your machine memory.

An example of iterative combinations generator you can find here:
http://mindprod.com/jgloss/combination.html

Regards,
piotr
jacksu - 16 Jun 2006 16:14 GMT
Thanks.!

> > Say i have int array, want to know all the sum combination, ignore
> > orders.
[quoted text clipped - 46 lines]
> Regards,
> piotr


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.