Immutable graph data structures
JavaScript HTML
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Immutable graph data structures



npm install -S giraph
bower install -S giraph


  .add('a', { my: 'data' })
  .connect('a', 'b', 3)
  .connect('a', 'c')


Giraph comes with a number of different graph types:

Undirected Graph

Exported as a factory function on the root namespace:


Options and defaults

  immutable: true
require('giraph')() // => empty graph

The undirected graph has the following members

Property: .map: {}

Hashes vertices by id.

Property: .options

Instance options

Property: .length

The number of vertices

.add( id[, data] )

Returns a new instance with Vertex(id[, data]) added to it.

.get( id )

Gets the Vertex at id.

.remove( id )

Returns a new instance with id removed.

.connect( a, b, edgeWeight )

Returns a new instance with a and b connected.

.contains( String|Vertex id )

Returns a boolean indicating whether or not id is in the graph.

Note: if passing in a String, is equivalent to checking id in

.merge( a[, b[, ...]] )

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.


var g1 = giraph()
  .add('b', { some: 'thing' })
  .connect('a', 'b', 1);

var g2 = giraph()
 .add('b', { some: 'data' })
 .conect('b', 'c');
// a->b->c
var g3 = g1.merge( g2 );

// { some: 'data' }
console.log( g3.get('b').data );

.each( Function iterator )

Iterates through each vertex. The iterator argument has the following signature:

function( Vertex v, Number i, Graph g )


  .add('a', { some: 'data' }).add('b').add('c')
  .each( function( vertex, i, graph ){
    console.log(,, i );
  // a { some: 'data' }
  // b null
  // c null

Returns this

.reduce( Function iterator, Mixed initialValue )

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

  .mutate(function( g ){
    // Batch operations here


Returns a Minimum Spanning Tree represented as a Graph.

Directed Graph


Directed Acyclic Graph



A vertex is essentially an Identifier with data attached and connections to other vertices.

It's available under .vertex( id[, data[, options]]).

.id String

ID of the vertex

.data Mixed

Optional attached data

.options Object

Optional options:

  immutable: true


Returns a new instance of the vertex