80

To simplify the problem, I have a graph that contains nodes and edges which are on a 2D plane.

What I want to be able to do is click a button and it make the automatically layout the graph to look clean. By that I mean minimal crossing of edges, nice space between nodes, maybe even represent the graph scale (weighted edges).

I know this is completely subjective of what is a clean looking graph, but does anyone know of an algorithm to start with, rather than reinventing the wheel?

Thanks.

6 Answers 6

91

You will find http://graphdrawing.org/ and this tutorial, by Roberto Tamassia, professor at Brown University, quite helpful.

I like a lot Force-Directed Techniques (pp. 66-72 in the tutorial) like the Spring Embedder.

You assume there is a spring or other force between any two adjacent nodes and let nature (simulation) do the work :)

Sign up to request clarification or add additional context in comments.

Comments

29

I would suggest that you take a look at at graphviz. The dot program can take a specification of a graph and generate an image of the network for you somewhat "cleanly". I've linked to the "theory" page that gives you some links that might be relevant if you're interested in the theoretical background. The library and tools themselves are mature enough if you simply want a solution to a problem with layout that you're facing.

4 Comments

The question is about an Algorithm for graph layout, not application.
That's why I specifically mentioned the theory link on that page.
The answer isn't explicitly referring to the theory link. It's more of an "if you are interested in theory". It would've been better to refer to the theory link and "if you are interested in an application" instead.
Fair enough. I understood the OP as wanting to solve the layout problem (rather than reimplement a solution) and based my answer on that (a well tested, production quality "wheel" that he or she didn't have to reinvent).
5

I would say as Noufal Ibrahim, but you could also look more precisely at the C API of the graphviz project. It includes a lib to build your graph (libgraph.pdf) with all the nodes and edges, and a lib to layout the graph (libgvc.pdf) (just compute each nodes position), so you can then display it in your own UI for example.

Comments

5

Also JGraph if you want the layouts in Java (I work on the project).

1 Comment

The link redirects to draw.io now. Is github.com/jgraph/jgraphx more correct?
3

A good visual guide how the most popular layouts actually look: follow the link

1 Comment

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
2

For javascript I recommend this library from Microsoft: https://microsoft.github.io/msagljs/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.