If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Graph Routing - A real challenge

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-26-05, 08:42
GMMorris GMMorris is offline
Registered User
 
Join Date: Sep 2005
Posts: 2
Graph Routing - A real challenge

Hi,

I tried to do this my self but I'm not a DBA and am new to MySQL (I work with Oracle mostly, and have a team of DBAs who do this stuff for me, but this job is for a different company so I cant use my team) so I thought I'd ask you guys for an idea on how I can do this:

I have two tables, defining a graph:

t_Items and t_Relations.

t_Items is basically a list of items, identified by a unique ID.

|ID | Name
---------------
| 0 | item 0
---------------
| 1 | item 1
---------------
| 2 | item 2
---------------
| 3 | item 3
---------------

t_Relations defines relations between two items.

| FirstID | SecondID
------------------------
| 0 | 1
------------------------
| 1 | 3
------------------------
| 3 | 5
------------------------

This means I have a graph of nodes with relations between them (in both directions).

What I need is a stored procedure which selects all the "Routes" from one node to another and logs them in another table: t_Routes.

The way it would be used is - I would call a stored procedure (I'm using MySQL 5) giving it two Node IDs as parameters.
Then I'd be able to select all the rows from t_routes for these two IDs and I'd have a row for each route.

How can I do this? I racked my head for several days and couldnt come to a solution which worked. I heard there are some briliant minds (DBAs ) here so I hope you dont let me down!!

Thanks for any help or advice you may be able to give!

Gidi
Reply With Quote
  #2 (permalink)  
Old 09-26-05, 12:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by GMMorris
I heard there are some briliant minds (DBAs ) here
the set of brilliant minds here and the set of DBAs here are not necessarily congruent sets

i'm neither of the above, and i can't help you

i just remember from one of my college math courses that the number of different paths increases astronomically as the number of nodes increases, and that the problem of traversing all possible paths is non-trivial
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-26-05, 12:44
marist89 marist89 is offline
Registered User
 
Join Date: Oct 2002
Location: greenwich.ct.us
Posts: 279
If this were ORACLE, you could be done in about 30 seconds with a CONNECT BY. However, it's not. You are looking at using recursion (in the client on 4.x, maybe with a stored procedure in 5) to traverse your graph.
__________________
Jeff Hunter
http://marist89.*************
Reply With Quote
  #4 (permalink)  
Old 09-26-05, 12:46
marist89 marist89 is offline
Registered User
 
Join Date: Oct 2002
Location: greenwich.ct.us
Posts: 279
__________________
Jeff Hunter
http://marist89.*************
Reply With Quote
  #5 (permalink)  
Old 09-26-05, 13:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
jeff, the only problem is, this isn't hierarchical data
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 09-26-05, 13:27
marist89 marist89 is offline
Registered User
 
Join Date: Oct 2002
Location: greenwich.ct.us
Posts: 279
same basic concept.
__________________
Jeff Hunter
http://marist89.*************
Reply With Quote
  #7 (permalink)  
Old 09-26-05, 14:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
no, it isn't

how many ways can you get from 1 to 5 in this graph?
Attached Images
File Type: gif graph.gif (1.1 KB, 49 views)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 09-27-05, 02:27
GMMorris GMMorris is offline
Registered User
 
Join Date: Sep 2005
Posts: 2
Yea, I know all the things you guys said, any solutions in mind?

As far as the actualy code goes- I'd rather it be in an SP rather than the software, to save my self SELECTing all the nodes...

As far as the Number of routes goes- I dont really need all- just a few of the shortest, lets say 3-5 routes. Each relation has a Weight and I'd need the routes with the heighest weight... (the # of nodes should come into play too, actually. meaning if I have two routesd one has a weight of 10 and 5 nodes, if may still come second to a routes with a weight of 5 with 3 nodes in it.)

Any ideas how I can solve this problem? It has to be MySQL, and it isn't heirarchial so the above link is of no use (I've read it before, but I appreciate the post anyway ).

Edit:: My real problem is that I cant figure out a way to prevent my recursion from going through the same node twice. Since the relations between the nodes are bi-directional it is very likely that my loops will rech nodes that they have already visited, and I can't allow that. I need each node to be visited only once.

Last edited by GMMorris; 09-27-05 at 03:28.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On