Giter Site home page Giter Site logo

digraph-demo's Introduction

DAG (Directed Acyclic Graph) - Demo

This project demonstrates the implementation of a directed and acyclic graph structure. It is essentially an improvement over the encoding, queries and some technical issues as present in the act-as-dag gem.

Read the article Encoding and Querying Graphs in the Relational Model for more information.

Scalability

The inner workings of this implementation are such that it won't fall on your feet for large and even huge graphs. It essentially (unlike the act-as-dag gem) scales very well. I would recommend to replace some queries with CTEs, and use db-triggers for maximum performance on really huge networks. Read the article for a start.

Extending to General Graphs

The acyclic structure, and other graph properties are enforced by hooks in the app/model/arc.rb. The implementation is flexible and any of the constraints can be disabled at will:

before_save :prevent_back_loop
before_save :prevent_cycle
before_save :prevent_multi_arc
before_save :prevent_self_loop

Usage By Example

Node.create

Node.first.successors << Node.create

Node.first.successors

Node.first.graph_descendants

Node.last.graph_ancestors

Generator Tasks

There are a few generators to create graphs to explore or play with.

  1. rake generator:twostar N=10, creates two connected stars in a particular manner, see the corresponding figure in the article.

  2. rake generator:cycle N=3, tries to create a cycle, results in a chain if before_save :prevent_cycle is not disabled

  3. rake generator:er N=100 M=500, creates an Erdős–Rényi like random network

N is the target number of nodes, and likewise M the target number of arcs (if applicable). Defaults are in place.

Notation

The customary descendants and ancestors terms as used in graph theory clash with existing ruby methods. They are replaced by graph_descendants and graph_ancestors respectively.

digraph-demo's People

Contributors

drtom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.