Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breadth first search query in MySQL?

Tags:

sql

mysql

graph

I want to build a mySQL query, which returns all nodes in a graph in x depth from a given node. The depth will be only 2-4.

The table structure is (neighborIDs can contain multiple values):

Id  Name  Desc  neighborIDs

So the task is basically a Breadth-first search in mySQL. I have found a way to do it in T-SQL, is this possible in mySQL? Is a single SQL query better, than writing a PHP function, that runs a simple SELECT on every neighbour of a node (so basically making tons of simple queries)?

Thanks for help


A try:

SELECT  root.ID,
        d1.ID,
        d2.ID
FROM    Locations root
        LEFT JOIN Locations d1 ON
          root.neighborIDs LIKE CONCAT('%',d1.id,'%')
        LEFT JOIN Locations d2 ON
          d1.neighborIDs LIKE CONCAT('%',d2.id,'%')
WHERE root.id = 1  # i guess this defines the starting node for the search..

An example table is:

id   name   desc                   neighborIDs  
1    id1    --     
2    id2    ---        
3    id3    neighborours are 1,2   1,2  
4    id4    neighbour is 3         3
10   id10   neigh is 4             4

If i run the query with the input id=1, it should return the row with id=3 if the BFS goes 1 level deep.


Another try:

SELECT id,neighborIDs
FROM locations
WHERE id = 3
OR
neighborIDs LIKE '%3%'
OR (SELECT neighborIDs FROM locations WHERE id = 3) LIKE CONCAT('%',id,'%')

This query selects the neighbors of the node with id = 3.

like image 402
sydd Avatar asked Mar 18 '11 19:03

sydd


2 Answers

step 0: Create a view that shows all neighbour pairs

CREATE VIEW neighbour AS
( SELECT loc1.id AS a
       , loc2.id AS b
  FROM locations loc1
     , locations loc2
  WHERE FIND_IN_SET(loc1.id, loc2.neighbours)>0
     OR FIND_IN_SET(loc2.id, loc1.neighbours)>0
) ;

step 1: Find neighbours of depth 1

SELECT b AS depth1
FROM neighbour
WHERE a = 1;               <-- for root with id=1

step 2: Find neighbours of depth 2

SELECT DISTINCT d2.b AS depth2
FROM neighbour d1
  JOIN neighbour d2
    ON d1.b = d2.a
      AND d2.b != 1
WHERE d1.a = 1                <-- for root with id=1
  AND d2.b NOT IN
     ( SELECT b AS depth1     <- depth1 subquery
       FROM neighbour
       WHERE a = 1            <-- for root with id=1
      )
;

step 3: Find neighbours of depth 3

SELECT d3.b as depth3
FROM neighbour d1
  JOIN neighbour d2
    ON d1.b = d2.a
    AND d2.b != 1
    AND d2.b NOT IN
       ( SELECT b as depth1
         FROM neighbour
         WHERE a = 1
       )
  JOIN neighbour d3
    ON d2.b = d3.a
    AND d3.b != 1
WHERE d1.a = 1
  AND d3.b NOT IN
     ( SELECT b as depth1
       FROM neighbour
       WHERE a = 1
      )
  AND d3.b NOT IN
     ( SELECT d2.b AS depth2
       FROM neighbour d1
         JOIN neighbour d2
           ON d1.b = d2.a
           AND d2.b != 1
       WHERE d1.a = 1
         AND d2.b NOT IN
            ( SELECT b AS depth1
              FROM neighbour
              WHERE a = 1
            )
     )
;

As you can see, the growth is exponential for the number of query lines, so I won't try the level 4.

like image 162
ypercubeᵀᴹ Avatar answered Nov 05 '22 11:11

ypercubeᵀᴹ


As mentioned in my comment, you've made your life difficult. But something similar to the following will produce a list of neighbour IDs at each depth. Depending on your exact needs, the result set can be used a subquery and manipulated further to necessary (such as retrieving the names of the neighbours).

SELECT  root.ID,
        d1.ID,
        d2.ID
FROM    Locations root
        LEFT JOIN Locations d1 ON
          root.Neighbours LIKE '%'+CAST(d1.ID as varchar)+'%'  --Or equivalent mysql pattern matching function
        LEFT JOIN Locations d2 ON
          d1.Neighbours LIKE '%'+CAST(d2.ID as varchar)+'%'

EDIT: Changed INNER JOIN to LEFT JOIN

like image 2
Disillusioned Avatar answered Nov 05 '22 11:11

Disillusioned