Consider that I have a map of sets of values to values, in Java the type of this map would be:
Map<Set<Object>, Object> setToObjMap;
Given a new set of objects set, I wish to find all values in the setToObjMap where the associated key is a subset of a “search set”.
So, for example, if my map was:
["telephone", "hat"] -> "book"
["laugh", "fry", "mouse"] -> "house"
["dog", "cat"] -> "monster"
Then, given the search set ["telephone", "hat", "book", "dog", "cat"] I would retrieve the values “book” and “monster”.
In practice there may be tens of thousands of entries in the setToObjectMap, with tens of thousands of possible values in the sets. The search set will typically have around 10 elements.
I’m hoping there is an efficient way to do this that doesn’t require iterating through all keys in the map. Can anyone offer any suggestions?
You can create a lookup data structure
With
Finderhaving an intcountandmax, and aresword. Take note that the list is there to take care of the case where many sets insetToObjMapcan share the same word, which is not in your examples.This lookup collection is quick to build and even quicker to flush after a lookup.
The lookup algorithm iterates through
set, for each word, and each Finder for this word, it increases thecountvariable. Second pass, take all values of the lookup map, ifcount==max, putresin the result.Init algorithm:
Lookup algorithm:
Reset algorithm:
As for the complexity, if n is the number of elements in
setand m the number of values insetToObjMap, the complexity will be O(n+m)