Simulating Graphs as Physical Systems

Dr. Dobb's Journal August 1999

A spring-embedder system for force-directed layout

By Arne Frick, Georg Sander, and Kathleen Wang

The authors are research staff members at Tom Sawyer Software. They can be reached, respectively, at africk@acm.com, sander@cs.uni-sb.de, and kwang@tomsawyer.com.

Graphs are commonly used to represent data and their relationships. They occur in many forms, including directory trees, flow charts, PERT charts, organization diagrams, and networks such as the World Wide Web. A graph consists of a set of nodes, which are used to represent the data items, and a set of edges, which are used to represent the relationships between items. Manually drawing a graph for a large data set, so that the graph looks good and properly accounts for the structure of the data, is an arduous process.

Automatic visualization tools generate comprehensible graphs from data, enabling users to see important relationships in the data. However, optimally placing the nodes (assigning coordinates to them, for example) and routing the edges between them (generating a line that connects the nodes) is a difficult problem. There are many criteria for judging a good drawing, including:

Each one of these criteria can be mathematically formulated as an optimization problem. However, computer science theory shows that finding an optimal solution for some of these problems is generally not feasible, and this is much more so for this combined optimization problem. Therefore, it is sufficient in practice to find good solutions instead. Finding good solutions rather than optimal ones is the objective of a whole research area called "graph drawing."

In this article, we describe a variant of a graph drawing algorithm called "spring-embedder," which models the graph as a physical system of particles that move under the influence of forces until the system reaches equilibrium. The result is a good, visually useful graph.

Force-Directed Layout

Spring-embedder is a force-directed layout. Its name results from mass-spring models in physics, in which a set of steel rings (nodes) is connected by springs (edges), and the system is allowed to reach equilibrium. This graph-drawing algorithm is particularly useful for undirected graphs, where the directions of the edges are not important. Typical applications of undirected graphs are the display of telecommunication network topologies and the Web. P. Eades published the first paper on force-directed layout algorithms in "A Heuristic for Graph Drawing" (Congressus Numerantium, 42, 1984). The algorithm discussed in this article was first described in Arne Frick, Andreas Ludwig, and Heiko Mehldau's "A Fast Adaptive Layout Algorithm for Undirected Graphs" (Proceedings of Graph Drawing '94, Volume 894 of Lecture Notes in Computer Science, edited by Roberto Tamassia and Ioannis Tollis, Springer-Verlag, 1995).

Eades modified the physical model with attractive spring forces between adjacent nodes by assigning each pair of nodes a virtual repulsive force. This helps guarantee that topologically near nodes are placed in the same vicinity, and far nodes are placed far from each other. The attractive force between two nodes, u and v, is defined in Figure 1, where -- uv is the distance between the nodes u and v. The term (v-u)/ -- uv is the normalized distance vector between the points u and v, meaning that it has a length of 1. c1 and c2 are constants. c1 allows us to adjust the attractive force between two nodes. c2 equals the desired edge length.

The repulsive force between u and v is defined in Figure 2, where c3 should be approximately the square of the desired edge length (that is, c22). Increasing c3 iteratively helps reduce the number of overlapping nodes.

The sum of the force vectors determines which direction the node should move. We could define a constant c4 for the step width, which would determine how far a node could move in a single step. Using a constant step width works well for small (30-50) node graphs, but performance quickly degrades as graphs grow in size. The algorithm moves nodes that are far out of place to the final position more quickly, but can also miss good positions by jumping over them, possibly leading to divergence and/or oscillations. There is no guarantee that the system reaches equilibrium at all. Because of this, we do not use a constant step width. Instead, we use the notion of temperatures.

Temperatures

One way to improve the spring-embedder system is to introduce temperatures that are better able to control the algorithm's performance. In their paper, "Graph Drawing by Force-Directed Placement" (Software: Practice and Experience, 21(11), 1991), T.M.J. Fruchterman and E.M. Reingold introduced a global temperature that controls the step width of node movements and the algorithm's termination. The step width is proportional to the temperature, so if the temperature is hot, the nodes move faster (that is, a larger distance c4 in each single step). The temperature is the same for all nodes, and cools down at each iteration. Once the nodes stop moving, the system terminates.

Although using a global temperature will create an overall satisfying layout, there will be deficiencies in some local areas of the graph. To improve the situation, we will remodel the system using the "adaptive temperature scheme" based on local temperatures: