Wednesday, March 21, 2012

degree of seperation

Hi,
how can I efficiently find out the shortest connection in the following scenario:
there's a table which has two colums, both of the same type.
two items in the same row means that they are connected. (there is no direction of the connection so that a connection shows up in both ways in a table for query performance issues)
ie. item1 is connected with item2 and item2 is connected with item3, then the table looks like:

row1: item1 , item2
row2: item2 , item1

row3: item2 , item3
row4: item3 , item2

so the connection item1 to item3 would be item1 - item2 - item3.
degree of seperation is 2 in this case. how can I implement this in general with sql?
making an exhaustive search would cost too much time. thanks for any hints!

Perhaps you could dream up a recursive CTE to do this, although I can't think how. You could write a SQL CLR routine to do itwith the CLR it should be easy enough to do a search of the graph.

What confuses me is that you don't want "an exhaustive search". There's no magic here: assuming the shortest path is of length n, I don't see a way to get away without considering all paths shorter than n in one way or another.

Cheers,

No comments:

Post a Comment