使用特定的连接规则构建项目组合

发布于 2024-10-01 02:21:11 字数 763 浏览 0 评论 0原文

我试图生成一组具有特定属性的对象的所有可能组合。问题是这些对象只能以某些方式组合,并且当您“验证”整个对象组合时,它必须满足某些条件。

对象数据存储在 SQL Server 数据库中(我可以完全控制该数据库,因此如果需要以不同方式排列数据,它可以更改)

我正在处理的对象的简化版本是

class Item { 
public string Start {get;set;}
public string End {get;set;}
}

如果我有这些对象的列表对象例如:

  1. Item {Start = "A", End = "B"}
  2. Item {Start = "B", End = "C"}
  3. 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:

  1. Item {Start = "A", End = "B"}
  2. Item {Start = "B", End = "C"}
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-10-08 02:21:11

如果您想通过数据库中的查询来执行此操作,可以查看递归查询使用通用表表达式

If you want to do this with a query in the database, you can look into Recursive Queries Using Common Table Expressions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文