Oh! You don't want a simple algorithm! You want a mini-max algorithm!
This falls into the category of logic programming. I used to really enjoy
algorithms like these (at my geeky best) in languages like LISP and Prolog.
I hear XSB is terrific for solving problems like this (and problems much
much harder), but I haven't had the chance to really dig into XSB yet.
What I didn't understand at first: You are not just trying to end up with
buckets. You are trying to find a path through the data to allow the least
number of buckets to hold the greatest number of items. Would that be a
correct statement?
(Sorry for calling it a homework problem. Your original description was a
lot simpler... I misunderstood).
Logic problems can be attacked with three methods: mathematical sorting,
heuristical analysis and brute force. Mathematical sorting (my term) would
require the use of higher-level calculus, and would probably be implemented
using something like a neural network... probably overkill for your
application (in addition to requiring a great deal of skill to develop and
maintain).
You could use brute force, with two alternatives. Brute force method 1 is
the backtracking method. This means that you try a combination, and if it
doesn't work, you back up to your most recent decision and change it. If
that doesn't work, you go back two decisions, change it, and follow to the
end of the tree. C# is not your friend here. You'll end up creating a
logic engine to determine your backtracking mechanism, and not only is that
difficult to do (and thus error prone), but you also tend into some higher
mathematics to prove that you don't drop into infinite loops (which is why
it took many years for Prolog to evolve into XSB). If you could post your
data into a data file, you could use XSB to do the work and then use C# to
read the data in and continue processing... (probably a pipe dream, but fun
to suggest). Brute force method 2 is combinational analysis... I'll get
back to that.
I'd like to see you use heuristical analysis. You need to create a "factor"
or "hash" function that, when applied to your inputs, will provide you with
clues as to where this object is "best" placed. The function may take, as
it's input, the complete set of assignments so far. While this is not as
powerful as either brute force method and will NOT find every possible
solution, it will find the vast majority of them and will do so much
quicker.
I'd need to know a lot more about the character of your data, over time, to
suggest the hash function, and how it would be used. You'd need to test it,
to make sure you get the results you expect.
If you need to find the solution, every single time, even when the problem
is "hard" (e.g. out of 65536 possible combinations of data, there is only
one that works), then you are probably going to have to go with one of the
brute force methods.
Combinational analysis (Brute force method 2) is what it says: you create a
list of every possible combination. Each time you add a possibility to the
list of possibilities, you check it. (A possibility includes a complete
assignment of all inputs to all buckets). The moment you find one that
works, you stop. It could take a while, depending on the statistical size
of your possibility set. Some simple pattern matching algorithms can help
you to start with "likely" combinations before moving on to the rest of what
I affectionately call "the slag heap."
Once again, without a good idea of what your data actually looks like, I
can't suggest the pattern match algorithms. I can help with the
combinational analysis brute-force list algorithm, if you'd like. I'll need
the following bits of data:
How many inputs do you have at one time?
How many buckets are you allowed to use (e.g. how many are allowed to be
partially filled?)
How much time should you spend (in milliseconds) to solve this problem?
It doesn't really matter what the attributes are: integers, complex numbers,
polynomial functions... I really don't need to know that to create the
combinational method... I would need to know that information to create your
pattern match algorithms, to start with the "likely" combinations. It's up
to you if that is something you'd like me to help with.
--- Nick
cody said:
sounds like a homework problem.
Actually, it is a company problem.
OK, so you have objects with two properties, A, and B. Let's call these
objects Things (out of respect for Dr. Seuss).
[..]
Thank you for trying to help but your algorithm cannot solve the problem.
I'll explain you why:
Lets say you have following 8 input objects:
A B
1 1
2 2
3 3
4 4
5 2
6 3
7 9
8 9
With this, you could theoretically fill 2 buckets with no duplicates.
But lets see what your algorithm would do:
Bucket1:
1 1
2 2
3 3
4 4
Bucket2:
5 2
6 3
7 9
8 9
It would put all items from 1st to 4th in the first bucket and the 5th to
7th in the second bucket. Now when it reaches the 8th item there is a
duplicate.
But one of the multiple valid solutions would be:
Bucket1:
1 1
2 2
3 3
8 9
Bucket2:
5 2
6 3
7 9
4 4
Sorry my problem is not as simple as it seems on the first glimpse.
--
cody
[Freeware, Games and Humor]
www.deutronium.de.vu ||
www.deutronium.tk