开发者

SQL Random Tables

开发者 https://www.devze.com 2023-03-10 13:17 出处:网络
I want to implement graph coloring using databases. There is a table that will store all the vertices (1,2,3...) and a table that stores the name of all colors(red,blue,g开发者_如何学编程reen,etc..).

I want to implement graph coloring using databases.

There is a table that will store all the vertices (1,2,3...) and a table that stores the name of all colors(red,blue,g开发者_如何学编程reen,etc..).

Now a want to create a coloring table with columns vertex and color which will take all possible combinations from the above tables and then check the constraints in each of those tables. Whichever table satisfies the constraints of graph coloring is a solution.

Now how to create tables for each combinations??

Guys please help. Stuck on it from a while...

An example instance:

vertex 1 2 3

Colors red blue

coloring a) 1 red 2 blue 3 red b) 1 red 2 red 3 blue c) 1 blue 2 red 3 red . . . 6 tables


I'm not sure I understand your question, so I'll make some assumptions. Assuming you have a table called Vertex, with the following rows:

1
2
3

... and a table called Color, with the following rows:

Red
Green
Blue

... you can generate a table of all possible combinations with a simple unconstrained join, like this:

SELECT * INTO VertexColor FROM Vertex, Color

The result will be a new table, with the following rows:

1, Red
1, Green
1, Blue
2, Red
2, Green
2, Blue
3, Red
3, Green
3, Blue

Happy to help further if this does not answer your question.


SELECT Vertices.vertex, Colors.Color from Vertices
CROSS JOIN Color from Colors

EDIT: Seeing the new comments: This doesn't sound like a problem that is well suited for SQL, mainly because your number of columns in your resultset is dependent on the number of rows in your vertices table. That's not something that is easy in SQL (you probably need a multistep process, using dynamic sql through sp_execute). Since the ordering of the colums carries significance, you can't return a resultset containing only each vertex - color pair either, because the order in which the rows are returned may vary. To me it sounds like a problem better handled outside the database engine. You can still use the above cross join to get a preliminary dataset, where you filter out some conditions you have on the set.

0

精彩评论

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