Git Product home page Git Product logo

sqlite3-bfsvtab-ext's Introduction

bfsvtab: A virtual table extension for breadth-first search queries in Sqlite3

Automated Testing

This extension allows Sqlite3 to perform breadth-first search queries against graph data from any table or view in an existing database.

Can't you do this with recursive common table expressions (RCTEs)?

Yes, but in practice RCTEs are very slow for non-tree graphs.

How does it work?

This extension defines a virtual table called bfsvtab.

The virtual table requires 4 SQL constraints to be set for all queries:

  • tablename: The name of the table or view which contains the graph edges (can be any table or view in the database).
  • fromcolumn: The node id column where an edge starts from (must be integer).
  • tocolumn: The node id column where an edge goes to (must be integer).
  • root: The root node id of the breadth-first traversal.

The virtual table also provides the following columns that can be returned or used as contraints:

  • id: The id of the current node being visited.
  • distance: The shortest distance to the current node from the root node.
  • parent: The id of the parent node to the current node in the spanning tree rooted at the root node.
  • shortest_path: Slash delimited string containing the shortest path from the root to the given node.

Check out the examples below for more details.

Build From Source

$ git clone [email protected]:abetlen/sqlite3-bfsvtab-ext.git
$ cd sqlite3-bfsvtab-ext
$ make
$ make test

Basic Examples

/* Load the compiled extension from the current directory */
.load ./bfsvtab

/* Insert for the following examples */
create table edges(fromNode integer, toNode integer);
insert into edges(fromNode, toNode) values
  (1, 2),
  (1, 3),
  (2, 4),
  (3, 4);

/* Find the minimum distance from node 1 to node 4 */
select 
  id, distance 
  from bfsvtab 
  where 
    tablename  = 'edges'    and
    fromcolumn = 'fromNode' and
    tocolumn   = 'toNode'   and
    root       = 1          and
    id         = 4;

/* Find the shortest path from node 1 to node 4 */
select 
  shortest_path
  from bfsvtab 
  where 
    tablename  = 'edges'    and
    fromcolumn = 'fromNode' and
    tocolumn   = 'toNode'   and
    root       = 1          and
    id         = 4;

/* Find the minimum distance from node 1 to all nodes */
select 
  id, distance
  from bfsvtab 
  where 
    tablename  = 'edges'    and
    fromcolumn = 'fromNode' and
    tocolumn   = 'toNode'   and
    root       = 1;

/* Return the edge set of a spanning tree rooted at node 1 */
select 
  parent, id 
  from bfsvtab 
  where 
    tablename  = 'edges'    and
    fromcolumn = 'fromNode' and
    tocolumn   = 'toNode'   and
    root       = 1          and
    parent     is not null;

sqlite3-bfsvtab-ext's People

Contributors

abetlen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.