Skip to main content

A introduction to knapsack problem

Knapsack problem is a very well known problem. The reason of my interest in this problem is that as it may sound a computer science problem, it actually arises from different scenarios as economics, educations especially education technology , asset portfolio and many other applied fields too. In this post, I intend to get to the details of this problem, both theoretical and practical codes and applications. So sit tight and read on.
The first scenario of the problem:
Imagine a burglar entering a house. The burglar, like any human being, has brought a definite sized bag and can carry up to a certain weight only. This burglar is allowed to steal anything, but each thing has two characters, weight and volume. Now, from this, there can be three problems.
(1) The items are binary in sense of stealing. i.e. each item can either be stolen or not. All items are present in 1 piece only. This is called 0–1 knapsack problem.
(2) The items are present in finite number. The burglar can steal a certain number of each item inclusive of 0 obviously. This is called bounded knapsack problem.
(3) The items are present in infinite number ( not a real scenario, but a mathematical scenario to consider) and therefore, the space of choice is infinite too. This is called unbounded knapsack problem.
Further generalization of this problem:
Now, as this problem is a pretty natural looking problem, one can obviously think that why to take discreet number of objects. That’s where the more general scenario comes. If you imagine the bounded problem, but lets say instead of books, the thief is hungry and steals from a milk cartoon, then easily the weight or volume of the milk stolen will become non-discreet and a real positive number. As nature does not follow discreet values in all things, so clearly, an important generalization of this problem is considering that the amount of objects stolen are real numbers, bounded by some number (in shade of bounded generalization problem) or unbounded.
Background requirements:
The knapsack problem is proved to be NP complete; i.e. there does not exist any polynomial timed solution for this problem. In general, dynamical programming is used to solve this or some useful approximations are implemented to ease out the problem. Now, one may wonder what dynamical programming is? but then that is a domain of programming itself and I will not go on to discuss about that but rather suggest to check on GeeksforGeeks to read on it.
Study on solutions:
So, where do we start? well our first stop is obviously the 0–1 knapsack problem. There will be painfully slow recursive and more intelligent dynamic programming solutions to this problem. Let’s dive in. Here I will mention only the algorithms only. First we only consider one feature problem, i.e. the weight only. Next we will proceed to two or more featured problem.
(1) Stupid solution: The stupid solution will be to calculate the value for each possible subset and then finding the maximum value for it. But we will obviously not do it.
(2) Recursion: The recursion will deal with the following idea. Consider a solution. Now, each of the items are either picked or not in this case. So, we can actually try from a side i.e. left or right and then do the following:
(i) Consider the maximum value possible within the n-1 objects left, excluding the n th object. Now again consider the maximum value possible with considering the following object added. Therefore we cover the two choices clearly.
(ii) Now, to put recursion, we can call the same function again and again in each of the above cases just by reducing the parameter to n-1. Thus it will reduce back to smaller case and being finite, the algorithm definitely terminates.
(iii) To add marginal solution, we put that if number of objects possible to take is 0 or the weight constraint is 0 then maximum value possible is 0.
Now, in this case, the problem which arrives is that one calculates the same values multiple times. And therefore recursive process on higher values tend to provide bad results.
Now, we provide a dynamical programming solution. The main properties of DP are (1) bottoms up and (2) memoization. Not going into the details, but to keep up the flow of the discussions, it needs to discuss is that
(1) bottoms up is storing values in array or some other types of data structure, starting from small values and growing up to larger values.
(2) Memoization is more like creating a memo; i.e. it starts from bigger values, just like a recursive function but stores the created values in a temporary array and there after, uses later on during the process and decreases the complexity.
In this case also, observe that the knapsack value problem is dependent on two parameters in two feature case. Therefore, in dynamical programming, we create first an empty two dimensional array for storing values of the function created stored for each of the integers started from 1 to w, the maximum weight and 1 to n, the number of items available. Then once the function value for some (n,w) pair is evaluated, it gets stored in the two dimensional array.
Clearly this can lead to solving at most nW times calculation of the function. Hence clearly the complexity is O(nW).
Now, we get down to the fractional knapsack problem. Fractional knapsack problem can be described as below:
It is a generalization of the 0–1 knapsack problem. In this case, a fraction(<1) of a object can also be picked. In this case, the solution becomes desperately easy. In this case, we will divide the values by weights. Then we will sort the items decreasing on the (value/weight). Then it is as clear as water, we will fill up the bag with the items from the start i.e. with highest par unit value and then proceed to next. In the last item, we will take fractional weight to fill the bag and that will be the best fit. This process is called Greedy method.
In the next post, we will discuss some implementation and non-orthodox methods.

Comments

Popular posts from this blog

Tinder bio generation with OpenAI GPT-3 API

Introduction: Recently I got access to OpenAI API beta. After a few simple experiments, I set on creating a simple test project. In this project, I will try to create good tinder bio for a specific person.  The abc of openai API playground: In the OpenAI API playground, you get a prompt, and then you can write instructions or specific text to trigger a response from the gpt-3 models. There are also a number of preset templates which loads a specific kind of prompt and let's you generate pre-prepared results. What are the models available? There are 4 models which are stable. These are: (1) curie (2) babbage (3) ada (4) da-vinci da-vinci is the strongest of them all and can perform all downstream tasks which other models can do. There are 2 other new models which openai introduced this year (2021) named da-vinci-instruct-beta and curie-instruct-beta. These instruction models are specifically built for taking in instructions. As OpenAI blog explains and also you will see in our

Can we write codes automatically with GPT-3?

 Introduction: OpenAI created and released the first versions of GPT-3 back in 2021 beginning. We wrote a few text generation articles that time and tested how to create tinder bio using GPT-3 . If you are interested to know more on what is GPT-3 or what is openai, how the server look, then read the tinder bio article. In this article, we will explore Code generation with OpenAI models.  It has been noted already in multiple blogs and exploration work, that GPT-3 can even solve leetcode problems. We will try to explore how good the OpenAI model can "code" and whether prompt tuning will improve or change those performances. Basic coding: We will try to see a few data structure coding performance by GPT-3. (a) Merge sort with python:  First with 200 words limit, it couldn't complete the Write sample code for merge sort in python.   def merge(arr, l, m, r):     n1 = m - l + 1     n2 = r- m       # create temp arrays     L = [0] * (n1)     R = [0] * (n

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle