All Projects → anvaka → Ngraph.graph

anvaka / Ngraph.graph

Licence: bsd-3-clause
Graph data structure in JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Ngraph.graph

Urbanaccess
A tool for GTFS transit and OSM pedestrian network accessibility analysis
Stars: ✭ 137 (-53.56%)
Mutual labels:  graph, network
Libgrape Lite
🍇 A C++ library for parallel graph processing 🍇
Stars: ✭ 169 (-42.71%)
Mutual labels:  graph, graph-algorithms
Data Structures
Common data structures and algorithms implemented in JavaScript
Stars: ✭ 139 (-52.88%)
Mutual labels:  graph, graph-algorithms
Workbase
Grakn Workbase (Knowledge IDE)
Stars: ✭ 106 (-64.07%)
Mutual labels:  graph, network
Grakn
TypeDB: a strongly-typed database
Stars: ✭ 2,947 (+898.98%)
Mutual labels:  graph, graph-algorithms
Ogre
Clojure library for querying Apache TinkerPop graphs
Stars: ✭ 118 (-60%)
Mutual labels:  graph, graph-algorithms
Hgp Sl
Hierarchical Graph Pooling with Structure Learning
Stars: ✭ 159 (-46.1%)
Mutual labels:  graph, graph-algorithms
Serial Studio
Multi-purpose serial data visualization & processing program
Stars: ✭ 1,168 (+295.93%)
Mutual labels:  graph, network
Yfiles For Html Demos
Contains demo sources for the JavaScript diagramming library yFiles for HTML
Stars: ✭ 202 (-31.53%)
Mutual labels:  graph, graph-algorithms
Quiver
A reasonable library for modeling multi-graphs in Scala
Stars: ✭ 195 (-33.9%)
Mutual labels:  graph, graph-algorithms
Rgl
RGL is a framework for graph data structures and algorithms in Ruby.
Stars: ✭ 279 (-5.42%)
Mutual labels:  graph, graph-algorithms
Graph Data Science
Source code for the Neo4j Graph Data Science library of graph algorithms.
Stars: ✭ 251 (-14.92%)
Mutual labels:  graph, graph-algorithms
Verse
Reference implementation of the paper VERSE: Versatile Graph Embeddings from Similarity Measures
Stars: ✭ 98 (-66.78%)
Mutual labels:  graph, graph-algorithms
Reddit Detective
Play detective on Reddit: Discover political disinformation campaigns, secret influencers and more
Stars: ✭ 129 (-56.27%)
Mutual labels:  graph, network
Deepwalk C
DeepWalk implementation in C++
Stars: ✭ 88 (-70.17%)
Mutual labels:  graph, graph-algorithms
Sparkling Graph
SparklingGraph provides easy to use set of features that will give you ability to proces large scala graphs using Spark and GraphX.
Stars: ✭ 139 (-52.88%)
Mutual labels:  graph, graph-algorithms
G6
♾ A Graph Visualization Framework in JavaScript
Stars: ✭ 8,490 (+2777.97%)
Mutual labels:  graph, network
Pyrwr
Python Implementation for Random Walk with Restart (RWR)
Stars: ✭ 48 (-83.73%)
Mutual labels:  graph, network
Programming Languages Influence
Code to retrieve data for the programming languages influence visualizations from Freebase
Stars: ✭ 171 (-42.03%)
Mutual labels:  graph, network
P2p Graph
Real-time P2P network visualization with D3
Stars: ✭ 245 (-16.95%)
Mutual labels:  graph, network

ngraph.graph

Graph data structure for javascript. This library belongs to a family of javascript graph packages called ngraph.

build status

Install

With npm do:

npm install ngraph.graph

Or download from CDN:

<script src='https://unpkg.com/[email protected]/dist/ngraph.graph.min.js'></script>

If you download from CDN the library will be available under createGraph global name.

Creating a graph

Create a graph with no edges and no nodes:

var createGraph = require('ngraph.graph');
var g = createGraph();

Growing a graph

The graph g can be grown in two ways. You can add one node at a time:

g.addNode('hello');
g.addNode('world');

Now graph g contains two nodes: hello and world. You can also use addLink() method to grow a graph. Calling this method with nodes which are not present in the graph creates them:

g.addLink('space', 'bar'); // now graph 'g' has two new nodes: 'space' and 'bar'

If nodes already present in the graph 'addLink()' makes them connected:

// Only a link between 'hello' and 'world' is created. No new nodes.
g.addLink('hello', 'world');

What to use as nodes and edges?

The most common and convenient choices are numbers and strings. You can associate arbitrary data with node via optional second argument of addNode() method:

// Node 'world' is associated with a string object 'custom data'
g.addNode('world', 'custom data');

// You can associate arbitrary objects with node:
g.addNode('server', {
  status: 'on',
  ip: '127.0.0.1'
});

// to get data back use `data` property of node:
var server = g.getNode('server');
console.log(server.data); // prints associated object

You can also associate arbitrary object with a link using third optional argument of addLink() method:

// A link between nodes '1' and '2' is now associated with object 'x'
g.addLink(1, 2, x);

Enumerating nodes and links

After you created a graph one of the most common things to do is to enumerate its nodes/links to perform an operation.

g.forEachNode(function(node){
    console.log(node.id, node.data);
});

The function takes callback which accepts current node. Node object may contain internal information. node.id and node.data represent parameters passed to the g.addNode(id, data) method and they are guaranteed to be present in future versions of the library.

To enumerate all links in the graph use forEachLink() method:

g.forEachLink(function(link) {
    console.dir(link);
});

To enumerate all links for a specific node use forEachLinkedNode() method:

g.forEachLinkedNode('hello', function(linkedNode, link){
    console.log("Connected node: ", linkedNode.id, linkedNode.data);
    console.dir(link); // link object itself
});

This method always enumerates both inbound and outbound links. If you want to get only outbound links, pass third optional argument:

g.forEachLinkedNode('hello',
    function(linkedNode, link) { /* ... */ },
    true // enumerate only outbound links
  );

To get a particular node object use getNode() method. E.g.:

var world = g.getNode('world'); // returns 'world' node
console.log(world.id, world.data);

To get a particular link object use getLink() method:

var helloWorldLink = g.getLink('hello', 'world'); // returns a link from 'hello' to 'world'
console.log(helloWorldLink);

To remove a node or a link from a graph use removeNode() or removeLink() correspondingly:

g.removeNode('space');
// Removing link is a bit harder, since method requires actual link object:
g.forEachLinkedNode('hello', function(linkedNode, link){
  g.removeLink(link);
});

You can also remove all nodes and links by calling

g.clear();

Listening to Events

Whenever someone changes your graph you can listen to notifications:

g.on('changed', function(changes) {
  console.dir(changes); // prints array of change records
});

g.add(42); // this will trigger 'changed event'

Each change record holds information:

ChangeRecord = {
  changeType: add|remove|update - describes type of this change
  node: - only present when this record reflects a node change, represents actual node
  link: - only present when this record reflects a link change, represents actual link
}

Sometimes it is desirable to react only on bulk changes. ngraph.graph supports this via beginUpdate()/endUpdate() methods:

g.beginUpdate();
for(var i = 0; i < 100; ++i) {
  g.addLink(i, i + 1); // no events are triggered here
}
g.endUpdate(); // this triggers all listeners of 'changed' event

If you want to stop listen to events use off() method:

g.off('changed', yourHandler); // no longer interested in changes from graph

For more information about events, please follow to ngraph.events

License

BSD 3-clause

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].