棘手的算法...在嵌套的哈希集中找到子集的多种组合?
我遇到一个问题,我必须在嵌套哈希集中找到子集的多种组合。基本上我有一个“主”嵌套哈希集,并且从“可能”嵌套哈希集的集合中,我必须以编程方式找到可能是“主”的同时子集的“可能”。
可以说我有以下内容:
var master = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "A", "B", "C"}),
new HashSet<string>( new string[] { "D", "E"}),
new HashSet<string>( new string[] { "F"})
}
);
var possible1 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "A", "B", "C"}),
new HashSet<string>( new string[] { "F"})
}
);
var possible2 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "D", "E"})
}
);
var possible3 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "F"})
}
);
var possible4 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "X", "Y", "Z"})
}
);
var possible5 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "A", "B" }),
new HashSet<string>( new string[] { "D", "E"})
}
);
我应该从算法中获得的输出应如下所示:
所有可能的组合子集:
possible1 and possible2
possible3 and possible5
possible2 and possible3
possible1
possible2
possible3
possible5
我正在尝试找出解决此问题的最佳方法。当然,还有蛮力选项,但我会尽力避免这种情况。
我只是希望我的问题足够清楚。
编辑
为了进一步详细说明子集的构成,这里有一些示例,假设主 {{"A","B","C"},{"C","D"," E",F"},{"X","Y","Z"}} :
- 的子集
- {{"A","B"}{"C","D"}} 将是{{" A","B","C"},{"X","Y"}} 是子集
- {{"A","B"},{"A","B"}} 不会是子集
- {{"A","B","C","D"}} 不会是子集
- {{"A","B","C"},{"C","D", “X”}} 不会是子集
基本上每个子集都需要是主集中相应子集的子集。
I have a problem where I have to find multiple combinations of subsets within nested hashsets. Basically I have a "master" nested HashSet, and from a collection of "possible" nested HashSets I have to programmatically find the "possibles" that could be simultaneous subsets of the "master".
Lets say I have the following:
var master = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "A", "B", "C"}),
new HashSet<string>( new string[] { "D", "E"}),
new HashSet<string>( new string[] { "F"})
}
);
var possible1 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "A", "B", "C"}),
new HashSet<string>( new string[] { "F"})
}
);
var possible2 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "D", "E"})
}
);
var possible3 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "F"})
}
);
var possible4 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "X", "Y", "Z"})
}
);
var possible5 = new HashSet<HashSet<string>>(new HashSet<string>[] {
new HashSet<string>( new string[] { "A", "B" }),
new HashSet<string>( new string[] { "D", "E"})
}
);
The output I should get from my algorithm should be as follows:
All possible combination subsets:
possible1 and possible2
possible3 and possible5
possible2 and possible3
possible1
possible2
possible3
possible5
I'm trying to figure out the best way to approach this. There is, of course, the brute force option, but I'm trying to avoid that if I can.
I just hope my question was clear enough.
EDIT
To further elaborate on what constitutes a subset, here are some examples, given the master {{"A","B","C"},{"C","D","E",F"},{"X","Y","Z"}} :
- {{"A","B"}{"C","D"}} would be a subset of
- {{"A","B","C"},{"X","Y"}} would be a subset
- {{"A","B"},{"A","B"}} would NOT be a subset
- {{"A","B","C","D"}} would NOT be a subset
- {{"A","B","C"},{"C","D","X"}} would NOT be a subset
Basically each child set needs to be a subset of a corresponding child in the master.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用暴力破解:
在代码中:
不过,代码不处理
{{"A","B"},{"A","B"}}
情况。这要困难得多(标记主控中使用的子集,甚至可能是单个元素 - 递归地)。Edit2:第三种方法也处理
{{"A","B"},{"A","B"}}
情况(以及更多)。Use bruteforce:
And in code:
The code does not handle the
{{"A","B"},{"A","B"}}
case, though. That is a LOT more difficult (flagging used subsets in master, maybe even individual elements - recursively).Edit2: The third method handles the
{{"A","B"},{"A","B"}}
case as well (and more).尽可能使用最简单的解决方案。
请记住,如果其他人必须查看您的代码,他们应该能够以尽可能少的努力理解它在做什么。我已经发现很难从你的描述中理解你想要做什么,而且我还没有必要阅读代码。
如果运行后发现速度太慢,请对其进行优化。
如果可能的话编写单元测试。单元测试将确保您的优化解决方案也正常工作,并将帮助其他人确保他们的更改不会破坏任何内容。
Use the simplest solution possible.
Keep in mind that if someone else has to look at your code they should be able to understand what it's doing with as little effort as possible. I already found it hard to understand from your description what you want to do and I haven't had to read code yet.
If you find that it's too slow after it's working optimize it then.
If possible write unit tests. Unit tests will ensure that your optimized solution is also working correctly and will help others ensure their changes don't break anything.