• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Alatar

Recursive Algorithm Help

5 posts in this topic

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

Share this post


Link to post
Share on other sites
The function doesn't seem to be doing any additions anywhere. Don't you want to check if the sum of two numbers equal to target?
0

Share this post


Link to post
Share on other sites
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 _ [] = Nothing
sat 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).
1

Share this post


Link to post
Share on other sites
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:

[CODE]
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);
[/CODE]
1

Share this post


Link to post
Share on other sites
Your problem looks like the famous algorithmic problem "[url="http://www.algorithmist.com/index.php/Coin_Change"]Coin change[/url]". You could google around for solutions and make small modifications to fit your exact problem.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0