473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

help with recursion

5
hello.

I have an array of int and a number
I need to build a recursion that print all the sub groups that if I summarize them, the summery is smaller then the number .
if some one can help me and write me the code I will be greatful.

Thank you.
Mar 17 '08 #1
7 2137
Sick0Fant
121 100+
When you talk of subgroups, are you speaking of the Algebraic structure? Or did you mean subsets? More info, please.
Mar 17 '08 #2
sanctus
84
hello.

I have an array of int and a number
I need to build a recursion that print all the sub groups that if I summarize them, the summery is smaller then the number .
if some one can help me and write me the code I will be greatful.

Thank you.
How are the subgroup defined? Any possible combination of the elements of the int array or something else?

Why don't you post what you have come up with?
Mar 17 '08 #3
adato
5
this is my problem

A thief has a knapsack of a certain capacity. He looks around and sees different items of different weights and values. He would like the items he chooses for his knapsack to have the greatest value possible yet still fit in his knapsack. The problem is called the 0-1 Knapsack Problem because the thief can either take (1) or leave (0) each item.

out put the best value in minimum weight and an array of indactors that indicate by 1 or 0 which items the thief take
Mar 17 '08 #4
sicarie
4,677 Expert Mod 4TB
So what do you have on this so far?
Mar 17 '08 #5
JosAH
11,448 Expert 8TB
this is my problem

A thief has a knapsack of a certain capacity. He looks around and sees different items of different weights and values. He would like the items he chooses for his knapsack to have the greatest value possible yet still fit in his knapsack. The problem is called the 0-1 Knapsack Problem because the thief can either take (1) or leave (0) each item.

out put the best value in minimum weight and an array of indactors that indicate by 1 or 0 which items the thief take
This question is entirely different from yor first question. Now you want this:

- max(sum(a_i*w_i) <= W
- w.r.t. a_i in [0,1] integer

Your first question completely skipped the maximization constraint. You have to
be accurate when you formulate a problem and ask a question about it.

kind regards,

Jos
Mar 17 '08 #6
adato
5
sorry
I have tried to dived the problem to smaller problems
i though it will be easy for you.
Mar 17 '08 #7
JosAH
11,448 Expert 8TB
sorry
I have tried to dived the problem to smaller problems
i though it will be easy for you.
No it isn't; maximixing that a_i*w_i essentially demands another algorithm than
just finding feasible solutions. For the 0/1 Knapsack problem think along the
following (recursive) line:

Start with k == n.

- set up a sequence w_0, w_1, ... w_k
- the knapsack has volume W
- only the elements 0 ... k are allowed to be in the knapsack.

solve the two subproblems (this is the recursive step)

- w_0, w_1 ... w_k-1
- the knapsack has volume W-w_k
- ony elements 0 ... k-1 are allowed

and

- w_0, w_1 ... w_k-1
- the knapsack has volume W
- only element 0 ... k-1 are allowed

The value k starts at k == n.

for the second (locally optimal!) solution see if w_k still fits in the knapsack.

Take the better of the two solutions. The recursion ends when k < 0.

kind regards,

Jos
Mar 17 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.