Hi everyone,

I am trying to write a recurisve algoritm that, when given a list of numbers, checks to see if there is any combination of the numbers that will add up to a target value.

I'm attempting to write it using tail recursion.

The method works occasionally, but does not work every time. My problem is if I have a list {9, 7, 2, 5, 1} and the target is 11, obviously 2 and 9 will add together to become 11. BUT the way I have it written, it will add 1 + 5 (making 6). The next iteration will add the 2, making 8. After that, it will see the 7 and 9 and decide that the group does not exist. I do not see how to code it so that it can detect the 2 and 9 combination. Did that make sense?

[source lang=java]public static boolean groupExists(final List<Integer> numbers, final int target) { if (numbers == null || numbers.isEmpty()) { return false; } else if (target == 0) { return true; } else if (numbers.size() == 1) { return (numbers.get(0) == target); } else if (target == numbers.get(numbers.size()-1)) { return true; } else { if (target > numbers.get(numbers.size()-1)) { return groupExists(numbers.subList(0, numbers.size()-1), target - (numbers.size()-1)); } else { return groupExists(numbers.subList(0, numbers.size()-1), target); } } }[/source]

I do not see how to get the method to check ALL possible combinations.

Can anyone give me some advice? Any help will be appreciated.

Thank you.

**0**

# Recursive Algorithm Help

Started by Alatar, Dec 07 2012 02:41 AM

5 replies to this topic

Sponsor:

###
#3
Members - Reputation: **1822**

Posted 07 December 2012 - 03:58 AM

Your first case is wrongly placed: if numbers is empty, the method should still return true if target == 0.

Your third case (numbers.size() == 1) is a bit ugly: it is not really necessary, as it should be taken care of by the rest of the code.

The last case (else) seems to be wrong: to find a solution, you should both try out picking the last number and skipping it. You only check the skip path if the last number is greater than the target.

This would be the code in Haskell; you'll need to translate it into java. My algorithm is not tail recursive, but it is trivial to make it so by just returning a bool instead of the integer selection.

[source lang="plain"]sat :: Integer -> [Integer] -> Maybe [Integer]----------------------------------------------sat 0 _ = Just []sat _ [] = Nothingsat n (x:xs) = if n < 0 then Nothing else do solution <- sat (n - x) xs return (x : solution) `mplus` sat n xs[/source]

The last case (sat n (x:xs)) should coincide with your last else-branch. My algorithm works on the first item of the list instead of the last as you do. As you can see, first it tries picking the first value (sat (n - x) xs), and if that doesn't work, it tries skipping it (sat n xs).

Your third case (numbers.size() == 1) is a bit ugly: it is not really necessary, as it should be taken care of by the rest of the code.

The last case (else) seems to be wrong: to find a solution, you should both try out picking the last number and skipping it. You only check the skip path if the last number is greater than the target.

This would be the code in Haskell; you'll need to translate it into java. My algorithm is not tail recursive, but it is trivial to make it so by just returning a bool instead of the integer selection.

[source lang="plain"]sat :: Integer -> [Integer] -> Maybe [Integer]----------------------------------------------sat 0 _ = Just []sat _ [] = Nothingsat n (x:xs) = if n < 0 then Nothing else do solution <- sat (n - x) xs return (x : solution) `mplus` sat n xs[/source]

The last case (sat n (x:xs)) should coincide with your last else-branch. My algorithm works on the first item of the list instead of the last as you do. As you can see, first it tries picking the first value (sat (n - x) xs), and if that doesn't work, it tries skipping it (sat n xs).

###
#4
Crossbones+ - Reputation: **3431**

Posted 07 December 2012 - 04:22 AM

A simple non-recursive brute force method:

If you were to assign the inclusion of every member in the list as a bit in an integer: 5 numbers == 11111, gives you 32 combinations;

Then simply try out every option:

If you were to assign the inclusion of every member in the list as a bit in an integer: 5 numbers == 11111, gives you 32 combinations;

Then simply try out every option:

for(int i = 0; i < 32; ++i){ int total = 0; //use list-value when bit is set for(int j = 0; j < 5; ++j) if(i & (1 << j)) if((total += list[j]) == target) return(true); } return(false);

###
#5
Members - Reputation: **1167**

Posted 07 December 2012 - 06:53 AM

Your problem looks like the famous algorithmic problem "Coin change". You could google around for solutions and make small modifications to fit your exact problem.

###
#6
Members - Reputation: **303**

Posted 07 December 2012 - 09:29 PM

Thank you for the replies.

Here is my updated program:

[source lang=java]public static boolean groupExists(final List<Integer> numbers, final int target) { if (numbers == null) { return false; } else if (target == 0) { return true; } else if (numbers.size() == 0) { if (target == 0) { return true; } else { return false; } } else if (target == numbers.get(numbers.size()-1)) { return true; } else { if (target > numbers.get(numbers.size()-1)) { return groupExists(numbers.subList(0, numbers.size()-1), target - numbers.get(numbers.size()-1)); } else { if (numbers.size() > 3) { return groupExists(numbers.subList(0, numbers.size()-2), target); } else { return groupExists(numbers.subList(0, numbers.size()-1), target); } } } }[/source]

It passes every single test I have written for it (about 20) except for one:

assertTrue(Exercises.groupExists(list, 31));

The variable list is defined: list = java.util.Arrays.asList(2, 7, 13, 9, 11);

Does anyone have any advice on how to change this so that the test does not fail?

Thank you

Here is my updated program:

[source lang=java]public static boolean groupExists(final List<Integer> numbers, final int target) { if (numbers == null) { return false; } else if (target == 0) { return true; } else if (numbers.size() == 0) { if (target == 0) { return true; } else { return false; } } else if (target == numbers.get(numbers.size()-1)) { return true; } else { if (target > numbers.get(numbers.size()-1)) { return groupExists(numbers.subList(0, numbers.size()-1), target - numbers.get(numbers.size()-1)); } else { if (numbers.size() > 3) { return groupExists(numbers.subList(0, numbers.size()-2), target); } else { return groupExists(numbers.subList(0, numbers.size()-1), target); } } } }[/source]

It passes every single test I have written for it (about 20) except for one:

assertTrue(Exercises.groupExists(list, 31));

The variable list is defined: list = java.util.Arrays.asList(2, 7, 13, 9, 11);

Does anyone have any advice on how to change this so that the test does not fail?

Thank you

"All you have to decide is what to do with the time that is given to you." - Gandalf