Immutable graph data structures
Install
npm install -S giraph
bower install -S giraph
Usage
require('giraph')()
.add('a', { my: 'data' })
.add('b')
.add('c')
.connect('a', 'b', 3)
.connect('a', 'c')Giraph comes with a number of different graph types:
Exported as a factory function on the root namespace:
Options and defaults
{
immutable: true
}require('giraph')() // => empty graphThe undirected graph has the following members
Hashes vertices by id.
Instance options
The number of vertices
Returns a new instance with Vertex(id[, data]) added to it.
Gets the Vertex at id.
Returns a new instance with id removed.
Returns a new instance with a and b connected.
Returns a boolean indicating whether or not id is in the graph.
Note: if passing in a String, is equivalent to checking id in graph.map.
Returns a new instance with all other graphs merged into the current.
Note: merging takes Left-to-right precedence. If a and b both contain the same node, c, but have different data attached, b will take precedence.
Example:
var g1 = giraph()
.add('a')
.add('b', { some: 'thing' })
.connect('a', 'b', 1);
var g2 = giraph()
.add('b', { some: 'data' })
.add('c')
.conect('b', 'c');
// a->b->c
var g3 = g1.merge( g2 );
// { some: 'data' }
console.log( g3.get('b').data );Iterates through each vertex. The iterator argument has the following signature:
function( Vertex v, Number i, Graph g )
Example:
require('giraph')
.add('a', { some: 'data' }).add('b').add('c')
.each( function( vertex, i, graph ){
console.log( vertex.id, vertex.data, i );
});
// a { some: 'data' }
// b null
// c nullReturns this
Iterates through the graph, producing a single value. iterator has the following signature
function( Mixed currentValue, Vertex V, Number i, Graph g )
Returns Value of reduction
Returns this instance or a clone() depending on options.immutable.
Returns an array of edges with the following structure:
{ vertices: ['a', 'b'], weight: 10 }Optionally, pass a vertex ID to only get edges touching that vertex.
Returns a new instance of the graph
Returns The total weight of all edges in the graph
Allows mutation to occur on an immutable graph for a single turn of the event loop.
Returns the original instance
require('giraph')()
.mutate(function( g ){
// Batch operations here
g.add('a').add('b').add('c');
})Returns a Minimum Spanning Tree represented as a Graph.
TODO
TODO
A vertex is essentially an Identifier with data attached and connections to other vertices.
It's available under .vertex( id[, data[, options]]).
ID of the vertex
Optional attached data
Optional options:
{
immutable: true
}Returns a new instance of the vertex
- Topological Sorting
- Cliques and maximum cliques
- Planarism
- Creating sub-graphs and walks
