Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

representing graph using relational database

Tags:

database

graph

I need to represent graph information with relational database.

Let's say, a is connected to b, c, and d.

a -- b
|_ c
|_ d

I can have a node table for a, b, c, and d, and I can also have a link table (FROM, TO) -> (a,b), (a,c), (a,d). For other implementation there might be a way to store the link info as (a,b,c,d), but the number of elements in the table is variable.

  • Q1 : Is there a way to represent variable elements in a table?
  • Q2 : Is there any general way to represent the graph structure using relational database?
like image 875
prosseek Avatar asked Jun 03 '10 18:06

prosseek


People also ask

How is graph database different from relational database?

The main difference between these two types of databases is in the way relationships between entities are stored. In a graph database, relationships are stored at the individual record level, while a relational database uses predefined structures, a.k.a. table definitions.

When should someone use a graph database over a relational database?

The relational focus is between the columns of data tables, not data points. Both databases make adding new data easy. The flexibility of a graph database enables the ability to add new nodes and relationships between nodes, making it reliable for real-time data.

How are graphs stored in database?

In a graph database, there are no JOINs or lookups. Relationships are stored natively alongside the data elements (the nodes) in a much more flexible format. Everything about the system is optimized for traversing through data quickly; millions of connections per second, per core.


2 Answers

Q1 : Is there a way to represent variable elements in a [database] table?

I assume you mean something like this?

 from | to_1 | to_2 | to_3 | to_4 | to_5 | etc...
 1    | 2    | 3    | 4    | NULL | NULL | etc...

This is not a good idea. It violates first normal form.

Q2 : Is there any general way to represent the graph structure using database?

For a directed graph you can use a table edges with two columns:

nodeid_from nodeid_to
1           2
1           3
1           4

If there is any extra information about each node (such as a node name) this can be stored in another table nodes.

If your graph is undirected you have two choices:

  • store both directions (i.e. store 1->2 and 2->1)
  • use a constraint that nodeid_from must be less than nodeid_to (i.e. store 1->2 but 2->1 is implied).

The former requires twice the storage space but can make querying easier and faster.

like image 126
Mark Byers Avatar answered Oct 12 '22 23:10

Mark Byers


In addition to the two tables route mentioned by Mark take a look at the following link:

http://articles.sitepoint.com/article/hierarchical-data-database/2

This article basically preorders the elements in the tree assigning left and right values. You are then able to select portions or all of the tree using a single select statement.

Node | lft | rght
-----------------
  A  |  0  |  7
  B  |  1  |  2
  C  |  3  |  4
  D  |  5  |  6

EDIT: If you are going to be updating the tree heavily this is not an optimum solution as the whole tree must be re-numbered

like image 22
gregwhitaker Avatar answered Oct 12 '22 21:10

gregwhitaker