Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 851061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:22:07+00:00 2026-05-15T07:22:07+00:00

Consider the 0/1 knapsack problem . The standard Dynamic Programming algorithm applies only when

  • 0

Consider the 0/1 knapsack problem.
The standard Dynamic Programming algorithm applies only when the capacity as well as the weights to fill the knapsack with are integers/ rational numbers. What do you do when the capacity/weights are irrational?

The issue is that we can’t memoize like we do for integer weights because we may need potentially infinite decimal places for irrational weights – leading to an infinitely large number of columns for the Dynamic Programming Table .

Is there any standard method for solving this? Any comments on the complexity of this problem? Any heuristics?

What about associated recurrences like (for example):

f(x)=1, for x< sqrt(2)

f(x)=f(x-sqrt(2))+sqrt(3),otherwise

?

Or the Pibonacci number problem here: http://www.spoj.pl/problems/PIB/ ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:22:08+00:00Added an answer on May 15, 2026 at 7:22 am

    I don’t know of any general method which will solve problems of the kind you stated. Perhaps the memoization technique used in Pibonacci (see second section below) can be used.

    In any case, sometimes, we can give really fast algorithms by exploiting the problem (see the sqrt(2) & sqrt(3)) solution below.

    Reducing such problems to knapsack might not be such a good idea as I expect there will be other ways which will be much faster.

    So to answer your questions:


    Problem involving sqrt(2) and sqrt(3)

    I will answer your second question first.

    f(x) = 1 for x < sqrt(2). (x >= 0 also, I presume)
    f(x) = f(x-sqrt(2)) + sqrt(3)
    

    This can be solved very fast (in O(log logn) time!), using only integer arithmetic (which is assumed O(1)), expect for one last step which requires multiplying by sqrt(3) and adding 1.

    Given an n we need to find the smallest m such that

    n - m sqrt(2) < sqrt(2)
    

    i.e.

    n - m sqrt(2) < sqrt(2) => n < (m+1)*sqrt(2) => n * sqrt(2) < m+1
    

    and

    n - (m-1)sqrt(2) > sqrt(2) => n > m sqrt(2) => n*sqrt(2) > m.
    

    Thus m is the integer part of n*sqrt(2)

    and we have that f(n) = (m-1)*sqrt(3) + 1.

    Thus we only need to calculate [n *sqrt(2)] the integer part of n*sqrt(2).

    This can be quickly calculated by using the Continued Fractions of sqrt(2) which are rational approximations to sqrt(2) and they are in some sense the ‘best’ approximations with given denominator sizes.

    The continued fraction a(i)/b(i) of sqrt(2) can be formed using the recurrence:

    a0 = 1
    b0 = 1
    a(i+1) = a(i) +2*b(i)
    b(i+1) = a(i) + b(i)
    

    It can be shown that in order to approximate [n*sqrt(2)] it is enough to consider some odd i for which b(i) > 10*n^2 (using Liouville’s approximation Theorem and theorems on continued fractions) and that [n*sqrt(2)] = [n*a(i)/b(i)] for that i.

    Now a(i), b(i) satisfies the matrix equation

    [1 2] [a(i)]    [a(i+1)]
    [1 1] [b(i)]  = [b(i+1)]
    

    Thus we need to compute powers of the matrix

    [1 2]
    [1 1]
    

    So that the entries get bigger than 10*n^2.

    It can be shown that the required power of the matrix is O(logn) and thus can be calculated in O(log log n) time using only integer arithmetic (assuming that is O(1)).

    So the value of your function f at n can be calculated in O(log logn) time using only integer arithmetic (except for the last step, where you need to multiply an integer by sqrt(3)).


    Pibonacci Number

    From your comment, this is the problem

    g(x) = 1 if 0 <= x < 4
    g(x) = g(x-1) + g(x-pi) x >= 4
    

    This can be solved using memoization:

    Let h(m,n) = g(m - n*pi)

    Then we have that

    h(m,n) = h(m-1, n) + h(m, n+1)
    

    And so we have that

    g(m) = g(m-1) + h(m, 1)
    

    You can now use memoization by maintaining two tables, one for g(m) and other for h(m,n). Note that even though you need to calculate h(m,n+1), increasing n only reduces m -n*pi and will become between 0 and 4 within a reasonable time (O(m) I suppose), thus you won’t keep going on forever.

    This is not as nice (or fast) as the sqrt(2) and sqrt(3) solution, but I believe it does give a way to do the calculation.


    0-1 Knapsack with irrational coefficients

    Perhaps taking better and better rational approximations to the irrationals and then solving the 0-1 knapsack problem for approximation will ultimately converge to the right solution.

    My guess is, the fixed point in this iteration will give you the solution.

    Of course, as the approximations get better the W in O(nW) of they dynamic programming algorithm might become exponential soon and you might be better off just consider all possibilities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this problem: I have a program which should fetch (let's say) 100 records
consider this class,with no instance variables and only methods which are non-synchronous can we
consider the following algorithm with arrays: class MyType; { // some stuff } class
Consider this url: www.anysite.com/get_count/?sex=5&sex=6&city=5&city=7&job=7&job=8...... For using in dynamic query in view: model = anyModel.objects.filter(sex=5).filter(sex=6).filter(city=5)....
Consider the following setup: A windows PC with a LAN interface and a WiFi
Consider the need to develop a lightweight desktop DB application on the Microsoft platforms.
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider the Oracle emp table. I'd like to get the employees with the top
Consider an indexed MySQL table with 7 columns, being constantly queried and written to.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.