第 3 步(迭代子集)效率低下,因为它对幂集中的每个元素都执行“j 包含 a”或“a 不在 j 中”。
这是我的建议:
1) 生成幂集 {{j, b, m}, {j, m}, {j, b} {b, m}, {m}, {j}, {b}}。您不需要此步骤,因为您不关心最终输出中的空映射。
2)遍历原始数据结构中的所有元素并构造以下内容:
[a] : [j, b, m]
[b] : [j, m]
[c] : [j, b]
[d] : [j, b]
[e] : [b, m]
[f] : [m]
3)反转上述结构和聚合(使用 [j, b,...] 的映射到 [a,b...] 的列表应该可以做到这一点)得到这个:
[j, b, m] : [a]
[j, m] : [b]
[j, b] : [c, d]
[b, m] : [e]
[m] : [f]
4) 比较 3 和 1 以填充剩余的空映射。
编辑:Hers 是 Java 中的完整代码
// The original data structure. mapping from "john" to [a, b, c..]
HashMap<String, HashSet<String>> originalMap = new HashMap<String, HashSet<String>>();
// The final data structure. mapping from power set to [a, b...]
HashMap<HashSet<String>, HashSet<String>> finalMap = new HashMap<HashSet<String>, HashSet<String>>();
// Intermediate data structure. Used to hold [a] to [j,b...] mapping
TreeMap<String, HashSet<String>> tmpMap = new TreeMap<String, HashSet<String>>();
// Populate the original dataStructure
originalMap.put("john", new HashSet<String>(Arrays.asList("a", "b", "c", "d")));
originalMap.put("bob", new HashSet<String>(Arrays.asList("a", "c", "d", "e")));
originalMap.put("mary", new HashSet<String>(Arrays.asList("a", "b", "e", "f")));
// Hardcoding the powerset below. You can generate the power set using the algorithm used in googls guava library.
// powerSet function in https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/collect/Sets.java
// If you don't care about empty mappings in the finalMap, then you don't even have to create the powerset
finalMap.put(new HashSet<String>(Arrays.asList("john", "bob", "mary")), new HashSet<String>());
finalMap.put(new HashSet<String>(Arrays.asList("john", "bob")), new HashSet<String>());
finalMap.put(new HashSet<String>(Arrays.asList("bob", "mary")), new HashSet<String>());
finalMap.put(new HashSet<String>(Arrays.asList("john", "mary")), new HashSet<String>());
finalMap.put(new HashSet<String>(Arrays.asList("john")), new HashSet<String>());
finalMap.put(new HashSet<String>(Arrays.asList("bob")), new HashSet<String>());
finalMap.put(new HashSet<String>(Arrays.asList("mary")), new HashSet<String>());
// Iterate over the original map to prepare the tmpMap.
for(Entry<String, HashSet<String>> entry : originalMap.entrySet()) {
for(String value : entry.getValue()) {
HashSet<String> set = tmpMap.get(value);
if(set == null) {
set = new HashSet<String>();
tmpMap.put(value, set);
}
set.add(entry.getKey());
}
}
// Iterate over the tmpMap result and add the values to finalMap
for(Entry<String, HashSet<String>> entry : tmpMap.entrySet()) {
finalMap.get(entry.getValue()).add(entry.getKey());
}
// Print the output
for(Entry<HashSet<String>, HashSet<String>> entry : finalMap.entrySet()) {
System.out.println(entry.getKey() +" : "+entry.getValue());
}
上面代码的输出将是:
[bob] : []
[john] : []
[bob, mary] : [e]
[bob, john] : [d, c]
[bob, john, mary] : [a]
[mary] : [f]
[john, mary] : [b]