This is a somewhat follow-up question on how to represent priorities
The answers direct me towards basing the design on the Priority Queue ADT
.
My problem is that I can not figure out how to model this problem (I understand how a PQ
works).
So using my original (trivial example) assume I have PersonA
PersonB
...PersonY
(represented as classes) which favor various dishes Pizzas
Spaggeti
Steak
etc.
I want to find a way to specify which dish should be served to wish person according to Preference
(which could be an extra class as friends in answers of the starting question thread also suggested).
I am not sure how this should be modelled. My first thought is to create a PriorityQueue
for each dish (PizzaPQ
, SpaggetiPQ
etc), throw-in each queue all the Persons
and start removing the top from each queue (as the one that has the max preference for that dish) and remove that Person
from the other queues. This process goes over all queues sequentially.
Now although conceptually it seems correct (but it is not best approach since I have in my mind that discrepancies will appear due to the process itself), I don't think that I am on the right track, since
- The remove from the other queues is linear operation (I am talking about
remove(Object)
whereObject
has already been servedPizza
and should not be in the other queues any more) and would costO(N*k)
wherek
is the number of queues (and this seems to me to add quite a lot to the usage of the priority queue in the first place) - It seems to me that I need some abstraction to work over a "pool" of priority queues and I am not sure if there is actually such data structure and I am not aware of it.
I guess this problem could generalize as how to assign jobs or how to manipulate multiple queues (perhaps?)
There must be a standard design approach for problems like these.
Any input is highly welcome
Update after @Thomas answer:
The problem is slightly more complicated.
Besides the preference there could be other attributes (of a person) that could come into place.
E.g. PersonA
and PersonB
both prefer steak over anyother dish. But PersonA
has high cholesterol and PersonB
is an athlete. Taking somehow these attributes into account then PersonB
should get the steak. And perhaps PersonA
could eventually end up with something else.
This is why I orinally thought about PQs
of dishes