Search code examples
algorithmgraphtreememcachedweighted

Create a tree using get / set functions that accept an arbitrary number of parameters


I'm trying to create two functions: get & set, that both accept an arbitrary # of parameters (let's say 4). The set function accepts an array and sets it in memcached, the get function gets an array from memcached and returns it.

function get($a, $b, $c, $d) {
    ...
    return $array;
}

function set($a, $b, $c, $d, $array) {
    ...
}

Let there be 5 possibilities for $a, 10 possibilities for $b, 100 possibilities for $c, and 1000 possibilities for $d.

The twist: only $a is guaranteed, but there should always be a response to get (based on whatever data is passed it). It should also be able to handle a case where for a given $a, $b, & $c, if there's nothing set that matches all three of those params, it goes "up" (in a tree sense) and finds the closest match based on the priority of the param).

Right now I'm thinking of building some form of weighted tree, with the params being prioritized and assigned weights (e.g. $a has priority 100, $b has priority 10, $c has priority 90, $d has priority 50)...

Code is language agnostic, looking more for ideas on how to approach this (most effectively / efficiently). Thanks in advance!


Solution

  • I would approach this as a nearest neighbor search problem (the wikipedia link is a fairly good summary). If you can find a suitable distance metric perhaps you can be best off with a off-the-shelf algorithm/data structure, say the R-Tree.