390

I'm writing a python application that will make heavy use of a graph data structure. Nothing horribly complex, but I'm thinking some sort of graph/graph-algorithms library would help me out. I've googled around, but I don't find anything that particularly leaps out at me.

Anyone have any good recommendations?

Alex Bitek
  • 6,529
  • 5
  • 47
  • 77
cpatrick
  • 4,446
  • 4
  • 18
  • 11

8 Answers8

273

There are two excellent choices:

NetworkX

and

igraph

I like NetworkX, but I read good things about igraph as well. I routinely use NetworkX with graphs with 1 million nodes with no problem (it's about double the overhead of a dict of size V + E)

If you want a feature comparison, see this from the Networkx-discuss list

Feature comparison thread

John Y
  • 14,123
  • 2
  • 48
  • 72
Gregg Lind
  • 20,690
  • 15
  • 67
  • 81
  • 4
    In particular, what I like about Networkx.... it's mostly in python, easy to edit and understand the source code, and it feels mostly "pythonic". – Gregg Lind Mar 03 '09 at 15:36
  • 1
    I was wondering, have you used it with a* or similar algorithms? – dassouki Feb 11 '10 at 18:37
  • 5
    I just evaluated both. `networkx` is installable via `pip`, whereas `igraph` is not. This makes `igraph` harder to use as dependencies in your `setup.py` files. – exhuma Aug 10 '12 at 07:46
  • 3
    As an update for 2013, I'm going with networkx just b/c it has a github and looks most up to date of all the options in this answer and the others – Matthew Turner Feb 20 '13 at 17:16
  • @GreggLind I am using Networkx but I can see in my profiler that getting edges from large graph consumes a lot of time. Are there any guidelines or some documentation for better performance? It would be really helpful. – Naman Nov 17 '14 at 08:32
  • 1
    igraph also has a github: https://github.com/igraph/python-igraph – user_1_1_1 May 07 '17 at 22:09
  • 1
    igraph can be installed via pip: pip install python-igraph – user_1_1_1 May 07 '17 at 22:10
  • I recently used NetworkX for a project, I had lines in a text file that referenced other lines, etc and I need to be able to detect circular references and walk the graph in a 'directed' fashion. NetworkX worked PERFECTLY for this. It was fast and easy to use. It has the "is acyclic graph" function to test if the graph isn't circular. Cheers! – Ben DeMott Nov 09 '17 at 22:19
119

I would like to plug my own graph python library: graph-tool.

It is very fast, since it is implemented in C++ with the Boost Graph Library, and it contains lots of algorithms and extensive documentation.

Tiago Peixoto
  • 5,149
  • 2
  • 28
  • 28
  • 7
    graph-tool is fantastic. – Sean Jun 08 '11 at 04:57
  • 5
    +1 For graph-tool. We've been using it in our lab. It is really fast compared to other python libraries. Besides, drawing and displaying graph is pretty awesome in graph-tool. Takes a lot of time to compile though! – Dilawar Mar 14 '13 at 08:44
  • 1
    I think it would better to give readers a link for comparing the performance of those graph libraries: http://graph-tool.skewed.de/performance – MengT Mar 11 '14 at 18:22
  • 1
    http://stackoverflow.com/questions/606516/python-graph-library Hello, I wanna give graph-tool a try and I find the instruction to install it as in the above link. However I'm a windows user and of course I don't want to switch to Linux just to use this pack. Is it any way to use this library in Windows using pre-built, easy-to-install method? (Of course they offered the method to compile this library by myself but this seems to elaborate too much). – Jim Raynor Mar 16 '14 at 18:29
  • @Tiago Peixoto: I am keen to try and use the graph-tool library, I have one question, How can I use it with pyQt, basically I am in need of a pyton graph library that allows me to create interactive graphs on a Qt Qgraphicsscene/Qgraphicsview. I noticed that graph-tool can create static graphs, pngs etc using cairo, do you know if its possible to use your library with QgraphicsScene? if so how? Many thanks – user595985 May 03 '14 at 19:49
  • @user595985 I'm afraid not. Graph-tool uses GTK+/cairo, and hence cannot be easily integrated with Qt. However it does include a ready-to-use GTK+ widget for drawing interactive graphs. – Tiago Peixoto May 03 '14 at 20:30
  • 5
    No windows support unfortunately :( – Mike Chaliy Jul 19 '14 at 12:43
  • 2
    @TiagoPeixoto This looks so so promising but can't use it on windows. I am stuck with NetworkX, finding it too slow. – Naman Nov 19 '14 at 00:13
  • 1
    So embarrassing that such a well-praised tool has no port for Windows user... I find there are too much hassle to build and install on Windows (not to mention there's very high chance that the build will fail). – Jim Raynor Jan 02 '15 at 19:40
  • Looks great from the documentation. Why doesn't `pip install graph-tool` work? – Colonel Panic Nov 19 '15 at 13:01
  • 2
    @ColonelPanic This is a FAQ, see https://graph-tool.skewed.de/download: "The short answer is that it can't be done, since graph-tool depends crucially on some (excellent) C++ libraries such as Boost, which are not installable via pip." – Tiago Peixoto Nov 20 '15 at 13:04
  • 2
    I would love to use it but the GPL license does not allow me to use it :( – Gustavo Muenz Jan 21 '16 at 17:31
  • @TiagoPeixoto Hi, I would be delighted if you could help out with this post, very much in unknown territories here :( http://stackoverflow.com/questions/37944901/basic-questions-about-nested-blockmodel-in-graph-tool –  Jun 21 '16 at 13:01
  • @EdisonGustavoMuenz of course the GPL license allows you to use it... You can even make modifications and distribute those! – Tiago Peixoto Jul 03 '17 at 12:20
  • @TiagoPeixoto how does it compares against http://snap.stanford.edu/ringo/ in terms of speed ? They seem promising for speed and multicore processing – Tushar Goswami Jul 27 '17 at 12:37
  • @TiagoPeixoto Can we import planar graphs from shapefiles in graph-tool? I couldn't find anything in the documents so I guess no, but I thought it might be worth it asking the developers. – Duccio A Dec 01 '17 at 13:20
  • 1
    @DuccioA Appending to this comment is not the proper way to ask such questions. Please use the mailing list instead: https://graph-tool.skewed.de/mailing – Tiago Peixoto Dec 01 '17 at 16:36
32

Have you looked at python-graph? I haven't used it myself, but the project page looks promising.

zweiterlinde
  • 14,557
  • 2
  • 27
  • 32
11

Also, you might want to take a look at NetworkX

lmount
  • 1,282
  • 11
  • 9
7

Take a look at this page on implementing graphs in python.

You could also take a look at pygraphlib on sourceforge.

Breezer
  • 483
  • 3
  • 10
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
7

Use the Boost Graph Library - Python Bindings.

Frank
  • 64,140
  • 93
  • 237
  • 324
  • 1
    Nice one dehmann, I went for that first (being a C++ programmer by trade and absolutely loving boost), but this scares me: BGL-Python bindings are no longer being maintained – cpatrick Mar 03 '09 at 14:25
  • 4
    Look at graph-tool instead, it's bgl based and active. – Sean Jun 08 '11 at 04:58
1

I'm having the most luck with pydot. Some of the others are hard to install and configure on different platforms like Win 7.

http://code.google.com/p/pydot/

syvex
  • 7,518
  • 9
  • 43
  • 47
0

I second zweiterlinde's suggestion to use python-graph. I've used it as the basis of a graph-based research project that I'm working on. The library is well written, stable, and has a good interface. The authors are also quick to respond to inquiries and reports.

jtguerin
  • 43
  • 3