How can I conveniently call igraph from Mathematica?
Update: I am no longer maintaining IGraphR. I recommend that you transition to IGraph/M instead. If you need a feature that IGraph/M does not yet have, please submit a feature request.
I wrote a small package for this. The main reason why I'm posting it here is to get some feedback on how to improve it. I'm new both R and RLink.
You can get the package here. Please see the installation instructions in README.md, especially if you're a Mac user (important!).
How does it work?
RLink does not play well with igraph objects, so an edge-list representation is used to transfer data between R and Mathematica. Using a simple edge-list is good for performance and transparency too. On the R side, edge lists are marked with the mmaDirectedGraph
attribtue. On the Mathematica side, Graph
objects are auto-converted to R edge lists using RLink's RDataTypeRegister
mechanism.
The function to be called and the argument list are passed separately to R. On the R side a function takes care of converting the egde lists to proper igraph objects, and then calls the function. If the return value is a graph, it also converts that back to an edge list.
Usage examples
Mac users will need to load RLink like this first (again, follow the instructions on github!!):
<< RLink`
SetEnvironment[
"DYLD_LIBRARY_PATH" ->
"/Library/Frameworks/R.Framework/Resources/lib"];
InstallR["RHomeLocation" ->
"/Library/Frameworks/R.Framework/Resources"]
Note: if using Mathematica 10.0.1, also specify the R version, e.g.
InstallR["RHomeLocation" ->
"/Library/Frameworks/R.Framework/Resources", "RVersion" -> "3.1"]
Examples:
IGraph["vertex.connectivity"][CycleGraph[5]]
(* ==> {2.} *)
IGraph["barabasi.game"][50]
To overcome some limitations of RLink with regard to performance and parallelizability, I also created a LibraryLink based interface to igraph:
- IGraph/M
Since the original release, IGraph/M has grown into a large package of ~300 functions that is not only an igraph interface, but also provides many unique functions for working with graphs in Mathematica.
Functionality highlights:
Graph theory:
- Isomorphism: vertex/edge coloured graphs, multigraphs, subgraph finding
- Fast exact graph colouring
- Work with planar graphs and combinatorial embeddings
- Degree sequences: check graphicality, construct realizations, random sampling
- Replacement for most of Combinatorica's functions that are not yet built into the Wolfram Language
- ...
Network analysis:
- Implementations for many random graph models
- Centrality measures: support for weighted graphs, multigraphs
- Find network motifs
- Community detection algorithms (fully documented and referenced)
- Random walks on graphs
- Fast shortest path histogramming
- Additional graph layouts
- ...
Geometrical computation:
- Mesh/graph conversion
- Proximity graphs (Delaunay graph, Gabriel graph, $\beta$-skeletons)
- Lattice generation
Utility functions:
- Quick and easy edge/vertex property handling and transformations
- Convenient graph styling based on stored or computed properties
- ...
Many other things such as useful graph generators, finding/counting all cliques, minimum feedback arc set, edge rewiring, bipartite graph handling, matching, alternative algorithms for multiple built-ins, etc.
Compatibility: 64-bit Windows/macOS/Linux or Raspberry Pi; Mathematica 10.0 or later (including full compatibility with 12.0).
I recommend using IGraph/M instead of IGraphR if the functionality you need is already included. It is faster and more robust than IGraphR.
After installing the package, load it as
<<IGraphM`
and open the documentation notebook with
IGDocumentation[]
It contains many examples.
I am hoping that eventually IGraph/M will become a community project. Contributions of all kinds are welcome: comments/suggestions on features & the API, editing usage message and documentation, adding more cool examples, converting the documentation to a format compatible with the Documentation Centre, implementing new functionality, etc.