Kamada GraphDrawing Letters89
Kamada GraphDrawing Letters89
Kamada GraphDrawing Letters89
North-Holland
AN ALGORITHM FOR DRAWING GENERAL UNDIRECTED GRAPHS
Tomihisa KAMADA and Satoru KAWAI
12 April 1989
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( IV I + IE 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
a
Fig. 1. Symmetric and planar drawings of a graph.
12 April 1989
Volume 31, Number 1
is a good way to d
a given graph. Tho
L=L
o
where L
o
is the Ie
The parameter
expression (1) can
real ones for all p
used in (1). Then,
are commonly admitted [4,21]. One is to reduce the number of edge crossings, and the other is to distribute
the vertices and edges uniformly. The reduction of edge crossings can, however, be shown not to be a good
criterion as follows. Pictures with minimal number of edge crossings are not always the best. For example,
Fig. 1 shows two different drawings of a 16-vertex graph. If they are not interested in planarity, almost all
people think Fig. l(a) is better for the representation of internal structure though it has five edge crossings.
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
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
latter condition (uniformity) does imply the total balance. Accordingly, we now pay attention to how
judging the total balance of a layout. In our model, the total balance condition is formulated as the square
summation of the differences between desirable distances and real ones for all pairs of vertices.
3. Graph drawing algorithm
3.1. Spring model
We introduce a dynamic system in which n (= 1V I) particles are mutually connected by springs. Let
PI' P2"'" Pn be the particles in a plane corresponding to the vertices P
b
P
2,
... , P
n
E V respectively. We
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.,
kij=K
where K is a cons
Here we analyz
large, because eve
symmetric graphs
ing E.
3.2. Local minimiz,
We prepare sor
particle in a plane
coordinate variable
by using these 2n
n-
E= L
Our purpose is to
(Hereafter the par
minimum, so we ir
We present a IT
Newton- Raphson
n-l n
E = L L ikij( 1 Pi - Pj I-(jt
i=1 j=i+l
(1)
The state satisfyin]
The partial derivat
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 P
j
in a
graph is defined as the length of the shortest paths between Pi and P
j
[3]. Then the length (j is defined as
(2)
where L is the desirable length of a single edge in the display plane. When the display space is restricted, it
8
We must solve the:
using a 2n-dimens
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
1<)
(3)
9
3.2. Local minimization of global energy
(7)
(8)
(5)
(6)
n-l n
E= L L
Our purpose is to compute the values of these variables which nrininrize E(x
l,
X
2,,
X
n
' 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
Newton-Raphson method [14]. The necessary condition of local minimum is as follows.
aE = aE = 0 f 1
or m n.
aX
m
aYm
We must solve these 2n simultaneous non-linear equations of (6). But they cannot be directly solved by
using a 2n-dimensional Newton-Raphson method, because they are not independent of one another.
The state satisfying (6) corresponds to the dynamic state in which the forces of all springs are balanced.
The partial derivatives of (5) by X
m
and Ym are as follows.
where K is a constant. The parameters lij and k
i)
are symmetric, i.e., 1;) = I
j l
and k
ij
= k
j i
(i =f. j).
Here we analyze the properties of our spring model briefly. The density of particles does not become
large, because every two nodes are forced to keep certain distance by the tension of a spring. Note that
symmetric graphs correspond to symmetric spring systems, which result in symmetric layouts by minimiz-
ing E.
We prepare some definitions for describing the algorithm and outline our method. The position of a
particle in a plane is expressed by x and Y coordinate values. Let (Xl' YI), (X2' Y2), ... , (X
n,
Yn) be the
coordinate variables of particles PI' P2"'" Pn respectively. Then, the energy E defined as (1) is rewritten
by using these 2n variables as follows.
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
(4)
iinimum E in our
ile length between
ces /Ii and /lj in a
h lij is defined as
(2)
(1)
ace is restricted, it
:d by springs. Let
r respectively. We
sult, the degree of
ier is to distribute
1 not to be a good
lest. For example,
inarity, almost all
ve edge crossings.
; should be drawn
e algorithms have
nee of layout is as
aderstanding, The
attention to how
ated as the square
vertices.
Starting from which is equal to the current position (x
m,
Ym)' the following step is iterated:
Ym
comput
comput
comput
initializ
while (I
let p
while
cc
Volume 31, Number 1
}
}
regular n-polygon
positions, the ener
The algorithm i
Here we discuss
preliminarily. (We
tional time is main
son iteration, O( n )
And in the outer 1,
(i -=1= m) after movir
the time needed to
(9)
(16)
(14)
(15)
(13)
(12)
(11)
(10)
12 April 1989
fort=O, 1,2, ...
INFORMATION PROCESSING LETTERS
= + 8y
a
2
E a
2
E st:
_ (x(t) y(t) 8x + (x(t) y(t) 8y = __(x(t) y(t)
a
2 m' m ax ay m' m ax m' m ,
X
m
m m m
a
2E
a
2E
aE
-;:----;:---(x(t) y(t) 8x + - (x(t) y(t) 8y = - _ (x(t) y(t)
ay ax m > m a 2 m' m ay m' m .
m m Ym m
Ll =
m
The unknowns Bx and 8y satisfy a pair of linear equations as follows:
Volume 31, Number 1
The coefficients of above equations (11) and (12), which are the elements of Jacobian matrix, are
computed from the partial derivatives of (7) and (8) by X
m
and Ym as follows.
Then, we adopt another way in which only one particle Pm(x
m,
Ym) is moved to its stable point at a time,
freezing the other particles. That is, viewing E as a function of only X
m
and Ym' we compute a local
minimum of E by using a two-dimensional Newtori-Raphson method. We can obtain a local minimum
which satisfies (6) iterating this step. In each step, we choose the particle that has the largest value of lim'
which is defined as
The unknowns 8x and 8y can be computed from (11)-(16). The iteration (10) terminates when the value
of lim at (x;';), 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
i j
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,
~ compute a local
a local minimum
rgest value of Ll m'
(9)
ig step is iterated:
(10)
(11)
(12)
)bian matrix, are
regular n-polygon circumscribed by a circle whose diameter is La (as for La, see (3)). After initializing the
positions, the energy E is decreased, step by step, by moving a particle to a stable position.
The algorithm is summarized in a simple form as follows.
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;
initialize Pl' P2'"'' Pn;
while (max.zi, > ){
let Pm be the particle satisfying Ll
m
= maxiLl
i
;
while (Ll
m
> ){
compute ox and oy by solving (11) and (12);
x.; :=x
m
+ ox;
Ym:=Ym + oY;
}
}
Here we discuss the computational cost of above algorithm briefly. As for d
i j
, 0(n
3
) time is required
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.
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)
A
c
A
B
c
D
(15)
(14)
(16)
E
E
b
a
c
es when the value
F
F
A
rs of vertices in a
. Next (j and k
i j
imization process,
A
Ie initial positions
E
all particles lie on
c
E
d
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
We show some pictures generated by the system implementing our graph drawing algorithm. Figure 3
shows four pictures of symmetric graphs. Figure 3(a) and 3(d) are the pictures of a regular hexahedron
(cube) graph and regular dodecahedron graph respectively. They look as if they were the projected images
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
A
graphs is visualizr
asymmetric graphs
The CPU time
seconds (Fig. 3(d);
4.2. Isomorphic grc
When the vie"
isomorphic graphs
A
E
G
a
D
M ~ __-;;r__
c
12
L
c
Fig. 3.
K
b
d
12 April 1989
r of vertices (n),
an effective way
the particle B is
is obtained after
nimum of E. In
imple test to the
Volume 31, Number 1
E
L
a
INFORMATION PROCESSING LETTERS
D
Q
b
Fig. 4. Pictures of asymmetric graphs.
F
12 April 1989
rrithm. Figure 3
ular hexahedron
orojected images
igulated internal
rw, symmetry of
graphs is visualized pleasingly as symmetric pictures in our system. Figure 4 shows two pictures of
asymmetric graphs. In these cases needless edge crossings are avoided completely.
The CPU time needed to compute a layout in these pictures is from 0.4 seconds (Fig. 3(a)) to 7.6
seconds (Fig. 3(d)) on a VAX 8600.
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
H
a
G
F
H
c
F
F
H
b
A
d
E
B
A
Fig. 5. Pictures of isomorphic graphs.
13
Volume 31, Number 1
a
INFORMATION PROCESSING LETTERS
c
H
b
G
Fig. 6. Pictures of weighted graphs.
H
12 April 1989
Volume 31, Number 1
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
helpful discussion:
to the referee for
References
pictures. Therefore, graph drawing algorithms should meet this requirement of congruity or uniqueness of
generated pictures [17]. The problem of isomorphism determination of general graphs itself is an
intractable problem, and all known algorithms have a running time exponential on the number of vertices
in the worst case [5,9,12].
In our system, isomorphic graphs are mostly drawn as the pictures one of which can be identified with
the other by translating, rotating, and sometimes reflecting it. Figure 5 illustrates pictures of isomorphic
graphs. Figure 5(a) and 5(b) show the picture of two initial states in our algorithm. It is difficult for the
viewer to detect isomorphism from these pictures, while it is pretty easy to find out isomorphism from the
respective resultant pictures in Fig. 5(c) and 5(d). The CPU time needed to compute both layouts in Fig.
5(c) and 5(d) is about 2 seconds on a VAX 8600. On the other hand, it takes about 18 minutes CPU time
to solve this isomorphism problem combinatorially by a naive thorough search. This "drawing" approach
can be said to be a good approximate method for graph isomorphism, though the pictures of isomorphic
graphs cannot always be regarded as identical because the total spring energy does not always converge to
the same minimum in our algorithm. The judgement of isomorphism from the resultant pictures could be
done automatically by making use of the geometric attributes yielded by the drawing system. In addition,
our algorithm brings a satisfactory result as for the "likeness" of graphs. A small difference between
graphs can be found out easily from the pictures.
4.3. Weighted graphs
In order to model practical problems, weighted graphs in which "weights" are associated with edges are
often used. It is naturally required to visualize weighted graphs by regarding weights as geometric
distances in the drawing. We apply our graph drawing algorithm to weighted graphs straightforwardly. In
weighted graphs, the distance between vertices is defined as the summation of weights. Figure 6 shows two
pictures of weighted graphs generated by our system. The overall images regarding weights can be
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
[1] C. Batini, E. N,
gorithm for data
Eng. SE-12 (1986
[2] B. Becker and G.
graphs with fixed
946-972.
[3] M. Behzad, G. CI
& Digraphs (Prii
1979).
[4] M. Carpano, Aut.
computer-aided d
Cybern. SMC-IO
[5] D.G. Corneil and
graph isomorphis
[6] P. Eades, A heur
42 (1984) 149-16'
[7] P. Eades and R. 1
An annotated bit
of Compo Sci., UJ
[8] R.W. Floyd, Algc
(1962) 345.
[9] J.E. Hopcroft an,
isomorphism of u
Syst. Sci. 7 (1973
[10] T. Kamada and ~
structures for hill
88-7, Dep. InL s,
[11] R. Lipton, S. N
drawing graphs, j
Geometry, 1985, I
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
~ r a p h s itself is an
number of vertices
1 be identified with
.ures of isomorphic
. is difficult for the
morphism from the
loth layouts in Fig.
minutes CPU time
drawing" approach
ures of isomorphic
always converge to
t pictures could be
ystem. In addition,
difference between
ated with edges are
ights as geometric
might forwardly. In
:;"igure 6 shows two
19 weights can be
standing, It can be
: and intuitive; the
[1] C. Batini, E. Nardelli and R. Tamassia, A layout al-
gorithm for data flow diagrams, IEEE Trans. Software
Eng. SE-12 (1986) 538-546.
[2] B. Becker and G. Hotz, On the optimal layout of planar
graphs with fixed boundary, SIAM J. Comput. 16 (1987)
946-972.
[3] M. Behzad, G. Chartrand, and L. Lesniak-Foster, Graphs
& Digraphs (Prindle, Weber & Schmidt, Boston, MA,
1979).
[4] M. Carpano, Automatic display of hierarchized graphs for
computer-aided decision analysis, IEEE Trans. Syst., Man,
Cybern. SMC-I0 (1980) 705-715.
[5] D.G. Corneil and c.c. Gotlieb, An efficient algorithm for
graph isomorphism, J. ACM 17 (1970) 51-64.
[6] P. Eades, A heuristics for graph drawing, Congr. Numer.
42 (1984) 149-160.
[7] P. Eades and R. Tamassia, Algorithms for drawing graphs:
An annotated bibliography, Technical Rep. No. 82, Dep.
of Compo Sci., Univ. of Queensland, Australia, 1987.
[8] R.W. Floyd, Algorithm 97: shortest path, Comm. ACM 5
(1962) 345.
[9] J.E. Hopcroft and R.E. Tarjan, A V log V algorithm for
isomorphism of triconnected of planar graphs, J. Comput.
Syst. Sci. 7 (1973) 323-331.
[10] T. Kamada and S. Kawai, Automatic display of network
structures for human understanding, Technical Rep. No.
88-7, Dep. Inf. Sci., Univ. of Tokyo, Japan, Feb. 1988.
[11] R. Lipton, S. North and J. Sandberg, A method for
drawing graphs, Proc. ACM Symposium on Computational
Geometry, 1985, pp. 153-160.
[12] H.B. Mittal, A fast backtrack algorithm for graph isomor-
phism, Inform. Process. Lett. 29 (1988) 105-110.
[13] A. Moffat and T. Takaoka, An all pairs shortest path
algorithm with expected running time O(n
210g
n), Proc.
Con! Found. Compo Sci., 1985, pp. 101-105.
[14] L.A. Rowe, M. Davis, E. Messinger, C. Meyer, C. Spirakis
and A. Tuan, A browser for directed graphs, Software
Pract. Exper. 17 (1987) 61-76.
[15] R. Sedgewick, Algorithms (Addison-Wesley, Reading, MA,
1983).
[16] P.M. Spira, A new algorithm for finding all shortest paths
in a graph of positive arcs in average time O(n
210g 2n),
SIAM J. Comput. 2 (1973) 28-32.
[17] K. Sugiyama, Achieving uniqueness requirement in draw-
ing digraphs: optimum code algorithm and hierarchy iso-
morphism, Research Rep. No. 58, lIAS-SIS, FUJITSU,
Japan, 1985.
[18] K. Sugiyama, S. Tagawa and M. Toda, Methods for visual
understanding of hierarchical system structures, IEEE
Trans. Syst. Man, Cybern. SMC-ll (1981) 109-125.
[19] R. Tamassia, G. Battista and C. Batini, "Automatic graph
drawing and readability of diagrams", IEEE Trans. Syst.,
Man, Cybern. SMC-18 (1988) 61-79.
[20] W.T. Tutte, How to draw a graph, Proc. London Math.
Soc. 3 (1963) 743-768.
[21] J.N. Warfield, Crossing theory and hierarchy mapping,
IEEE Trans. Syst., Man, Cybern. SMC-7 (1977) 505-523.
15