Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-01 : 05:52:33
|
Here a code for finding all minimal loops (cyclic paths) in a graphwith vertexes of degree >= 3. Almost obviously that before seekingfor loops we should eliminate from the graph all its vertexes of degree < 3(degree of a vertex is the number of edges outcoming from the vertex).Note: there are no any 'parent' - 'child' nodes here. All vertexes areabsolutely equitable.if object_id('g3')>0 drop table g3if object_id('g3x')>0 drop table g3xif object_id('g3y')>0 drop table g3yif object_id('g3l')>0 drop table g3lGOcreate table g3y(v1 int, v2 int) -- ancillary tableGOcreate table g3x(n int, v1 int, v2 int) -- ancillary tableGOcreate table g3l(nl int, v1 int, v2 int)-- table for storing of 'detected' loopsGOcreate table g3(v1 int, v2 int)-- table of test data with pairs of adjoining vertexes-- each vertex is named by an arbitrary numberGOinsert into g3select 2, 3 union allselect 2, 4 union allselect 1, 4 union allselect 3, 5 union allselect 5, 6 union allselect 1, 6 union allselect 4, 7 union allselect 6, 8 union allselect 3, 9 union allselect 1, 7 union allselect 2, 7 union allselect 1, 8 union allselect 5, 8 union allselect 2, 9 union allselect 5, 9 ----union all/*select 2, 13 union allselect 3, 13 union allselect 13, 14 union allselect 12, 14 union allselect 12, 15 union allselect 11, 15 union allselect 11, 13 union allselect 10, 11 union allselect 10, 12 union allselect 10, 14 union allselect 10, 15*/GOinsert into g3 select v2, v1 from g3declare @i int, @n int, @v1 int, @v2 intset @i=1while 0=0beginset @n=1truncate table g3x truncate table g3yselect top 1 @v1=g3.v1, @v2=g3.v2 from g3 left join g3l on(g3.v1=g3l.v1 and g3.v2=g3l.v2)or(g3.v1=g3l.v2 and g3.v2=g3l.v1)where g3l.nl is null if @@rowcount=0 breakinsert into g3x select @n, @v1, @v2while @v1<>(select top 1 v2 from g3x order by n desc)beginset @n=@n+1insert into g3x select top 1 @n, v1, v2 from g3 where v2=@v1and v1<>@v2 and v1=(select top 1 v2 from g3x order by n desc)if @@rowcount=0begininsert into g3x select top 1 @n, v1, v2 from g3 wherev2 not in (select v1 from g3x union all select v2 from g3x) andv1=(select top 1 v2 from g3x order by n desc) and not exists(select 0 from g3y where g3y.v1=g3.v1 and g3y.v2=g3.v2) if @@rowcount=0 if @n>2 begin insert into g3y select v1, v2 from g3x where n=@n-1 delete from g3x where n=@n-1 set @n=@n-2 end else begin insert into g3l select 0, v1, v2 from g3x break endendelse begin insert into g3l select @i, v1, v2 from g3x set @i=@i+1 endendendselect * from g3l order by nl Below is what we get:nl v1 v2 ----------- ----------- ----------- 1 2 31 3 51 5 61 6 81 8 11 1 41 4 22 1 62 6 82 8 13 4 73 7 13 1 44 3 94 9 24 2 35 2 75 7 45 4 26 5 86 8 66 6 57 5 97 9 37 3 5 Of course, in general case not all found by the code loops are minimal.But this is exactly my approach:firstly find any possible loops (avoiding excessiveness!!),then, in WHILE loop, try to mark out minimal loop(s) from intersection oftwo non-minimal loops... seems it will be an interesting t-sql job. |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-10-01 : 11:02:54
|
Damn...now my head hurts...How about a brief (and really simple...use small words please) description as to what this is doing...and why..Practical applications?Brett8-)SELECT @@POST FROM Brain ORDER BY NewId()That's correct! It's an AlphaNumeric! |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2003-10-01 : 12:27:14
|
I think he's working up to Hopcroft & Tarjan's graph planarity test in T-SQL (I never understood how that one worked). |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-01 : 15:17:37
|
LOL, guys... for the last 40-50 minutes I was trying to 'concoct'an understandable description of what's going on in the code andfailed. Just because my English grows quite messy by the night (only?I really envy Owais' English :))Practical applications? No!! I can't even think up any appliance for that.BTW, my test graph is made of two 'main' triangles (the second one iscommented) and these triangles are joined to each other by twoedges: (2, 13) and (3, 13). If you remove one of these edges thenyou get two sub-graphs (I don't know right math's terms) joinedby one 'bad' edge which is not owned by any loop. And my codeneatly recognizes such 'bad' edges, otherwise it would get stuckin deadlock state... omg I'm going crazy on this stuff... |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-10-01 : 15:20:13
|
Stoad....Dude...You need to go down to a local bar and talk to the first woman you find...Brett8-)SELECT @@POST FROM Brain ORDER BY NewId()That's correct! It's an AlphaNumeric! |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-01 : 16:31:03
|
quote: and talk to the first woman you find...
Talk about cyclic paths in the graph???Hm... Seems this kind of talking will cost me a sum,plus, it may turn out to be a quite dangerous conversation... LOL |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-10-01 : 20:38:42
|
You never know, you might bump into a hot mathematician babe. |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-02 : 07:15:16
|
LOL, Rob,two wet blankets in one barroom... it's too much :)Now back to our loops. Suppose we got the following two loops(along with many other ones):(1, 2)(2, 3)(3, 4)(4, 5)(5, 6)(6, 7)(7, 1)and(2, 3)(3, 4)(4, 5)(5, 2)'Subtracting' lesser-length loop from another loop we get some newchain of edges. And, if length of this chain is lesser than the lengthof the bigger loop, then, obviously, this new chain is a loop as well.And, if the length of this new loop is lesser than the length of thebigger loop, then we can safely delete the bigger old loop from thetable of loops (g3l) and replace it with the new smaller loop.It's just our sample case:Loop1 - Loop2 = (1, 2)(2, 5)(5, 6)(6, 7)(7, 1)Processing in this manner, in WHILE cycle, all possible combinationsof two loops, in the long run we get only minimal loops of the graph.This is my basic idea, not implemented so far. |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-10-02 : 14:58:03
|
WOW, Vit, you really flatter me . I go away for a week and this is what I come back to, people pulling my leg...I think my English is pretty mediocre, if you really want to envy someone, it should be Rob, Jeff or Sam Btw, I really can't comprehend what you've done here (I am a business grad), but it sure looks complicated, must be one of those astronomy things!Owais Make it idiot proof and someone will make a better idiot |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-02 : 15:19:08
|
OMG, Owais,I can't tell your written English from rrb's poetic English.Secondly, seems I should draw this unhappy graph on a sheet ofpaper, then scan it and post here the picture :) |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-10-03 : 07:37:29
|
I dont think rrb's gonna be very happy about that comparision... Make it idiot proof and someone will make a better idiot |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-03 : 07:51:54
|
LOL, Owais, here a picture for you. Does it look like night starry sky?This is a sample graph with 12 vertexes marked by numbers from 1 to 12.It has 18 following edges:(4, 7) (7, 1) (1, 10) (10, 2) (2, 9) (9, 4) (4, 6) (7, 8) (1, 12)(5, 12) (2, 11) (9, 3) (10, 12) (5, 8) (5, 11) (3, 11) (3, 6) (6, 8)You can easily see that the graph has 7 minimal cyclic paths in it.For example, the loop going along sides of the inner pentagon:(8, 5) (5, 11) (11, 3) (3, 6) (6, 8)And we have to find all these 7 loops of this graph. |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-03 : 09:38:42
|
INCREDIBLE!!Honestly speaking, up to the last moment I didn't believe that it can be done.My new code finds all minimal (only) loops + (as extra bonus) the loop alongedges of the outer border of a graph. BTW, seems it can be also used forchecking of planarity of a graph (cheers, Arnold!), because only for planargraphs there is the simple equation:(Number of Loops) = (Number of Edges) - (Number of Vertexes) + 1Here the result of the new code below for the sample graph on the picture:nl ce----------- ---------------------------------1 (1 10)(10 12)(12 1)2 (2 9)(9 3)(3 11)(11 2)3 (3 6)(6 4)(4 9)(9 3)4 (4 7)(7 8)(8 6)(6 4)5 (5 8)(8 7)(7 1)(1 12)(12 5)6 (5 11)(11 2)(2 10)(10 12)(12 5)7 (4 7)(7 1)(1 10)(10 2)(2 9)(9 4)8 (6 8)(8 5)(5 11)(11 3)(3 6)---------------------------------------------if object_id('g3')>0 drop table g3if object_id('g3x')>0 drop table g3xif object_id('g3y')>0 drop table g3yif object_id('g3l')>0 drop table g3lGOcreate table g3y(n int, v1 int, v2 int)GOcreate table g3x(n int, ne int, v1 int, v2 int)GOcreate table g3l(nl int, ne int, v1 int, v2 int, ce varchar(80))GOcreate table g3(ne int, v1 int, v2 int)GOinsert into g3 (v1, v2)select 4, 7 union allselect 7, 1 union allselect 1, 10 union allselect 10, 2 union allselect 2, 9 union allselect 9, 4 union allselect 4, 6 union allselect 7, 8 union allselect 6, 8 union allselect 1, 12 union allselect 5, 12 union allselect 2, 11 union allselect 9, 3 union allselect 10, 12 union allselect 5, 8 union allselect 5, 11 union allselect 3, 11 union allselect 3, 6GOupdate g3 set ne=(select count(*) from g3 gwhere (g.v1=g3.v1 and g.v2<=g3.v2) or g.v1<g3.v1)insert into g3 select ne, v2, v1 from g3declare @i int, @n int, @nemax int, @ne int, @ns int,@v1 int, @v2 int, @ce varchar(80), @f intset @nemax=(select max(ne) from g3)set @ns=2 set @i=1while 0=0beginset @f=0 set @ns=@ns+1 set @ne=0while @ne<@nemaxbeginset @ne=@ne+1select @v1=v1, @v2=v2 from g3 where ne=@ne and v1<v2 andnot exists (select 0 from g3l where ne=@ne)if @@rowcount>0beginset @f=1 set @n=1truncate table g3x truncate table g3yinsert into g3x select @n, @ne, @v1, @v2while 0=0beginset @n=@n+1 insert into g3x select top 1 @n, ne, v1, v2 from g3 where v2 not in (select v1 from g3x union all select v2 from g3x) and v1=(select top 1 v2 from g3x order by n desc) and not exists (select 0 from g3y where g3y.v1=g3.v1 and g3y.v2=g3.v2) if @@rowcount=0 if @n>2 begin insert into g3y select n, v1, v2 from g3x where n=@n-1 delete from g3x where n=@n-1 delete from g3y where n>@n-1 set @n=@n-2 end else break else if @n=@ns-1 begin insert into g3x select top 1 @n, ne, v1, v2 from g3 where v2=@v1 and v1<>@v2 and v1=(select top 1 v2 from g3x order by n desc) if @@rowcount=0 begin insert into g3y select n, v1, v2 from g3x where n=@n delete from g3x where n=@n set @n=@n-1 end else begin insert into g3l select @i, ne, v1, v2, '' from g3x set @i=@i+1 break end endendend endif @f=0 breakenddelete from g3 where ne in(select ne from g3l group by ne having count(*)>1)while 0=0beginset @n=1 truncate table g3xinsert into g3x select top 1 @n, ne, v1, v2 from g3 where v1<v2if @@rowcount=0 breakdelete from g3 where ne=(select ne from g3x)while 0=0beginset @n=@n+1 insert into g3x select top 1 @n, ne, v1, v2 from g3 where v1=(select v2 from g3x where n=@n-1) delete from g3 where ne=(select ne from g3x where n=@n)set @f=0set @f=(select n from g3x where v1=(select v2 from g3x where n=@n)) if @f>0 begin insert into g3l select @i, ne, v1, v2, '' from g3x where n>=@f set @i=@i+1 delete from g3x where n>=@f set @n=@f-1 if @n=0 break endendendset @n=0while @n<(select max(nl) from g3l)beginset @ce='' set @n=@n+1select@ce=@ce+'('+cast(v1 as varchar(8))+' '+cast(v2 as varchar(8))+')'from g3l where nl=@nupdate g3l set ce=@ce where nl=@nendselect distinct nl, ce from g3l order by nl |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-10-06 : 00:44:47
|
Cheers Stoad, you are now a real scientist!!When was the last time you were on a date? wais Make it idiot proof and someone will make a better idiot |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-06 : 02:54:27
|
LOL, Owais,... on a date? Guess it was (if any :)) somewhere in the last century... |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-10-14 : 11:29:15
|
Have you seen this site...not only do the get in to some heavy duty math..it's all sql server babyhttp://skyserver.sdss.org/en/skyserver/EDIT AND they give you the ability through the browswer to query their database...You can even get at the catalog...or used to...Brett8-)EDIT: It took awhile to find it...but here it is...http://skyserver.sdss.org/en/tools/search/sql.aspThe put a timelimit on queries so you don't hose the box....They say it 818 gb with billions of rows...Now that's scaling up...or is it out? |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-14 : 16:18:20
|
LOLBrett,you scared me to my guts (?)... astronomy... maths... graphs... quazars.Ugh... All these nice sites end up with Buy this excellent book, then Buy thatstill better superb!! book... In short, I'm in the 451° F mood.Today I googled 'algorithms' mistakingly typing 'algorythms' instead and it wasa narrow escape from the mob of dirty punks... guess I'm lucky having picturesturned off in my browser. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-12 : 09:02:54
|
I have found a use or this (under the cirumstance that the paths are one-way only).Assume you are responsible for an online apartment swap site. Every user has an apartment that he wants to get rif of by swapping to another (can be same city or another city).Then you can have a button on the web page named "Get me all apartments that are suitable for me".First you get all direct swaps (vertexes = 2).The you can get all swaps with vertexes = 3.For example:You have an apartment in New York, but want to move to Los Angeles. You find an apartment in LA, but that user want to move to Miami. Luckily, there is a third user who has an apartment in Miami who wants to move to New York.There you have a 3rd degree swap!Peter LarssonHelsingborg, Sweden |
|
|
|
|
|
|
|