Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Information Processing Letters 31 (1989) 7-15 12 April 1989

North-Holland

AN ALGORITHM FOR DRAWING GENERAL UNDIRECTED GRAPHS

Tomihisa KAMADA and Satoru KAWAI


Department of Information Science, Faculty of Science, University of Tokyo, 7-3-1 Hongo, Bunkyo-ku, Tokyo, 113 Japan

Communicated by E.C.R. Hehner


Received 4 July 1988
Revised 13 November 1988

Keywords: Graph, network structure, layout, drawing algorithm

1. Introduction

Graphs (networks) are very common data structures which are handled in computers. Diagrams are
widely used to represent the graph structures visually in many information systems. In order to
automatically draw the diagrams which are, for example, state graphs, data-flow graphs, Petri nets, and
entity-relationship diagrams, basic graph drawing algorithms are required.
The state of the art in automatic drawing is surveyed comprehensively in [7,19]. There have been only a
few algorithms for general undirected graphs. This paper presents a simple but successful algorithm for
drawing undirected graphs and weighted graphs. The basic idea of our algorithm is as follows. We regard
the desirable "geometric" (Euclidean) distance between two vertices in the drawing as the "graph
theoretic" distance between them in the corresponding graph. We introduce a virtual dynamic system in
which every two vertices are connected by a "spring" of such desirable length. Then, we regard the optimal
layout of vertices as the state in which the total spring energy of the system is minimal.
The "spring" idea for drawing general graphs was introduced in [6], and similar methods were used for
drawing planar graphs with fixed boundary [2,20]. This paper brings a new significant result in graph
drawing based on the spring model.

2. Graph drawing problem

We will clarify the graph drawing problem we treat in this paper. There are really a lot of ways to
visualize graph structures. In some methods, the positions of vertices are restricted, e.g., they are placed on
grid points [1,19], concentric circles [4], or parallel lines [14,18]. Edges can be drawn as straight lines,
polygonal lines, or curves. We treat here the drawings in which the positions of vertices are not restricted
and edges are drawn as straight lines. So our purpose is to determine the positions of vertices for a given
graph G. (G is expressed by the set of vertices V and the set of edges E). And we assume that a given
graph is connected. The picture of a disconnected graph is obtained by drawing its connected components
separately. A graph can be decomposed into connected components in running time O( I V I + I E I) [15].
First of all we must discuss the fundamental conditions of the general view of a graph. The graph
structure encompasses so many kinds of structures, from trees to complete graphs, that it is difficult to
find out the common criteria of nice drawings. However, the following two requirements in drawing graphs

0020-0190/89/$3.50 © 1989, Elsevier Science Publishers BY (North-Holland) 7


Volume 31, Number 1 INFORMATION PROCESSING LETTERS 12 April 1989 Volume 31, Number 1

is a good way to d
a given graph. Tho
L=Lo

where L o is the Ie
The parameter
expression (1) can
a real ones for all p
Fig. 1. Symmetric and planar drawings of a graph. used in (1). Then,

kij=K
are commonly admitted [4,21]. One is to reduce the number of edge crossings, and the other is to distribute
where K is a cons
the vertices and edges uniformly. The reduction of edge crossings can, however, be shown not to be a good
Here we analyz
criterion as follows. Pictures with minimal number of edge crossings are not always the best. For example,
large, because eve
Fig. 1 shows two different drawings of a 16-vertex graph. If they are not interested in planarity, almost all
symmetric graphs
people think Fig. l(a) is better for the representation of internal structure though it has five edge crossings.
ing E.
Figure l(b) sacrifices symmetry for the reduction of edge crossings. Symmetric structures should be drawn
as symmetric pictures because symmetry is a valuable characteristic of structures. Some algorithms have
3.2. Local minimiz,
really tried to draw graphs symmetrically [6,11]. This example suggests that the total balance of layout is as
important as or even more important than the reduction of edge crossings for human understanding. The
We prepare sor
latter condition (uniformity) does imply the total balance. Accordingly, we now pay attention to how
particle in a plane
judging the total balance of a layout. In our model, the total balance condition is formulated as the square
coordinate variable
summation of the differences between desirable distances and real ones for all pairs of vertices.
by using these 2n
n-
E= L
3. Graph drawing algorithm

Our purpose is to
3.1. Spring model (Hereafter the par
minimum, so we ir
We introduce a dynamic system in which n (= 1V I) particles are mutually connected by springs. Let We present a IT
PI' P2"'" Pn be the particles in a plane corresponding to the vertices Pb P2, ... , Pn E V respectively. We Newton- Raphson
relate the balanced layout of vertices to the dynamically balanced spring system. As a result, the degree of
imbalance can be formulated as the total energy of springs, i.e.,

n-l n The state satisfyin]


E = L L ikij( 1 Pi - Pj I-(jt (1) The partial derivat
i=1 j=i+l

Pleasing layouts can be obtained by decreasing E, then the best layout is the state with minimum E in our
model. The original length (j of the spring between Pi and Pj corresponds to the desirable length between
them in the drawing, and is determined as follows. The distance dij between two vertices Pi and Pj in a
graph is defined as the length of the shortest paths between Pi and Pj [3]. Then the length (j is defined as

(2)
We must solve the:
where L is the desirable length of a single edge in the display plane. When the display space is restricted, it using a 2n-dimens

8
12 April 1989 Volume 31, Number 1 INFORMAnON PROCESSING LETTERS 12 April 1989

is a good way to determine L depending on the diameter (i.e., the distance between the farthest pair [3]) of
a given graph. That is,
L = La/max d ij (3)
1<)

where L o is the length of a side of display square area.


The parameter k ij is the strength of the spring between Pi and Pj- and is determined as follows. The
expression (1) can be regarded as the square summation of the differences between desirable distances and
real ones for all pairs of particles. From this point of view, the differences per unit length is better to be
used in (1). Then, we define k ij as

kij=K/di~ (4)
ier is to distribute
where K is a constant. The parameters lij and k i) are symmetric, i.e., 1;) = Ijl and k ij = k ji (i =f. j).
1 not to be a good Here we analyze the properties of our spring model briefly. The density of particles does not become
lest. For example,
large, because every two nodes are forced to keep certain distance by the tension of a spring. Note that
inarity, almost all
symmetric graphs correspond to symmetric spring systems, which result in symmetric layouts by minimiz-
ve edge crossings.
ing E.
; should be drawn
e algorithms have
3.2. Local minimization of global energy
nee of layout is as
aderstanding, The
We prepare some definitions for describing the algorithm and outline our method. The position of a
attention to how
particle in a plane is expressed by x and Y coordinate values. Let (Xl' YI), (X2' Y2), ... , (X n, Yn) be the
ated as the square
coordinate variables of particles PI' P2"'" Pn respectively. Then, the energy E defined as (1) is rewritten
vertices.
by using these 2n variables as follows.
n-l n
E= L L -ikij{(Xi-X;)2+(Yi-y;)2+1;~-21;)(Xi-Xj)2+(Yi-y;)2}. (5)
i~l j~i+l

Our purpose is to compute the values of these variables which nrininrize E(x l, X2,···, Xn ' YI' Y2"'" Yn)'
(Hereafter the parameters of the function E are omitted). It is, however, quite difficult to compute the
minimum, so we instead compute a local minimum.
We present a method of computing a local minimum of E from a certain initial state based on the
:d by springs. Let
r respectively. We
Newton-Raphson method [14]. The necessary condition of local minimum is as follows.
sult, the degree of aE = aE = 0 f or 1 ~ m ~ n. (6)
aXm aYm

The state satisfying (6) corresponds to the dynamic state in which the forces of all springs are balanced.
(1) The partial derivatives of (5) by Xm and Ym are as follows.

iinimum E in our (7)


ile length between
ces /Ii and /lj in a
h lij is defined as
(8)

(2)
We must solve these 2n simultaneous non-linear equations of (6). But they cannot be directly solved by
ace is restricted, it using a 2n-dimensional Newton-Raphson method, because they are not independent of one another.

9
Volume 31, Number 1 INFORMATION PROCESSING LETTERS 12 April 1989 Volume 31, Number 1

Then, we adopt another way in which only one particle Pm(x m, Ym) is moved to its stable point at a time, regular n-polygon
freezing the other particles. That is, viewing E as a function of only X m and Ym' we compute a local positions, the ener
minimum of E by using a two-dimensional Newtori-Raphson method. We can obtain a local minimum The algorithm i
which satisfies (6) iterating this step. In each step, we choose the particle that has the largest value of lim'
comput
which is defined as
comput
comput
Ll m = (9) initializ
while (I
let p
Starting from (x~), y~O») which is equal to the current position (x m, Ym)' the following step is iterated:
while
y~t+l) = y~t) + 8y fort=O, 1,2, ... (10) cc

The unknowns Bx and 8y satisfy a pair of linear equations as follows:


Ym
a 2
aE
2
st: }
_ E (x(t) y(t») 8x + (x(t) y(t») 8y = _ _ (x(t) y(t») (11)
aX
2 m' m
m
ax may m·
m' m m
ax
m' m , }

Here we discuss
a 2E
-;:----;:---(x(t) y(t») 8x + -a
2E
aE
(x(t) y(t») 8y = - _ preliminarily. (We
aymax m m > m a Ym
2 m' m aym (x(t)
m'
y(t»)
m . (12)
tional time is main
son iteration, O( n )
The coefficients of above equations (11) and (12), which are the elements of Jacobian matrix, are And in the outer 1,
computed from the partial derivatives of (7) and (8) by X m and Ym as follows. (i -=1= m) after movir
the time needed to
(13)

(14)

(15)

(16)

The unknowns 8x and 8y can be computed from (11)-(16). The iteration (10) terminates when the value
of lim at (x;';), y~t») becomes small enough.

3.3. Algorithm

In our graph drawing algorithm, first the distance d i j must be computed for all pairs of vertices in a
given graph. For the present, we are using a simple shortest-path algorithm of Floyd [8]. Next Ii} and k ij
are computed for all pairs from d i j by the use of (2), (3) and (4). Before starting the minimization process,
the initial positions of particles must be determined. The experiments have shown that the initial positions
do not have a great influence on the resultant pictures, except for the special cases: e.g., all particles lie on
a single line. Now we are using a simple initialization by which the particles are placed on the nodes of the

10
12 April 1989 Volume 31, Number 1 INFORMATION PROCESSING LETTERS 12 April 1989

.le point at a time, regular n-polygon circumscribed by a circle whose diameter is La (as for La, see (3)). After initializing the
~ compute a local positions, the energy E is decreased, step by step, by moving a particle to a stable position.
a local minimum The algorithm is summarized in a simple form as follows.
rgest value of Ll m'
compute d i j for 1 ~ i oF j ~ n;
compute Ii) for 1 ~ i oF j ~ n;
compute k i j for 1 ~ i oF j ~ n;
(9) initialize Pl' P2'"'' Pn;
while (max.zi, > €){
ig step is iterated: let Pm be the particle satisfying Ll m = maxiLl i ;
while (Ll m > €){
(10) compute ox and oy by solving (11) and (12);
x.; :=x m + ox;
Ym:= Ym + oY;
}
(11) }

Here we discuss the computational cost of above algorithm briefly. As for d i j , 0(n 3 ) time is required
(12) preliminarily. (We can use instead more efficient algorithms [13,16] for large graphs.) Required computa-
tional time is mainly determined by the nested while loops. In the inner loop which is the Newton-Raph-
son iteration, O( n) time is required to compute Ll m and to compute ox and 0Y respectively at each step.
)bian matrix, are
And in the outer loop, O( n) time is required to compute the maximum of Ll i because updating each Ll i
(i oF m) after moving Pm can be performed in 0(1) time by memorizing the old position of Pm' As a result,
the time needed to terminate the while loops is O( T . n) where T is the total number of inner loops. It is
(13)

c B c

(14)

A
A
(15) D

E
(16) E
a b

c
es when the value
F F

rs of vertices in a
. Next (j and k ij
imization process, A
Ie initial positions E
E d
all particles lie on c
11 the nodes of the Fig. 2. The energy minimization process.

11
Volume 31, Number 1 INFORMATION PROCESSING LETTERS 12 April 1989 Volume 31, Number 1

difficult to estimate T because T depends both on the given graphs, especially the number of vertices (n),
and on the initial positions of vertices. However, lowering the convergence precision (e ) is an effective way
to reduce T.
Figure 2 illustrates the process of minimizing E in the case of a 6-vertex graph. First the particle B is
moved (Fig. 2(b)) and next the particle F is moved (Fig. 2(c)). The final state (Fig. 2(d)) is obtained after
21 moving steps. The algorithm proposed here does not guarantee to compute the true minimum of E. In
order to prevent the energy from converging to a large local minimum, we have added a simple test to the
algorithm [10].

4. Examples

4.1. Symmetric and asymmetric graphs

graphs is visualizr
We show some pictures generated by the system implementing our graph drawing algorithm. Figure 3 asymmetric graphs
shows four pictures of symmetric graphs. Figure 3(a) and 3(d) are the pictures of a regular hexahedron The CPU time
(cube) graph and regular dodecahedron graph respectively. They look as if they were the projected images seconds (Fig. 3(d);
of a cube and a dodecahedron. Figure 3(b) shows a picture of the graph which has the triangulated internal
structure. Figure 3(c) shows a picture of the graph cited in Fig. 1. As these examples show, symmetry of 4.2. Isomorphic grc

When the vie"


A isomorphic graphs

a b

K
M~ _ _-;;r_ _

L
c d
Fig. 3.

12
12 April 1989 Volume 31, Number 1 INFORMATION PROCESSING LETTERS 12 April 1989

r of vertices (n), F
an effective way E

the particle B is
is obtained after
nimum of E. In
D
imple test to the

L
Q
a b
Fig. 4. Pictures of asymmetric graphs.

graphs is visualized pleasingly as symmetric pictures in our system. Figure 4 shows two pictures of
rrithm. Figure 3 asymmetric graphs. In these cases needless edge crossings are avoided completely.
ular hexahedron The CPU time needed to compute a layout in these pictures is from 0.4 seconds (Fig. 3(a)) to 7.6
orojected images seconds (Fig. 3(d)) on a VAX 8600.
igulated internal
rw, symmetry of 4.2. Isomorphic graphs
When the viewer wants to compare some graphs, it is highly required of the system to display
isomorphic graphs as the same picture. Otherwise the viewer cannot make proper comparisons from the

D c D c

F .---\--+----,~-I-+-= .. A F A

H H
a b
G E

F
B

A
H
c d
Fig. 5. Pictures of isomorphic graphs.

13
Volume 31, Number 1 INFORMATION PROCESSING LETTERS 12 April 1989 Volume 31, Number 1

c graph theoretic di
the drawing. As d
relatively small n
precise proof of th
of our algorithm t
lines is underway.

Acknowledgments

We would like
H helpful discussion:
b H to the referee for
G
a
Fig. 6. Pictures of weighted graphs.
References

pictures. Therefore, graph drawing algorithms should meet this requirement of congruity or uniqueness of [1] C. Batini, E. N,
generated pictures [17]. The problem of isomorphism determination of general graphs itself is an gorithm for data
intractable problem, and all known algorithms have a running time exponential on the number of vertices Eng. SE-12 (1986
in the worst case [5,9,12]. [2] B. Becker and G.
graphs with fixed
In our system, isomorphic graphs are mostly drawn as the pictures one of which can be identified with 946-972.
the other by translating, rotating, and sometimes reflecting it. Figure 5 illustrates pictures of isomorphic [3] M. Behzad, G. CI
graphs. Figure 5(a) and 5(b) show the picture of two initial states in our algorithm. It is difficult for the & Digraphs (Prii
viewer to detect isomorphism from these pictures, while it is pretty easy to find out isomorphism from the 1979).
respective resultant pictures in Fig. 5(c) and 5(d). The CPU time needed to compute both layouts in Fig. [4] M. Carpano, Aut.
computer-aided d
5(c) and 5(d) is about 2 seconds on a VAX 8600. On the other hand, it takes about 18 minutes CPU time Cybern. SMC-IO
to solve this isomorphism problem combinatorially by a naive thorough search. This "drawing" approach [5] D.G. Corneil and
can be said to be a good approximate method for graph isomorphism, though the pictures of isomorphic graph isomorphis
graphs cannot always be regarded as identical because the total spring energy does not always converge to [6] P. Eades, A heur
the same minimum in our algorithm. The judgement of isomorphism from the resultant pictures could be 42 (1984) 149-16'
[7] P. Eades and R. 1
done automatically by making use of the geometric attributes yielded by the drawing system. In addition, An annotated bit
our algorithm brings a satisfactory result as for the "likeness" of graphs. A small difference between of Compo Sci., UJ
graphs can be found out easily from the pictures. [8] R.W. Floyd, Algc
(1962) 345.
4.3. Weighted graphs [9] J.E. Hopcroft an,
isomorphism of u
Syst. Sci. 7 (1973
In order to model practical problems, weighted graphs in which "weights" are associated with edges are
[10] T. Kamada and ~
often used. It is naturally required to visualize weighted graphs by regarding weights as geometric structures for hill
distances in the drawing. We apply our graph drawing algorithm to weighted graphs straightforwardly. In 88-7, Dep. InL s,
weighted graphs, the distance between vertices is defined as the summation of weights. Figure 6 shows two [11] R. Lipton, S. N
pictures of weighted graphs generated by our system. The overall images regarding weights can be drawing graphs, j

Geometry, 1985, I
understood intuitively from these pictures.

5. Concluding remarks

We have proposed a method of drawing general undirected graphs for human understanding. It can be
widely used in the systems which deal with network structures. Our idea is quite simple and intuitive; the

14
12 April 1989 Volume 31, Number 1 INFORMATION PROCESSING LETTERS 12 April 1989

graph theoretic distance between vertices in a graph is related to the geometric distance between them in
the drawing. As described above, our spring algorithm has many good properties; symmetric drawings, a
relatively small number of edge crossings, and almost congruent drawings of isomorphic graphs. The
precise proof of these properties is left as a challenging problem. And the study of a "constrained" version
of our algorithm by which vertices are constrained, for instance, to lie on concentric circles or on parallel
lines is underway.

Acknowledgments

We would like to thank Mr. Katuhiro Ota (University of Tokyo), who studies graph theory, for many
helpful discussions about the problems in drawing graphs and our drawing algorithm. We are also grateful
to the referee for useful suggestions.

References
ty or uniqueness of [1] C. Batini, E. Nardelli and R. Tamassia, A layout al- [12] H.B. Mittal, A fast backtrack algorithm for graph isomor-
~raphs itself is an gorithm for data flow diagrams, IEEE Trans. Software phism, Inform. Process. Lett. 29 (1988) 105-110.
number of vertices Eng. SE-12 (1986) 538-546. [13] A. Moffat and T. Takaoka, An all pairs shortest path
[2] B. Becker and G. Hotz, On the optimal layout of planar algorithm with expected running time O(n 210g n), Proc.
graphs with fixed boundary, SIAM J. Comput. 16 (1987) Con! Found. Compo Sci., 1985, pp. 101-105.
1 be identified with [14] L.A. Rowe, M. Davis, E. Messinger, C. Meyer, C. Spirakis
946-972.
.ures of isomorphic [3] M. Behzad, G. Chartrand, and L. Lesniak-Foster, Graphs and A. Tuan, A browser for directed graphs, Software
. is difficult for the & Digraphs (Prindle, Weber & Schmidt, Boston, MA, Pract. Exper. 17 (1987) 61-76.
morphism from the 1979). [15] R. Sedgewick, Algorithms (Addison-Wesley, Reading, MA,
loth layouts in Fig. [4] M. Carpano, Automatic display of hierarchized graphs for 1983).
computer-aided decision analysis, IEEE Trans. Syst., Man, [16] P.M. Spira, A new algorithm for finding all shortest paths
minutes CPU time Cybern. SMC-I0 (1980) 705-715. in a graph of positive arcs in average time O(n 210g 2n),
drawing" approach [5] D.G. Corneil and c.c. Gotlieb, An efficient algorithm for SIAM J. Comput. 2 (1973) 28-32.
ures of isomorphic graph isomorphism, J. ACM 17 (1970) 51-64. [17] K. Sugiyama, Achieving uniqueness requirement in draw-
always converge to [6] P. Eades, A heuristics for graph drawing, Congr. Numer. ing digraphs: optimum code algorithm and hierarchy iso-
t pictures could be 42 (1984) 149-160. morphism, Research Rep. No. 58, lIAS-SIS, FUJITSU,
[7] P. Eades and R. Tamassia, Algorithms for drawing graphs: Japan, 1985.
ystem. In addition, [18] K. Sugiyama, S. Tagawa and M. Toda, Methods for visual
An annotated bibliography, Technical Rep. No. 82, Dep.
difference between of Compo Sci., Univ. of Queensland, Australia, 1987. understanding of hierarchical system structures, IEEE
[8] R.W. Floyd, Algorithm 97: shortest path, Comm. ACM 5 Trans. Syst. Man, Cybern. SMC-ll (1981) 109-125.
(1962) 345. [19] R. Tamassia, G. Battista and C. Batini, "Automatic graph
[9] J.E. Hopcroft and R.E. Tarjan, A V log V algorithm for drawing and readability of diagrams", IEEE Trans. Syst.,
isomorphism of triconnected of planar graphs, J. Comput. Man, Cybern. SMC-18 (1988) 61-79.
Syst. Sci. 7 (1973) 323-331. [20] W.T. Tutte, How to draw a graph, Proc. London Math.
ated with edges are
[10] T. Kamada and S. Kawai, Automatic display of network Soc. 3 (1963) 743-768.
ights as geometric structures for human understanding, Technical Rep. No. [21] J.N. Warfield, Crossing theory and hierarchy mapping,
might forwardly. In 88-7, Dep. Inf. Sci., Univ. of Tokyo, Japan, Feb. 1988. IEEE Trans. Syst., Man, Cybern. SMC-7 (1977) 505-523.
:;"igure 6 shows two [11] R. Lipton, S. North and J. Sandberg, A method for
19 weights can be drawing graphs, Proc. ACM Symposium on Computational
Geometry, 1985, pp. 153-160.

standing, It can be
: and intuitive; the

15

You might also like