使用特定的连接规则构建项目组合
我试图生成一组具有特定属性的对象的所有可能组合。问题是这些对象只能以某些方式组合,并且当您“验证”整个对象组合时,它必须满足某些条件。
对象数据存储在 SQL Server 数据库中(我可以完全控制该数据库,因此如果需要以不同方式排列数据,它可以更改)
我正在处理的对象的简化版本是
class Item {
public string Start {get;set;}
public string End {get;set;}
}
如果我有这些对象的列表对象例如:
- Item {Start = "A", End = "B"}
- Item {Start = "B", End = "C"}
- Item {Start = "B", End = "A"}
结果输出这组项目将是:
A -> B
B-> C
B->一个
A-> B->例如
项目1可以与项目2组合以产生A→A→A。 B-> C.
但项目 2 不能与项目 3 组合,因为项目 2 的结束值与项目 3 的开始值不匹配。
此外,还存在“总体”规则,其中开始和结束的总体组合不能导致项目继续: A-> B->一个
(第 1 项和第 3 项)。
最多可以将 5 个这些项目连接在一起。
有没有通用的方法或算法可以达到相同的结果?
任何帮助(甚至指示)将不胜感激。
I am attempting to generate all possible combinations of a set of objects with specific properties. The issue is that these objects can only be combined in certain ways and that when you "validate" the object combination as a whole it must meet certain conditions.
The object data is stored in a SQL Server database (that I have complete control over, so it can change if the data needs to be arranged differently)
A simplified version of the objects I'm dealing with is
class Item {
public string Start {get;set;}
public string End {get;set;}
}
If I have a list of these objects for example:
- Item {Start = "A", End = "B"}
- Item {Start = "B", End = "C"}
- Item {Start = "B", End = "A"}
The resulting output of this set of items would be:
A -> B
B -> C
B -> A
A -> B -> C
e.g.
Item 1 can be combined with Item 2 to produce A -> B -> C.
But Item 2 cannot be combined with Item 3 because the End value of Item 2 does not match the Start value of Item 3.
Also there are "overall" rules where the overall combination of Start and Ends cannot result in the items going:
A -> B -> A
(item 1 with item 3).
There can be up to 5 of these items joined together.
Is there any generic way of doing this or algorithm that achieves the same result?
Any help (even pointers) would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想通过数据库中的查询来执行此操作,可以查看递归查询使用通用表表达式。
If you want to do this with a query in the database, you can look into Recursive Queries Using Common Table Expressions.