1987 - A Versatile Graph Structure For Edge-Oriented Graph Algorithms (Ebert1987AVD)
1987 - A Versatile Graph Structure For Edge-Oriented Graph Algorithms (Ebert1987AVD)
Edgar H. Sibley An abstract graph module that allows for easy and secure programming of a
Panel Editor
great number of graph algorithms is implemented by symmetrically stored
forward and backward adjacency lists, thus supporting edge-oriented
traversals of general directed and undirected graphs.
JiiRGEN EBERT
It is widely accepted that graphs are a useful In this article we describe an abstract module for
medium for modeling relevant parts of reality in graph handling that is especially suited for the edge-
computer programs. Graphs are rather natural oriented paradigm of programming graph algorithms,
models for road maps, electrical networks, chemical and show how this module can be implemented effi-
structure formulas, data and control flow of com- ciently in Algol-like languages. This graph realiza-
puter programs, state spaces of discrete games, socio- tion is of the adjacency-list type and is suitable for
logical diagrams, timetables, etc. In addition, many directed and undirected graphs (with multiple edges
discrete problems in several areas (e.g., formal lan- allowed). Undirected graphs are represented as di-
guage theory, automata theory, compiler construc- rected graphs through arbitrary assignment of direc-
tion, operating-systems theory, operations research) tions to every edge (and not through storage of the
can be transformed into equivalent graph problems corresponding symmetric graph). This representation
and then solved using graph algorithms. has been used successfully in a number of applica-
Representation greatly influences the efficiency tions; for instance, it has been used as a tool for
of graph algorithms. Often, linearity can only be representing the graphs in the EMS project on the
achieved through appropriate storage of adjacency implementation of functional languages [3].
information (see, e.g., [6]). There are several differ-
ent ways of internally representing graphs in pro- GRAPHS
cedural languages, including adjacency matrices, There is a large variety of graph types. Depending
sequential or linked adjacency lists, and edge lists on the area of application, graphs can be directed or
(cf. [Z]). On the other hand, good and clear algorithm undirected, weighted or unweighted, and ordered
design is greatly enhanced by rather abstract graph or unordered. Multiple edges and loops are either
representations that include operations on the graph, permitted or forbidden. Here, we present a graph
as well as control statements (e.g., for-loops) trig- representation that is suitable to all of these variants
gered by the graph. ([l] and [5] are introductory books on graph theory;
[Z] and [4] introduce graph algorithms).
01987 ACMOOOI-0782/87/0600-0513 750 Using a very general type of graph definition, a
The sign of the edges makes it possible to talk about Note that these functions assume an arbitrary but
the “other” vertex. fixed order on all sets. For handling “signed” edges,
Using loops over edges and giving a state to the some auxiliary transfer functions like those listed in
edges allow a straightforward translation for for- Figure 3 are necessary. Figure 4 shows how the
loops into a combination of a whi le-loop and some bridge detection algorithm of Figure 1 can be pro-
standard function calls, since signed edges contain
enough information to (re)enter a while-loop to
process the next edge. This is not possible for loops
over (e.g., successor) vertices. Furthermore, travers- vertex procedure alpha (e : edge)
returns start vertex of e.
ing neighborhoods by explicitly looking at all inci- vertex procedure omega (e : edge)
dent edges clarifies the fact that successors are listed returns end vertex of e.
more than once, if multiple edges exist. (A vertex- vertex procedure this (e : edge)
oriented for-loop is often valid only for graphs returns start vertex of e, if e is positive,
without multiple edges.) and end vertex, otherwise.
vertex procedure that (e : edge)
In addition, the sign on the edges allows sufficient returns end vertex of e, if e is positive,
information to be kept for deferred processing, if-as and start vertex, otherwise.
in some search algorithms-edges are stored for later edge procedure normal (e : edge)
use in an intermediate data structure. When an edge returns edge e with positive direction.
is retrieved, its sign helps to deduce its provenance. edge procedure reverse (e : edge)
returns edge e with reversed direction.
Assigning directions (signs) to edges also helps to
distinguish incoming from outgoing edges during un-
directed searches in directed graphs. Furthermore it FIGURE3. Auxiliary Transfer Procedures for Handling “Signed” Edges
helps to describe the direction of edges in (undi-
rected) cycles and/or cuts of directed graphs.
As an example, Figure 1 shows an edge-oriented
pseudocode version of a bridge detection algorithm procedure DFS (v : vertex);
(derivable from [6]). Note that this algorithm works LOWPT[v] := NUMBER[v] := NUM := NUM + 1;
e := first (v);
on undirected graphs, though the input graph might while e <> 0 do
be directed. Here, the PARENT-entries, which de- w := that (e);
note a spanning tree in a multigraph, contain edges if NUMBER[w] = 0 then
(instead of vertices), since there is at most one in- PARENT[w] := normal (e);
coming tree edge for every vertex. DFS (w);
if LOWPT[w] 2 NUMBER[w] then
output (e, "is bridge")
GRAPH OPERATIONS fi;
We now give the description of a module (in the LOWPT[v] := min (LOWPT[v], LOWPT[w])
sense of an abstract data structure) for implementing else
edge-oriented algorithms in Algol-like languages. if NUMBER[v] 1 NUMBER[w]
and normal(e) <> PARENT[v] then (*)
For programming a pseudostatement like the for-
LOWPT[v] := min (LOWPT[v], NUMBER[w])
loop over the outward star from above, we use tra- fi
versal functions first-out and next-out and an fi;
auxiliary function omega according to the following: e := next (e)
od;
e :=first-out (v); NUM := 0;
while e<>O do v := first-vertex ( );
w:=omega (e); while II <> 0 do
PARENT[v] := NUMBER[v] := 0;
process w; v := next-vertex (v)
e:=next-out(e) od;
od. v := first-vertex ( );
while v <> 0 do
Note that this is a very straightforward way of trans- if NUMBER(v] = 0 then
lating for-loops, which is made possible by the “di- DFS (v)
rection” (sign) assigned to every edge value. Since fi;
the edges denoted by e point outward, it is possible II := next-vertex (v)
od.
to identify the “next” edge as the next one going out
of the same start vertex.
Figure 2 lists the traversal functions necessary for FIGURE4. A Concrete Program for Bridge Detection, Showing
the translation of the pseudocode for-statements. How the Procedures from Figures 2 and 3 Can Be Used
Using an array
vertex procedure alpha (e : edge);
NODE : array [edge] of vertex return NODE[--abs(e)].
vertex procedure omega (e : edge);
we can store cy(e)in NODE [-e] and w(e) in return NODE[abs(e)].
NODE [+e] for every edge e. This allows all edge- vertex procedure this (e : edge);
list-oriented algorithms to be used on our structure return NODE[-e].
by accessing the array NODE alone. vertex procedure that (e : edge);
return NODE [ e] .
Conversely, the adjacency lists for every vertex z, edge procedure normal (e : edge);
are made traversable by using array indexes as links: return abs(e).
edge procedure reverse (e : edge);
FIRST : array [vertex] of edge return -e.
NEXT : array [edge] of edge
These arrays link all edges incident with a vertex v FIGURE7. Implementation of the Auxiliary Procedures
to v by using the chain of indexes starting at from Figure 3, Assuming Validity of Input Parameters
FIRST [v] , following the NEXT-entries, and ending
with a zero-entry. The indexes are positive for edges
going out of v and negative for edges going into v. this array suffices. Figure a shows how the graph
Then, the nonzero entries in the FIRST/NEXT- representation described in the previous section can
arrays are the signed edges themselves, with their be reconstructed from its NODE-array in linear time.
direction seen from the corresponding vertex. This algorithm traverses the edge list in reverse or-
Figure 5 gives an example by showing a sample der. It inserts each (positive) edge e at the front of
graph and its array representation. Figure 6 shows the adjacency list of a(e) and inserts -e to the list
how the traversal procedures of Figure 2 can be im- of w(e).
plemented using our structure, and Figure 7 gives
the implementation of the auxiliary functions of Fig-
ure 3. (Note that in practical applications all gener-
ally usable procedures should check their argu-
for v := 1 to n do
ments. These checks have been skipped to simplify
FIRST[v] := 0
the presentation.) od;
These implementations of the traversal functions for e := m downto 1 do
lead to a complexity of for-loops over (for/back- NEXT[e] := FIRST[NODE[-e]] ;
ward) stars of a vertex v proportional to y(v). FIRST [NODE [-e] ] := e;
NEXT[-e] := FIRST[NODE[e] I ;
Equally, for-loops over V and E have a complexity
FIRST[NODE[e]] := -e
proportional to n and m, respectively. All the imple- od.
mentations of the auxiliary functions apparently use
constant time.
The first/next-pair of functions enumerates FIGURE8. Reconstruction of the Graph Representation
self-loops twice. If this is not wanted, first and from Its NODE-Array in Linear Time
next should be modified to ignore negative edge
values e, if
NODE [e] = NODE [-e] . This algorithm is order preserving: If the NODE-
array is compatible with all the adjacency lists, then
The graph module can of course be augmented by the adjacency lists are reconstructed as they were
additional operations, if they are needed, such as before. On the other hand, if the NODE-array gets
procedures for determining degrees, for testing the sorted (e.g., according to some weight), then all adja-
existence of edges, etc. Note that is-edge ( v, w) - cency lists are sorted as well-after reconstruction.
tests have a complexity at least proportional to This procedure might also be used for simplifying
min (Y(V), r(W graph input to simply reading an edge list. Graph
output can be performed by just printing the edge list
RECONSTRUCTION AND COMPRESSION while traversing the NODE-array. If, however, the
The NODE-array alone (being an edge-list representa- NODE-array order is not compatible with the orders
tion of the graph) already contains all incidence in- of the adjacency lists, some topological sorting has to
formation. Thus, for example, for external storage, be done when the graph is written to an external
procedure test-and-mark (e : edge): Using the zero-entries in these arrays (which are
if e <> 0 and not is-marked (e) then unused up to now) as a start pointer, we can link the
mark (e); unused entries together following a stack ‘discipline.
if is-marked (-e) then To distinguish used from unused entries, we add a
enqueue (abs(e)) large value LARGE (preferably MMAX) to the link val-
fi;
if NODE[-e] = NODE[e] then ues in FIRST and NEXT, if we use them for chaining
test-and-mark (-e) unused entries.
fi In this case the graph should be initialized as an
fi; empty graph using the following procedure:
init-queue ( ); procedureinit ( );
init-marking ( );
for Y := 1 to n do forv:=OtoNMAXdo
test-and-mark (FIRST[v]) FIRST [ v] := v+ 1 + LARGE
od; od;
while not is-empty-queue ( ) d6 fore:=OtoMMAXdo
e := extract-front-of-queue ( ); NEXT[-e] := 0;
output (NODEI-e], "A', NODE[e]);
test-and-mark (NEXT[ej); NEXT [e] :=e+ 1 +LARGE
test-and-mark (NEXT[-e]) od;
od. n .=*
. .=
. 0.
DYNAMIC GRAPHS
Many applications use graphs whose size and struc-
vertex procedure create-vertex ( );
ture change during execution. This implies a need returns a new vertex.
for procedures to create and delete vertices and procedure delete-vertex (v : vertex);
edges. The representation presented in this article deletes vertex v.
can easily be extended for graphs with a (moder- edge procedure create-edge (v, W : vertex);
ately) varying number of vertices and/or edges, as returns a new edge.
procedure delete-edge (e : edge);
long as a maximum number (NMAX/MMAX) can be deletes edge e.
given for both. Figure 10 gives the procedures neces-
sary for handling dynamic graphs.
To implement dynamic graphs, we link all unused FIGURE10. Creation and Deletion Procedures
(nonnegative) entries in the FIRST/NEXT arrays. for Handling Dynamic Graphs
CONCLUSION
vertex procedure create-vertex ( );
II := FIRST[O] - LARGE; Following the paradigm of edge orientation, our
FIRST[O] := FIRST[v]; software module for general graphs and their imple-
FIRST[v] := LAST[v] := 0; mentation on von Neumann machines allows for
n := n+ 1; easy and secure programming of a great number of
return v.
procedure delete-vertex (v : vertex);
graph algorithms, since traversal of forward and
while FIRST[v] <> 0 do backward stars as well as edge enumeration is par-
delete-edge (FIRST [ v] ) ticularly easy. Since there is no distinction between
od; a directed graph and its underlying undirected
FIRST[v] := FIRST[O]; graph, our module allows a very straightforward
FIRST[O] := " + LARGE;
n := n - 1.
transliteration of published algorithms into concrete
edge procedure create-edge (v, w : vertex); programming code.
e := NEXT[O] - LARGE; The module has successfully been used in various
NEXT[O] := NEXT[e]; applications. A version written in the C language is
NEXT[e] := 0; one of the basic cornerstones of the EMS system for
In := In + 1;
create-entry (Y, e) ;
the implementation of functional languages by
create-entry (w, -e) ; graphs.
return e.
procedure delete-edge (e : edge);
e := abs (e);
delete-entry (NODE [-e] , e) ; REFERENCES
1. Berge. C. Graphs and Hypergraphs. North-Holland, Amsterdam, 1973.
delete-entry (NODE[e], -e); A textbook on graph theory.
NODE [-e] := NODE[e] := NEXT[-e] := 0; 2. Ebert. J. Effiziente Graphenalgorithmen.Aula, Wiesbaden. West Ger-
NEXT [e] := NEXT[O] ; many. 1981. A textbook on graph algorithms and their implementa-
NEXTlO] := e + LARGE; tion in Algal-like languages using abstract data and refinements.
3. Ebert, J. Implementing a functional language on a vom Neumann
In :=m- 1. computer. Tech. Rep. 3/&i, EWH Koblenz, Fachbericht Informatik,
Mar. 1985. A report on an implementation technique for functional
languages using attributed and ordered directed graphs.
FIGURE11. Implementation of the Creation and Deletion 4. Even, S. Graph Algorithms. Pitman, Marshfield, Mass., 1979. A text-
Procedures, Assuming Validity if Input Parameters book on graph theory featuring algorithms.
5. Harary, F. Graph Theory. Addison-Wesley, Reading, Mass., 1969.
and without Checking Overflow Conditions A textbook on graph theory.
6. Tarjan, R.E. Depth-first search and linear graph algorithms. SIAM J.
Comput. I, 2 (1972). 146-160. A fundamental paper showing that
some connectivity problems are solvable in linear time using depth-
first search on adjacency-list representations of graphs.
procedure create-entry (v : vertex, e : edge);
if FIRST[v] = 0 then
FIRST[v] .= e
else CR Categories and Subject Descriptors: E.1 [Data]: Data Structures-
arrays: graphs; lists; E.2 [Data]: Data Storage Representations-contiguous
NEXT[LAST [VI] := e representafions;linked representations: F.2.2 [Analysis of Algorithms and
fi; Problem Complexity]: Nonnumerical Algorithms and Problems-compu-
LAST[v] := e; tations on discrete structures; G.2.2 [Discrete Mathematics]: Graph The-
NODE[-e] := v. ory-graph algorithms
General Terms: Algorithms
procedure delete-entry (v : vertex, e : edge); Additional Key Words and Phrases: Edge-oriented algorithms, graph
if FIRST [v] = e then traversal
FIRST[v] := NEXT[e];
if LAST [v] = e then
LAST[v] := 0
fi Received 6/86; accepted U/86
else
i := FIRST[v];
while NEXT[i] <> e do
i := NEXT[i] Author’s Present Address: Jiirgen Ebert. EHW Koblenz, Informatik,
od; Rheinau 3-4, 5400 Koblenz, West Germany.
NEXT[i] := NEXT[e];
if LAST [v] = e then
LAST[v] := i
fi Permission to copy without fee all or part of this material is granted
provided that the copies are not made or distributed for direct commer-
fi. cial advantage, the ACM copyright notice and the title of the publication
and its date appear, and notice is given that copying is by permission of
the Association for Computing Machinery. To copy otherwise, or to
FIGURE12. Auxiliary Creation and Deletion Procedures republish, requires a fee and/or specific permission.