237

Has anyone had good experiences with any Java libraries for Graph algorithms. I've tried JGraph and found it ok, and there are a lot of different ones in google. Are there any that people are actually using successfully in production code or would recommend?

To clarify, I'm not looking for a library that produces graphs/charts, I'm looking for one that helps with Graph algorithms, eg minimum spanning tree, Kruskal's algorithm Nodes, Edges, etc. Ideally one with some good algorithms/data structures in a nice Java OO API.

Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55
Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134

18 Answers18

114

If you were using JGraph, you should give a try to JGraphT which is designed for algorithms. One of its features is visualization using the JGraph library. It's still developed, but pretty stable. I analyzed the complexity of JGraphT algorithms some time ago. Some of them aren't the quickest, but if you're going to implement them on your own and need to display your graph, then it might be the best choice. I really liked using its API, when I quickly had to write an app that was working on graph and displaying it later.

lbalazscs
  • 17,474
  • 7
  • 42
  • 50
Bartosz Bierkowski
  • 2,782
  • 1
  • 19
  • 18
  • JGraph does have an analysis package now that includes a range of analysis functions, http://jgraph.github.com/mxgraph/java/docs/index.html. – Frodo Baggins Mar 25 '13 at 20:56
71

Summary:

koppor
  • 19,079
  • 15
  • 119
  • 161
mansu
  • 993
  • 1
  • 10
  • 13
  • Alot of those are extremely complicated... Using factory Methods and so forth. I just need something simple to prep for an interview. Any ideas? – SoftwareSavant Sep 28 '12 at 11:50
  • 5
    If these are complicated than what kind of job you are looking for – Maytham Fahmi Mar 06 '17 at 22:44
  • 1
    Graph algorithms are explained here http://www.geeksforgeeks.org/graph-data-structure-and-algorithms/ with simple code – mosh Jul 27 '17 at 03:06
41

Check out JGraphT for a very simple and powerful Java graph library that is pretty well done and, to allay any confusion, is different than JGraph. Some sample code:

UndirectedGraph<String, DefaultEdge> g =
        new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);

    String v1 = "v1";
    String v2 = "v2";
    String v3 = "v3";
    String v4 = "v4";

    // add the vertices
    g.addVertex(v1);
    g.addVertex(v2);
    g.addVertex(v3);
    g.addVertex(v4);

    // add edges to create a circuit
    g.addEdge(v1, v2);
    g.addEdge(v2, v3);
    g.addEdge(v3, v4);
    g.addEdge(v4, v1);
gvlasov
  • 18,638
  • 21
  • 74
  • 110
Joe Liversedge
  • 4,124
  • 26
  • 19
37

JUNG is a good option for visualisation, and also has a fairly good set of available graph algorithms, including several different mechanisms for random graph creation, rewiring, etc. I've also found it to be generally fairly easy to extend and adapt where necessary.

Kai
  • 5,260
  • 5
  • 29
  • 36
  • Packages hep.aida.* are LGPL (http://acs.lbl.gov/software/colt/license.html). This is imported via colt (http://jung.sourceforge.net/download.html). This prevents JUNG from being used in projects under the umbrella of ASF and ESF. Maybe one should use the github fork https://github.com/rortian/jung2 and remove that dependency. https://github.com/rortian/jung2/commit/f4ca0cdcd3312589cbb48de7350b84cbff6067b9 is mirroring the last CVS commit. The current commits seem to remove visualization functionality. – koppor May 26 '13 at 11:58
  • There is no released since 2010, I think that this project is abandoned – Yacino May 12 '17 at 08:35
14

Apache Commons offers commons-graph. Under http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/ one can inspect the source. Sample API usage is in the SVN, too. See https://issues.apache.org/jira/browse/SANDBOX-458 for a list of implemented algorithms, also compared with Jung, GraphT, Prefuse, jBPT

Google Guava if you need good datastructures only.

JGraphT is a graph library with many Algorithms implemented and having (in my oppinion) a good graph model. Helloworld Example. License: LGPL+EPL.

JUNG2 is also a BSD-licensed library with the data structure similar to JGraphT. It offers layouting algorithms, which are currently missing in JGraphT. The most recent commit is from 2010 and packages hep.aida.* are LGPL (via the colt library, which is imported by JUNG). This prevents JUNG from being used in projects under the umbrella of ASF and ESF. Maybe one should use the github fork and remove that dependency. Commit f4ca0cd is mirroring the last CVS commit. The current commits seem to remove visualization functionality. Commit d0fb491c adds a .gitignore.

Prefuse stores the graphs using a matrix structure, which is not memory efficient for sparse graphs. License: BSD

Eclipse Zest has built in graph layout algorithms, which can be used independently of SWT. See org.eclipse.zest.layouts.algorithms. The graph structure used is the one of Eclipse Draw2d, where Nodes are explicit objects and not injected via Generics (as it happens in Apache Commons Graph, JGraphT, and JUNG2).

koppor
  • 19,079
  • 15
  • 119
  • 161
12

http://neo4j.org/ is a graph database that contains many of graph algorithms and scales better than most in-memory libraries.

Jonathan Hendler
  • 1,239
  • 1
  • 17
  • 23
11

In a university project I toyed around with yFiles by yWorks and found it had pretty good API.

Turismo
  • 2,064
  • 2
  • 16
  • 23
  • I've used yFiles for visualisation of interdependencies between data items (as part of a commercial software platform). I didn't really use any graph analysis algorithms, but check if the y.algo package has what you need: http://www.yworks.com/products/yfiles/doc/api/ – Jonik Feb 01 '09 at 19:35
  • 3
    yFiles is not opensource, but offers commercial licenses – koppor May 22 '13 at 18:47
9

check out Blueprints:

Blueprints is a collection of interfaces, implementations, ouplementations, and test suites for the property graph data model. Blueprints is analogous to the JDBC, but for graph databases. Within the TinkerPop open source software stack, Blueprints serves as the foundational technology for:

Pipes: A lazy, data flow framework

Gremlin: A graph traversal language

Frames: An object-to-graph mapper

Furnace: A graph algorithms package

Rexster: A graph server

Pablo R. Mier
  • 719
  • 1
  • 7
  • 13
alex
  • 1,757
  • 4
  • 21
  • 32
7

http://incubator.apache.org/hama/ is a distributed scientific package on Hadoop for massive matrix and graph data.

xyz
  • 61
  • 1
  • 1
7

JDSL (Data Structures Library in Java) should be good enough if you're into graph algorithms - http://www.cs.brown.edu/cgc/jdsl/

mr.sverrir
  • 414
  • 4
  • 12
5

It's also good to be convinced that a Graph can be represented as simply as :

class Node {
   int value;
   List<Node> adj;
}

and implement most the algorithms you find interesting by yourself. If you fall on this question in the middle of some practice/learning session on graphs, that's the best lib to consider. ;)

You can also prefer adjacency matrix for most common algorithms :

class SparseGraph {
  int[] nodeValues;
  List<Integer>[] edges;     
}

or a matrix for some operations :

class DenseGraph {
  int[] nodeValues;
  int[][] edges;     
}
Snicolas
  • 37,840
  • 15
  • 114
  • 173
5

For visualization our group had some success with prefuse. We extended it to handle architectural floorplates and bubble diagraming, and it didn't complain too much. They have a new Flex toolkit out too called Flare that uses a very similar API.

UPDATE: I'd have to agree with the comment, we ended up writing a lot of custom functionality/working around prefuse limitations. I can't say that starting from scratch would have been better though as we were able to demonstrate progress from day 1 by using prefuse. On the other hand if we were doing a second implementation of the same stuff, I might skip prefuse since we'd understand the requirements a lot better.

Jacob Rigby
  • 1,323
  • 2
  • 15
  • 20
  • What were your personal thoughts with prefuse? At my last job, a project started to use it, but ended up with a 90%+ rewritten (and optimized, with additions of new features) version of prefuse. – Thomas Owens Sep 09 '08 at 11:33
5

Try Annas its an open source graph package which is easy to get to grips with

http://annas.googlecode.com

4

If you need performance, you might take a look at Grph. The library is developed in the French University and CNRS/Inria.

http://www.i3s.unice.fr/~hogie/grph/

The project is active and reactive support is provided!

Luc
  • 21
  • 1
  • 5
4

I don't know if I'd call it production-ready, but there's jGABL.

koppor
  • 19,079
  • 15
  • 119
  • 161
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
3

Instructional graph algorithm implementations in java could be found here (by prof. Sedgewick et al.): http://algs4.cs.princeton.edu/code/

I was introduced to them while attending these exceptional algorithm courses on coursera (also taught by prof. Sedgewick):

https://www.coursera.org/course/algs4partI

https://www.coursera.org/course/algs4partII

0

JGraph from http://mmengineer.blogspot.com/2009/10/java-graph-floyd-class.html

Provides a powerfull software to work with graphs (direct or undirect). Also generates Graphivz code, you can see graphics representations. You can put your own code algorithms into pakage, for example: backtracking code. The package provide some algorithms: Dijkstra, backtracking minimun path cost, ect..

Bob
  • 1
0

If you are actually looking for Charting libraries and not for Node/Edge Graph libraries I would suggest splurging on Big Faceless Graph library (BFG). It's way easier to use than JFreeChart, looks nicer, runs faster, has more output options, really no comparison.

Jacob Rigby
  • 1,323
  • 2
  • 15
  • 20
  • You misunderstood the question: it is about the kind of graphs that have nodes and edges, not the kind that has pies and bars. – amarillion Oct 20 '11 at 10:55