I have a problem in Algorithms Class which i am trying to solve for the past two days but I can't. I want to fully fill a container with the MINIMUM number of given boxes with dynamic programming. My container has a certain size and I have different box sizes AND quantities of them (so i have 24 boxes of size 20, 15 boxes of size 48 etc). I think that i gotta somehow modify the knapsack problem? But the knapsack is not necessarily fully filled whilst in my problem i have to fill the container completely. Also I gotta somehow connect the hypothetical value of the knapsack with the sizes of the boxes. Any hints?
Related
wondering if anyone has any insight as to how to ascertain the order of differently sized rectangles from left to right and from top to bottom when they are not already aligned to any grid, and they are differently sized and/or rotated. Some might also be missing.
As anyone can see from the illustration, the objects should be numbered as shown. But how, mathematically or programmatically, can I determine this? What is the logic? I don't even know what words to use to describe the problem.
This looks like a rather complex problem; maybe some algorithm already exists, IDK.
Approach 1: grid positioning.
One approach could start with trying to position the rectangles on a grid whose mesh size will have to be calculated; maybe a best fit to the size of the rectangles (H & W, or surface, maybe?)
Once a reasonable grid has been determined, it must be appropriately placed over the rectangles; maybe in such a way that minimizes row overlap and column overlap of the rectangles?
The last step would consist of traversing the grid row by row, and assigning a label to each rectangle; maybe based on the max common surface shared by a grid cell and a rectangle?
There will be many edge cases to identify and resolve.
Approach 2: sweep line.
Alternatively, a sweep line numbering of the rectangles from N, S, E, and W, and an appropriate weighting/averaging of the numbering of the rectangles from each direction, might give good results?
It may require several passes, after identifying what could be rows and columns, in order to find a "best fit".
This second approach is likely easier to implement.
The page linked to here has been a great help to me. The method of using the named function (=(ROW(INDIRECT("1:361"))-1)*PI()/180) to produce the circle data points is very slick compared to my original method that was to calculate them individually, writing them in to rows.
My data set includes some 50k rows of data, each one defining a circle. The set is divided into 50 groups and I need to plot one circle from each group as selected via a scroll bar controlling a LOOKUP routine.
Please can someone suggest how I might modify the function (=(ROW(INDIRECT("1:361"))-1)*PI()/180) to reduce the number of data points it produces? I want to reduce the computing load and also, it's not practical to display & format data markers with such high data density. My existing circles are produced with just 18 coordinate pairs and are satisfactorily rounded.
Thanks in advance. Steve.
This would give you 19 data points, 0 and 360 as the start/end points with another every 20%
=(ROW(INDIRECT("1:19"))-1)*PI()/9
I'm trying to combine several box plots across categories of different size.
Here is an example illustrating problem:
sysuse auto
graph box mpg, by(rep78, rows(1)) name(g1, replace )
graph box mpg, by(foreign, rows(1)) name(g2, replace )
graph combine g1 g2 , ycom r(2)
This gives me the following results.
All works according to the manual so for but I have two problems with this output.
Firstly - aesthetics. Personally, I think plot with the same width across rows would look better.
Secondly, and more importantly - on more complex graphs the font size for categories, axes, etc. is also sized proportionally. So even if I specify, let's say - medium size of axis label on all graphs - some of them will be slightly bigger or smaller.
I was wondering if there is an option to programmatically force width of second row of box plots to have the same size as the first one.
Is this you want? It is based on a trick, but the trick is quite general.
sysuse auto, clear
expand 2
gen what = cond(_n <= 74, rep78, 6 + foreign)
label def what 6 Domestic 7 Foreign
label val what what
graph box mpg, by(what, note("Repair record and Foreign") row(2) holes(8 9 10))
The logic is that
The two categorical variables are combined lengthwise. That ensures that each box plot will be the same size.
By specifying holes, we persuade graph box to put graphs on two rows.
I guess that your label size problem will disappear once 1 is solved.
For even more flexibility, you may need to abandon graph box and use twoway instead. A detailed discussion was given by me in the Stata Journal in 2009: you can go straight to http://www.stata-journal.com/sjpdf.html?articlenum=gr0039
The app I'm working on uses a grouped GridView. Different templates are being used for different items on display and this is causing me an issue with the layout because the VariableSizedWrapGrid sets the row & column sizes based on the first item in each group.
I've tried to use the commonly-suggested solution of PrepareContainerForItemOverride and I'm encountering two problems:
It seems that I can only adjust the height & width around multiples of the column & row spans. That can leave me with quite a bit of wasted space if, say, item #2 needs to have 1.5 x the row height of item #1. I also seem to have to "guess" at what the most appropriate multiple is, which doesn't seem to be appropriate when apps are supposed to scale dynamically.
If I get the multiples too big, the content is horizontally & vertically centred. I've tried changing the alignment from stretch to left in various places but I cannot find which control property to set to get this to work.
Is there a better way of adjusting the item sizes than grid spans? If there isn't, is there a better way for me to lay out my content with variably sized items?
Thanks.
http://winrtxamltoolkit.codeplex.com/ includes WrapPanel which does the job perfectly. It stacks the individual items either horizontally or vertically and wraps onto the next row/column as required.
Where would i go to look for algorithms that take a 2d grid of values that are either 0 or 1 as input and then identifies all possible non-overlapping rectangles in it?
In a more practical explanation: I am drawing a grid that is represented by a number of squares, and i wish to find a way to combine as many adjacent squares into rectangles as possible, in order to cut down on the time spent on cycling through each square and drawing it.
Maximum efficiency is not needed, speed is more important.
Addendum: Apparently what i am looking for seems to be a technique called Tesselation. Now i only need to find a good description for this specific case.
Addendum 2: The boundary of the "1" squares will be irregular and in some cases not even connected, as the distribution of "1" squares will be completely random. I need these irregular shapes to be identified and split up into regular rectangles.
Correct answer: To get the best balance between speed and efficiency it is optimal to use the grid data to fill a quad-tree with each node having a status value of either empty/partly filled/filled.
I've done something similar for a quick-and-dirty voxel visualization of 3d boxes with OpenGL.
I started from the top left box and stored the empty/filled flag. Then I tried to expand the rectangle to the right until I hit a box with a different flag. I did the same in the down direction.
Draw the rectangle, if it is filled.
If there are boxes remaing, recursivly repeat the procedure for all three remaing rectangles induced by the last rectangle, which are right, bottom and bottom right:
xxxx 1111
xxxx 1111
xxxx 1111
2222 3333
2222 3333
2222 3333
Have a look at this article from Dr Dobb's Portal on finding a maximal rectangle in your situation. It is a very detailed discussion of an extremely efficient algorithm, and I think that repeating it iteratively would possibly solve your problem.
As you are not looking for the minimum number of squares I would suggest using a compromise that still keeps your algorithm simple.
What the best solution is depends on your data, but one simple alternative is to just collect boxes along one row. I.e:
0 0 1 1 1 0 0 0 1 1 1 1 0
Will result in:
skip 2
draw 3
skip 3
draw 4
skip 1
This will reduce the number of calls to draw box without any need of caching (i.e you can build your boxes on the fly).
If you want to create bigger boxes I would suggest a backtracking algorithm there you find the first 1 and try to expand the box in all directions. Build a list of boxes and clear the 1:s as you have used them.
So you are looking for the rectangular boundary of the 'ON' squares?
Do you want the inner or outer bound?
ie. Must the boundary only have 'ON' squares or do you want the rectangle to contain all the 'ON' squares in a group?
I had to solve a similar problem, my algorithm supports jagged arrays, I have heavily tested and commented it but it's slower than joel-in-gö's suggestion :
https://stackoverflow.com/a/13802336