sql - Storing data about undirected graph edges in a database -


i have data structure undirected graph (probably dense) nodes , edges. edge between nodes , j have additional data associated , want able uniquely identify edge when querying and able determine if edge between , j exists.

i have decided accomplish using 2 tables:

table nodes ----------- node_id pk ... (additional fields)   table edges ----------- nodes_hash(node_id, node_id) pk edge_thickness ... (additional fields) 

where primary key every edge computed hash function nodes_hash(node_id, node_id) taking 2 node ids.

my questions:

  • how come hash function compute edge ids?
  • any major drawbacks approach may overlooking?

there's no reason why should need encode edges hashes: ensuring there no duplicates simple matter of unique constraint.

while there ways of calculating hashes (creating path enumeration string, , calculating md5 hash or along lines), there's no value in storing hash , not use path enumeration scheme storing graph data itself. path enumeration works in filesystem, storing /a/b/c (if enumerating node id) or 1.2.1.5 (if enumerating edge ordinal).

for specific usecase use common adjacency list table setup (unless other operations in tree call more specialized data structures such node sets or path/edge enumeration). in structure, top-level node has parent_id = null:

create table nodes(   node_id int not null,   -- other node attributes   primary key (node_id) );  create table edges(   node_id int not null,   parent_id int,   edge_weight int not null,   -- other edge attributes   foreign key (node_id) references nodes(node_id),    foreign key (parent_id) references nodes(node_id),   check (parent_id <> node_id),                   -- avoids simple cycles   constraint uniq_edge unique (node_id,parent_id) -- avoids duplicate edges ); 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -