There is an optimization program I am writing. I have to write it in 3 different ways. Dynamic approach, recursive approach and memoization+recursive approach. I wrote the codes for each, they all compiled. According to my prof, the algorithms I chose were poor, and do not guarantee the true optimal result. I would like to have your attention on the recursive approach for solving my problem for now.
The problem itself is as follows:
There is a random number of 2D blocks with different dimensions each, (hight x width)
These blocks are arranged in a certain order that can't be modified.
Suppose there is a bookshelf-like container with shelves. All shelves have the same length, but each shelf could have a different height. We don't yet know how many shelves there are in a bookshelf.
My job is to take that string of 2D blocks and distribute them inside that bookshelf in such a way, that we get the minimal height of a bookshelf. The height of a bookshelf is determined by a sum of heights of all the shelves. The height of each shelf is determined by the highest block in that shelf (MAX(Block.height))
-The range of a shelf (length) is given
-The number of blocks and their dimensions are given
To make it easier to understand, I have attached a .jpg file that graphically illustrates the problem. I recommend to have a look at it at this point.
There are many ways for us to arrange them, but we need the most optimal (minimal) height of the bookshelf.
At first it may seem like its a greedy algorithm, but it's not! There are cases where its better not to fit
the shelf completely, but instead skip to the next shelf, leaving the previous one unfilled.
My approach to solution:
We can see that this system accepts a recursive strategy.
At first, I need to figure out how many blocks can actually fit the shelf completely. Let us call this value: "int fitted". I'll skip this step in here. Suppose we found
The main formula here, is as follows:
int N; //N is given, number of blocks
int begin = 0; //begin is the first block in the shelf
//0 from main() (initial step). begin later accepts a different value at the next recursion.
for (int i=begin; i<fitted; i++)
MAX[i] = max(Block_height, begin, i) + max(Block_height, begin+1+i, N);; //Block.height is the array of heights of each block.
this cycle defines the height of a particular shelf under different configurations (first with 1 block, then 2 blocks, 3 up to how many fit) + The max(block_height) of the rest of the string of Blocks (not yet broken up into shelves). After that is done, I find the argmin(MAX) to find out the most optimal configuration for that particular shelf. (i.e. number of blocks accepted in a particular shelf under the best configuration)
I repeat the process by calling this function again from that same function (recursion) but now with a different int begin value
int begin = accepted+1; //the next block after the last accepted block in a previous shelf.
By adding up the heights of resulting shelves, I find the optimal value of the hight of the whole bookshelf.
So my professor has a problem with that formula. He says that this formula doesn't guarantee the optimal value.
To be specific, he pointed out the second part of the formula.
MAX[i] = max(Block_height, begin, i) + max(Block_height, begin+1+i, N)
To be honest, I think he has a point
How should I solve this problem? I would first prefer some help on the recursive approach that I've described. I believe that once I get the recursion down, I may be able to figure out the dynamic approach. If you guys need my source code, I will provide it to you. But this particular part (the main formula) is what I need help with.
Thanks for your time
- This is the graphical explanation..
- blocks.jpg (71.07 KiB) Viewed 743 times