28
$\begingroup$

Mathematica has lots of functions for working with graphs and networks, but there are still some functionality holes that are provided by other packages. Also, due to the nature of graph theory, often it is very hard (in practice impossible) to verify results returned by Mathematica without having an alternative implementation.

For these reasons it would be very useful to have access to another graph analysis library. igraph is one of the most mature open source network analysis packages. How can we access it from Mathematica?

$\endgroup$
26
$\begingroup$

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]

enter image description here

$\endgroup$
  • 2
    $\begingroup$ Very cool! One suggestiong would be that if you want to transfer meta-data as well, then use the graph.data.frame() and get.data.frame() functions to convert between igraph objects and simple tables (one for the vertices, one for edges). $\endgroup$ – Gabor Csardi May 3 '13 at 17:36
  • $\begingroup$ @Gabor Thanks for the suggestion! I'm not yet experienced with either igraph or R, so any advice is most welcome. $\endgroup$ – Szabolcs May 3 '13 at 17:38
  • $\begingroup$ Thanks to Leonid for showing me how to use an external R installation on Mac! $\endgroup$ – Szabolcs May 23 '13 at 4:25
13
$\begingroup$

Join the chat at https://gitter.im/IGraphM/Lobby GitHub (pre-)release contributions welcome DOI

To overcome some limitations of RLink with regard to performance and parallelizability, I also created a LibraryLink based interface to igraph:

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.

enter image description here


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.

$\endgroup$
  • $\begingroup$ I played around a few functions in IGraphM. I notice that most functions for graph isomorphism don't support digraphs. But they support digraphs in the original igraph C library. Is it possible to make those functions compatible with digraphs? $\endgroup$ – Han Xiao Nov 15 '15 at 9:02
  • $\begingroup$ @HanXiao What are you referring to precisely? The current version of IGraph/M has better support for directed graphs than the released version of igraph (0.7.1). Each isomorphism function supports directed graphs. In igraph 0.7.1, Bliss doesn't. I submitted a patch to igraph which updates Bliss to the latest version exactly to support directed ones. There was a confusion with LAD and if you use it on directed graphs, will will give a warning. This is a bug (already fixed in the development version), and the warning can be ignored. LAD supports directed ones too. $\endgroup$ – Szabolcs Nov 15 '15 at 11:30
  • $\begingroup$ Thanks for the clarification. I have to admit that I only tested functions using LAD. The warning did confuse me. And I didn't find any demonstrations on digraphs. But now my questions are solved. I'm thinking of using some functions from IGraphM to speed up my code. Really appreciate your work on this package. $\endgroup$ – Han Xiao Nov 15 '15 at 12:41
  • $\begingroup$ @HanXiao All suggestions, comments, etc. are welcome! Feel free to write me an email. Even if you just tell me which functions you use from the package, that will be useful. $\endgroup$ – Szabolcs Nov 16 '15 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.