Code Monkey home page Code Monkey logo

graph's Introduction

#graph

Simple library for graph manipulations.

I need some graph-related operations for other project, so I decided to move them to separate module.

It is not intended to be full-featured graph manupulation (at least for now) but pull requests are welcome.

It will develop as my requirements will get more sophisticated.

Don't use it for large graphs β€”Β it uses array's .filter() methods to scan for nodes and edges, so it could be slow on large structures.

Installation

npm install node-graph

Usage

###new Graph(structure)

Returns graph instance.

var Graph = require('node-graph');

var structure = {
    nodes: [
        {
            name: 'A'
        },
        {
            name: 'B',
            youCanPutArbitraryDataOnYourNodes: { ... }
        }
    ],
    edges: [
        {
            name: 'A->B',
            from: 'A',
            to: 'B',
            youCanPutArbitraryDataOnYourEdgesToo: { ... }
        }
    ]
}

var gr = new Graph(structure);

Constructor checks that argument conforms to graph JSON schema (locates in schema/graph.json).

tl;dr It requires name field for nodes and name, from, to fields for edges. Also it checks for name uniqueness amongst edges and nodes respectively. Then is checks that every edge points to existing nodes (from and to fields containing valid node names).

###outboundEdges(node)

Returns array of edges that came out of specified node:

// You can pick outbound edges by node name
var outboundEdges = gr.outboundEdges('B');

// Or providing node object
var node = gr.getNode('B');
var outboundEdges2 = gr.outboundEdges(node);

Result example:

[{"name":"BD","from":"B","to":"D"}]

###inboundEdges(node)

Returns array of edges that came in to specified node:

// You can pick outbound edges by node name
var inboundEdges = gr.inboundEdges('D');

// Or providing node object
var node = gr.getNode('D');
var inboundEdges2 = gr.inboundEdges(node);

Result example:

[
    {"name":"BD","from":"B","to":"D"},
    {"name":"CD","from":"C","to":"D"}
]

###isTerminalNode(node)

If there are no outbound edges from specified node, returns true, false otherwise.

    // using node object
    var node = gr.getNode('D');
    gr.isTerminalNode(node);
    
    // using node name
    gr.isTerminalNode('D');

##getNode(name)

Returns node object by its name.

##getEdge(name)

Returns edge object by its name.

##License

MIT

graph's People

Contributors

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