Algorithm for determine applicable rule?

Status
Not open for further replies.

Onderdonk

Seniorius Lurkius
8
Let's say I have three items, each with descendents...

X
- X1
- X2
-- X2A
-- X2B
- X3

Y
- Y1
-- Y1A
-- Y1B
- Y2
- Y3

Z
- Z1
- Z2
- Z3
-- Z3A
-- Z3B

Let's also say there are rules for the value of A depending on different combinations of these items. Each rule applies to an item's descendents unless there is another rule to supersede.

For this example our rules are:

For case X-Y-Z, A=1
For case X2-Y-Z, A=2
For case X2-Y1-Z3, A=3

So, for a combination of X2A-Y3-Z1, the rules determine that A=2.

I could loop through every single combination of items and save the corresponding values. But if instead of 3 item families there are 10 or 20 families, the number of combinations will be huge. And each time there is a rule change the list of values will need to be regenerated.

Is there a better approach for determining the value for a specific case?
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Is it guaranteed that there will be exactly one "closest" rule? For example, is it possible to have a rule for case X-Y3-Z (also has distance = 3 hops, like X2-Y-Z does)?

If not, then a simple greedy algorithm should work. If so, then you need something closer to a full BFS, where you crawl from the given combo upward instead of down from the roots.
 
Status
Not open for further replies.