I'm working on a project which is implementing the "Full-bin packing" algorithm in Java. This algorithm name comes from Decision A-Level Maths - but I can't find much information about it on the internet.
The algorithm is described as follows:
Use observation to find items that will fill a bin. Pack those items first.
Use the first-fit algorithm (which I have already written the methods for) to pack the rest of the items.
So I had 2 questions:
Is this the same as what is called "best-fit" algorithm?
How can I implement this in a program? (specifically, what is the best way of finding combinations that will fill a bin?)
In the case of my program, the items are going to be numbers and both the maximum number of numbers and the number of bins is going to be limited to 6.
I haven't actually written any code yet as I'm not sure about how to implement it in the first place - but I edited the first post to show one way I was thinking of doing it.
Edit -
Let's say the 6 numbers I have are: {1, 2, 3, 4, 5, 6}.
The way I thought of it was to first sort the numbers into decreasing order, and then having 2 loops to try every possible combination to see if any of them fill a bin (e.g. 1 & 2, 1 & 3, 1 & 4, 1 & 5, 1 & 6 and then 2 & 3, 2 & 4 and so on), would this be a good way of doing it?
Related
I am working on a project for an algorithms course I'm in and I'm completely at an impass. The assignment is to find all sets of 4 numbers in an array for which i+j=k+l in O(n^2*log(n)) time.
I know this is similar to the 3sum problem, where you have to find all the sets in an array for which i+j+k=0. We have discussed a solution to this problem in our lecture that solves it in O(n^2*log(n)) time by iterating through all unique pairs of 2 (n^2 time) and using a binary search on a sorted array to find a value that satisfies the problem (log(n) time).
However, I don't see how the problem with 4 numbers could be solved in this time. I think it's a safe guess that the log(n) in the complexity comes from a binary search, which i'll use for the last one. However, that would mean I have to iterate over all possible combinations of 3 in n^2 time, which I just don't think is possible.
I'm not looking for someone to do my work for me, but maybe if someone knows how this can be solved, they could give me a gentle tap in the right direction. Thanks.
Edit: it might also be helpful to note that I am required to solve this using sorting. Once I do that, I have to write another implementation that makes it faster using hashing, but I think I'll be able to do that just fine on my own. I just need to figure out how I can solve the problem with sorting first.
Keep a sorted list of sums and pairs. For instance, given the list
[1, 2, 4, 8, 16, 32, 9, 82]
we will want to identify 1+9 = 2+8
Identify the largest two numbers in the list, O**(N)**. In this case, they're (82, 32), a sum of 114. Allocate an array pair_sum of 114 locations; set all locations to null pointers.
Now iterate through the list for your i, j pairs. For each pair, insert the two numbers as a tuple-value at index i+j. When you get a collision at some index, you're done: you found a second pair-sum.
I'll outline this in some not-quite-pseudo code; you can translate to your favorite implementation language.
bank = [1, 2, 4, 8, 16, 32, 9, 82]
size = length(bank)
// Find the two largest numbers and allocate the array ...
pair_sum = array[114], initialize to all nulls
for lo in 0:size-1
for hi in lo+1:size
sum = bank[lo] + bank[hi]
if pair_sum[sum]
// There is already a pair with that sum
print pair_sum[sum], "and", bank[lo], bank[hi]
else
// Record new sum pair
pair_sum[sum] = tuple(bank[lo], bank[hi])
This is O(N^2), with bounded space dependent on the array values.
If you aren't allowed to use the sum as an index for practical reasons, I think you can adapt this to a binary search and insertion, giving you the log(n) component.
I need to write a program that prints out all the possible groups that can be formed from a given user input. The user also specifies the number of steps (k) and at every step, only two elements can be added to form one group.
My first instinct was to use a simple function to calculate the combinations using nCk and then print out all of them. However, it was pointed out to me that the number of possible combinations will be much higher than nCk.
Assume a set of n=5 numbers 1,2,3,4,5 and k=2
step 1: One group is created. out of all the possible combinations assume a group {(1, 2), 3, 4, 5}.
step 2: Here there are many other possibilities. It can either be {(1, 2, 3), 4, 5}
OR {(1, 2), 3, (4, 5)} OR any other combination
How can I calculate the number of ALL such groups possible in the given number of steps? And also print these groups?
EDIT: at every step two elements are added to show the sum. So after step 1, 1+2=3, the set would look like 3,3,4,5. But for clarifying the problem I'm writing it as (1,2) instead of writing the sum.
I am required to solve the "N Queens" problem using a generate and test approach in Java, so basically if N=8 my program must generate the 8^8 possible lists and test each one to return the 92 distinct lists that result in a solution to the problem. I must also use a DFS algorithm with backtracking to enumerate the possibilities.
To provide an example, list (2,4,6,8,3,1,7,5) means that the first queen is column 1 row 2, the second is column 2 row 4, the third is column 3 row 6...and so on.
The two main things preventing me from making headway on this are:
1) I have no idea how to generate every possible list of length N (and integers size N or less) in Java
2) I don't really understand how once I have all these lists, to abstract them to a datatype that can be traversed with a DFS algorithm.
I'm not begging someone to do my homework for me, more I'd like a conceptual walkthrough of how #2 can be thought of and a (somewhat) tangible example of how given an input N I can generate all N^N lists.
I need a dynamic matrix generator class which will return me an int[4][4] matrix, values should be between 1 to 16 and there would be no repetitive values.
Matrix solutions will be somthing like this:
or
If you carefully observe the matrix image then you will find every matrix have a particular logic/pattern in the position of the values of the matrix. It is not important that what is the pattern, but it is important to maintain a pattern (it means there should be a pattern of any type). currently I am using static code like:
// for the second image
int temp4[][] = { { 1, 2, 3, 4 }, { 12, 13, 14, 5 },
{ 11, 16, 15, 6 }, { 10, 9, 8, 7 } };
Currently I have 3/4 static pattern, but implementing this statically is a bad approach, so it should be dynamic because I need random patterns runtime.
So my question is: Is it possible to make a solution like this ?? What would be the code ?? If you know a link related to this then please post it or post the raw code.
Thanks in advance.
UPDATE
What is the pattern means for this question ??
If you carefully follow the 1st matrix you will find the number are like 1st row: 1,2,3,4 then 2nd row: 5,6,7,8 and so on..
for 2nd matrix its 1st row is 1,2,3,4 then it comes downwards as 5,6,7 then returns back as 8,9,10 ans so on.
Another pattern may be like this 1st column: 1,2,3,4 then 2nd column: 5,6,7,8 so the matrix will look like:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16 .. and so on
Thus I need a matrix generator which will return int[4][4] with non repetitive value from 1 to 16, and values should be in a order. another pattern may be like:
and
and the patterns of the above 2 are like this:
Based on your update I would create a Strategy for each pattern. In each pattern you can choice a starting point, a direction, and ascending or descending (if that is allowed)
Once you have a set of patterns, you can choice one at random and randomise it, or generate every possible pattern and select one at random.
You can generate non repeating values in a random order using Collections.shuffle()
You can then test your matrix to see if it has enough patterns or is acceptable. If not, repeat until you do.
I am looking for a clear explanation to my question (NOT looking for code), but if a bit of code helps to explain yourself, then please do.. thank you :)
Question:
-using Java
-Main class asks user for 2 integer inputs, then places them into 2 arraylists, of type integer. Each digit is broken up and stored in its own index, so it is its own "element", so to speak.
For example, with my code right now, it goes something like this:
"Please enter an integer:"
688
"Please enter another integer:"
349
At this point now, internally, I have stored the input as 2 arraylists, that look like this:
ArrayList1: [6, 8, 8]
ArrayList2: [3, 4, 9]
Now, lets say I want to perform some addition, such as ArrayList1 + ArrayList2.
I'll probably go ahead and create a temporary 'result' arraylist, then move that answer over to arraylist1 when my calculation is complete.
But the part I am having trouble with, is coming up with a systematic clear way to add the arraylists together. Keep in mind that this example uses an arraylist which represents an integer of length 3, but this could be anything. I could, for example, have an arraylist with 50 elements, such as [2, 4, 4, 3, 7, 3, 6, 3,.............] which could represent a huge number in the trillions, etc.
Think about how you would do grade-school addition. You'd start up by lining up the numbers like this:
1 3 7
+ 4 5
-----------
Then, you'd add the last two digits to get
1 3 7
+ 4 5
-----------
2
And you'd have a carry of 1. You then add the next two digits, plus the carry:
1 3 7
+ 4 5
-----------
8 2
Now you have carry 0, so you can add the last digit and the missing digit to get
1 3 7
+ 4 5
-----------
1 8 2
The general pattern looks like this: starting from the last digit of each array, add the last two numbers together to get a sum and a carry. Write the units digit of the sum into the resulting array, then propagate the carry to the next column. Then add the values in that column (plus the carry) together, and repeat this process across the digits. Once you have exhausted all of the digits in one of the numbers, continue doing the sum, but pretend that there's a 0 as the missing digit. Once you have processed all the digits, you will have the answer you're looking for.
Hope this helps!
If you store digits backwards, your arrays will be much easier to manipulate, because their ones, tens, hundreds, etc. will be aligned with each other (i.e. they will be sitting at the same index).
You could then implement the addition the same way they teach in the elementary school: go through arrays of digits one by one, add them, check for digit overflow (>=10), and pay attention to the carry flag (result digit is (a+b) % 10, carry flag is (a+b)/10). If the carry flag is not zero when you are done with the addition, and there are no additional digits remaining on either side, add the carry flag to the end of the result array.
The only remaining issue is displaying the lists. You can do it with a simple backward loop.
P.S. If you would like to double-check your mulch-trilion calculation against something that is known to work, use BigInteger to compute the expected results, and check your results against theirs.
Think of an arraylist as a storage container. It can hold items in it that are of type "integer", but it's type is still "storage container". You can't perform math on these type of objects--only their contents.
you have
list1
list2
and need an extra variable
int carry
then
1 do add(0,0) on short list, so that at the end two lists have same length.
2 reversely loop the two list.
sum=(carry+(e1+e2))
set e1 (list1 element) = sum%10,
carry = sum/10,
till the first element.
3 if carry==1, list1.add(0,1)
now list1 stores the result.
Note, step1 is not a must. it could be done in loop by checking the short list's length.