开发者

Building Combinations of Items with Specific joining rules

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开发者_StackOverflow {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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜