1. Numbers problem

I have a programmin problem.

Suppose I have 3 integer numbers: 7, 10, 24.

The problem is how to get an integer number by summing any of the numbers above.

Example: to get 44 use 10 10 24.
     to get 41 use 7 10 24.

I see on many websites it is called knapsack problem. But I don\'t understand
the explanations on many websites I found.

Please help me, ASAP because I need it very much.

Thanks very much!!

new topic     » topic index » view message » categorize

2. Re: Numbers problem

Hi Aku,

----------
> ïÔ: akusaya at gmx.net
> ëÏÍÕ: EUforum <EUforum at topica.com>
> ôÅÍÁ: Numbers problem
> äÁÔÁ: 12 ÉÀÌÑ 2002 Ç. 18:43
> I have a programmin problem.
> 
> Suppose I have 3 integer numbers: 7, 10, 24.
> 
> The problem is how to get an integer number 
> by summing any of the numbers above.
> 
> Example: to get 44 use 10 10 24.
>      to get 41 use 7 10 24.
> 
> I see on many websites it is called 
> knapsack problem. But I don\'t understand the 
> explanations on many websites I found.
> 
> Please help me, ASAP because I need it very much.
> 
> Thanks very much!!

Try please one explanation more,
this one is the Euphoria program 
by Art Adamson:

http://members.aol.com/EnjoyCruz/knapdown.zip

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

3. Re: Numbers problem

On Fri, 12 Jul 2002 09:43:22 -0500, akusaya at gmx.net wrote: 
 
>======== The Euphoria Mailing List ========  
> 
>I have a programmin problem. 
> 
>Suppose I have 3 integer numbers: 7, 10, 24. 
> 
>The problem is how to get an integer number by summing any of the numbers
>above.
> 
>Example: to get 44 use 10 10 24. 
>     to get 41 use 7 10 24. 
> 
>I see on many websites it is called knapsack problem. 
> But I don\'t understand the explanations on many websites I found. 
 
Neither do I, but the following seems to work: 
 
--# 
--# Program to find the set of numbers which add to a specified total. 
--# Eg given a set of 7, 10, 24, we want to find what sums to 44: in 
this case 10+10+24. 
--# 
 
sequence numset 
		 numset = {7,10,24} 
--# you will probably find the first set faster if numset is sorted 
highest first. 
integer target 
		target = 44 
 
procedure printsets() 
sequence testset	-- holds eg {0,2,1} for (7*0+)10*2+24=44 
integer testidx, testtot, setsfound 
testsetrepeat(0,length(numset)) 
testidx=1 
testtot0 
setsfound=0		-- true/false/"+" required in display flag 
while 1 do 
	while testidx<=length(numset) do 
--# you may or may not get much better performance here using 
floor((target-testtot)/numset[testidx]) 
		if target-testtot>=numset[testidx] then 
			testtot+=numset[testidx] 
			testset[testidx]+=1 
		else 
			testidx+1 
		end if 
	end while 
	if testtot=target then -- set found 
		setsfound0		-- to put "+" in display 
		for i = 1 to length(numset) do 
			if testset[i] then 
				if setsfound then puts(1,"+") end if 
				printf(1,"%d",numset[i]) 
				if testset[i]>1 then 
					printf(1,"*%d",testset[i]) 
				end if 
				setsfound1	-- skip "no sets found" message 
			end if 
		end for 
		printf(1,"=%d\n",target) 
	end if 
	--# 
	--# backtrack; remove last entry in testset 
	--# 
	testidx-1 
	while testidx do 
		if testset[testidx] then 
			testset[testidx]-=1 
			testtot-=numset[testidx] 
			testidx+=1 
			exit	-- backtrack done, carry on 
		end if 
		testidx-=1 
	end while 
	if testidx=0 then exit end if		-- no set exists (or no more 
sets exist) 
end while 
if setsfound=0 then 
	puts(1,"no set exists") 
end if 
end procedure 
 
printsets() 
	if getc(0) then end if 
 
 
Pete

new topic     » goto parent     » topic index » view message » categorize

4. Re: Numbers problem

Try this:

It's like a tree approach... and redundant and over-the-limit trees are 
removed to make it faster....

So, you have {7,10,24} and you want to get 44, for example.
( # = removed )
|
|                                { }
(time)          /                  |                \
|           {7}                  {10}              {24}
\|/        /   |    \         /     |      \        /  |   \
      {7,7}  {7,24} {7,10}  (#)   {10,10} {10,24}  (#) (#) {24,24}

and so forth.
Add another field to the element to keep a running total, and voila!

See code below.....
=====================================================
.______<-------------------\__
/ _____<--------------------__|===
||_    <-------------------/
\__| Mr Trick

--This example grows an array of totals...
--for each element, the first entry, added together, equals the second.

--ie state 1 -> {  {{101},101}, {{65},65},....
--   state 2 -> {  {{101,101},202}, {{101,65},166}, {{65,65},130},...
--   state 3 -> {  {{101,101,101},303}, {{101,101,65},267}, 
{{101,101,40},242}.....
--etc
--until an instance temp[a][2] is equal to the target. if this never 
happens,
--then it returns not found.

include sort.e
sequence numbers
atom target, k
sequence data, temp

numbers = {101,65,40,31} --or whatever. (if you can, avoid small numbers
target = 1001            --and high targets.)

--function to remove all duplicate elements from a sequence
function trim(sequence s)
    sequence d
    d = {}
    for a = 1 to length(s) do
        if not find(s[a],d) then
            d &= {s[a]}
        end if
    end for
    return d
end function

temp = {{{},0}} --starts as blank (0)
while 1 do
    data = trim(temp)   --copy the next dataset to the current dataset
                        --taking out any reduntant variables
    temp = {}           --set up for the next run
    k = 0
    for a = 1 to length(data) do    --for each element of the current 
dataset
        if data[a][2] < target then --if the total isn't already over the 
target
            for b = 1 to length(numbers) do --for each number
                temp &= {data[a]} --copy a current element into the next 
dataset
                k += 1
                temp[k][1] = sort(temp[k][1] & numbers[b]) --sort the 
constituents
                temp[k][2] += numbers[b]         -- and add that number

                if temp[k][2] = target then  --if the new total is the 
target...
                    puts(1,"Found: if you add ")            --do
                    print(1,temp[k][1])                     --a
                    printf(1," you get %d",{temp[k][2]})    --little
                    abort(0)                                --dance.
                end if
            end for
        end if
    end for
    --any elements that were over the limit did not get copied to the next
    --state.... so if they all are:
    if not length(temp) then
        puts(1,"No matches") --doodums.
        abort(0)
    end if
end while
--<end>--




>From: akusaya at gmx.net
>Reply-To: EUforum at topica.com
>To: EUforum <EUforum at topica.com>
>Subject: Numbers problem
>Date: Fri, 12 Jul 2002 09:43:22 -0500
>
>
>I have a programmin problem.
>
>Suppose I have 3 integer numbers: 7, 10, 24.
>
>The problem is how to get an integer number by summing any of the numbers 
>above.
>
>Example: to get 44 use 10 10 24.
>      to get 41 use 7 10 24.
>
>I see on many websites it is called knapsack problem. But I don\'t 
>understand the explanations on many websites I found.
>
>Please help me, ASAP because I need it very much.
>
>Thanks very much!!
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

5. Re: Numbers problem

{{{ On Sat, 13 Jul 2002 12:46:02 +0000, aku saya <akusaya at gmx.net> wrote:

======== The Euphoria Mailing List ========

The knapsack in the archives is very different.
it uses float numbers and the goal is seek as close as possible. But I
need the exact sum..
If the knapsack in the archives does not work, and <I assume> my program does not either (which *IS* exact), then, sheesh, I guess we

  • CANNOT HELP YOU* until you give us more info!

Just what, exactly, are you trying to do?

Pete

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu