Skip to content

bfontaine/Graphs.rb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Graphs.rb

Build Status Gem Version Coverage Status Inline docs Dependency Status

This library allows you to perform some basic operations on graphs, with import/export from/to JSON and GDF files.

Note: Before the 0.1.5 version, nodes & edges were represented as hashes, and now they are Node & Edge objects, respectively. They behave like hashes to preserve the backward compatibility.

See the changelog for more info.

Install

The best way is to use the graphs gem:

gem install graphs

If you want to have the latest version, clone this repo, build the gem, and install it:

git clone git://github.com/bfontaine/Graphs.rb.git
cd Graphs.rb
gem build graphs.gemspec
gem install ./graphs-*.gem # you may want to use sudo

If you want to use the high security trust policy, you need to add my public key as a trusted certificate (you only need to do this once. Note: the key changed on August 24, 2014):

gem cert --add <(curl -Ls https://gist.github.com/bfontaine/5233818/raw/gem-public-key.pem)

Then, install the gem with the high security trust policy:

gem install graphs -P HighSecurity

Tests

To perform the tests, clone this repo, run bundle install, then rake (you need Ruby ≥1.9.x):

git clone git://github.com/bfontaine/Graphs.rb.git
cd Graphs.rb
bundle install
bundle exec rake

Docs

The Graph class is a simple graph with nodes and edges. It provides three read-write attributes: nodes, edges, and attr (attributes of the graph, like author or description). It can be written in a file using Graph#write method.

Node and Edge are special classes which inherit from Hash. A graph object provide two important attributes:

  • nodes: A NodeArray object (Array-like)
  • edges: An EdgeArray object (Array-like, too)

For backward compatibility, you can create nodes and edges both with Node and Edge objects, and Hash ones, e.g.:

require 'graph'

nodes = [ {:label => 'foo'}, {:label => 'bar'} ]

g = Graph.new nodes


nodes2 = nodes.map { |n| Graph::Node.new n }
g2 = Graph.new nodes2

g == g2 # true

You can perform some operations on graphes using the |, &, ^, + or - operators. See the documentation for more informations.

Import/Export

The library currently support JSON and GDF formats.

You can read from files using the ::load methods of each module:

require 'graph'
require 'graphs/gdf'
require 'graphs/json'

g1 = GDF::load('myGraph.gdf')
g2 = JSONGraph::load('myGraph.json')

You can also export a graph using the .write method. It guesses the format using the file extension. If the file extension is unknown, it uses the YAML format.

require 'graph'
require 'graphs/gdf'
require 'graphs/json'

g = Graph.new

g.write('myGraph.gdf')
g.write('myGraph.json')

Note to Gephi users who want to export in GDF: You can add the :gephi option to g.write(…) if you have big numbers. Graph#write method use BIGINT type for big numbers, but Gephi does not support it and parses it as a string field. So using the following:

g.write('new_trips.gdf', {:gephi=>true})

make sure that INT is used for all BIGINT fields.