Sunteți pe pagina 1din 133

4.1.

Graphs and their Representation in a Computer

Chapter 4. Algorithms on Graphs

In this hapter we deal with e ient algorithms for many basi problems on erning
graphs: topologi al sorting and transitive losure, onne tivity and bi onne tivity,
least ost paths, least ost spanning trees, network ow and mat hing problems,
and planarity testing. Most of these algorithms require methods for the systemati
exploration of a graph. We will introdu e su h a method in Se tion 4.4 and then
spe ialize it to breadth- rst-sear h and depth- rst-sear h. Novi e readers should
read Se tions 4.1, 4.2, 4.4 and 4.5 before pro eeding to higher numbered se tions;
readers, whi h have some familiarity with graph algorithms, may plunge into any
se tion dire tly.
4.1. Graphs and their Representation in a Computer

A dire ted edge over a set V is an element of V  V . Given a dire ted edge
e = (v; w), v is alled its tail and w its head, both v and w are alled endpoints
of e. A dire ted edge e = (v; w) is said to leave its tail v and to enter its head w.
An undire ted edge over V is a subset of V of ardinality exa tly two. Given
an undire ted edge e = fv; wg, v and w are alled its endpoints. A dire ted or
undire ted edge is said to be in ident on its endpoints. If e = (v; w) is a dire ted
edge with v 6= w, the reverse e 1 of e is the dire ted edge (w; v), and the undire ted version of e is the undire ted edge fv; wg. The dire ted versions of an
undire ted edge fv; wg are the two edges (v; w) and (w; v).
Given a nite, nonempty set V , a (dire ted, undire ted) graph on the vertex
set V is a pair G = (V; E ), where E is a set of (dire ted, undire ted) edges over V .
The elements of V are alled the verti es or nodes of G and the elements of E are
alled the edges of G. We use graph to denote both undire ted and dire ted graph
and frequently use digraph instead of dire ted graph.
Given a dire ted graph G = (V; E ), the undire ted version of G is the undire ted graph (V; ffv; wg; (v; w) 2 E g). Given an undire ted graph G = (V; E ),
the dire ted version of G is the dire ted graph (V; f(v; w); fv; wg 2 E g), i.e., ea h
undire ted edge is repla ed by its two dire ted versions. The edges of the dire ted
version of an undire ted graph G are alled the darts of G.
Let G = (V; E ) be a digraph. A path from v to w, where v; w 2 V , is a
sequen e v0 ; v1 ; : : : ; vk of nodes su h that v0 = v; vk = w and (vi ; vi+1 ) 2 E for
0  i < k; k is the length of the path. Note that there is always the path of length
zero from v to v. A path is simple if vi 6= vj for 0  i < j < k. A y le is a path
from v to v. If, in addition, the path is simple then the y le is simple. A y le
in a dire ted graph is trivial if its length is 0, otherwise, it is non-trivial. A path
(simple path, y le, simple y le) in an undire ted graph G is a path (simple path,
y le, simple y le) in the dire ted version of G. A y le in an undire ted graph is
trivial if its length is either 0 or 2, otherwise, it is non-trivial.
Version: 19.10.99

Time: 11:01

{1{

Chapter 4. Algorithms on Graphs

A graph is a y li if it does not ontain any non-trivial y les. Let T  E . We


 w if there is a path from v to w using only edges in T . We write v !
+
write v !
w
T
T
if the length of the path is at least 1 and we use v !
w to denote the existen e of
T
the edge (v; w) 2 T .
The indegree of a node v in a dire ted graph is the number of edges entering v,
indeg G (v) = jfw; (w; v) 2 E gj. Similarly, the outdegree of v is the number of
edges starting in v, outdegG (v) = jfw; (v; w) 2 E gj. The degree of a vertex v in
an undire ted graph is the number of edges in ident to v.
A graph G0 = (V 0 ; E 0 ) is a subgraph of G = (V; E ), if V 0  V and E 0  E .
If G = (V; E ) is a graph and V 0  V , then the subgraph indu ed (or spanned)
by V 0 is (V 0 ; E 0 ), where E 0 onsists of those edges of E whi h have both endpoints
in V 0 . G V 0 denotes the subgraph indu ed by V V 0. If V 0 = fvg is a singleton,
then we write G v instead of G fvg.
A digraph A = (V; T ) is a dire ted in-forest (out-forest, respe tively) if
A is a y li and indeg A (v)  1 (outdeg A (v)  1) for all v 2 V . A node v with
indeg A (v) = 0 (outdeg A (v) = 0) is alled a root of the forest. Note that a dire ted
forest has at least one root. If jT j = jV j 1, then A = (V; T ) is a dire ted tree.
In a dire ted tree there is a single root r. Also, an in-tree has a unique path from
the root to any node v and an out-tree has a unique path from any node v to the
root. Finally, if v is any node of an in-tree (out-tree), then the subtree Av rooted
at v is the subgraph indu edby the des endants
(prede essors) of v, i.e., Av is the
 vg).
subgraph indu ed by fw; v !
wg (fw; w !
T
T
Let G = (V; E ) be a digraph. A dire ted forest A = (V; T ) with T  E is
alled a spanning forest of G. If A is a tree then it is alled a spanning tree
of G. A spanning forest (tree) of an undire ted graph G is the undire ted version
of a spanning forest (tree) of the dire ted version of G.
Heaving laid out these basi de nitions, we are now ready to dis uss algorithmi
questions. For the algorithmi treatment of graphs, we assume that the verti es of
a graph G = (V; E ) are numbered from 1 to jV j, i.e., we assume V = f1; 2; : : : ; jV jg.
We also set n = jV j and m = jE j. The rst question is the representation problem: how to store a graph in a omputer. Two methods of storing a digraph are
ustomary.
a) Adja en y matrix: A digraph G = (V; E ) is represented by a n  n boolean
matrix AG = (aij )1i;jn with

E;
aij = 01 ifif ((i;i; jj )) 2
2= E .
The storage requirement of this representation is learly (n2 ).
b) Adja en y lists: A digraph G = (V; E ) is represented by n linear lists. The i-th
list ontains all nodes j with (i; j ) 2 E . The headers of the n lists are stored
in an array. The storage requirement of this representation is O(n + m). The
lists are not ne essarily arranged in sorted order.
Version: 19.10.99

Time: 11:01

{2{

4.1. Graphs and their Representation in a Computer

3
5
Graph GEx

Figure 1.

0
B0
B
B0

0
1

1
1
0
0
0

Figure 2.

0
1
0
1
0

0
1
0
0
0

01
1C
0C
C
1A
0

Representation of GEx by adja en y matrix

Figure 3.

Representation of GEx by adja en y lists

Figures 1 to 3 show an example digraph and its representation by adja en y matrix


and adja en y lists.
Sin e 0  m  n2 , we on lude that the adja en y list representation is often
mu h smaller than the adja en y matrix representation and never mu h larger.
Sin e most graphs whi h arise in appli ations are sparse, i.e., m  n2, this is an
Version: 19.10.99

Time: 11:01

{3{

Chapter 4. Algorithms on Graphs

important point to keep in mind. The fa t that the hoi e of the representation an
have a drasti in uen e on the time omplexity of graph algorithms is even more
important. In this hapter, we will see that many graph problems an be solved
in linear time O(n + m) if the adja en y list representation is used. However, any
algorithm for these problems using the matrix representation must have running
time
(n2 ), f. Se tion 4.2. For this reason we will always use the adja en y list
representation if not expli itly stated otherwise ( f. Chapter 5).
To go into further detail, the adja en y list representation is based on the
following de larations:
type edge = re ord node : [1 : : n;
..
.
next :"edge
end

and

adjhead :

array[1 : : n of "edge .
Array adjhead ontains the heads of the adja en y lists. The elements of the adja en y lists are of type edge , ea h element representing an edge. In some ases these
elements will ontain additional information, e.g., the ost of an edge in least ost
path problems, the apa ity of an edge in ow problems, the apa ity and the ost
of an edge in min ost ow problems, : : : .
An undire ted graph G is represented by its dire ted version. We also assume
that ea h dart of G has a link to its reverse dart, i.e., there is a eld reverse : "edge
in the edge re ord and this eld in the re ord representing the dart (v; w) points to
the re ord representing the dart (w; v) for every fv; wg 2 E .
Exer ises 1) and 2) dis uss the problem of how to onvert other graph representations into the one postulated above.

Version: 19.10.99

Time: 11:01

{4{

4.2. Topologi al Sorting and the Representation Problem

4.2. Topologi al Sorting and the Representation Problem

A topologi al sort of a digraph G = (V; E ) is a mapping ord : V ! f1; : : : ; ng


su h that for all edges (v; w) 2 E we have ord (v) < ord (w). Clearly, if a graph G
has a topologi al sort then G is a y li . The onverse is also true and is easily
proved by indu tion on the number of nodes. So suppose, G = (V; E ) is a y li . If
n = 1, then G has a topologi al sort. If n > 1, then G must have a node v with
indegree 0. (Su h a node an be found by starting at an arbitrary node w and
traversing edges in reverse dire tion. Sin e the graph is a y li , no node is entered
twi e in this pro ess, and hen e the pro ess terminates. It terminates in a node
with indegree 0.) By the deletion of v we obtain an a y li graph G0 with one node
less. By the indu tion hypothesis G0 has a topologi al sort and so has G.
A tually, the argument given above is an algorithm for omputing the mapping
ord . We formulate it in Program 1.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)

G urrent G; ount 0;
while G urrent has at least one node with no prede essor
do let v be a node with no prede essor;
ount
ord [v

ount + 1;
ount ;

G urrent G urrent v
od;
if G urrent is nonempty
then G is y li else G is a y li fi.
Program 1

The orre tness of Program 1 follows immediately from the pre eding dis ussion. With respe t to omplexity the ru ial lines are lines (3) and (6). How do we
e iently nd a node with indegree 0 in line (3)? A brute for e approa h would be
a omplete sear h of graph G urrent. Sin e su h a sear h would at least take time

(n), the entire algorithm would be


(n2 ) at best.
A better approa h is to exploit the interdependen e of lines (3) and (6). In
line (6) node v and all edges leaving v are deleted. This hanges the indegrees of
exa tly those verti es whi h are heads of edges leaving v. It is therefore reasonable
to use an array indeg [1 : : n to store the urrent indegree of all nodes. Array indeg
is updated in line (6). In line (3) we need to know one node with indegree 0. The
indegree of a node an only be ome zero in line (6) and it is easy to dete t this
fa t there. It is therefore wise to keep all nodes of G urrent with indegree 0 in a set
zeroindeg .
Program 2 re nes our algorithm and makes use of the variables indeg : array
[1 : : n of integer and zeroindeg : subset of V . The graph G urrent is not stored
expli itly. Instead, we store it impli itly by using the fa t that G urrent is the
subgraph of G indu ed by the nodes whi h have not been given a number ord yet.
Version: 19.10.99

Time: 11:01

{5{

Chapter 4. Algorithms on Graphs

The set zeroindeg ontains the nodes of zero indegree in G urrent and indeg maps
ea h node to its indegree in G urrent. Initially, G urrent = G and so indeg should
be initialized to the indegrees in G. This an be done e iently by traversing all
adja en y lists.
(1.1)
(1.2)
(1.3)
(1.4)
(1.5)
(1.6)
(1.7)
(1.8)
(1.9)
(1.10)
(2)
(3.1)
(3.2)
(4)
(5)
(6.1)
(6.2)
(6.3)
(6.4)
(6.5)
(7)
(8)
(9)

ount 0;
zeroindeg

;; for all i 2 V do indeg [i 0 od;


for all i 2 V
do for all j 2 V with (i; j ) 2 E
do indeg [j indeg [j + 1
od
od;
for all i 2 V
do if indeg [i = 0 then add i to zeroindeg fi
od;
while zeroindeg 6= ;
do let v be any node in zeroindeg ;
delete v from zeroindeg ;
ount
ord [v

ount + 1;
ount ;

for all w 2 V with (v; w) 2 E


do indeg [w indeg [w 1;
if indeg [w = 0
then add w to zeroindeg fi
od

od;
if ount < n
then halt (\graph is y li ") else halt (\graph is a y li ") fi.
Program 2

An implementation for set zeroindeg remains to be spe i ed. On this set the
following operations are performed: insertion of an arbitrary and deletion of an
unspe i ed element, and test for emptiness. In Chapter 1 we saw that implementing
zeroindeg by a sta k or by a queue will allow us to exe ute ea h of these operations
in time O(1). We prefer the sta k be ause of its simpli ity and higher e ien y, so
zeroindeg is a sta k of elements of V (sta k of [1 : : n).
Finally, we need to explain lines (1.4) and (6.1) in detail. They are realized by
traversing the adja en y list orresponding to the nodes i and v respe tively and
take time proportional to the outdegree of those nodes. Program 3 gives a detailed
implementation of lines (1.4) and (1.5). (p is of type "edge .)
We are now able to determine the performan e of our algorithm for topologi al
sorting. Line (1.1) takes time O(1), line (1.2) and lines (1.8){(1.10) take time O(n).
The exe ution of lines (1.4) to (1.6) for a xed i takes time O(outdeg G(i)) and
Version: 19.10.99

Time: 11:01

{6{

4.2. Topologi al Sorting and the Representation Problem

p adjhead [i;
while p 6= nil
do j p":name ;
indeg [j indeg [j + 1;
p p":next
od;

Program 3

hen e lines (1.3){(1.7) take time O(n + m). Altogether, the initialization takes time
O(n + m). The main loop is exe uted O(n) times and hen e the total time spent
in lines (3.1), (3.2), (4) and (5) is O(n). For a xed v, lines (6.1){(6.5) take time
O(outdeg G (v)). Sin e every node v is deleted from zeroindeg at most on e the total
running time of that loop is O(n + m). This shows that the running time of the
entire algorithm is O(n + m).
Theorem 1. A topologi al sort of digraph G = (V; E ) an be omputed in linear
time O(n + m).
Proof :

Given by the dis ussion above.

Next we will show that any algorithm is doomed to ine ien y if we store the graph
in the form of a matrix.
Theorem 2. Any algorithm for topologi al sorting whi h re eives the digraph as
an adja en y matrix has running time
(n2 ).
Proof :

Consider the behavior of any su h algorithm on the empty graph, i.e., on


the entire zero matrix. Suppose, there is a pair i; j of nodes, i 6= j , su h that
the algorithm neither inspe ts aij nor aji. Then we ould hange both entries to
one and the algorithm would still return a topologi al sort. However, the graph
is y li after having added edges (i; j ) and (j; i). This shows that the algorithm
has to inspe t at least half of the entries of the matrix and hen e has running time

(n2).
We saw that a topologi al sort of an a y li digraph an be omputed in linear time.
Given the mapping ord : V ! f1; : : : ; jV jg it is easy to rearrange the adja en y lists
in in reasing order as follows: Generate set f(ord (v); ord (w)); (v; w) 2 E g and sort
it using bu ket sort. This takes time O(n + m) by Se tion 2.2.1 and generates the
adja en y lists in sorted order. An alternative linear time algorithm for topologi al
sorting will be given in Se tion 4.5.

Version: 19.10.99

Time: 11:01

{7{

Chapter 4. Algorithms on Graphs

4.3. Transitive Closure of A y li Digraphs

Let G = (V; E ) be a digraph. The digraph G = (V; E  ), where (v; w) 2 E  if


and only if there is a path from v to w in G, is alled the re exive, transitive
losure of G or simply transitive losure. In this se tion we present an algorithm for
omputing the transitive losure of an a y li digraph; the algorithm is extended to
general digraphs at the end of Se tion 4.6. We will assume that the a y li digraph
is topologi ally sorted, i.e., (i; j ) 2 E implies i < j and that the adja en y lists are
sorted in in reasing order. We saw in the previous se tion that this an be a hieved
in linear time O(n + m).
The idea underlying the algorithm is very simple. We onsider the nodes of G
in de reasing order. Suppose that we onsider node i. Then for every j> i we have
already omputed the set of nodes rea hable from j , rea h [j = fk; j ! kg. Then
[
rea h [i = fig [
rea h [j :
(i;j )2E

This equation demonstrates that rea h [i an be omputed by outdeg (i) union operations on sets of nodes. For many graphs the number of union operations required
to ompute rea h [i an be redu ed onsiderably as follows. We onsider the edges
(i; j ) emanating from i in in reasing order of j . When edge (i; j ) is onsidered, we
rst test whether j 2 rea h [i is at this stage already. If this is the ase, then there
must be a node h 6= j with i ! h ! j and hen e rea h [h  rea h [j . Thus we do
not have to add rea h [j to rea h [i. Program 4 gives the omplete algorithm.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)

for i from n downto 1


do rea h [i fig;
for all j with (i; j ) 2 E
do if j 2= rea h [i
then rea h [i rea h [i [ rea h [j
fi
od
od.
Program 4

in in reasing order!! o

How should we represent the set rea h [i? We re ommend oding rea h [i as a
bit ve tor array[1 : : n of boolean. Then lines (2) and (5) take time O(n) ea h and
line (4) takes time O(1). Sin e line (2) is exe uted exa tly on e for ea h node and
line (4) exa tly on e for ea h edge, the running time is O(n2 + m + m0  n) where
m0 is the number of edges (i; j ) for whi h line (5) is exe uted.
De nition: Let G = (V; E ) be an a y li digraph. Let Ered = f(i; j ) 2 E ; there
is no path of length at least two from i to j in Gg, let Gred = (V; Ered ), and let
mred = jEred j. Gred is alled the transitive redu tion of G.
Version: 19.10.99

Time: 11:01

{8{

4.3. Transitive Closure of A y li Digraphs

Lemma 1. Let G = (V; E ) be an a y li digraph.


a) G = (Gred) .
b) The algorithm orre tly omputes the transitive losure.
) If line (5) is exe uted for (i; j ) i (i; j ) 2 Ered.
Proof : a) (Gred) is ertainly a subgraph of G . In order to prove the onverse
onsider any (i; j ) 2 E  . Let i0 ; i1 ; : : : ; ik be a path of maximal length form i = i0
to j = ik . Then (il ; il+1 ) 2 Ered for all l, 0  l < k, and hen e (i; j ) 2 (Ered) .

b) It is obvious that our algorithm omputes a subset of the transitive losure.


Suppose that it  omputes a proper subset. Then let i be maximal su h that a node
h exists with i !
h and h is never added to rea h [i. Consider a maximal length
path i0 ; : : : ; ik from i = i0 to h = ik . Then h 2 rea h [i1 by de nition of i. Also,
(i0 ; i1 ) 2 Ered. If the test in line (4) is exe uted with j = i1 then j 2= rea h [i
be ause there is no path of length at least two from i0 to i1 ; otherwise the path
would not be of maximal length. Hen e h is added to rea h [i in line (5) if it is not
there already.
+
) Suppose that (i; j ) 2 E Ered. Then h exists with (i; h) 2 Ered and h !
j.
E
Hen e j is added to rea h [i when edge(i; h) is onsidered in loop (3) to (6). Thus
j 2 rea h [i when edge(i; j ) is onsidered. Conversely, if (i; j ) 2 Ered then line (5)
is ertainly exe uted.
Theorem 1. The transitive losure of an a y li digraph G = (V; E ) an be omputed in time O(n  (n + mred)) = O(n3 ).
Proof : We have argued above that the running time is O(n2 + m + m0  n), where
m0 is the number of edges for whi h line (5) is exe uted. By Lemma 1 we have
m0 = mred  m. Also, m  n2 .
Of ourse, mred  m. Unfortunately, mred = m = O(n2 ) an o ur. Consider for
example V = f1; : : : ; ng and E = f(i; j ); i  n=2 < j g. In general however, mred is
onsiderably smaller than m. We will support this laim by an analysis of random
digraphs below. Before doing so, we will slightly improve upon the running time
of the algorithm by using pa ked bit ve tors instead of just bit ve tors for the sets
rea h [i. We divide the bit ve tor rea h [i into segments of length L where L is an
integer to be determined later (L  log n). The idea now is to perform the union
operation in line (5) not bit by bit but rather segment by segment. In this way
we may ombine L bit operations into a single operation and hen e speed up the
omputation by a fa tor L. For the analysis of this approa h we need the following
assumption:
(A): The arithmeti operations +, , , =, 6=, < on integers in the range [0 : : n
take time O(1).
Assumption (A) is reasonable sin e node names are integers in that range and we
assumed already that they an be handled in onstant time. A bit string aL 1 : : : a0
Version: 19.10.99

Time: 11:01

{9{

10

Chapter 4. Algorithms on Graphs

of length L represents the integer a = PLl=01 al 2l in the range R = [0 : : 2L 1. We


de ne fun tions union : R  R ! R, power : [0 : : L 1 ! R, is in : [0 : : L 1 R !
f0; 1g,
index : [1 : : n ! [0 : : dn=Le 1, and bit : [1 : : n ! [0 : : L 1 as follows: If
PL 1
P
a = `=0 a`  2` and b = L`=01 b`  2` then
union (a; b) =

L
X1
`=0

max(a` ; b` )  2`

power (l) = 2l

is in (l; a) = a`
index (j ) = (j

1) div L
bit (j ) = (j 1) mod L:

We an now represent the bit ve tor rea h [i as rea h [i; dn=Le 1; : : : ; rea h [i; 0
where ea h rea h [i; h is an integer in the range R = [0 : : 2L 1, i.e., a bit string of
length L. Node j orresponds to the bit (j )-th bit in rea h [i; index (j ). With these
de nitions we an rewrite Program 4 as Program 5.
(1) for i from n downto 1
(2a) do o initialize rea h [i to empty set o
(2b)
for l, 0  l < dn=Le
(2 )
do rea h [i; l 0 od;
(2d)
o add i to rea h [i o
(2e)
rea h [i; index (i) power (bit (i));
(3)
for all j with (i; j ) 2 E
o in in reasing order!! o
(4)
do if is in (bit (j ); rea h [i; index (j )) = 0
(5a)
then for l, 0  l < dn=Le
(5b)
do rea h [i; l union (rea h [i; l; rea h [j; l) od
(6)
fi
(7)
od
(8) od.
Program 5

Lemma 2. Under the assumption that fun tions union , power , bit , index and
is in an be evaluated in time O(1), Program 5 runs in time O((n + mred )  n=L) =
O(n3 =L).
Proof :

Obvious.

How an we evaluate these fun tions qui kly? The easiest solution is to tabulate
them.
Version: 19.10.99

Time: 11:01

{10{

4.3. Transitive Closure of A y li Digraphs

11

Lemma 3. The fun tions union , power , bit , index and is in an be tabulated in
time O(L  22L ).
Proof : We only dis uss the fun tions is in and union and leave the other fun tions
to the reader. The value is in (i; a) is true i ai = 1. This is exa tly the ase if
a = (2l +1)2i + h for some h, 0  h  2i 1. The table is in [ ; is therefore lled
orre tly by Program 6. Program 6 runs in time O(L  2L ).
is in [i; a

0, for 0  i  L 1, 0  a  2L 1;
for i, 0  i  L 1
do for h, 0  h  power [i 1
do for l, 0  l  power [L i 1
do is in [i; (2l + 1)  power [i + h 1 od
od.

od

Program 6

Using the table is in [ ; , the entry union [a; b of table


omputed in time O(L) by Program 7.

union [ ;

an now be

0;
for j from 0 to L 1
do + max(is in [j; a; is in [j; b)  power [j od;
union [a; b ;
Program 7

The omplete table union [ ; an therefore be lled in time O(22L  L).


Theorem 2. Under assumption (A) the transitive losure of an a y li digraph
an be omputed in time O((n + mred)n= log n) = O(n3 = log n).
Proof :

We hoose L = log n log log n =


(log n). Then
22L  L  22(log n

log log n)

 log n  n2= log n

and the omputation of the various tables takes time O(n2 = log n) by Lemma 3.
Also the transitive losure is omputed in time O((n + mred)  n=L) = O((n +
mred)  n= log n) after prepro essing by Lemma 2.
Version: 19.10.99

Time: 11:01

{11{

12

Chapter 4. Algorithms on Graphs

Next, we turn to the average ase analysis of the transitive losure algorithm. We
postulate the following model of a random a y li digraph on n nodes. Let  be
a real between 0 and 1. For i < j the probability of the event \(i; j ) 2 E " is . For
(i; j ) 6= (l; h) the events \(i; j ) 2 E " and \(l; h) 2 E " are independent.
Theorem 3.
n  (n + 3) n  (1 + ln(1=))
.
a) E(m ) 
2

b) E(mred )  min(n  (2 + ln(1=)); n2  ).
) The expe ted running time of our algorithm on an n node random digraph is
O(min(n2  (2 + ln(1=)); n3  )) = O(n2  ln n):
d) Under assumption (A) the expe ted running time on an n node random digraph
is O(n2 ).

Proof :

a) and b): We prove parts a) and b) in four steps. In the rst step (Lemma 4)
we establish a onne tion between the expe ted size of the transitive losure and
the expe ted size of the transitive redu tion. This relation allows us to on entrate
on the expe ted size of the transitive losure. We will next (Lemma 5) derive a
re urren e for the probability that the number of verti es rea hable from vertex 1
in a random digraph on n verti es is exa tly l. This lemma in ombination with
a general property (Lemma 6) of this type of re urren e will allow us to get the
desired bound on the size of the transitive losure (Lemma 7) and on the size of the
transitive redu tion.

We use the following notation. The random variable outdeg red
n (v ) (outdeg n (v ))
denotes the outdegree of vertex v in the transitive redu tion (transitive losure) of
a random digraph on n verti es.
Lemma 4.

 prob((1; n) 2= E  ):
a) prob((1; n) 2 E red ) =
1




n(n 1)

b) E(mred ) =
E(m ) :
1 
2
Proof :

a) We have
prob((1; n) 2 E red) = prob((1; n) 2 E )  prob( 8| v; v 6= 1; v 6= n;{z1 ! v : (v; n) 2= E} )
:= A
=
=

Version: 19.10.99

n
X1
l=1

prob(Ajoutdeg n 1 (1) = l)  prob(outdeg n 1 (1) = l)

n
X1
l=1

Time: 11:01

(1 )l 1  prob(outdeg n 1 (1) = l)


[sin e prob(Ajoutdeg n 1(1) = l) = (1 )l 1
{12{

4.3. Transitive Closure of A y li Digraphs

13

n
X1

=1 

l=1

(1 )l  prob(outdeg n 1(1) = l)

= 1   prob((1; n) 2= E  )

[sin e prob 8 v; 1 ! v: (v; n) 2= E joutdeg n 1 (1) = l = (1 )l
b)

E(mred) =
=
=

X
1i<j n

1i<j n

E((i; j ) 2 E red)
prob((i; j ) 2 E red)


 prob((i; j ) 2= E  )
1

1i<j n

=1

[by part a)

(1 prob((i; j ) 2 E  ))

1i<j n



= 1    n(n2 1) E(m ) :

We infer from Lemma 4b) that we an derive an upper bound on E(mred ) by deriving
a lower bound on E(m ). Let pn;l denote the probability that outdeg n (1) = l.
Lemma 5. pn;l = 0 for l 2= f1; : : : ; ng, p1;1 = 1 and for n  2
pn;l = pn

1;l

 (1 )l + pn

1;l 1

 (1 (1 )l 1 )

Proof : In a digraph with only a single vertex the number of verti es rea hable from
vertex 1 is always 1. Assume n  2 next. If outdeg n(1) = l, then either l verti es
among the rst n 1 are rea hable from 1 and none of these verti es has an outgoing
edge into vertex n (probability pn 1;l  (1 )l ) or l 1 verti es among the rst
n 1 are rea hable from 1 and at least one of these verti es has an outgoing edge
into vertex n (probability pn 1;l 1  (1 (1 )l 1 )). Thus

pn;l = pn

1;l

 (1 )l + pn

1;l 1

 (1 (1 )l 1 ):

With l = 1 (1 )l the re urren e above an be rewritten as


p1;1 = 1;
pn;l = 0 for l 2= f1; : : : ; ng;
pn;l = (1 l )  pn 1;l + l 1  pn
Version: 19.10.99

Time: 11:01

{13{

1;l 1

14

Chapter 4. Algorithms on Graphs

This type of re urren e is known as a pure birth pro ess in probability theory. If
one interprets n as time and l as size of a population, then the re urren e reads as
follows. We start with a population of size 1. When the population has size l at
time n 1 then it keeps at size l with probability 1 l and grows to size l + 1
with probability l . We should expe t that on the average size 2 is rea hed at time
1=1 , size 3 is rea hed at time 1=1 + 1=2 , : : : . An exa t formulation of this is
Lemma 6. Let '(l) =

Pl 1

j =1

1=j for l  1. Then

E('(outdeg n(1))) = n 1
for all n.
Proof :

We use indu tion on n. For n = 1 we have


E('(outdeg 1 (1))) =

'(l)  p1;l

= '(1)  p1;1
=0

[sin e p1;l = 0 for l 6= 1


[sin e '(1) = 0

and for n  2
X
E('(outdeg n(1)) = '(l)  pn;l
l

'(l)  (l

 pn

1;l 1

+ (1 l )  pn 1;l) [by Lemma 5

X
l

X
l

'(l + 1)  l  pn
X

1;l

'(l)  pn

'(l)  l  pn

1;l

('(l + 1){z '(l))} l  pn


|
=1
pn

1;l

1;l

l

'(l)  pn

1;l

'(l)  pn

1;l

1;l

= 1 + n 2;
where Pl '(l)  pn 1;l = n 2 by indu tion hypothesis.
We an now use Lemma 6 to derive bounds on the expe ted size of the transitive
losure.
Version: 19.10.99

Time: 11:01

{14{

4.3. Transitive Closure of A y li Digraphs

15

Lemma 7.
1 + ln(1=)
a) E(outdeg n (1))  n + 1

1
+
ln(1
=)
n+3

b) E(m )  n 
2 n   
ln(1=)  n  (2 + ln(1=))
) E(mred )  n  1 +
1 
Proof :

a) We rst ompute an upper bound  for the fun tion '. The bound 
is a linear fun tion and hen e E((outdeg n(1))) = (E(outdeg n (1))). It is then
easy toPderive a lower bound on E(outdeg n(1)). Re all that j = 1 (1 )j and
'(l) = lj =11 1=j .
1 + ln(1=)
Claim: '(l)  l 2 +
Proof :

We have

'(l) = 1= +

 1= +

1
1 (1 )j
j =2

l 1
X

dx

1 (1 )x


ln(1
(1
)x ) l 1
= 1= + x
ln(1 ) 1
l 1
= 1= + l 1 ln(1 ln(1(1 )) ) 1 + ln(1ln  )
=)
 l 2 + 1 + ln(1
;

1

l 1
sin e ln(1 ln(1(1 )) ) > 0 and ln(1 )  .
Let (l) = l 2 + (1 + ln(1=))=. Then
n 1 = E('(outdeg n (1)))
 E((outdeg n(1)))
X
= (l)  pn;l
l

(l 2 + (1 + ln(1=))=)  pn;l

= E(outdeg n(1)) 2 + (1 + ln(1=))=:


Thus E(outdeg n (1))  n + 1 (1 + ln(1=))=:
Version: 19.10.99

Time: 11:01

{15{

[Lemma 6
[sin e '  

16

Chapter 4. Algorithms on Graphs

b) We have

n
X

E(m ) = E(
=
=


i=1

n
X
i=1
n
X
i=1

E(outdeg n(i))
E(outdeg n

n 
X
i=1

outdeg n (i))

i+1

(1))


1
+
ln(1
=)
n i+2


=)
;
= n  n +2 3 n  1 + ln(1

and part a) of Theorem 3 is ompleted.
Part ) of Lemma 7 follows from part b), Lemma 4b) and a simple omputation:
E(mred) = 1    (n  (n 1)=2 E(m ))
[Lemma 4b)



1
+
ln(1
=)
 1   n  (n 1)=2 n  (n + 3)=2 + n 
[part b)

= 1 n   ( 2 + 1 + ln(1=))


=)
 n  1 + ln(1
1 
!
2
3
(1
) + (1 2) + (1 3) +   
[Taylor expansion
=n 1+
1 
 n  (2 + ln(1=)):
To omplete part b) of Theorem 3 we only need the additional observation that
E(mred)  E(m)  n2  .
) The expe ted running time of our algorithm is E(n  (n + mred)) whi h is
O(min(n2  (2 + ln(1=)); n3  ))
by part b). Next observe that for   (ln n)=n we have
n2  (2 + ln(1=)) = O(n2  ln n)
and that for   (ln n)=n we have
n3   = O(n2  ln n):

d) Under assumption (A) the expe ted running time redu es to E (n(n+mred)= log n) =
O(n2 ) by the reasoning of part ) and Theorem 2.
Version: 19.10.99

Time: 11:01

{16{

4.4. Systemati Exploration of a Graph

17

A loser look at Theorem 3 shows that the expe ted running time of our algorithm
is optimal for dense digraphs. This an be seen as follows. Let 0 = 4ln n=n. Then
E (m ) =
(n2 ) for   0 by part a) of Theorem 3 and hen e the expe ted size of
the output is quadrati for   0 . The expe ted running time is also quadrati and
therefore optimal for   0 .
4.4. Systemati Exploration of a Graph

A fundamental requirement for most graph algorithms is the systemati exploration


of a graph starting at some node s. The basi idea is quite simple.
Suppose that we have already visited some set S of nodes and have traversed
some of the edges leaving nodes in S . Initially, S = fsg and no edge has been
traversed. At ea h step the algorithm sele ts one of the unused (= not traversed)
edges in ident to a node in S and explores it, i.e., the edge is marked used and
the other endpoint of the edge is added to S . The algorithm terminates when no
unused edges in ident to nodes in S are left. We summarize in Program 8.
S fsg;
mark all edges unused;
while there are unused edges leaving nodes in S
do hoose any v 2 S and an unused edge (v; w) 2 E ;
mark (v; w) used;
S S [ fwg
od.
Program 8

Lemma 1. Let G = (V; E ) be a digraph. Then


S = fv; there is a path from s to v in Gg
on termination of Program 8.
Proof :

If a node is added to S then it is ertainly rea hable from s. Suppose now


that v is rea hable from s, i.e., a path v0 ; : : : ; vk exists from s to v. We show by
indu tion on i that vi is added to S . Sin e s = v0 , this is ertainly true for i = 0.
Suppose now that vi is in S but vi+1 is not. Then edge (vi; vi+1 ) is unused and
in ident to a node in S . As long as this ondition prevails the algorithm annot
terminate and hen e vi+1 must be added to S .

Version: 19.10.99

Time: 11:01

{17{

18

Chapter 4. Algorithms on Graphs

Of ourse, Program 8 leaves many implementation details unresolved. The major


questions are how to mark edges used and unused, how to store the set S , and how
to sele t an edge marked unused and leaving a node in S .
The rst problem is easily solved. We have for ea h node i a pointer p[i into
the adja en y list of node i. The edges to the left of the pointer are used and the
other edges are unused. Initially, the pointer p[i points to the rst entry of the i-th
adja en y list, i.e., we mark all edges unused by exe uting Program 9.
for 1  i  n
do p[i adjhead [i od.

Program 9

The sele tion of an unused edge leaving a node in S is also fairly easy. We only
have to maintain the subset S~  S of all nodes v 2 S , whose adja en y list is not
yet exhausted. To sele t an unused edge we hoose any node in S~ and traverse any
unused edge leaving that node. This leads to Program 10.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)

pro edure explorefrom (s);


S fsg; mark all edges unused;
S~ fsg;
while S~ 6= ;
do hoose some node v 2 S~;
if there is an unused edge leaving v
then let (v; w) be any su h edge;
mark (v; w) used;
if w 2= S then add w to S ;
add w to S~
fi
else delete v from S~
fi
od
end.
Program 10

Lines (6) to (8) of Program 10 need to be re ned further. Using the fa t that
the pointer p[v always points to the rst unused edge in v's adja en y list, we an
rewrite these lines as Program 11.
We still have to solve the representation question for sets S and S~. On set S
the operations Insert, Member and Initialize to Empty Set are exe uted, on set S~
the operations Empty?, Insert, Sele t Some, Sele t and Delete Some and Initialize to Empty Set are exe uted. We saw in Se tion 3.8.1 that a boolean array is a
Version: 19.10.99

Time: 11:01

{18{

4.4. Systemati Exploration of a Graph

if p[v 6= nil
then w p[v":node ;
p[v p[v":next ;
if w 2= S then : : :

19

Program 11

good representation for S : Operations Insert and Member ost O(1) time units and
Initialize to Empty Set osts O(n) time units. For set S~ we use either a sta k or a
queue ( f. Se tion 1.4.1). Then all operations on S~ take O(1) time units. We are
now able to determine the e ien y of pro edure explorefrom .
Lemma 2. A all explorefrom (s) osts O(ns + ms ) time units (without ounting
 vgj and m is the
the ost of initialization in line (2)), where ns = jVs j = jfv; s !
s
number of edges in the subgraph indu ed by Vs .

Proof :

One exe ution of the body of the while-loop takes O(1) units of time. During
ea h iteration either an edge is used up or an element is deleted from S~. Sin e ea h
node in Vs is added exa tly on e to S~ (the test in line (9) avoids repetitions), the
total time spent in the while-loop is O(ns + ms).
We will now put pro edure explorefrom to its rst use: determining the onne ted
omponents of an undire ted graph.
De nition: An undire ted graph G = (V; E ) is onne ted if for every v; w 2 V
there is a path from v to w. A onne ted omponent of an undire ted graph G
is a maximal (with respe t to set in lusion) onne ted subgraph of G.
The problem of determining the onne ted omponents of an undire ted graph often
arises in the following disguise. V is a set and E  V  V is a relation on V . Then
the re exive, symmetri , transitive losure of E is an equivalen e relation. The
problem is to determine the equivalen e lasses of this relation. In the language of
graphs this amounts to determining the onne ted omponents of the undire ted
graph G = (V; ffv; wg; (v; w) 2 E or (w; v) 2 E g).
In an undire ted graph the set of nodes rea hable from s forms a onne ted
omponent. This observation leads us to the following theorem.
Theorem 1. The onne ted omponents of an undire ted graph an be found in
linear time O(n + m).
Proof : We embed pro edure explorefrom into Program 12 and hange line (2) in
explorefrom from \S fsg; mark all edges unused" to \S S [ fsg".
We infer from Lemma 2 that the ost of a all explorefrom (v) is proportional
to the size of the onne ted omponent ontaining v. Sin e explorefrom is alled

exa tly on e for ea h onne ted omponent, the total running time is O(n + m).

Version: 19.10.99

Time: 11:01

{19{

20

Chapter 4. Algorithms on Graphs

S ;;
for all v 2 V do p[v adjhead [v od;
for all v 2 V
do if v 2= S then explorefrom (v) fi od.
Program 12

In what sense does this program determine the onne ted omponents of a
graph? All nodes of a omponent are visited during one all of explorefrom . A list
of the nodes of ea h omponent an be obtained as follows. Let omp be a variable
of type \set of nodes" (implemented by a sta k). We initialize omp to a singleton
set fvg before explorefrom (v) is alled and insert the instru tion \add w to omp " in
line (10) of explorefrom . Then omp ontains all nodes of the omponent ontaining
v after return from explorefrom (v).
Depending on the representation of set S~, as sta k or queue, we have two versions
of the pro edure explorefrom at hand. They are known by the names depth- rstsear h (S~ is a sta k) and breadth- rst-sear h (S~ is a queue). In depth- rstsear h the exploration always pro eeds from the last node visited whi h still has
unused edges, in breadth- rst-sear h it pro eeds from the rst node visited whi h
still has unused edges.
In either ase explorefrom traverses the adja en y list of ea h node in a stri tly
sequential manner; the order of the edges in the adja en y lists has no in uen e
on the running time. In Se tion 4.5 we will take a loser look at depth- rst-sear h.
In Se tion 4.6 we will apply depth- rst-sear h to various onne tivity problems. In
Se tion 4.7 we will apply breadth- rst-sear h to distan e problems.

Version: 19.10.99

Time: 11:01

{20{

4.5. A Close Look at Depth-First-Sear h

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)
(20)
(21)
(22)
(23)

21

pro edure dfs (v : V );


add v to S ;
ount 1

ount 1 + 1; dfsnum [v ount 1;


for all (v; w) 2 E
do if w 2= S
then [add (v; w) to T ;
dfs (w)
 w then add (v; w) to F
[else if v !
T
 v then add (v; w) to B
else if w !
T
else add (v; w) to C fi fi
fi
od;
ount 2 ount 2 + 1; ompnum [v ount 2;
end;
begin
o main program o
S ;; ount 1 0; ount 2 0;
[T F B C ;;
for all v 2 V
do if v 2= S
then dfs (v)
fi
od
end.
Program 13

4.5. A Close Look at Depth-First-Sear h

In this se tion we take a detailed look at depth- rst-sear h of dire ted and undire ted graphs. In the depth- rst-sear h version of pro edure explorefrom set S~ is
handled as a sta k. It is onvenient to make that sta k impli it by formulating
depth- rst-sear h as a re ursive pro edure dfs , f. Program 13. An exe ution of
this program is alled a depth- rst-sear h or simply DFS on graph G.
Several remarks are to be made here. We have extended our basi algorithm
in two respe ts. First of all, we number the nodes in two di erent ways. The
rst numbering dfsnum is with respe t to the alling time of pro edure dfs , the
se ond numbering ompnum is with respe t to the ompletion time of pro edure
dfs . Se ond of all, we partition the edges of the graph into four lasses: the tree
edges T , the forward edges F , the ba kward edges B and the ross edges C . The
partitioning pro ess is only done on eptually (this fa t is indi ated by en losing
the orresponding statements in bra kets); it will fa ilitate the dis ussion of depth rst-sear h.
Third of all, we assume that the reader knows by now how to represent set S
Version: 19.10.99

Time: 11:01

{21{

22

Chapter 4. Algorithms on Graphs

and how to realize line (4). S is represented as a boolean array as dis ussed in
Se tion 4.4 and line (4) may be expanded into
(4a) p adjhead [v;
(4b) while p 6= nil
(4 ) do w p":node ; p p":next
where p :"edge and w : integer are lo al to pro edure dfs .
In the example of Figure 4 tree edges are drawn solid, ba k edges are drawn
dashed, ross edges are drawn squiggled and forward edges are drawn dash-dotted.
Name (an element of fa; b; ; d; eg), depth- rst-sear h numbers (dfsnum ) and ompletion numbers ( ompnum ) are indi ated in ea h node in that order. It is assumed
that the adja en y list for a is d, e, and that dfs (a) is alled rst. In our examples
we will always draw tree edges upwards and arrange the sons of a node (via tree
edges) from left to right in in reasing order of dfsnum .
e

3 1
d

2 2

1 4

b
Figure 4.

4 3
b

5 5

A graph and its dfs -tree

Lemma 1. A depth- rst-sear h on a digraph G = (V; E ) takes time O(n + m).


Proof :

A all dfs (v) osts O(outdeg G(v)) units of time; this a ounts for the time
spent in the body of dfs but does not a ount for further re ursive alls. Sin e a
node v is always added to S when the exe ution of dfs (v) starts and no node is ever
removed from S , dfs is alled at most on e for ea h node. Hen e the total time
spent inside dfs is learly O(n + m); the total time spent outside dfs is O(n).

Next we will state some important properties of depth- rst-sear h.


Lemma 2. (DFS-Lemma). Let G = (V; E ) be a digraph and let T; F; B; C; dfsnum
and ompnum be de ned by a depth- rst-sear h on G.
a) Sets T; F; B; C form a partition of E .
b) A = (V; T ) is a spanning forest of G.
 w i dfsnum [v  dfsnum [w and ompnum [w  ompnum [v.
) v !
T

Version: 19.10.99

Time: 11:01

{22{

4.5. A Close Look at Depth-First-Sear h

23

d) For all (v; w) 2 E : (v; w) 2 T [ F i dfsnum [v  dfsnum [w.


 w, :(v !
 z ), and (w; z ) 2 E . Then dfse) Let v; w; z be nodes su h that v !
T
T
num [z < dfsnum [v and (w; z ) 2 B [ C .
Moreover, if ompnum [z <
ompnum [v then (w; z ) 2 C ; if ompnum [z > ompnum [v then (w; z ) 2 B .
f) For all (w; z ) 2 E :
(w; z) 2 B i dfsnum [w > dfsnum [z and ompnum [w < ompnum [z.
g) For all (w; z ) 2 E :
(w; z) 2 C i dfsnum [w > dfsnum [z and ompnum [w > ompnum [z.
Proof :

a) Follows from the fa t that ea h edge is handled exa tly on e during the
depth- rst-sear h on graph G.
b) When edge (v; w) is added to T in line (6) of dfs then w is added to S in line (2)
in the following re ursive all. This shows that indeg A(w)  1 for all w 2 V and
that A is a y li .
) Observe
rst that the forest A orresponds to the alling history of pro edure dfs ,
 w i the all dfs (w) is nested within the all dfs (v). Observe next that
i.e., v !
T
the all dfs (w) is nested within the all dfs (v) i dfsnum [v  dfsnum [w and
ompnum [w  ompnum [v.
 w by de nition of T and F and hen e dfsnum [v 
d) \)": If (v; w) 2 T [F then v !
T
dfsnum [w by part ).
\(": Consider the exa t instant when edge (v; w) is handled in dfs (v). Either
exploration of that edge will lead to all dfs (w) or v = w or v 6= w and dfs (w)
was alled before edge (v; w) is handled. In the rst ase we have (v; w) 2 T .
In the se ond ase the edge (v; v) is added to F . Consider the third ase. Sin e
dfsnum [v < dfsnum [w, all dfs (w) was started after all dfs (v). Hen e all dfs (w)
 w by part ). Thus (v; w) 2 F in this
is nested within all dfs (v) and therefore v !
T
ase.
e) The situation is visualized in Figure 5. Sin e (w; z) 2 E , the all dfs (z) is started
before the  all dfs (w) is ompleted and hen e before the all dfs (v) is ompleted;
sin e :(v !
z ) the all dfs (z ) is not nested within the all dfs (v). Thus the all
T
dfs (z ) starts before the all dfs (v) and hen e dfsnum [z < dfsnum [v. This also
implies (w; z) 2 B [ C by parts a) and d).
 v i ompnum [z > ompnum [v by part ). Hen e
Observe next, that z !
T
 w i z !
 v i ompnum [z > ompnum [v. This ompletes the
(w; z) 2 B i z !
T
T
proof of part e).
f) and g) follow immediately from part e) with v = w and the observation that
(w; z) 2 B [ C implies :(w !
z ).
T
Version: 19.10.99

Time: 11:01

{23{

24

Chapter 4. Algorithms on Graphs

Cross or
ba k edge

the subtree of A = (V; T )


with root v

Figure 5.

Situation in Lemma 2e)

It is worthwhile to restate the ontent of Lemma 2 in an informal way. Forward


edges run from nodes to their des endants with respe t to tree edges, ba kward
edges run from nodes to their an estors, and ross edges run from right to left in
our drawings. Also, the depth- rst-sear h number ( ompletion number) of a node
is smaller (larger) than that of its tree des endants. Cross edges run from larger
to smaller depth- rst-sear h and ompletion numbers. Finally, if (u; w) is a ba k
edge, then all nodes whi h are ompleted between u and w are des endants of w.
In undire ted graphs the situation is simpler; no ross edges exist and every
forward edge is the reversal of a ba kward edge.
Lemma 3. Let G = (V; E ) be an undire ted graph and let T; F; B; C be de ned
by a depth- rst-sear h on the dire ted version of G.
a) C = ;.
b) (v; w) 2 B i (w; v) 2 T [ F for every dart (v; w) of G.
) If G is onne ted then A = (V; T ) is a tree.
Proof : a) (Indire t). Assume C 6= ;. Let (v; w) 2 C be the rst dart whi h
was added to C in the depth- rst-sear h on G. By Lemma 2g) the all dfs (w)
is ompleted when dart (v; w) is handled in the all dfs (v). Sin e graph G is
undire ted, dart (w; v) was explored in dfs (w) and sin e (v; w) is the rst dart
added to C , we must have (w; v) 2 T [ F [ B . In either ase we have w !
v or
T
 w. Hen e (v; w) is not added to C , ontradi tion.
v!
T

b) Sin e C = ; by part a) this is an immediate onsequen e of Lemma 2, parts a),


d) and f).
) If A = (V; T ) were not a tree but a proper forest, then A would ontain at least
two trees A1 and A2 . Sin e G is onne ted, there must be edges between A1 and A2 .
Sin e ba k edges and forward edges run parallel to paths of tree edges, su h edges
must be ross edges. However, depth- rst-sear h on an undire ted graph does not
produ e ross edges and hen e A must be a single tree.
Version: 19.10.99

Time: 11:01

{24{

4.6. Strongly Conne ted and Bi onne ted Components

25

In Program 13 the partitioning pro ess on the edges is only done on eptually. In
light of the DFS-Lemma it an also be done omputationally. We only have to
repla e lines (8) to (10) by:
else if dfsnum [v  dfsnum [w
then add (v; w) to F
else if ompnum [w is unde ned
then add (v; w) to B
else add (v; w) to C fi fi

part d) o

o
o

part f) o
part g) o

We nally observe that depth- rst-sear h gives us an alternative way of omputing


a topologi al sort of an a y li digraph G = (V; E ). In an a y li digraph there
are no ba k edges and hen e ompnum [w < ompnum [v for all edges (v; w) 2 E
by parts ) and g) of the DFS-Lemma. Hen e ord [v = n + 1 ompnum [v is a
topologi al sort.
Theorem 1. A topologi al sort of an a y li digraph an be omputed in time
O(n + m) .

4.6. Strongly Conne ted Components of Dire ted Graphs


and Bi onne ted Components of Undire ted Graphs

We will des ribe linear time algorithms to determine the strongly onne ted omponents of a digraph and the bi onne ted omponents of an undire ted graph. All
algorithms are based on depth- rst-sear h.
De nition:
 w!
 v for v; w 2 V .
a) A digraph G = (V; E ) is strongly onne ted if v !
b) A strongly onne ted omponent (s. . .) of a digraph G is a maximal

strongly onne ted subgraph.

The problem of determining the strongly onne ted omponents of a digraph often
arises in the following disguise. V is a set and E a relation on V . Two elements
v; w 2 V are alled equivalent if v ! w and w ! v. The equivalen e lasses of this
equivalen e relation are just the s. . .'s of G = (V; E ). Furthermore, shrinking the
equivalen e lasses (s. . .'s) to single points leaves us with a partial order (an a y li
graph).
We will des ribe two linear time algorithms for omputing s. . .'s. The rst
algorithm has a very simple orre tness proof, but uses two passes of DFS, the
se ond algorithm requires a more ompli ated proof, but uses only one pass of DFS.
Version: 19.10.99

Time: 11:01

{25{

26

Chapter 4. Algorithms on Graphs

Yet another one-pass algorithm is des ribed in Exer ise 8. Both one-pass algorithms
an be modi ed for omputing the bi onne ted omponents of an undire ted graph.
i
e

A graph with 5 s. . .'s

Figure 6.

i
e

b
Figure 7.

A DFS on the graph of Figure 6

Figure 6 shows a graph with ve s. . .'s, Figure 7 shows a DFS on this graph,
where the nodes were explored in the order a; b; ; d; e; f; g; h and i, and Figure 8
shows the a y li graph obtained by shrinking s. . .'s to single nodes. The s. . .'s
are C1 ; : : : ; C5 , where Ci is the i-th omponent for whi h all alls dfs (v), v 2 Ci,
Version: 19.10.99

Time: 11:01

{26{

4.6. Strongly Conne ted and Bi onne ted Components

27

C3
C2

C4
C1
C5
Figure 8.

The graph obtained by shrinking the s. . .'s to single nodes

were ompleted. Thus C1 = fag, C2 = feg, C3 = fig, C4 = f ; d; f; g; hg and


C5 = fbg. The omponents C1 ; C2 ; : : : ould be determined easily, if we knew
nodes v1 ; v2 ; : : : su h that vi lies in Ci . Be ause then a all dfs (v1 ) explores C1 , a
subsequent all dfs (v2 ) explores C2 , : : : . Unfortunately, there seems to be no easy
way to determine the desired sequen e v1 ; v2 ; : : : of nodes. However, we know a
node in the omponent ompleted last, namely the node v with highest ompletion
number; this is node b in our example. A all dfs (v) on the reversed graph G 1
explores exa tly the omponent ompleted last in the rst pass. Let now be w the
node with the highest ompletion number whi h has not yet been visited in the
se ond pass. A all dfs (w) explores the omponent ompleted next to last, : : : .
The formal basis for this strategy is provided by
Lemma 1. Let Ci = (Vi ; Ei ), 1  i  k, be the s. . .'s of G = (V; E ), n = jV j, and
let ri be the node with largest ompletion number in Ci . Let us also assume that
ompnum [r1 < ompnum [r2 <    < ompnum [rk .
a) ompnum [rk = n
 r then v 2 [ V
b) If v !
i
j
E
j i
 v then v 2 [ V
) If ri !
j
E 1
j i
 v for all v 2 V
d) ri !
i
1
E

Proof :

a) The node with ompletion number n belongs to some s. . ..


 r and hen e dfs (r ) is started before dfs (r )
 r . Then r !
b) Let v 2 Vj and v !
i
i
j
i
j
E
E
is ompleted. Thus, either ompnum [ri  ompnum [rj and hen e i  j by asVersion: 19.10.99

Time: 11:01

{27{

28

Chapter 4. Algorithms on Graphs

 r and hen e
sumption or all dfs (rj ) is nested within all dfs (ri ) and hen e ri !
j
E
i = j.
 r i r !
 v.
) follows immediately from part b) and the observation that v !
i
i
E
E 1
 r and the laim follows.
d) Let v 2 Vi . Then v !
i
E

Assume that a DFS on G has been performed and that the ompletion numbers
have been omputed. Now onsider a DFS on the graph G 1. Assume that the
rst all made is dfs (rk ). Note that this is easy to a hieve sin e rk is the node with
ompletion number equal to n. The all dfs (rk ) rea hes exa tly the verti es in Vk
by parts ) and d) of Lemma 1. Also, at this point rk 1 is the node with the highest
ompletion number whi h has not been rea hed by the sear h yet. A all dfs (rk 1)
will now rea h all verti es in Vk 1, : : : . This suggests the following algorithm for
omputing s. . .'s.
1) Perform DFS on G and ompute the ompletion number of ea h vertex (equivalently, ompute an array ord with ord [i = v i ompnum [v = i).
2) Compute the graph G 1.
3) Perform DFS on G 1, where the main program onsiders the nodes in de reasing order of ompletion number, i.e., lines (18) to (20) of Program 13 are
repla ed by
(180 ) for i from n downto 1
(190 ) do if ord [i 2= S
(200 )
then dfs (ord [i)
Lemma 2. Under the hypothesis of Lemma 1 the following holds: the alls made
in line (20) of Program 13 are exa tly the alls dfs (ri ), k  i  1, and furthermore
exa tly the nodes in Vi are added to S during the all dfs (ri ).
Proof : It su es to show that for all i the all dfs (ri ) is made by the main program
and that after ompletion of dfs (ri ) the equality S = Sji Vj holds. But this follows
from Lemma 1, parts ) and d) and the observation that
ompnum [ri = maxf ompnum [v; v 2 V

Vj g:

j>i

We summarize in
Theorem 1. The strongly onne ted omponents of a digraph G an be omputed
in linear time O(n + m).
Proof :

Steps 1) and 3) are linear by Lemma 1 of Se tion 4.5, and step 2) is linear
by Exer ise 1.
Version: 19.10.99

Time: 11:01

{28{

4.6. Strongly Conne ted and Bi onne ted Components

29

We now turn to an alternative algorithm whi h uses only one pass of DFS. The idea
underlying the one-pass algorithm is to maintain the s. . .'s of G ur = (V ur ; E ur )
whi h is the subgraph spanned by the set E ur of explored edges. Initially, V ur =
f1g, E ur = ; and there is only one s. . .. Suppose now that we explore an edge
e = (v; w). If e 2 T , then w is added to V ur and the node w by itself forms a s. . .,
if e 2= T , then the exploration of e may merge several s. . .'s into one. The main
di ulty is to perform this merging pro ess e iently.
e

h
f; g
; d

a
b

Figure 9. A snapshot of the exe ution of DFS on the graph of Figure 6.


Nodes for whi h the all of dfs is ompleted are shaded. The shrunken
graph is also shown. Completed omponents are shaded.

Figure 9 shows a typi al situation for the example graph of Figure 6. In this
situation the alls dfs (a), dfs (e), dfs (d) and dfs (g) have been ompleted and we
are urrently exploring edges out of node h. The s. . .'s of G ur are fbg, fag, feg,
f ; dg, ff; gg and fhg. A s. . . C of G ur is said to be ompleted if dfs (v) is
ompleted for all v 2 C . Otherwise, it is alled un ompleted. In our example, the
omponents fag and feg are ompleted and the omponents fbg, f ; dg, ff; gg and
fhg are un ompleted. Figure 9 also shows the graph obtained by shrinking the
s. . .'s of G ur to single nodes. We make two observations (whi h will be invariants
of our algorithm):
I1: There are no edges (x; y) with x belonging to a ompleted omponent and y
belonging to an un ompleted omponent.
I2: The un ompleted omponents form a path and we are urrently exploring edges
out of the last omponent of this path.
We an now further develop our basi idea. If we explore a tree edge (v; w), then a
new un ompleted omponent fwg is reated and added to the path of un ompleted
omponents. If we explore a non-tree edge (v; w) and w belongs to a ompleted
omponent, then no a tion is required be ause the edge (v; w) annot lose a y le
by invariant I1. If w belongs to an un ompleted omponent then some nal segment of the path of un ompleted omponents ollapses to a single s. . . ( f. Fig. 10).
Version: 19.10.99

Time: 11:01

{29{

30

Chapter 4. Algorithms on Graphs

Note that in all three ases the Invariants I1 and I2 are preserved. Finally, observe
that the invariants are also maintained when we omplete a s. . . be ause the omponent to be ompleted is always the last omponent on the path of un ompleted
omponents. The main algorithmi problem to be resolved at this point is the
representation of the path of un ompleted omponents.
v

the e e t of
exploring the
edge (v; w)

Figure 10. The path of un ompleted s. . .'s and the e e t of exploring


an edge (v; w), where w belongs to an un ompleted omponent.

Let un nished denote the sequen e of nodes belonging to un ompleted omponents of G ur in in reasing order of DFS-number. In the example of Figure 9,
un nished = (b; ; d; f; g; h). We observe:
I3: The nodes of ea h un ompleted s. . . form a ontiguous subsequen e of the
sequen e un nished .
For ea h s. . . C let us all the node with the smallest DFS-number in C the root
of C and let roots be the sequen e of roots of un ompleted s. . .'s in in reasing
order of DFS-number. Of ourse, roots is a subsequen e of un nished . In our
example, roots = (b; ; f; h). With these de nitions we an reformulate (and re ne)
invariants I2 and I3 as follows:
Let un nished = (v1 ; v2 ; : : : ; vs ) and let roots = (vi1 ; vi2 ; : : : ; vik ), where 1 = i1 <
i2 <    < ik , be the subsequen e of roots.
 v for 1  l < k, and
I2: The nodes in roots lie on a single tree path, i.e., vil !
il+1
T
we are urrently exploring edges out of vp , where p  ik .
I3: The nodes in the un ompleted s. . . with root vil are the nodes vil ; vil +1; : : : ;
vil+1 1 (with the onvention ik+1 = s + 1). Moreover, all these nodes are tree
des endants of the root vil .
Version: 19.10.99

Time: 11:01

{30{

4.6. Strongly Conne ted and Bi onne ted Components

31

Let us re onsider the exploration of edges and the ompletion of alls. If (v; w) is
the edge to be explored, let G0 ur = (V ur [ fwg; E ur [ f(v; w)g) be the new graph
spanned by the explored edges. Of ourse, w 2 V ur if (v; w) is not a tree edge.
 Exploration of a tree edge (v; w):
In G0 ur the node w is a s. . . by itself, of ourse, an un ompleted one; all other
s. . .'s stay the same. We an re e t this hange by adding the node w at the
end of sequen es un nished and roots . Note that this preserves all our invariants.
I1 is preserved sin e the node v belongs to an un ompleted omponent by I2; I2
is preserved sin e v is a tree des endant of the last element of sequen e roots (=
top (roots ) if we implement roots as a sta k) by I2, I3 and the fa t that (v; w) is a
tree edge; I3 is preserved sin e w is a s. . . by itself. Also, the sequen es un nished
and roots are still ordered by DFS-number.
In Program 14, lines (3) and (4) implement the a tions des ribed above. The sequen e roots and un nished are realized as pushdown stores; in addition, un nished
is also represented as a boolean array in un nished .
 Exploration of a non-tree edge (v; w):
We have to distinguish two ases: either w belongs to a ompleted omponent
(in un nished [w = false ) or it does not. The ase distin tion is made in line (8) of
Program 14.
Case 1 : w belongs to a ompleted omponent, i.e., in un nished [w = false .
In this ase no path exists from w to v, sin e v belongs to an un ompleted omponent
of G ur by I2 and no edge exists from a node in a ompleted omponent to a node
in an un ompleted omponent by I1. Thus G0 ur and G ur have the same s. . .'s
and no a tion is required. The three invariants are learly preserved.
Case 2 : w belongs to an un ompleted omponent, i.e., in un nished [w = true .
Let un nished = (v1 ; v2 ; : : : ; vs ) and let roots = (vi1 ; vi2 ; : : : ; vik ), where 1 = i1 <
i2 <    < ik . Let v = vp , where p  ik by I2, and w = vq where il  q < il+1 , i.e.,
vil is the root of the s. . . ontaining w. We laim that we an obtain the s. . .'s of
G0 ur by merging the s. . .'s of G ur with roots vil ; vil+1 ; : : : ; vik into a single s. . .
with root vil and leaving all other s. . .'s un hanged. This an be seen as follows.
Note rst that ompleted s. . .'s remain the same by I1. Consider any node z in an
un ompleted omponent next, i.e., z = vr for some r. If r  il , say ih  r < ih+1
with l  h  k, then
 v !
 v !
 v !
 v !
 v!w !
 v
vil !
ih
r
ih
ik
il
E ur

E ur

E ur

E ur

E ur

E ur

where the existen e of the rst, the fourth and the fth path follows from I2 and
I3, the existen e of the se ond and third path follows from the fa t that vih and vr
belong to the same s. . ., and the existen e of the seventh path follows from the
fa t that w and vil belong to the same s. . .. Thus z = vr and vil belong to the
same s. . . of G0 ur if r  il .
Version: 19.10.99

Time: 11:01

{31{

32
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)
(20)
(21)
(22)
(23)
(24)
(25)
(26)
(27)

Chapter 4. Algorithms on Graphs

pro edure dfs (v : node );


ount 1

ount 1 + 1; dfsnum [v ount 1; add v


push v onto un nished ; in un nished [v true;
push v onto roots ;

to S ;

for all w with (v; w) 2 E


do if w 2= S
then dfs (w)
else if in un nished [w
then o we now merge omponents o
while dfsnum [top (roots ) > dfsnum [w
do pop (roots ) od
fi
fi
od;
if v = top (roots )
then repeat w pop (un nished ); in un nished [w false;
o w is an element of the s. . . with root v o
until v = w;
pop (roots )

fi
end;
begin

main program o

un nished roots empty sta k ; S ;;


ount 1 0;
for all v 2 V do in un nished [v false od;
for all v 2 V do if v 2= S then dfs (v) fi od

end.

Program 14:

A one-pass s. . . algorithm

 v !
 v !
 w, sin e
If r < il , say ih  r < ih+1 with h < l, then vr E!
ih
il
E ur
E ur
ur
 v by I2.
vr and vih (vil and w respe tively) belong to the same s. . . and vih !
il
E ur
Sin e h < l no path exists from vil to vr = z in G ur . If did exist su h a path in
G0 ur , then it would have to use the edge (v; w) and hen e there must be a path
from w to vr in G ur . Thus w and vr would belong to the same s. . . of G ur , a
ontradi tion. This shows that un ompleted s. . .'s with roots vih , h < l, remain
un hanged.
We have now shown that the s. . .'s of G0 ur an be obtained from the s. . .'s
of G ur by merging the s. . .'s with roots vil ; : : : ; vik into a single s. . .. The newly
formed s. . . has learly root vil and hen e the merge an be a hieved by simply
deleting the roots vil+1 ; : : : ; vik from roots . Next note that il  q < il+1 <    <
ik , where w = vq and hen e dfsnum [vil  dfsnum [w < dfsnum [vil+1 < : : : <
Version: 19.10.99

Time: 11:01

{32{

4.6. Strongly Conne ted and Bi onne ted Components

33

dfsnum [vk

sin e un nished and roots are ordered by DFS-number. This shows


that the merge an be a hieved by popping all roots from roots whi h have a DFSnumber larger than w. That is exa tly what lines (10) and (11) of Program 14
do. The three invariants are preserved by the arguments above. This nishes the
des ription of how edges are explored. We now turn to the ompletion of alls dfs (v).
 Completion of a all dfs (v):
By I2 the node v is a tree des endant of top (roots ). If it is a proper tree des endant,
i.e., v 6= top (roots ), then the ompletion of dfs (v) does not omplete a s. . .. We
return to dfs (w) where w is the
parent of v. Clearly, w is still a tree des endant of

top (roots ) and also w ! v ! top (roots ) and therefore w and top (roots ) belong to
E ur E ur
the same s. . .. This shows that I2 and I3 are preserved; I1 is also preserved sin e
we do not omplete a omponent.
If v = top (roots ) then we omplete a omponent. By I3 this omponent onsists
of exa tly those nodes in un nished whi h do not pre ede top (roots ) and hen e these
nodes are easily enumerated as shown in lines (16) through (18) of Program 14. Of
ourse, top (roots ) eases to be a root of an un ompleted s. . . and hen e has to be
deleted from roots ; line (19). We still need to prove that the invariants are preserved.
For I1 this follows from the fa t that all edges leaving the just ompleted s. . . must
terminate in previously ompleted s. . .'s, sin e the un ompleted s. . .'s form a path
by I2. The invariants I2 and I3 are also maintained by a similar argument as in the
ase v 6= top (roots ).
We have now ompleted the orre tness proof of Program 14 and summarize in
Theorem 2. Program 14 omputes the strongly onne ted omponents of a digraph in time O(n + m).
Proof :

Having already proved orre tness, we still have to prove the time bound.
The time bound follows dire tly from the linear time bound for DFS and the fa t
that every node is pushed onto and hen e popped from un nished and roots exa tly
on e. This implies that the time spent in lines (11) and (16) is O(n). The time
spent in all other lines is O(n + m) by Lemma 1 of Se tion 4.5.
We now turn to bi onne ted omponents of undire ted graphs.
De nition:
a) A onne ted undire ted graph G = (V; E ) is bi onne ted if G v is onne ted
for every v 2 V .
b) A bi onne ted omponent (b. . .) of an undire ted graph is a maximal

bi onne ted subgraph.


) A vertex a 2 V is an arti ulation point of G if G a is not onne ted.
We start with a simple observation on bi onne ted omponents. Let G1 = (V1 ; E1 );
: : : ; Gm = (Vm ; Em ) be the bi onne ted omponents of an undire ted graph G =
Version: 19.10.99

Time: 11:01

{33{

34

Chapter 4. Algorithms on Graphs

(V; E ). Then E = E1 [  [ Em and Ei \ Ej = ; for i 6= j . To see this, note that for


ea h edge (v; w) 2 E the graph onsisting of verti es v and w and the single edge
(v; w) is bi onne ted, and hen e ontained in one of the bi onne ted omponents
of G. Thus (v; w) 2 Eh for some h. It remains to be shown that Ei \ Ej = ; for
i 6= j . Assume otherwise, say (v; w) 2 Ei [ Ej for some i 6= j . Sin e Gi and Gj
are maximal bi onne ted subgraphs, the subgraph G0 = (Vi [ Vj ; Ei [ Ej ) is not
bi onne ted. Thus a vertex a 2 Vi [ Vj must exist su h that G0 a is not onne ted.
Let x and y be verti es in di erent omponents of G0 a. Sin e Gi a and Gj a
are onne ted, we must have x 2 Vi and y 2 Vj (or vi e versa). Sin e a annot be
equal to both v and w, we may assume v 6= a. Sin e Gi a (Gj a) is onne ted,
a path exists from x to v (y to v) in Gi a (Gj a). Hen e a path exists from x
to y in G0 a, and we have rea hed a ontradi tion. We have thus shown that the
b. . .'s of a graph give a partition of the edges of the graph.
One further observation on b. . .'s will be useful in the sequel. If there is a
simple y le through nodes v and w then v and w belong to the same b. . .. In
the example of Figure 11 there are four b. . .'s, namely ff; g, fa; b; g, fe; gg and
fb; d; eg. The arti ulation points are , e and b. A depth- rst-sear h on the graph
of Figure 11 ould yield the stru ture of Figure 12; nodes are explored in the order
a; b; ; f; d; e; g.
a

d
b

Figure 11.

A graph with 4 b. . .'s

The bi onne ted omponents are easily re ognized in Figure 12. The rst edge
of ea h b. . . whi h is explored is a tree edge; we all the endpoint of that tree edge
the enter of the omponent (formally, the node with the se ond smallest dfsnum
in the b. . .). All ba k edges leaving the subtree rooted at the enter of a b. . .
end in the parent node of the enter or in a tree des endant of the enter. The
parent of the enter is always an arti ulation point or the root of the dfs -tree. The
b. . . with enter v onsists of the parent of v and the nodes whi h are rea hable
from v via tree edges without going through another enter. In our example the
enters of the four omponents are f; g; d and b. The b. . . with enter d onsists
of b = parent [d, and the tree des endants d and e of d.
Formally, we de ne the enter of a b. . . as the node with the se ond smallest
dfsnum in the b. . .. The enters of b. . .'s play a role similar to the roots of s. . .'s.
In fa t, Program 14 is easily modi ed to ompute the b. . .'s of undire ted graphs,
Version: 19.10.99

Time: 11:01

{34{

4.6. Strongly Conne ted and Bi onne ted Components

d
b

2
a

Figure 12.

35

A DFS on the graph of Figure 11

f. Exer ise 11. Here we des ribe an algorithm whi h is losely related to the s. . .
algorithm outlined in Exer ise 8. For ea h node v let
lowpt [v = min(fdfsnum [vg [

 w ! z g):
fdfsnum [z; there is w su h that v !
T
B

= (V; E ) be a onne ted undire ted graph, let T; F; B and


dfsnum be determined by a depth- rst-sear h on the dire ted version of G and
let lowpt be de ned as above.

Lemma 3. Let G

a) lowpt [v  dfsnum [parent [v for all v with dfsnum [v  2.


b) v is the enter of a b. . . i lowpt [v = dfsnum [parent [v and dfsnum [v  2.
) Let G0 = (V 0 ; E 0 ) be a b. . . with enter v. Then
V 0 = fparent [vg [
fw; v ! w and there is no enter 6= v on the tree path from v to wg.
T

lowpt [v = min(fdfsnum [vg

[
fdfsnum [z; (v; z) 2 B g [
flowpt [u; (v; u) 2 T g

d)

for all v 2 V .
Proof : a) If dfsnum [v  2 then parent [v exists and edge (v; parent [v) is a ba k
edge. Hen e lowpt [v  dfsnum [parent [v.
Version: 19.10.99

Time: 11:01

{35{

36

Chapter 4. Algorithms on Graphs

b) \)": Let v be the enter of a b. . .. Then ertainly dfsnum


[v  2. Sup
pose lowpt [v = dfsnum [u < dfsnum [parent [v. Then a path v !
w!
u exists for
T
B
some w. Also, u is a tree an estor of v and sin e u 6= parent [v a tree an estor of
parent [v as well. Hen e u, parent [v, v and w lie on a simple y le and hen e all
belong to the same b. . .. This b. . . ontains at least two nodes, namely u and
parent [v, whose dfsnum 's are smaller than v's dfsnum , a ontradi tion.
\(": Suppose dfsnum [v  2 and lowpt [v = dfsnum [parent [v. Consider the
b. . . G0 = (V 0 ; E 0 ) ontaining the edge fparent [v; vg. We will show that v is the
enter of G0. Assume the existen e of u 2 V 0, u 6= parent [v and dfsnum [u <
dfsnum [v. Sin e G0 parent [v is onne ted, there must be a simple path v0 ; : : : ; vk
from v = v0 to vk = u avoiding node parent [v. Let vi be the rst
node on that

path whi h is not a tree des endant of v, i.e., v !T vi 1 and :(v !
vi ). Then edge
T
(vi 1 ; vi) must be a ba k edge. Also, lowpt [v  dfsnum [vi by the de nition of
lowpt . Furthermore, sin e vi is a tree an estor of vi 1 and sin e vi is not a tree
des endant of v and vi 6= parent [v, vi must be a proper an estor of parent [v. Hen e
lowpt [v  dfsnum [vi < dfsnum [parent [v, a ontradi tion.
) Let G0 = (V 0; E 0 ) be a b. . . with enter v. In the proof of the se ond half
of part b) it was shown that parent [v 2 V 0 . Also, by the de nition of enter
dfsnum [v < dfsnum [w for all w 2 V 0 fv; parent [vg. Sin e G0 parent [v is
onne ted, all verti es w 2 V 0 fv; parent [vg are rea hed by the sear h before
dfs (v) is ompleted. None of them is rea hed before dfs (v) is started and hen e
 w for all w 2 V 0 fparent [vg. This proves V 0  fparent [vg [ fw; v !
 wg.
v!
T
T
 z and z 2= V 0 . Let be the enter of the b. . . G00 ontaining
Next suppose v !
T
 z by the rst part of the proof of part ) and
the edge fparent [z; zg. Then !
T
 v. = v is impossible sin e z 2= V 0 . In the rst ase
hen e either v !T or !
T
 v. Sin e G00 parent [v is onne ted (parent [v
we have nished. So suppose !
T
might not even be a node of G00), a path z0 ; : : : ; zj must exist from z to avoiding

parent [v. Be ause of this fa t and sin e v ! z a path v0 ; : : : ; vk must exist from
T
v = v0 to = vk avoiding parent [v. Let vi be the rst node of that path whi h
is not a tree des endant of v. As in the proof of part b) one shows lowpt [v 
dfsnum [vi < dfsnum [parent [v, a ontradi tion. This ompletes the proof of part
).
d) Follows dire tly from the de nition of lowpt .
Lemma 3 dire tly leads to the algorithm shown in Program 15. Lines (4), (10)
and (11) ompute the lowpt -values; note that in line (11) the test whether (v; w) 2
B , i.e., dfsnum [w < dfsnum [v, is unne essary, sin e the line has no e e t if (v; w) 2
F , i.e., dfsnum [v  dfsnum [w. In line (14) the enters of b. . .'s are re ognized
by Lemma 3, part b). By part ) the b. . . with enter v onsists of parent [v and
Version: 19.10.99

Time: 11:01

{36{

4.6. Strongly Conne ted and Bi onne ted Components

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)
(20)
(21)
(22)
(23)

pro edure dfs (v : node );


ount 1

ount 1 + 1; dfsnum [v

add v to S ;

37

ount 1;

lowpt [v

dfsnum [v;
push v on un nished ;
for all (v; w) 2 E
do if w 2= S
then parent [w v;
dfs (w);
lowpt [v min(lowpt [v; lowpt [w)
else lowpt [v min(lowpt [v; dfsnum [w)

fi
od;
if dfsnum [v  2 and lowpt [v = dfsnum [parent [v
then repeat w pop (un nished );
until w = v
o the nodes popped together with the
node parent [v form the b. . . with enter v o
fi
end;
begin
o main program o
S ;; un nished empty sta k ;

ount 1

0;
for all v 2 V do if v 2= S then dfs (v) fi od
end.
Program 15

all tree des endants w of v whi h are not tree des endants of another enter. These
nodes w were not added to un nished before v. Conversely, if w was not added to
un nished not before v and is on un nished when line (14) is exe uted then the all
dfs (w) is nested within the all dfs (v) (observe that we are about to omplete the
all dfs (v)) and hen e w is a tree des endant of v but not a tree des endant of any
other enter. Thus lines (15) and (16) orre tly enumerate the nodes in the b. . .
with enter v. This establishes the orre tness of our algorithm.
For the running time we only have to observe that ea h node is popped from
un nished exa tly on e and hen e the time spent in lines (15) and (16) is O(n). The
time spent in all other lines is O(n + m) by Lemma 1 of Se tion 4.5. We summarize
in
Theorem 3. The bi onne ted omponents of an undire ted graph an be determined in time O(n + m).

In our example we have lowpt [f = 3, lowpt [g = 6, lowpt [e = lowpt [d = 2 and


= lowpt [b = lowpt [a = 1. The rst enter found is f . Just prior to the

lowpt [

Version: 19.10.99

Time: 11:01

{37{

38

Chapter 4. Algorithms on Graphs

exe ution of line (15) in dfs (f ) the ontent of un nished is a; b; ; f . In line (15) f
is deleted. Then d; e and g are added to un nished and so prior to the exe ution of
line (15) in dfs (g) the ontent of un nished is a; b; ; d; e; g. In line (15) g is removed.
The next enter found is d and so d and e will be removed. Finally, enter b is found
and and b are removed in line (15) of dfs (b).
We end this se tion with an appli ation of the s. . . algorithm to the omputation of
the transitive losure of digraphs. Let G = (V; E ) be a digraph. Let V1 ; V2 ; : : : ; Vk
be the (node sets of the) s. . .'s of G. Let G0 = (V 0; E 0 ) be de ned by
V 0 = fV1 ; : : : ; Vk g and
E 0 = f(Vi ; Vj ); v 2 Vi ; w 2 Vj exist su h that (v; w) 2 E g:
Then G0 = (V 0 ; E 0 ) is an a y li digraph. Let G0 = (V 0 ; E 0  ) be the transitive
losure of G0. Then G = (V; E  ), where
E  = f(v; w) 2 V  V ; v 2 Vi ; w 2 Vj and (Vi ; Vj ) 2 E 0  for some i and j g
is the transitive losure of G. The pro ess des ribed above is easily turned into an
algorithm. First, determine V1 ; : : : ; Vk in time O(n + m). Se ondly, onstru t G0 in
time O(n + m). Thirdly, ompute the transitive losure of G0 in time O(k3 ) by the
methods des ribed in Se tion 4.3. Finally, E  an be omputed from G0 in time
O(k + m ), where m = jE  j. We summarize in
Theorem 4. Let G = (V; E ) be a digraph. Then the transitive losure of G an be
omputed in time O(n + m + k3 ), where m is the number of edges in the transitive
losure and k is the number of s. . .'s of G.

4.7. Least Cost Paths in Networks

A network N is a dire ted graph G = (V; E ) together with a ost fun tion
: E ! R. We are interested in determining the least ost path from a xed
vertex s (the sour e) to all other nodes (the single sour e problem) or from ea h
node to every other node (the all pairs problem). The latter problem is also
treated in Chapter V.
A path p from v to w is a sequen e v0 ; v1 ; : : : ; vk of nodes with v = v0 , w = vk
and (vi ; vi+1P) 2 E for 0  i < k. The length of the path p is k and the ost (p) of
the path is ik=01 (vi ; vi+1 ). The ost of the path of length 0 is 0. The path above
is simple if vi 6= vj for 0  i < j < k. We de ne the ost (u; v) of the least ost
path from u to v by
(u; v) = inff (p); p is a path from u to vg;
the in mum over the empty set being 1.
Version: 19.10.99

Time: 11:01

{38{

4.7. Least Cost Paths in Networks

39

Example: In the example of Figure 13 we have (a; e) = 1, (e; a) = +1, (a; b) =


(a; ) = (a; d) = 1. (Note that the path ab( db)i from a to b has length 1 + 3i
and ost 3 i.)

1
a

4 d
Figure 13. Graph with osts +1 and 1
We on entrate on the single sour e problem rst, i.e., we are given a network
= (V; E; ), : E ! R, and a node s 2 V and we have to determine (s; v) for
all v 2 V . Our algorithm for this problem is based on the following observation:
The osts (s; v) ertainly satisfy the triangle inequalities

8(u; v) 2 E : (s; u) + (u; v)  (s; v);


i.e., a path from s to v whi h onsists of a least ost path from s to u followed by
the edge (u; v) an ertainly be of no smaller ost than the least ost path from s
to v. Furthermore, for every v 6= s there must be at least one edge (u; v) 2 E su h
that (s; u) + (u; v) = (s; v), e.g., let (u; v) be the last edge on a least ost path
from s to v.
These observations lead to the following algorithm for determining least ost paths.
We start with a fun tion ost [v, v 2 V , whi h overestimates (s; v), e.g., the
fun tion ost [s = 0 and ost [v = +1 for v 6= s will do. Then we look for an edge
(u; v) su h that ost does not satisfy the triangle inequality with respe t to edge
(u; v), i.e., ost [u + (u; v) < ost [v. Whenever su h an edge is found we use it to
redu e ost [v to ost [u + (u; v).
In the rst formulation of our algorithm by Program 16 we do not only ompute
the osts of the least ost paths but also the paths themselves, i.e., path [v ontains
a path of ost ost [v from s to v stored as a sequen e of nodes. We use (s) to
denote the path of length zero from s to s and we use \path [u at v" for extending
a path from s to u to a path from s through u to v.
Program 16 is nondeterministi . Any edge violating the triangle inequality
an be hosen in line (6). We will show that the orre tness of the algorithm does
not depend on the sequen e of hoi es made. However, the running time depends
heavily on it as the following example shows.
Example:

Let Nn = (Vn ; En ; n ) be the following network:

Version: 19.10.99

Time: 11:01

{39{

40
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)

Chapter 4. Algorithms on Graphs


ost [s

0; path [s (s);
all v 2 V , v 6= s do ost [v +1;
path [v unde ned
od;
while 9(u; v) 2 E : ost [u + (u; v) < ost [v
do hoose any edge (u; v) 2 E with ost [u + (u; v) < ost [v;
ost [v ost [u + (u; v);
path [v path [u at v
od.
for

Program 16

Vn = fvi ; ui ; si ; 0  i < ng [ fsn g,


En = f(si+1 ; vi ); (si+1 ; ui ); (ui ; si ); (vi ; si ); 0  i < ng and
n : En ! R with
n ((si+1 ; ui )) = n ((ui ; si )) = n ((vi ; si )) = 0
n ((si+1 ; vi )) = 2i :

Also s = sn. Figure 14 shows N3.


4

v2

s3

v1

s2

u2

v0

s1

u1

s0

u0

Figure 14. N3

If the edges are hosen in the order (sn; un 1 ), (un 1 ; sn 1), (sn ; vn 1 ), (sn 1 ; un 2 ),
(un 2 ; sn 2), (sn 1 ; vn 2 ); : : : in line (6) then the body of the while loop is exe uted
exa tly jVn j 1 = 3n times.
Now onsider the following indu tively de ned sequen e Sn of hoi es. On
N1 we use S1 = (s1 ; v0 ); (v0 ; s0 ); (s1 ; u0 ); (u0 ; s0 ) of length 4 and on Nn we use
Sn = (sn ; vn 1 ); (vn 1 ; sn 1 ); Sn 1 ; (sn ; un 1 ); (un 1 ; sn 1 ); Sn 1 of length jSn j =
4+2jSn 1 j = 4(2n 1). Note that after using edges (sn ; vn 1 ); (vn 1 ; sn 1); Sn 1
we will have ost [sn = 0, ost [un 1 = 1, and ost [v  2n 1 for all other nodes v.
The hoi e of edges (sn; un 1 ); (un 1 ; sn 1) will redu e ost [sn 1 to zero. We an
now run through sequen e Sn 1 again.
Version: 19.10.99

Time: 11:01

{40{

4.7. Least Cost Paths in Networks

41

Lemma 1.
a) (s; v) > 1 for all v 2 V i the algorithm terminates.
b) If the algorithm terminates then (s; v) = ost [v for all v 2 V on termination.
Proof :

a) \)": The following laim an easily be proved by indu tion on the


number of iterations of the loop.
Claim: Before any exe ution of line (6) the following holds true: path [v is a path
of ost ost [v from s to v and if path [v = (v0 ; : : : ; vk ) then for all i < k we have
that (v0 ; : : : ; vi ) was the ontent of path [vi previously.
We will now show that path [v is always a simple path from s to v if (s; v) > 1.
Sin e the number of simple paths is nite and sin e no variable path [v an ontain
the same path twi e this implies termination.
Assume to the ontrary that path [v may be a non-simple path from s to v,
i.e., path [v = (v0 ; : : : ; vi ; : : : ; vj ; : : : ; vk ) and u = vi = vj for some i < j . Then
p1 = (v0 ; : : : ; vi ) as well as p2 = (v0 ; : : : ; vi ; : : : ; vj ) have been the ontent of path [u
at some point during the exe ution of the algorithm. Also (p2 ) < (p1 ) sin e
ost [u de reases whenever path [u is hanged. However, (p2 ) (p1 ) is the ost of
the y le vi; : : : ; vj . So the graph ontains a y le of negative ost. Going around
that y le su iently many times we an make the ost of a path to v as small as
we want. Hen e (s; v) = 1, ontradi tion. This shows that path [v is always a
simple path.
\(": When the algorithm terminates we obviously have ost [v > 1 for all v 2 V
on termination. Hen e this dire tion follows from part b).
b) Suppose that the algorithm terminates and (s; v) < ost [v for some v 2 V on
termination. Then there must be a path p = (v0 ; : : : ; vk ) from s = v0 to v = vk with
(p) < ost [v. Let pi = (v0 ; : : : ; vi ) be the pre x of p leading from s to vi , 0  i < k.
There must be a minimal i su h that (pi ) < ost [vi on termination. Sin e (p0 ) = 0
and ost [v0  0 (re all s = v0 ) we dedu e i  1, and thus ost [vi 1 = (pi 1) on
termination. This implies
ost [vi > (pi ) = (pi 1 ) + (vi 1 ; vi )
= ost [vi 1 + (vi 1 ; vi )
and hen e the algorithm does not terminate be ause the triangle inequality for edge
(vi 1 ; vi) is violated.
Lemma 1 states that the algorithm will terminate with the orre t osts whenever
(s; v) > 1 for all v 2 V . However, we have also seen that the sequen e of hoi es
made in line (6) has a ru ial in uen e on the running time. Also, we have to say
more about the test in line (5). How do we nd out whether some edge violates the
triangle inequality?
Note rst that when the loop is entered for the rst time only the edges out
of s are andidates for sele tion in line (6). Assume now that whenever an edge
Version: 19.10.99

Time: 11:01

{41{

42

Chapter 4. Algorithms on Graphs

(u; v) is sele ted in line (6) we will also he k all other edges (u; v0 ) going out of u
for satisfa tion of the triangle inequality. Then the edges going out of u do not have
to be he ked again until ost [u is redu ed. This observation leads to Program 17,
a re nement of our basi algorithm.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)

ost [s

0; U fsg;
for all v 6= s do ost [v +1 od;
while U 6= ;
do o if u 2= U then ost [u + (u; v)  ost [v for all (u; v) 2 E o
sele t any u 2 U and delete u from U ;
for all (u; v) 2 E
do if ost [u + (u; v) < ost [v
then ost [v ost [u + (u; v);
add v to U
fi
od
od.
Program 17

Our main problem still remains: Whi h point u shall we sele t from U in
line (5)? The following lemma states that U always ontains at least one perfe t
hoi e: a node with ost [u = (s; u).
Lemma 2.
a) If v 2= U then ost [v + (v; w)  ost [w for all (v; w) 2 E .
b) Let +1 > (s; v) > 1 and let v0 ; : : : ; vk be a least ost path from s = v0 to
v = vk . If ost [v > (s; v) then there is an i, 0  i < k, su h that vi 2 U and
ost [vi = (s; vi ).
) If (s; v) > 1 for all v 2 V then either U = ; or there exists u 2 U with
ost [u = (s; u).
d) If a node u having ost [u = (s; u) is always hosen in line (5) then the body
of the loop is exe uted at most n times.
Proof : a) (By indu tion on the number of exe utions of the while loop). The laim
holds ertainly true before the rst exe ution of the loop. Now suppose that v 2= U
after exe ution of the body. Then either v 2= U before exe ution and then ost [v
was not hanged and ost [w for w 6= v was not in reased in the body of the loop
and hen e ost [v + (v; w)  ost [w by indu tion hypothesis or v 2 U before
exe ution and then edge (v; w) has been onsidered in lines (7) and (8) and hen e
ost [v + (v; w)  ost [w by the algorithm.
Version: 19.10.99

Time: 11:01

{42{

4.7.1. A y li Networks

43

b) Let i = minfj ; ost [vj+1 > (s; vj+1 )g. Then i < k sin e ost [vk > (s; vk )
by assumption and i  0 sin e (s; s) = 0 (note that (s; s) < 0 would imply
[(s; v) = +1 or (s; v) = 1 for all v 2 V ) and hen e (s; s) = ost [s. Sin e
i  0 we have ost [vi = (s; vi ). If vi were not in U then by part a) (s; vi+1 ) =
(s; vi ) + (vi ; vi+1 ) = ost [vi + (vi ; vi+1 )  ost [vi+1 , a ontradi tion to the
de nition of i. Thus vi 2 U .
) Let v 2 U be arbitrary. If ost [v = (s; v) then we are done. Otherwise there is
a node u 2 U on the least ost path from s to v with ost [u = (s; u) by part b).
d) If a node u with ost [u = (s; u) is always hosen in line (5) then no node an
reenter U after having left U , sin e ost [u is redu ed whenever a node is added
to U . Hen e every node is deleted at most on e from U , i.e., the body of the loop
is exe uted at most n times.
Lemma 2 states that U always ontains at least one perfe t hoi e for the sele tion
pro ess in line (5). Unfortunately, in the ase of arbitrary real osts we do not
know any e ient method for making a perfe t hoi e. We treat two spe ial ases:
a y li networks (the underlying graph is a y li ) and non-negative networks (the
fun tion : E ! R+0 assigns non-negative osts to every edge). In these ases we
obtain O(n + m) and O(m + n log n) algorithms, respe tively. In the general ase
we an only make sure that a good hoi e is made at ea h O(n)-th iteration of the
loop. This will lead to an O(n  m) algorithm.
4.7.1. A y li Networks

Let G = (V; E ) be an a y li graph and : E ! R be a ost fun tion on the edges.


We assume that G is topologi ally sorted, i.e., V = f1; : : : ; ng and E  f(i; j ); 1 
i < j  ng and s = 1. In Se tion 4.2 we saw that a graph an be topologi ally
sorted in time O(n + m). We repla e line (5) by
(50 ) sele t and delete u 2 U with u minimal.
Then u is always a perfe t hoi e, i.e., ost [u = (s; u) when u is sele ted from U :
Indeed, by Lemma 2 there must be a node v 2 U on the least ost path from s to u
with ost [v = (s; v). Sin e the graph is topologi ally sorted we must have v  u
and hen e v = u by the de nition of u in line (50 ).
Line (50 ) steps through the nodes of G in in reasing order. We an therefore
do ompletely away with set U and reformulate the algorithm as Program 18.
Theorem 1. In a y li graphs the single sour e least ost paths problem an be
solved in time O(n + m).
Proof :

Topologi al sorting takes time O(n + m). Program 18 also learly runs in
time O(n + m).

Version: 19.10.99

Time: 11:01

{43{

44

Chapter 4. Algorithms on Graphs

(1)
(2)
(3)
(4)
(5)
(6)
(7)

ost [1

0;

for all v  2 do ost [v +1 od;


for u from 1 to n 1
do for all (u; v) 2 E
do ost [v min( ost [v; ost [u + (u; v))
od
od.
Program 18

There is a generalization of a y li graphs whi h still allows for a linear time shortest
path algorithm: shortest-path-orderable graphs.
De nition: A digraph G = (V; E ) with sour e s is shortest-path-orderable
with respe t to s (or brie y, sp-orderable) if there is permutation e1 ; e2 ; : : : ; em
of E su h that every simple path starting in s uses edges in in reasing order. The
sequen e e1 ; : : : ; em is alled an sp-order.
Figure 15 shows examples of sp-orderable and non-sp-orderable graphs. For the
two orderable graphs the edges are labelled a; b; ; : : : orresponding to a possible
ordering. The third graph of Figure 15 is not sp-orderable sin e there is a simple
path using e before e0 and a simple path using e0 before e.
e
a

d

s

e0

e
s

Two sp-orderable graphs and one graph whi h is not sp-orderable


Theorem 2. Let G = (V; E ) with sour e s be sp-orderable and let e1 ; e2 ; : : : ; em
Figure 15.

be an sp-order of E . Then the single sour e least ost path problem an be solved
in linear time O(n + m) for any ost fun tion : E ! R.
Proof : Consider Program 19. It runs in time O(n + m) and omputes a fun tion
ost : V ! R. If this fun tion satis es the triangle inequality, a fa t that an be

he ked in linear time, Program 19 orresponds to a run of Program 16 with the


edges onsidered in the order e1 ; : : : ; em and hen e ost [v = (s; v) for all v by
Lemma 1b. Suppose now that the resulting ost fun tion does not satisfy the triangle inequality. Let W be a set of targets of edges violating the triangle inequality,
i.e., W = fw; 9(v; w) 2 E; ost [w > ost [v + (v; w)g, and let CW be the set of
nodes rea hable from a node in W .
Version: 19.10.99

Time: 11:01

{44{

4.7.2. Non-negative Networks

45

ost [s 0;
for al v 6= s do ost [v +1 od;
for i 1 to m
do
let ei = (v; w);
ost [w = min( ost [w; ost [v + (v; w))
od.
Program 19

Claim: (s; v) = ost [v for v 2= CW and (s; v) = 1 for v 2 CW .


Proof : We show rst that (s; v) > 1 implies (s; w) = ost [w. If (s; w) > 1
then (s; w) is the ost of a simple path from s to w. Sin e e1 ; : : : ; em is an sp-order
of G, p onsists o edges ei1 ; ei2 ; : : : ; eik with i1 < i2 <    < ik . It is now obvious
that ost [w = (s; w) on termination of Program 19. Consider any node w with
(s; w) = 1 next. Then there is a path p1 p2 p3 from s to w su h that p2 is a y le
and (p2 ) < 0. Let p2 onsist of edges ei1 ; ei2 ; : : : ; eik where eij = (vj ; vj+1 ) and
vk+1 = v1 . Then
k
X
j =1

( ost [vj+1

ost [vj

(eij )) = ost [vk+1 ost [v1

(eij )

= 0 (p2 ) > 0
and hen e there is some j with ost [vj+1 > ost [vj + (eij ), i.e., vj+1 2 W . Also
w is learly rea hable from vj +1 and hen e w 2 CW .
We have now shown that (s; w) = 1 implies w 2 CW and w 2= CW implies
(s; w) > 1 and hen e (s; w) = ost [w. It remains to show that w 2 CW
implies (s; w) = 1. Let w 2 W , i.e., there is an edge e = (v; w) with ost [w >
ost [v + ((v; w)). Then either ost [w 6= (s; w) or ost [v 6= (s; v) and hen e
either (s; w) = 1 or (s; v) = 1. In either ase we have (s; v) = 1. This
ompletes the proof of the laim and of Theorem 2.
The best algorithm known to de ide whether a dire ted graph is sp-orderable and, if
need be, to ompute an sp-order runs in time O(n2 ), f. the bibliographi remarks.
Together with Theorem 2 this yields an O(n2 + m) algorithm for the single sour e
shortest path problem for sp-orderable graphs whi h ompares favorably with the
O(n  m) bound for arbitrary graphs; f. Se tion 4.7.4. Furthermore, there are pra ti ally important sub lasses of sp-orderable graphs where an order an be omputed
in linear time; f. Exer ises 1101 and 1102.
4.7.2. Non-negative Networks

A network N = (V; E; ) is non-negative if : E ! R+0 assigns non-negative osts to


every edge. Non-negative networks arise very frequently in pra ti e and therefore
Version: 19.10.99

Time: 11:01

{45{

46

Chapter 4. Algorithms on Graphs

the least ost path problem for these networks has been studied intensively. This
se tion is divided into four parts. In the rst part we redu e the least ost path
problem to a data stru ture problem, namely the e ient realization of a priority
queue. Di erent implementations of priority queues, whi h we treat in the se ond
part, yield
p di erent running times. We show how to a hieve time O(m + n log n),
O(m+n log C ), and O(m log log C ) respe tively; for the latter bounds it is assumed
that edge osts are integers in the range [0 : : C . In the third part we deal with the
one-pair problem and in the fourth part we des ribe a s aling approa h for the least
ost path problem. In this approa h edge osts are also assumed to be integral.
The method works in phases and omputes su essively better approximations to
the nal solution. Although the a hieved running time is only O(m  logm=n C ),
the algorithm is very simple and serves as a rst illustration of the s aling method.
Further appli ations of the method will be seen in the se tions on mat hing and
network ow.
4.7.2.1. A Basi Algorithm for Non-Negative Networks

We repla e line (5) of Program 16 by


(500 ) sele t and delete u 2 U with ost [u minimal.
Then u is always a perfe t hoi e, i.e., ost [u = (s; u) when u is sele ted from U :
By Lemma 2 there must be a node v 2 U on the least ost path from s to u with
ost [v = (s; v). Sin e u is sele ted we have ost [u  ost [v and sin e v is on the
least ost path from s to u and edge osts are non-negative we have (s; v)  (s; u).
Therefore ost [u  ost [v = (s; v)  (s; u) and sin e ost [u  (s; u) always
we even have ost [u = (s; u).
How shall we implement set U ? What operations are required on set U and
fun tion ost ? In line (500 ) we need to sele t and delete u 2 U with ost [u minimal.
In line (7) we need to obtain the value ost [v given node v and in line (8) we need
to hange the fun tion value at argument v. In line (9) we need to add v to U if it
is not already there. Finally, we need to initialize U and ost in lines (1) and (2).
A data stru ture supporting these operations is alled a priority queue. A pre ise
de nition is as follows.
Let K be any linearly ordered set with linear order  and let INF be any set.
A priority queue (over K and INF ) is a partial fun tion pq : I ! K  INF ,
where I is a set of items (in implementations of priority queues I is typi ally a set
of array indi es or a set of storage lo ations). For a pair p = (k; inf ) 2 K  INF
let key(p) = k and inf (p) = inf . The following operations on priority queues are
provided.
pro edure Create (PQ : priority queue; n : integer)
o reates a set I of n items and gives PQ the value pq 0 where pq 0 is the
fun tion with empty domain. o
Version: 19.10.99

Time: 11:01

{46{

4.7.2.1. A Basi Algorithm for Non-Negative Networks

fun tion Insert (PQ : priority queue; k : K; inf : INF ) : item


o let pq be the fun tion denoted by PQ and let i 2 I
\unused" item. Then PQ is made to denote pq 0 where
pq 0 (j ) =

(k; inf )
pq (j )

dom (pq )

47

be any

if j = i;
if j 6= i.

Also, the item i is returned by the fun tion. o


fun tion Findmin (PQ : priority queue) : item
o Let pq be the fun tion denoted by PQ and let i 2 dom (pq ) be su h that
key(pq (i))  key(pq (j )) for all j 2 dom (pq ). Then i is returned. If dom (pq) = ;
then this fun tion is unde ned. o
fun tion is empty (PQ : priority queue) : boolean
o Let pq be the fun tion denoted by PQ ; returns true if dom (pq ) = ; and
false otherwise. o
fun tion get key (PQ : priority queue; i : item) : K
o Let pq be the fun tion denoted by PQ ;
then key(pq (i)) is returned if i 2 dom (pq ). If i 2= dom (pq ) then the result is
unde ned. o
fun tion get inf (PQ : priority queue; i : item) : K
Let pq be the fun tion denoted by PQ ; Then inf (pq (i)) is returned if i 2
dom (pq ). If i 2= dom (pq ) then the result is unde ned. o
pro edure Delete (PQ : priority queue; i : item)
o Let pq be the fun tion denoted by PQ ; then PQ is made to denote pq 0
where dom (pq 0) = dom (pq ) fig and pq 0(j ) = pq (j ) for all j 2 dom (pq 0 ). o
pro edure Deletemin (PQ : priority queue)
o Delete (PQ ; Findmin (PQ )) o
pro edure De rease key (PQ : priority queue; i : item; k : key)
o Let pq be the fun tion denoted by PQ . This operation assumes i 2 dom (pq )
and key(pq (i)) > k. Then PQ is made to denote pq 0 where
pq 0 (j ) =

(k; get inf (PQ ; j ))


pq (j )

if j = i
otherwise.

In the least ost path problem we use a priority queue with K = R+0 and INF = V .
Rewriting Program 17 yields Program 20. In line (0f) we make PQ a priority queue
whi h an hold up to n items and initialize it with the empty fun tion. In (1a) we
insert the pair (0; s) into PQ and remember the item reated in I [s. In (5a) we
sele t the item of minimal key, extra t the node asso iated with it in line (5b) and
delete the item in (5 ). In line (8a) we distinguish whether v was not added yet to
Version: 19.10.99

Time: 11:01

{47{

48

Chapter 4. Algorithms on Graphs

PQ ( ost [v = 1) or whether v already belongs to PQ . In the rst ase we insert


the pair ( ost [v; v) into PQ and remember the new item in I [v, in the se ond ase

we de rease the key of item I [v to the new ost.


(0a)
(0b)
(0 )
(0d)
(1f)
(1a)
(1b)
(2)
(3)
(4)
(5a)
(5b)
(5 )
(6)
(7)
(8a)
(8b)
(9)
(8 )
(8e)
(10)
(11)
(12)

: priority queue with K = R+0 and INF = V ;


I : array[1 : : n of items;
ost : array[1 : : n of K = R+
0 [ f1g;
i; j : item, v; u : node, d : R+0 [ f1g;
Create (PQ ; n);
I [s Insert (P Q; 0; s);
ost [s 0;
for all v 6= s do ost [v +1; I [v nil od;
while :is empty (PQ )
PQ

do

od.

Findmin (PQ );
get inf (PQ ; i);
Delete (PQ ; i);

i
u

for all (u; v) 2 E


do if ost [u + (u; v) < ost [v
then ost [v ost [u + (u; v);
if I [v = nil
then I [v Insert (PQ ; ost [v; v)
else De rease key (PQ ; I [v; ost [v)
fi
fi
od
Program 20

Theorem 3. Program 20 solves the single sour e least ost path problem in nonnegative networks in time O(n + m + TCreate (n) + n  (TInsert (n) + TFindmin (n) +
Tget inf (n) + TDelete (n)) + m  TDe rease key (n)). Here TXYZ (n) denotes the ost of
priority queue operations XYZ on a queue of size at most n.
Proof :

For the orre tness we only need to observe that Program 20 re nes Program 18. For the time bound we only need to observe that there are never more
than n items in PQ , that Create is exe uted on e, that Findmin , get inf , Delete and
Insert are exe uted at most on e for ea h node and that De rease key is exe uted
at most on e for ea h edge.
Di erent implementations of priority queues are dis ussed in Se tion 4.7.2.2. We
lose this se tion with an important observation about the sequen es of nodes sele ted in line (500 ).
Version: 19.10.99

Time: 11:01

{48{

4.7.2.2.1. General Priority Queues

49

The usage of priority queue PQ is monotone if all alls Insert (PQ ; k; : : :)


and De rease key (PQ ; : : : ; k) satisfy k  get key (i) where i is the item returned by
the most re ent Findmin operation.

De nition:

Lemma 3. The least ost path algorithm uses its priority queue in a monotone

way.

Proof :

Let i be an item returned in line (5a) of Program 20, let u be the asso iated
node, and let ost [u be the asso iated ost. Then sin e edge osts are non-negative,
the new value inserted in line (9) is no smaller than ost [u and the de reased value
of line (8d) is no smaller than ost [u. Hen e the key of the item sele ted in the
next iteration is at least ost [u. This shows that the priority queue is monotone.
4.7.2.2. Priority Queues

In this se tion we present several implementations of priority queues. Ea h implementation gives us a on rete algorithm for the least ost path problem. We
divide the implementations into two groups: General priority queues and integer
valued queues. For the implementations in the rst group the set K of keys an be
any linearly ordered set, for integer valued queues we have K = [0 : : C for some
prespe i ed (in the Create operation) integer C .
4.7.2.2.1. General Priority Queues

The most simple implementation of priority queues ( alled the array implementation) uses three arrays PQ :K : array[1 : : n of K and PQ :INF : array[1 : : n of
INF and PQ :is used : array[1 : : n ofboolean for a priority queue reated by a all
Create (PQ ; n). The set I of items is equal to [1 : : n. Then Create allo ates the
three arrays and initializes PQ :is used to false in time O(n). Insert (PQ ; k; inf ) determines an unused item i by linear sear h through the array PQ :is used , de lares i
used, stores k and inf and returns i. All of this takes time O(n). Findmin s ans
through the arrays PQ :is used and PQ :K and determines an item of minimum key
in time O(n). is empty takes also O(n) by a s an through PQ :is used , and nally
get key , get inf , Delete and De rease key take learly time O(1). We summarize
in:
Theorem 4.
a) The array-implementation of priority queues supports the priority queue operations with time bounds TCreate (n) = TInsert (n) = TFindmin (n) = Tis empty (n) =
O(n) and Tget key (n) = Tget inf (n) = TDelete (n) = TDe rease key (n) = O(1).
Version: 19.10.99

Time: 11:01

{49{

50

Chapter 4. Algorithms on Graphs

b) The least ost path problem in non-negative networks an be solved within


time omplexity O(n2 ).
Proof :

Part a) follows from the dis ussion above and Part b) follows from Part a)
and Theorem 3.

For almost omplete graphs, i.e., m =


(n2 ), the O(n2) running time provided by
Theorem 4b is learly optimal. If m = O(n2 ) then the running time is dominated
by the n alls of Insert , Findmin and is empty .
A se ond implementation (the heap implementation) of priority queues
stores the pairs in range (pq ) in a heap, f. Se tion 2.1.2, ordered a ording to key values. More pre isely, let a  2 be an integer to be hosen later. A priority queue PQ
reated by a all Create (PQ ; n) is realized by four arrays PQ :K : array[1 : : n of
K , PQ :INF : array[1 : : n of INF , PQ :lo ation of item : array[1 : : n of integer
PQ :item of lo ation , a list unused items , and an integer PQ :free . The set I of
items is equal to [1 : : n. If pq is the fun tion denoted by priority queue PQ then
the following four onditions hold
(1) PQ :free = jdom (pq )j + 1,
(2) for i 2 dom (pq ) and j = PQ :lo ation of item [i: pq (i) = (PQ :K [j ; PQ :INF [j ),
1  j < PQ :free , and PQ :item of lo ation [j = i,
(3) unused items is a list of the integers in [1 : : n dom (pq ),
(4) for 2  j < PQ :free : PQ :K [d(j 1)=ae  PQ :K [j .
The rst two onditions state that pairs in range (pq ) are stored in lo ations
1 : : PQ :free 1 of the arrays PQ :K and PQ :INF and that the arrays lo ation of item
and lo it translate between items and lo ations. The third ondition states that the
list unused items ontains exa tly those items whi h do not belong to the domain
of pq and the fourth ondition nally states that if we identify lo ations with the
nodes of a omplete tree of degree a, i.e., the root is labelled 1, its hildren 2, 3, : : : ,
a + 1, and so on, then this tree has the heap property, i.e., the key of the parent of
any node is never larger than the key of the node itself. Figure 16 illustrates these
notions for a = 3. Note that in omplete a-ary tree the parent of node j  2 is
labelled d(j 1)=ae and that the hildren of node j have labels a  (j 1) + 2, : : : ,
a  j + 1.
Theorem 5.
a) The heap-implementation with parameter a supports the priority queue operations with time bound TCreate (n) = O(n), TFindmin (n) = Tis empty (n) =
Tget key (n) = Tget inf (n) = O(1), TDelete (n) = O(a  loga n) and TInsert (n) =
TDe rease key (n) = O(loga n).
b) For a  2 the single sour e least ost path problem in non-negative networks
an be solved in time O(a  n log n= log a + m  log n= log a)
Version: 19.10.99

Time: 11:01

{50{

4.7.2.2.1. General Priority Queues

1
2

51

4
4

13

5 6 7

8 9 10 11 12 13
8 12 9 17 14 13 5 7 8
Figure 16. The rst tree shows the numbering of the nodes in a ternary
heap. The se ond tree shows key values satisfying the heap property. it
orresponds to array [4; 7; 13; 5; 8; 12; 9; 17; 14; 13; 5; 7; 8.

) The single sour e least ost path problem in non-negative networks an be


solved in time O(n + m  log n= max(1; log m=n)).
Proof :

a) Create needs to allo ate four arrays of size n, a linear list of n elements
and takes therefore time O(n). is empty , get key and get inf take learly time O(1).
Findmin takes also time O(1) sin e the root of a heap always orresponds to an item
of smallest key and hen e Findmin an return PQ :item of lo ation [1. Operation
De rease key (PQ ; i; k) an be realized in time O(loga n) as follows. We start in the
lo ation lo = PQ :lo ation of item [i and walk towards the root. As long as the
key stored in the parent lo ation of lo is larger than k we move that key and the
orresponding information into lo ation lo , update the orresponden e between
lo ations and items, hange lo to its parent and ontinue. The details are given
by Program 21. Sin e the depth of an a-ary tree with n nodes is O(loga n) the ost
of a De rease key operation is O(loga n). The operation Insert (PQ ; k; inf ) is only
slightly more ompli ated. We take the rst item, say i, from the list unused items ,
establish the orresponden e between i and lo ation PQ :free , in rease PQ :free by
one and then exe ute the program for De rease key (PQ ; i; k). All of this takes time
O(loga n).
It remains to dis uss Delete (PQ ; i). Let lo be the lo ation orresponding
to item i. If lo = PQ :free 1 then we only have to de rement PQ :free . If
lo 6= PQ :free 1 we move the ontent of lo ation PQ :free 1 to lo ation lo
and then restore the heap property. If the ontent of that lo ation be ame smaller
then we essentially perform a De rease key and need time O(loga n). If it be ame
larger then we s an through the hildren of lo ation lo and nd the lo ation,
say lo new , with smallest key. This takes time O(a). We then inter hange the
ontents of lo ations lo and lo new and ontinue. Of ourse, we also update the
orresponden e between lo ations and items. All of this takes time O(a  loga n).
b) Follows immediately from part a) and Theorem 3.
) Follows from part b) with a = max(2; m=n).
It is worthwhile to look at Theorem 2 for some parti ular values of m. If m =
then running time is O(n log n), if m = n log n then running time is O(m 

O(n)

Version: 19.10.99

Time: 11:01

{51{

52

Chapter 4. Algorithms on Graphs

pro edure De rease key (PQ : priority queue; i : item; k : key);


PQ :lo ation of item [i; o lo is the lo ation orresponding to pq (i) o
PQ :INF [lo ;
while lo > 1 and PQ :K [d(lo 1)=ae > k
do lo new d(lo 1)=ae;
o We move the ontent of lo ation lo new into lo ation lo
and update the orresponden e between items and lo ations. o
PQ :K [lo PQ :K [lo new ;
PQ :INF [lo PQ :INF [lo new ;
PQ :item of lo ation [lo PQ :item of lo ation [lo new ;
PQ :lo ation of item [PQ :lo ation of item [lo lo ;

lo
inf

lo

lo new

od
o We store the pair (k; inf ) in lo ation lo and establish the
orresponden e between item i and lo ation lo . o
PQ :K [lo k;
PQ :INF [lo inf ;
PQ :item of lo ation [lo i;
PQ :lo ation of item [i lo ;
Program 21

log n= log log n), and if m = n1+1=k then running time O(k  m). So for su iently
dense graphs the running time is linear, but for sparse graphs the running time is
non-linear.
With a = 2 the heap implementation of priority queues supports all operations in time O(log n). We next des ribe the Fibona i heap implementation whi h
supports Deletemin and Delete in (amortized) time O(log n) whilst a hieving (amortized) time O(1) for all other operations. This will lead to an O(m + n log n) bound
for the single sour e least ost path problem in non-negative networks.
A Fibona i heap (F-heap) represents a priority queue pq as a olle tion of
heap-ordered trees; ea h item i 2 dom (pq ) uniquely orresponds to a node of one of
the trees in the olle tion. A tree is heap-ordered if for ea h non-root node v the
key of the item orresponding to v is no less than the key of the item orresponding
to the parent of v.
The storage representation of F-heaps makes use of the following types:
type node = re ord key : K ;
inf : INF ;
parent ; leftsib ; rightsib ; hild ": node ;
rank : integer;
marked : boolean
end;
item="node ;
Version: 19.10.99

Time: 11:01

{52{

4.7.3. General Networks

53

priority queue="node ;
Ea h node ontains a pointer to its parent (the value of the pointer is nil for a
root) and to one of its hildren. The hildren of ea h node and also the roots of the
trees in a F-heap form a doubly-linked ir ular list (pointers leftsib and rightsib ).
Finally, the rank eld of ea h node ontains the number of hildren of the node and
the marked eld is a boolean ag whi h will be explained later on. A F-heap is
a essed by a pointer to a root of minimum key. Figure 17 shows a F-heap and its
storage representation.
2
4

4
7

10

9 4

10

4
Figure 17. A F-heap and its storage representation. The information,
rank and marked elds are not shown, keys are integers and nil-pointers
are indi ated by .
We an now dis uss the implementation of the various
4.7.3. General Networks

We will now treat the ase of general networks N = (V; E; ), : E ! R. In this


ase no e ient method exists whi h guarantees a perfe t hoi e. However, if U
is organized as a queue, then between any two sele tions of the same node we will
have made one perfe t hoi e.
More pre isely, U is implemented as a queue UQ and a boolean array UB . We
use UQ in order to sele t elements in line (4) of Program 22 on a rst-in rst-out
basis. Furthermore, we use the boolean array representation of U in order to test
Version: 19.10.99

Time: 11:01

{53{

54

Chapter 4. Algorithms on Graphs

in line (11) whether v is already present in U and if not we add v to the end of the
queue. The omplete algorithm is spe i ed in Program 22.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)
(20)

ost [s
UB [s

0; UQ ;; add s to the end of UQ ;


true; ount [s 0;
for all v 6= s do ost [v +1; ount [v 0; UB [v false od;
while UQ 6= ;
do let u be the rst element in UQ ;
ount [u ount [u + 1;
if ount [u  n + 1 then goto Exit fi;
delete u from UQ ; UB [u false;
for all (u; v) 2 E
do if ost [u + (u; v) < ost [v
then ost [v ost [u + (u; v);
if :UB [v
then add v to the end of UQ ;
UB [v true
od;

od

fi

fi

Exit: if ount [v = n + 1 for some v 2 V


then \ y le of negative ost exists"
else \ ost [v = (s; v) for all v 2 V " fi.
Program 22

We have added the array of ounters in order to ensure termination even in


the presen e of y les of negative osts. We still have to show that the ounters do
not impede orre tness. Queue UQ is implemented as des ribed in I.4, i.e., either
by a linear list or by an array.
Theorem 6. In general networks the single sour e least ost path problem an be
solved in time O(n  e).
Proof :

By virtue of the ounters ea h node (ex ept for maybe one) is sele ted at
most n times in line (4). Whenever node v is hosen the time spent in lines (4)
to (16)Pis O(outdeg (v)). Hen e the total running time of the loop (3) to (17) is
O(n  v2V outdeg (v)) = O(n  e). The ost of the statements outside the loop is
learly O(n). Corre tness remains to be shown.
Claim: Assume (s; u) >
at most n times in line (4).
Version: 19.10.99

Time: 11:01

1 for all u 2 V . Let v be arbitrary. Then v is sele ted


{54{

4.7.4. The All Pairs Problem

55

Proof : Let Ui be set U when v is removed from U for the i-th time. Then Ui
ontains at least one element, say ui, with ost [ui = (s; ui) by Lemma 2 ). Sin e
U is organized as a queue ui is deleted from U before v is deleted for the (i + 1)-th
time. Sin e ui will never be added to U again (see proof of Lemma 2d)), we have
i  n.
In Exer ise 15 it is shown that the time bound may be redu ed to O(kmax  e) where
kmax is the maximal length (number of edges) of a least ost path from s to any
v 2 V . In Exer ise 16 the algorithm above is related to dynami programming.
Alternative approa hes to the single sour e least ost path problem are dis ussed
in Exer ises 18 and 19. A fast algorithm for planar graphs is des ribed in Se tion
4.10.
Another improvement an be made for almost a y li graphs. Let G = (V; E )
be a graph and let V = V1 [    [ Vk be the partition of V into strongly onne ted
omponents. We order the s. . .'s su h that (v; w) 2 E , v 2 Vi, w 2 Vj implies
i  j . Also we split the adja en y lists into two parts, the y li and the a y li
part. For ea h node v 2 Vi, the y li part ontains all edges (v; w) with w 2 Vi,
and the a y li part ontains all edges (v; w) with w 2 Vj , j > i. We an now
modify our algorithm as follows. There are k queues UQ 1; : : : ; UQ k one for ea h
s. . .. In line (4) we always sele t the rst element, say u, of the rst (smallest
index) non-empty queue. Then in line (8) we only step through the y li part of
u's adja en y list. On e a queue UQ i be omes empty we step through the a y li
parts of the adja en y lists of all nodes v 2 Vi and update the osts of the other
endpoints. An argument similar to the one used in the proof of Theorem 5 shows
that v 2 Vk is sele ted at most jVk j times from UQ k (provided that (s; v) > 1
for all v). Hen e the running time is bounded by


O e+

k
X
j =1

jVj j  jEj j

where (Vj ; Ej ), 1  j  k, are the s. . .'s of graph G. If the s. . .'s are small then
this is a onsiderable improvement on Theorem 5. Also note that the modi ed
algorithm will work in linear time on a y li networks.
Theorem 7. Let N = (V; E; ) be a network and let (Vj ; Ej ), 1  j  k, be the
strongly onne ted omponents of G = (V; E ). Then the single sour e least ost
P
path problem an be solved in time O(e + kj=1 jVj j  jEj j).
4.7.4. The All Pairs Problem

We now extend the solution of the previous se tion to a solution of the all pairs
least ost path problem; an alternative solution an be found in Chapter V.
Version: 19.10.99

Time: 11:01

{55{

56

Chapter 4. Algorithms on Graphs

Let N = (V; E; ) be a network. Suppose that we have a fun tion : V ! R


su h that
8(u; v) 2 E : (u) + (u; v)  (v):
Consider ost fun tion  : E ! R with
(u; v) = (u) + (u; v) (v)
for all (u; v) 2 E . Then  is a non-negative ost fun tion. Let (x; y) be the ost
of the least ost path from x to y with respe t to ost fun tion . There is a very
simple relation between  and .
Lemma 4. Let  and  be de ned as above. Then (x; y) = (x; y)+ (x) (y).
Proof : Let p = (v0 ; : : : ; vk ) be any path from x = v0 to y = vk . Then
(p) =

k
X1
i=0

(vi ; vi+1 )

k
X1
i=0

( (vi ) + (vi ; vi+1 ) (vi+1 ))

= (v0 ) +

k
X1
i=0

(vi ; vi+1 ) (vk )

= (p) + (x) (y):


Sin e p is an arbitrary path from x to y we infer (x; y) = (x; y) + (x) (y).
Lemma 4 implies that we an redu e a general least ost path problem to a nonnegative least ost path problem if we know a fun tion having the required properties. But solving one single sour e problem will give us (essentially) a fun tion,
namely (s; v) with the desired properties. There is only one problem we have to
ope with: (s; v) might be in nite and the 's are required to be real. We will
over ome this di ulty by augmenting the network as des ribed below.
Theorem 8. The all pairs least ost path problem an be solved in time


log
n
O ne
max(1; log(e=n)) :
Proof :

Let N = (V; E; ) be a network and let s 2 V be arbitrary. As a rst


step we will extend N to a network N 0 = (V; E 0 ; 0 ) by adding some edges, namely
E 0 = E [ f(s; v); v 2 V; v 6= sg, and

(u; v) if (u; v) 2 E ;
0
(u; v) = large
if (u; v) 2 E 0 E ,

Version: 19.10.99

Time: 11:01

{56{

4.8. Minimum Spanning Trees

57

where large = P(u;v)2E j (u; v)j. Let (x; y) and 0(x; y) be the ost of the least
ost path from x to y in N and N 0 respe tively. Then 0(s; v) < +1 for all
v 2 V be ause of the augmentation. Also N 0 ontains a y le of negative ost i
N ontains a y le of negative ost. This an be seen as follows: If N 0 ontains a
y le of negative ost, then N 0 ontains a simple y le of negative ost. Then ea h
0 E
edge of E 0 is used at most on e in this y le. It annot ontain
P an edge of E
be ause then the length of the y le would be at least large (u;v)2E j (u; v)j  0.
Hen e N ontains a y le of negative ost.
Next we use the algorithm of Se tion 4.7.3 to nd out whether N 0 (and hen e N )
has a y le of negative ost and if not to determine (s; v) for all v 2 V . In the
rst ase the algorithm stops, in the se ond ase we use (v) = (s; v) to transform
the all pairs problem on a general network into a set of n single sour e problems
on a non-negative network. Using the methods of 4.7.2 we obtain the time bound
O (e  n + n  e  log n= max(1; log(e=n))).
4.8. Minimum Spanning Trees

Let N = (V; E; ) be an undire ted network, i.e., (V; E ) is an undire ted graph
and is symmetri ( (v; w) = (w; v) for all (v; w) 2 E ). A tree A = (V; T ) with
T  E and jT j =
n 1 is alled a spanning tree of N . The ost of spanning
tree A is (A) = P(v;w)2T (v; w). It is a minimum spanning tree (or least ost
spanning tree) if (A)  (A0 ) for all other spanning trees A0. Throughout this
se tion we assume that (V; E ) is onne ted. Thus e  n 1.
1
7

2
4

4
1

3
9
2

7
3

3
2
1

8
Figure 18. A network and one of its minimum spanning trees
Program 23 is a ommon skeleton for many algorithms for omputing minimum
spanning trees.
Lemma 1. Program 23 omputes a minimum spanning tree.
Proof : We show by indu tion on m = jT1 j+jT2 j+ : : : +jTn j that there is a minimum
spanning tree A = (V; T ) with Ti  T for all i. If m = 0 then there is nothing
Version: 19.10.99

Time: 11:01

{57{

58
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)

Chapter 4. Algorithms on Graphs

for all i 2 V do Vi
do n 1 times

od.

fig; Ti

; od;

hoose any non-empty Vi;


hoose (v; w) 2 E su h that v 2 Vi, w 2= Vi and (v; w)  (v0 ; w0 )
for all (v0 ; w0 ) 2 E with v0 2 Vi, w0 2= Vi;
let j be su h that w 2 Vj ;
Vi Vi [ Vj ; Vj ;;
Ti Ti [ Tj [ f(v; w)g; Tj ;
Program 23

to show. So let us turn to the indu tion step. By indu tion hypothesis there is
a minimum spanning tree A = (V; T ) with Ti  T for all i. Let (v; w) 2 E be
the edge hosen in line (4). If (v; w) 2 T then we are done. If (v; w) 2= T then
(V; T [ f(v; w)g) ontains a y le. Hen e there must be an edge (v0 ; w0 ) 2 T su h
that v0 2 Vi , w0 2= Vi . We have (v; w)  (v0 ; w0 ) by the hoi e of (v; w). Hen e
T f(v0 ; w0 )g [ f(v; w)g is also a minimum spanning tree. Finally, ase m = n 1
implies the orre tness of the algorithm.
Various details are to be lled in. What set Vi should we hoose in line (3), how
do we nd (v; w) in line (4) and how do we represent sets Vi? Let us solve the
latter problem rst. We use the Union-Find data stru ture of Se tion III.8.3 to
represent sets Vi . Then line (6) is a Union operation (and we exe ute n 1 of
them) and testing whether both endpoints of edge (v; w) 2 E belong to the same
Vi orresponds to two Finds. Sin e this test has to be done at most on e for every
edge (v; w) 2 E the number of Finds is O(e). Thus the total ost of handling sets
Vi is O(e  (e; n)) where is de ned in Se tion III.8.3.
The former questions are more di ult to solve. We dis uss three strategies:
onsidering edges in order of in reasing weight, always growing omponent V1 and
growing omponents uniformly.
Theorem 1.
a) Let E = fe1 ; e2 ; : : :g be sorted a ording to ost, i.e., (e1 )  (e2 )    .
Then a minimum spanning tree an be onstru ted in time O(e  (e; n)).
b) A minimum spanning tree an be onstru ted in time O(e log n).
Proof
0 ) :leta) (We
(3
v; wrepla e
) be thelines
next(3)
edgeandon(4)E ;by
0
(4 ) while v and w belong to the same omponent
(400 ) do let (v; w) be the next edge on E od;
Corre tness of this re nement follows immediately from Lemma 1. Also, the bound
on the running time follows dire tly from the dis ussion above.
Version: 19.10.99

Time: 11:01

{58{

4.8. Minimum Spanning Trees

59

b) Follows from part a) and the fa t that we an sort the set of edges in time
O(e log e) = O(e log n).
We show next that we an improve upon Theorem 1 for dense graphs.
Theorem 2. A minimum spanning tree an be onstru ted in time



log
n
O e
max(1; log(e=n) :

Proof :

We always hoose V1 in line (3), i.e., we grow the spanning tree starting at
node 1. In order to fa ilitate the sele tion of edge (v; w) in line (4) we maintain a
priority queue P Q for set f( (w); v; w); w 2= V1 g ordered a ording to (w) where
(w) = minf (u; w); u 2 V1 g and v is su h that (w) = (v; w). Given this de nition
line (4) orresponds to operation Deletemin on priority queue P Q. Suppose that
edge (v; x) is hosen in line (4). In line (6) we have to add point x to V1 and
we have to update priority queue P Q. More pre isely, for every edge (x; w) 2 E
with w 2= V1 we have to he k whether (x; w) < (w) and if so we have to hange
element ( (w); ; w) of P Q to ( (x; w); x; w). In order to do this e iently we use
an array P [1 : : n of pointers. Pointer P [w points to element ( (w); ; w) on P Q
if w 2= V1 and is nil otherwise. With the help of array P [1 : : n line (6) redu es
to O(deg (x)) operations Demote ( f. III.5.3.1) on priority queue P Q. Thus the
ost of onstru ting a minimum spanning tree is the ost of n Deletemin, O(n)
Insert and O(e) Demote operations on P Q plus the time needed for initializing
the priority queue. Initially, P Q = f( (1; w); 1; w); w 6= 1 and (1; w) 2 E g and
hen e the initialization orresponds to deg (1) = O(n) Insert operations.
If we realize P Q as an unordered (a; 2a)-tree ( f. III.5.3.1) with a = max(2; e=n)
then the ost of a Delete is O(a  log n= log a), of a Deletemin and an Insert operation O(a  log n= log a), and of a Demote O(log n= log a). Hen e the total ost is
O(e log n= max(1; log(e=n))).
Theorem 2 is most signi ant for dense graphs. If e = n1+1=k for k 2 N then the
running time is O(k  e). For sparse graphs, say e = O(n), the running time is
O(n log n). Can we do better for sparse graphs?
We end this se tion giving a brief des ription of an O(e log log n) algorithm.
This algorithm is based on two ideas, on a strategy for growing omponents uniformly and on a spe ial purpose priority queue. Put sets V1 ; V2 ; : : : ; Vn into a queue
Q and repla e lines (3) and (6) by
(300 ) let Vi be the rst element of queue Q;
and
(600 a) delete Vi and Vj from Q;
(600 b) Vi Vi [ Vj ; Vj ;;
(600 ) add Vi to the end of Q;
Version: 19.10.99

Time: 11:01

{59{

60

Chapter 4. Algorithms on Graphs

The sele tion strategy des ribed above sele ts omponents in a round-robin fashion.
For the analysis we on eptually divide the algorithm into stages. Stages are de ned
as follows. We initially add a spe ial marker to the end of Q and start stage 0.
Whenever the spe ial marker appears at the front of queue Q we nish a stage,
move the marker to the end of the queue, and start the next stage.
Lemma 2.

a)

b)

All sets sele ted at line (300 ) in stage k have size at least 2k and all sets produ ed
in line (600 b) have size at least 2k+1 .
The number of stages is at most log n.

Proof :

a) We use indu tion on k. The laim is learly true for k = 0. If Vi is hosen in


stage k > 0 and ombined with Vj then Vi and Vj are reated in stage k 1 and
hen e have size at least 2k ea h, by indu tion hypothesis. Thus jVi [ Vj j  2k+1.
b) The algorithm terminates when a set of size n is produ ed in line (6). Hen e
the maximal stage number k must satisfy 2k+1  n.
Lemma 2 has an important onsequen e. Call a point v a tive during an iteration
of loop (2) to (8) if v belongs to omponent Vi sele ted in line (300 ). Then any node
v an be a tive at most on e in a stage and hen e an be a tive at most log n times
by Lemma 2b). In other words, any xed node v has to be onsidered at most log n
times in line (4).
We an use this fa t for deriving another O(e log n) algorithm as follows: In
line (4) we onsider all nodes v 2 Vi and determine the least ost edge (v; w) with
w 2= Vi . This an ertainly be done in time O(deg (P
v)). Sin e a node is a tive at
most log n times the total ost of this algorithm is O( v deg (v)log n) = O(e log n).
In order to obtain an O(e log log n) algorithm we need two additional on epts:
shrinking the graph and a spe ial purpose priority queue. Suppose that we exe ute
the algorithm above for log log n stages; this will take O(e log log n) time units and
build up omponents of at least 2log log n = log n verti es ea h; let U1 ; : : : ; Um , m 
n= log n, be the omponents after stage log log n. De ne network N 0 = (V 0 ; E 0 ; 0 )
as follows: V 0 = f1; : : : ; mg, E 0 = f(i; j ); 9v 2 Ui; w 2 Uj su h that (v; w) 2 E g
and 0 (i; j ) = minf (v; w); v 2 Ui; w 2 Uj g. N 0 an be onstru ted from N in time
O(e); f. Exer ise 2. We still have to ompute a minimum spanning tree of N 0 . For
every node v of N 0 we divide the edges in ident to v into deg (v)= log n groups of
log n edges ea h. We sort ea h group a ording to ost within O((log n) log log n)
time units per group. Thus the total prepro essing time is O(e log log n).
In line (4) we pro eed as follows. For every node v 2 Vi we inspe t every
group. For every group we inspe t the edges in order of in reasing ost and dis ard
edges whi h do not lead outside Vi. When this pro ess is nished we are left with
ddeg (v)= log ne edges leading from v to nodes outside Vi. We an ertainly nd the
one of minimal ost in time O(ddeg (v)= log ne). Thus the ost of nding minimum
ost edges going out of v is O(1 + deg (v)= log n + number of dis arded edges) per
Version: 19.10.99

Time: 11:01

{60{

4.9.1. Algorithms for Maximum Network Flow

61

stage. Sin e every edge is dis arded at most on e, sin e there are only log n stages
and sin e N 0 has only n= log n nodes the total ost is O(n + e). We have
Theorem 3. A minimum ost spanning tree of an undire ted network an be
omputed in time O(e log log n).
Proof :

By the dis ussion above.


We nally ome to an improvement for planar networks. In a planar graph we
always have e  3n 6, f. 4.10, Lemma 2. Suppose that we apply the shrinking
pro ess after every stage. Let Ni be the network after stage i. Then Ni is planar
and hen e ei  3ni 6 where ei (ni) is the number of edges (nodes) of network
Ni . Also stage i + 1 takes O(ei ) time units and Ni+1 an be onstru ted from Ni
in time O(ei ). Thus the total ost is
logX
n 1
i=0

O(ei ) = O

log n 1
X
i=0

ni

=O

log n 1
X
i=0

n=2

= O(n):

Theorem 4. Let N = (V; E; ) be a planar undire ted network. Then a minimum


ost spanning tree an be omputed in time O(n).

4.9. Maximum Network Flow and Appli ations


4.9.1. Algorithms for Maximum Network Flow

A dire ted network N = (V; E; ) onsists of a dire ted graph G = (V; E ) and a
apa ity fun tion : E ! R+ . Let s; t 2 V be two designated verti es, the sour e s
and the sink t. A fun tion f : E ! R is a legal (s; t)- ow fun tion (or legal ow
for short) if it satis es
a) the apa ity onstraints, i.e.,P0  f (e)  (e) P
for all e 2 E ;
b) the onservation laws, i.e., e2in(v) f (e) = e2out(v) f (e) for all nodes v 2
V fs; tg. Here in(v) resp. out(v) is the set of edges entering resp. leaving v.
If f : E ! R is a legal ow fun tion then
val (f ) =

e out(s)

f (e)

e in(s)

f (e)

is the ow value of f . The maximum network ow problem is to ompute a


legal ow fun tion with maximum ow value. In this se tion we will des ribe two algorithms for a hieving this goal. On the way we will derive a powerful ombinatorial
result: the max ow-min ut theorem.
Version: 19.10.99

Time: 11:01

{61{

62

Chapter 4. Algorithms on Graphs

10=1

10=0

1=1

10=0

10=1

2
Figure 19. Graph with apa ity/ ow
De nition: An (s; t)- ut is a partition S; T of V , i.e., V = S [ T , S \ T = ;, su h
that s 2 S , t 2 T . The apa ity of ut (S; T ) is given by
X
(S; T ) =
(e):
2 \(ST )

e E

The apa ity of a ut (S; T ) is thus the total apa ity of all edges going from S
to T . The easy dire tion of the min ut-max ow theorem is given by
Lemma 1. Let f be a legal ow and let (S; T ) be an (s; t)- ut. Then
val (f )  (S; T ):
Proof :

We have

val (f ) =

=
=

e out(s)

"

f (e)

v S e out(v )

2 \(ST )
 (S; T ):
e E

f (e)

e in(s)

f (e)

f (e)

e in(v )

f (e)

2 \(T S)

f (e)

e E

Here the se ond equality follows sin e the onservation law holds for all v 2 S fsg.
The third equality follows sin e every edge e = (u; v) 2 E \(S  S ) is ounted twi e,
positively sin e e 2 out(u) and negatively sin e e 2 in(v) for some u and v. Finally,
the inequality follows sin e f (e)  (e) for all e 2 E \ (S  T ) and f (e)  0 for all
e 2 E \ (T  S ).
Most algorithms for maximum network ow work iteratively and are based on the
on ept of an augmenting path, i.e., they start with any legal initial ow, say the
ow fun tion whi h is zero everywhere, and then use augmenting paths to in rease
the ow. In the example of Figure 19 we use the edge label a=b to denote apa ity a
and ow b.
Version: 19.10.99

Time: 11:01

{62{

4.9.1. Algorithms for Maximum Network Flow

63

There are three augmenting paths in this example: s; 1; t with bottlene k


value 9; s; 2; t with bottlene k value 9; s; 2; 1; t with bottlene k value 1. Paths
s; 1; t and s; 2; t an be used to in rease the ow value by 9 in an obvious way.
The use of path s; 2; 1; t is more subtle. We might send one additional unit from
s to 2. This relieves us from the obligation to push one unit from 1 to 2 and we
an therefore send this unit dire tly from 1 to t. Augmentation along path s; 2; 1; t
hanges the ow as shown in Figure 20.
s

10=1

10=1

1=0

10=1

10=1

2
Figure 20. After augmentation by 1 along path s; 2; 1; t
All shortest ( = minimum ardinality) augmenting paths are aptured in the
with respe t to the legal ow fun tion f whi h is de ned as
follows. Let
E1 = f(v; w); (v; w) 2 E and f (e) < (e)g
and let
E2 = f(w; v); (v; w) 2 E and f (e) > 0g;
i.e., edges in E1 an be used to push ow forward and the edges in E2 an be used to
push ow ba kward. If e = (v; w) 2 E then we use e1 to denote edge (v; w) 2 E1 (if+
it is there) and e2 to denote edge (w; v) 2 E2 (if it is there). Also  : E1 [ E2 ! R
is given by
(e1 ) = (e) f (e) for e1 2 E1
and
(e2 ) = f (e) for e2 2 E2 :
Note that E1 [ E2 is a multiset be ause if e = (v; w) 2 E and e0 = (w; v) 2 E then
e1 ; e2 ; e01 ; e02 2 E1 [ E2 is possible. For the example of Figure 19 we obtain the graph
of Figure 21. Edges in E1 are drawn solid and edges in E2 are drawn dashed.
layered network LN

9
10

1
1

10
9

1
2
Figure 21. E1 ; E2 and  for original graph
Version: 19.10.99

Time: 11:01

{63{

64

Chapter 4. Algorithms on Graphs

Next, let V0 = fsg and


Vi+1 = fw 2 V

(V0 [    [ Vi ); 9v 2 Vi : (v; w) 2 E1 [ E2g

for i  0, and let V = Si0 Vi. Then LN = (V ; (E1 [ E2 ) \ Si0(Vi  Vi+1 ); )


is the layered network with respe t to ow fun tion f . In our example we obtain
Figure 22.

9
10

10
9

v0

v1
Figure 22. LN

v2

for original graph

Any path from s to t in the layered network is an augmenting path and an be


used to in rease the ow. More generally, we have
Lemma 2. Let f be a legal (s; t)- ow in network N and let LN
the layered network with respe t to f .

= (V ; E; ) be

a) f is a maximum ow i t 2= V .
b) Let f be a legal (s; t)- ow in LN . Then f 0 : E ! R with
f 0(e) = f (e) + f(e1 ) f(e2 )

is a legal ow in N with ow value val (f ) + val (f). Here f(ei ) is de ned to be


zero if ei 2= E .
Proof :

b) We have to show that f1 satis es the apa ity onstraints and the onservation law. Let e 2 E be arbitrary. Then
0  f (e) f(e2 )
 f 0(e)
 f (e) + f(e1 )
 (e)
Version: 19.10.99

(sin e f (e) = (e2 )  f(e2 ))


(sin e f(e1 )  0)
(sin e f(e2 )  0)
(sin e f(e1 )  (e1 ) = (e) f (e)),

Time: 11:01

{64{

4.9.1. Algorithms for Maximum Network Flow

65

i.e., f 0 satis es the apa ity onstraints. Next, let v 2 V fs; tg be arbitrary. Then
X

e out(v )

e out(v )

2
4

f 0(e)
f (e)

e in(v )

e in(v )

e in(v )

f(e1 ) +

f 0 (e)

f (e) + 4

e out(v )

e out(v )

f(e1 ) +

e in(v )

f(e2 )5

f(e2 )5

=0+0
sin e f and f satisfy the onservation laws. Note that e2 2 E2 emanates from
node v if e 2 in(v) and that e2 ends in node v if e 2 out(v). Finally, the ow value
of f1 is learly val (f ) + val (f).
a) \)": If t 2 V then there is a path from s to t in the layered network. Let p be
any su h path and let  > 0 be the minimal apa ity of any edge of p. Then there
is learly a ow of value  in LN , namely f(e) =  for all edges e of p and f(e) = 0
otherwise. Hen e f is not maximum by part b).
\(": Let S = V and let T = V S . Then s 2 S and t 2 T , i.e., (S; T ) is an
(s; t)- ut. Furthermore, (E1 [ E2 ) \ (S  T ) = ; sin e no node of T is added to the
layered network. Thus f (e) = (e) for e 2 S  T and f (e) = 0 for e 2 T  S . We
on lude that the inequality in the proof of Lemma 1 turns into an equality and
hen e val (f ) = (S; T ). Sin e val (g)  (S; T ) for any legal ow g we infer that f
is a ow with maximum ow value.
It seems that we have not got very far. In order to in rease the ow through
network N we have to nd a (large) legal ow through layered network LN . Fortunately, an approximation to the maximum ow in LN is good enough. More
pre isely, it su es to ompute a blo king ow in LN .
De nition:
A legal
ow
f in layered network LN is blo king if for every path
ek
e1
e2
e3
s = v0 ! v1 ! v2 ! : : : ! vk = t from s to t at least one of the edges is saturated,
i.e., f(ei ) = (ei ) for at least one i, 1  i  k.
Now we outline the basi maximum ow algorithm in Program 24.
Two questions arise: How an we nd a blo king ow in a layered network and
how many iterations are required? We turn to the se ond question rst.
De nition: Let f be a (non-maximum) legal ow in N and let LN be the layered
network for f . Then k, where t 2 Vk , is alled the depth of LN .
Version: 19.10.99

Time: 11:01

{65{

66

Chapter 4. Algorithms on Graphs

(1)
(2)
(3)
(4)
(5)
(6)
(7)

let f (e) 0 for all e 2 E ;


onstru t layered network LN = (V ; E; ) from f ;
while t 2 V
do nd a blo king ow f in LN ;
update f by f as des ribed in Lemma 2b);
onstru t layered network LN from f
od.
Program 24

Lemma 3. Let ki be the depth of the layered network used in the i-th iteration,
i = 1; 2; : : : . Then ki > ki 1 for i  2.
Proof :

Let LNi be the layered network used in the i-th iteration. In LNi there is
a path p of length ki from s to t.
ek i 1
ek
e1
e2
e3
s = v0 !
v1 !
v2 !
::: !
vki 1 !i vki = t
For 0  j  ki, let dj be the length ( = number of edges) of the shortest path from
s to vj in LNi 1 , i.e., vj belongs to the dj 's layer of LNi 1 . If vj is not a node of
LNi 1 then dj = 1.

Claim: For all i  2 holds:


a) If there is an edge from vj
b) If there is no edge from vj
) ki 1 < ki .

1
1

to vj in LNi
to vj in LNi

Proof :

1
1

then dj
then dj

= dj 1 + 1.
 dj 1 .

a) Obvious sin e network LNi 1 is layered, i.e., if vj 1 belongs to layer


and there is an edge from vj 1 to vj in LNi 1 then vj belongs to layer dj 1 +1.
b) Let us assume for the sake of ontradi tion that dj  dj 1 + 1. Let fi 1 resp.
fi be the ow in network N whi h gives rise to the onstru tion of layered network
LNi 1 resp. LNi . Then fi 1(vj 1 ; vj ) = (vj 1 ; vj ) > fi(vj 1 ; vj ) if (vj 1 ; vj ) 2 E
or fi 1(vj ; vj 1 ) = 0 < fi(vj ; vj 1 ) if (vj ; vj 1 ) 2 E be ause (vj 1 ; vj ) 2= E i 1
and (vj 1 ; vj ) 2 E i. In either ase we on lude that (vj ; vj 1 ) 2 E i 1 and hen e
dj 1 = dj + 1. Thus dj = dj 1 1  dj 1 , ontradi tion.
) Sin e v0 = s and hen e d0 = 0 we on lude from parts a) and b) that dj  j .
Also, dj = j for all j  ki is only possible if edge ej from vj 1 to vj is present in
LNi 1 for all j  1. Thus dj = j for all j  ki implies that there is some path
p from s to t whi h exists in LNi 1 and LNi . This ontradi ts the fa t that fi
is obtained from fi 1 by \adding" a blo king ow with respe t to layered network
LNi 1 . We on lude that dj < j for some j  ki and hen e dki < ki by parts a)
and b). We an now omplete the proof of the laim and the lemma by observing
that ki 1 = dki by de nition.
dj

Version: 19.10.99

Time: 11:01

{66{

4.9.1. Algorithms for Maximum Network Flow

67

Corollary 1. The number of iterations is at most n.


Proof : Let ki be the depth of the layered network used in the i-th iteration, i  1.
Then k1  1 sin e s 6= t, ki 1 < ki by Lemma 3 and ki  n for all i. Hen e the
number of iterations is at most n.
Better bounds on the number of iterations an be derived for restri ted networks.
In parti ular, we will derive onsiderably smaller bounds for (0,1)-networks in Se tion 4.9.2. We will next des ribe two algorithms for onstru ting blo king ows in
layered networks, rst an O(n2) algorithm and then an O(eS (log n)2) algorithm.
Let
LN = (V; E; ) be a layered network, i.e., V = 0ik Vi for some k,
S
E  0i<k (Vi  Vi+1 ), V0 = fsg and : E ! R+ . We may assume w.l.o.g., i.e.,
the ondition an be established in linear time by simple graph exploration, that
every node v 2 V is rea hable from s and that t an be rea hed from all nodes. In
parti ular, Vk = ftg in this ase. The O(n2 ) algorithm is based on the on ept of
the potential of a node. Let f be a legal ow and let v 2 V . The potential of
node v with respe t to ow f is given by
"

P O(v) = min

e out(v )

(e) f (e);

e in(v )

(e) f (e) ;

i.e., the potential of node v is the maximum possible in rease in ow through node v.
Also
P O = minfP O(v); v 2 V g
is the minimal potential of any node in V . It is now quite simple to in rease the
ow by P O. Let v be any node with P O(v) = P O . Starting at node v we forward
P O additional units from node v through higher layers to node t and we su k P O
additional units ow into node v through lower layers. Forwarding the ow is done
as follows. We pro eed layer by layer, starting at the layer ontaining v. When
we onsider layer Vl we have determined a subset Sl P
 Vl of nodes whi h holds an


additional amount of P O units of ow, i.e., P O = x2Sl S (x) where S (x) is the
ex ess of ow available in node x 2 Sl. We onsider the nodes in Sl in turn and
push their ex ess of ow into the next layer. Sin e P O  P O(w) for all w no node
an re eive more ow than it an handle. We ontinue in this way until we have
pushed the additional ow all the way to t. Similarly, we work our way ba k from
node v towards sour e s and su k P O additional units of ow into the network.
In this way we in rease the ow by P O units. After having done so, we simplify
the network by deleting saturated edges and useless nodes, i.e., nodes whi h are not
onne ted to either s or t, and edges in ident to useless nodes. Note that at least
node v will be deleted from the network. (Remark: It would simplify the algorithm
if we forwarded additional ow starting at s. Corre tness would not be impeded,
however e ien y might su er.) If the network is not empty after the simpli ation
we repeat the pro ess. Sin e the simpli ation deletes at least one node from the
network the number of iterations is learly O(n).
Version: 19.10.99

Time: 11:01

{67{

68

Chapter 4. Algorithms on Graphs

We will next des ribe the algorithm in more detail. We assume that for every
node v 2 V the set of ingoing and the set of outgoing edges are ordered in some way.
Also set Sl  Vl is realized as a bit ve tor and as a linear list. In this way we an
test v 2 Sl , add an element to Sl and delete some element from Sl in time O(1). In
addition, we store for every node x 2 X the ex ess (de it) of ow available at node
x in S [x. The pro edure forward of Program 25 is fundamental to the algorithm; it
forwards the ow from node x into the next layer. There is a symmetri pro edure
su k whi h su ks the ow into node x from the previous layer.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)

pro edure forward (x; S; h);


o x is a node in layer Vh and there are S units of additional ow
available in x. These S units are pushed into nodes in layer Vh+1 o
while S > 0
do let e = (x; y) be the rst edge out of x;
delta min(S; (e) f (e));
in rease ow along e by delta , add y to Sh+1 (if it is not already there),
in rease S [y by delta , and de rease (e) by delta ;
S S delta ;
if (e) = 0 then delete e from the graph fi
od;
remove x from Sh and set S [x to zero;
if (out(x) = ; and x 6= t) or (in(x) = ; and x 6= s)
then add x to set del fi
end.
Program 25

In set del we olle t all nodes whi h have to be deleted from the network
be ause either they annot be rea hed from s or t annot be rea hed from them.
The running time of a all of forward is O(1+# edges deleted in line (7)), be ause at
ea h exe ution of the loop body (ex ept maybe the last) an edge is deleted and sin e
the ost outside the loop is learly O(1). The omplete algorithm for omputing a
blo king ow is given by Program 26.
Pro edure simplify (del ) removes all nodes (and edges in ident to them) in
del from the network. Also if some other node z loses its last ingoing (outgoing)
edge during this pro ess then z is also deleted. It is easy to see that simplify an be
implemented to run in time proportional to the number of nodes and edges removed
from the graph; an algorithm similar to Program 26 used for topologi al sorting will
do. The details are left to the reader (Exer ise 27).
Theorem 1. Let LN be a layered network. Then a blo king ow an be omputed
in time O(n2 ).
Proof :

Corre tness of Program 26 follows from the fa t that nodes (edges) are
removed only if all paths from s to t through that node (edge) are blo ked.

Version: 19.10.99

Time: 11:01

{68{

4.9.1. Algorithms for Maximum Network Flow

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)

69

all x 2 V do S [x 0 od;
all l, 0  l  k, do Sl ; od;
del ;;
while LN is not empty
do ompute P O[v for all v 2 V , let P O = minfP O[v; v 2 V g and
let v 2 Vl be su h that P O = P O[v;
S [v P O ; Sl fvg;
for h from l to k 1
do for all x 2 Sh do forward (x; S [x; h) od od;
S [v P O ; Sl fvg;
for h from l step 1 to 1
do for all x 2 Sh do su k (x; S [x; h) od od;
simplify (del )
od.
for
for

Program 26

The ost of lines (1) to (3) is learly O(n). Also, loop (4) to (13) is exe uted
O(n) times sin e at least one node, namely v, is removed from the graph in line (12).

The ost of an exe ution of the loop body outside the alls of forward , su k and
is learly O(n) and hen e O(n2) if summed over all O(n) iterations. Sin e
the ost of a all of forward (su k ) is O(1 + # deleted edges), sin e forward (su k )
is alled at most on e for ea h node during an exe ution of the loop body and sin e
every edge is deleted at most on e the total ost of all alls of forward (su k ) is
O(n2 + e) = O(n2 ). Finally, the total ost of all alls of simplify is O(e).
simplify

The algorithm an be made to run faster in (0,1)-networks, i.e., networks where

(e) = 1 for all e 2 E . Exer ise 28 des ribes an implementation with running time
O(e). We will later des ribe a simpler O(e) algorithm for omputing blo king ows

in (0,1)-networks.

Theorem 2. Let N = (V; E; ), s; t 2 V , be a network. Then a maximum ow


from s to t an be omputed in time O(n3 ).
Proof :

A maximum ow an be omputed by O(n) appli ations of the blo king


ow algorithm to layered networks. The onstru tion of the layered network and
the omputation of a blo king ow takes time O(n2 ). The time bound follows.
It is now easy to derive the min- ut max- ow theorem.
= (V; E; ), s; t 2 V , be a network. Let fmax be the maximum
ow value of any legal (s; t)- ow fun tion and let min be the minimal apa ity of
all (s; t)- uts. Then
fmax = min :

Theorem 3. Let N

Version: 19.10.99

Time: 11:01

{69{

70

Chapter 4. Algorithms on Graphs

Proof : Note rst that min exists be ause there is only a nite number of (s; t)- uts.
Also, fmax exists be ause we have an O(n3) algorithm for omputing a maximum
ow from s to t. fmax = min remains to be shown. We have fmax  min by
Lemma 1. Finally, let f be a ow fun tion with val (f ) = fmax . If we onstru t the
layered network with respe t to f then t is not added to the network. The proof of
Lemma 2a) shows how to onstru t an (s; t)- ut (S; T ) su h that fmax = val (f ) =
(S; T ). Sin e (S; T )  min , this proves fmax  min .
Our se ond algorithm for omputing blo king ows is based on dfs and is parti ularly well suited for sparse networks, i.e., e  n2. The basi idea is quite simple.
Starting at s we onstru t a path by always taking the rst edge out of every node
until we either rea h t or rea h a dead-end v, i.e., a node v with out (v) = ; and
v 6= t. In the se ond ase we ba k up one node, delete all edges leading into v from
the graph and ontinue. In the rst ase we ompute the bottlene k apa ity  of
the path, i.e., the minimal apa ity of any edge on the path, in rease the ow along
the path by , de rease the apa ities by , and delete all saturated edges from the
graph. Having done so, we onstru t the next path starting at node s.

Theorem 4. The algorithm above onstru ts a blo king ow in a layered network


in time O(e  n). In a (0,1)-network it runs in time O(e).
Proof :

Corre tness is obvious. The bound on the running time is derived as follows.
As before k denotes the depth of the layered network. Observe rst, that a path
from s to t is onstru ted in time O(k +# of edges found to be ending in dead-ends)
and that at least one edge on the path is saturated by in reasing the ow. Hen e
at most O(e) paths are onstru ted for a total ost of O(k  e + e) = O(e  n).
One additional observation is needed for (0,1)-networks. In (0,1)-networks all
edges on the onstru ted path are saturated and hen e the ost of onstru ting a
path from s to t is proportional to the number of deleted edges. The laimed time
bound follows.
We will next des ribe an improved implementation of the algorithm above whi h
redu es the time bound to O(e  (log n)2 ). In the algorithm above, whenever we
su eed in onstru ting a path from s to t we saturate edges and then forget about
the onstru ted path. A more e onomi al way to pro eed is to keep the remnants
of the path as path fragments (PF's). In the example of Figure 23 we split the path
into three PF's p, pf1 and pf2 .
pf 0

pf

p
Figure 23.
Version: 19.10.99

Time: 11:01

Splitting a path into three path fragments


{70{

4.9.1. Algorithms for Maximum Network Flow

71

We will always use p to denote the path fragment starting in s. We will


maintain the invariant that at most one PF goes through every node v, i.e., that
there is at most one path fragment pf su h that v is a node of pf but not the last
node of pf. In other words the PF's form a forest with edges dire ted towards the
roots. We an now start to onstru t a new path from s to t starting at the last
vertex last (p) of PF p.
There are four ways of hanging path p. If there is a path fragment whi h goes
through last (p), say pf, then we split pf at last (p) and on atenate one of the parts
to p. If last (p) is the rst vertex of pf then the splitting is trivial. See Figure 24.
pf
p

Figure 24.

The rst two ways of hanging path p

If there is an unblo ked edge out of last (p) then we add this edge to p. If last (p)
is a dead-end, i.e., there is neither a PF going through last (p) nor an unblo ked
edge leaving last (p), then we shrink p by deleting its last edge. Finally, if last (p) = t
then we saturate some of p's edges and split p into path fragments. The details are
as des ribed in Program 27.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)

p path onsisting of s only;


while s is not a dead-end
do extend p by adding an unblo ked edge out of last (p);
while a PF pf goes through last (p)
do split pf at last (p) into pf 0 and pf 00 ;
o pf 0 ends in last (p), pf 00 starts in last (p) o
ompute the apa ities of pf 0 and pf 00;
on atenate pf 00 to the end of p and update p's apa ity
od;
if last (p) = t
then in rease the ow along p by the apa ity of p, split p into PF's

by deleting all saturated edges, ompute the apa ities


of the fragments and let p be the fragment starting in s

od.

fi;
while last (p) is a dead-end and s 6= last (p)
do delete the last edge from p and update p's apa ity
od

Version: 19.10.99

Program 27
Time: 11:01

{71{

72

Chapter 4. Algorithms on Graphs

There are two points whi h we have to deal with now. How do we maintain
the invariant and an we always exe ute line (3)?
Lemma 4. The following holds at all times during the exe ution of Program 27:
a) For every node v there is at most one path fragment going through v.
b) Whenever line (3) has to be exe uted there is an unblo ked edge out of last (p)
and no PF goes through last (p).
Proof :

(By indu tion on the number of steps exe uted.) Claims a) and b) are
ertainly true prior to the rst exe ution of the loop body. Suppose now that a)
and b) hold prior to exe ution of line (3). We will show that a) and b) hold at
the end of the loop body. Sin e b) holds line (3) an be exe uted and exe ution of
line (3) does not impede the truth of part a). Nor does the exe ution of lines (4)
to (7). Before exe uting line (8) we know that no PF goes through last (p). We
laim that this is also true after exe uting lines (8) to (10). The laim is obvious
if last (p) 6= t. If last (p) = t then we reset p to an initial segment p0 of p. Sin e
the edge on p whi h emanates from last (p0 ) is saturated in (9) and sin e a) holds
we on lude that no PF goes through last (p0 ). Thus a) holds prior to exe ution
of line (11) and no PF goes through last (p) at this point. Exe ution of lines (11)
to (13) ertainly does not a e t a). Also these lines ensure that no PF goes through
last (p) (this fa t is an invariant of line (12) be ause of a)) and that either last (p) = s
or that there is an unblo ked edge out of last (p). Thus b) and a) hold prior to the
next exe ution of line (3) be ause of the test in line (2).

Lemma 4 implies the orre tness of Program 27. Let us turn to e ien y next. We
need to dis uss two points: how to represent path fragments so that the various
operations on them an be done fast and how to derive bounds on the number of
exe utions of the various statements.
Path fragments are stored as balan ed trees. More pre isely, we store the edges
of a PF in the leaves of a (2,4)-tree in the natural order: Then every vertex z of
the tree represents a path pf(z) in the network, namely the path omprised of the
edges stored in the subtree rooted at z. We store two informations about path
pf(z ) in vertex z : f (z ) ( ow) and (z ) ( apa ity). The ow eld f (z ) indi ates
that f (z) units of ow have been pushed through pf(z) without distributing these
units overPthe subpaths. Thus, if e is an edge of the network, the ow through e is
given by f (z) where the summation is over all verti es z on the path from the
leaf representing edge e (note that this leaf is uniquely de ned by Lemma 4a)) to
the root of the tree representing the path fragment ontaining e. Field (z) is the
minimal residual apa ity ( = apa ity ow) of any edge in pf(z) ignoring the
ow asso iated with proper an estors of z. Thus
(z ) =
Version: 19.10.99

Time: 11:01

min (e)

e pf (z )

{72{

y ver (e;z )

f (y )

4.9.1. Algorithms for Maximum Network Flow

73

where ver(e; z) is the set of verti es of the tree path from the leaf representing e
to z (the leaf representing e and the vertex z are in luded). In parti ular, if z is
the root of a tree then (z) is the residual apa ity of pf(z), i.e., (z) and no more
additional units of ow an be pushed through pf(z).
Lemma 5.

a)

b)

If pf1 and pf2 are path fragments with last (pf1 ) = rst (pf2 ) then pf1 and pf2
an be on atenated in time O(log n).
Let pf be a PF represented as a balan ed tree and let v be a node of pf. Then
pf an be split at v in time O(log n). Also, if the residual apa ity of pf is zero
then a saturated edge of pf an be lo ated in time O(log n).

Proof :

For both parts we need to push ow information into trees. If z is a vertex


with sons zi, i = 1; 2; : : :, then
(f (zi ); (zi )) (f (zi ) + f (z); (zi ) f (z));
(f (z); (z)) (0; (z))
is a onsistent hange of the information elds asso iated with verti es z; z1 ; z2 ; : : : .
Also, it pushes ow from vertex z into the subpaths represented by verti es zi ,
i = 1; 2; : : : .
a) Let Ti of height hi be the tree representing pfi , i = 1; 2. Assume w.l.o.g. that
h1  h2 . Then we on atenate T1 and T2 by rst pushing the ow down the left
spine of T2 for h2 h1 + 1 levels and then on atenating T1 and T2 as des ribed in
Se tion III.5. Note that the ow and apa ity eld of the verti es a e ted by the
operation are easily omputed. More pre isely, the ow eld is set to zero and the
apa ity eld is set to the minimum residual apa ity of the sons. This shows that
T1 and T2 an be on atenated in time O(jh2 h1 j + 1).
b) Let T represent path fragment pf and let v be a node in pf. Note rst, that v
orresponds to a \gap" between two leaves of T in a natural way. Let e be the edge
(leaf) following v in pf. We prepare the splitting by tra ing the tree path from e to
the root of T and then push the ow down this path. This hanges the ow eld
of all verti es on the tree path to zero and therefore they an be safely removed.
Splitting is ompleted by a sequen e of on atenations as in ordinary (2,4)-trees.
Finally, we show how to nd a saturated edge if the residual apa ity of pf is
zero. Push the ow from the root z of T into its sons. Then 0 = (z) = min( (zi ))
where zi ranges over the sons of z. Therefore one of the sons has a zero apa ity
eld. If we ontinue in this way we nd a saturated edge in time O(log n).
Lemma 6. A single exe ution of lines (3), (5), (6) and (12) takes time O(log n).
A single exe ution of line (9) takes time O(d log n) where d is the number of edges
deleted in line (9).
Version: 19.10.99

Time: 11:01

{73{

74

Chapter 4. Algorithms on Graphs

Proof : Follows immediately from Lemma 5. Note that line (3) an be visualized
as onstru ting a path fragment onsisting of a single edge and on atenating it
with p.

Now we are (almost) able to determine the running time of the improved algorithm.
Note rst that the total time spent outside lines (4) to (7) is O(e log n) be ause the
number of exe utions of the loop body is at most e, be ause the total number of
edges deleted in lines (9) and (12) is at most e, and be ause the ost of handling
an edge is O(log n) by Lemma 6. It remains to bound the osts arising in lines (4)
to (7), i.e., we need to bound the number of exe utions of lines (5) and (6). Call
this number K . We show K = O(e log n) in a two step pro ess. As a rst step
we rephrase the problem of bounding K as a game problem (whi h bears great
resemblan e to the Union-Find Problem studied in Se tion III.8.3) and as a se ond
step we derive a bound on the number of moves in the game. The argument will
be similar to the one used in Se tion III.8.3.
For step one we on eptually assign non-negative integers to path fragments
as follows. To path fragment p (starting at s) no number is ever assigned. When
p is split in line (9) into path fragments p, pf1 , pf2 , : : : , pfk (in this order from s
to t) then we assign integer L + i to pfi, 1  i  k, where L is the largest integer
given to a PF prior to that point. Also if we split PF pf into pf 0 and pf 00 and
on atenate pf 00 to p, then pf 0 inherits the number of pf provided that pf 0 is nontrivial. We use num (pf) to denote the number assigned to PF pf. We learly have
1  num (pf)  e for all path fragments pf sin e new numbers are assigned only in
line (9) and assigning a new number orresponds to deleting an edge. We need one
more property of path fragment numbers. If pf1 and pf2 are PF's then pf1 points
to pf2 if pf2 goes through last (pf1 ). We have
Lemma 7. At all times during the exe ution holds:

a)
b)

If pf is a PF then pf points to at most one other PF.


If pf1 points to pf2 and pf2 6= p then num (pf1 ) < num (pf2 ).

Proof : a) Follows dire tly from Lemma 4a) sin e there is at most one path fragment
going through last (pf) by that lemma.
b) (By indu tion on exe ution time.) New path fragments are reated in lines (5)
and (9). Line (9) ensures that b) holds true sin e \large" numbers are assigned to
the newly reated path fragments and sin e the newly reated path fragments do
not point to any other path fragment by part a). Line (5) keeps b) true sin e pf 0
inherits num (pf ), sin e pf 0 is a subpath of pf, and sin e pf 0 points to p after its
reation and therefore to no other path fragment by part a).

We an now view our algorithm as manipulating a set S  f(x; y); 1  x < y  eg


of pairs, namely S = f(num (p); num (q)); p; q are PF's and p points to qg. Set S is
manipulated in stages, where a stage orresponds to a single exe ution of the body
(3) to (14) of the main loop. Thus the number of stages is at most e. In a stage we
Version: 19.10.99

Time: 11:01

{74{

4.9.1. Algorithms for Maximum Network Flow

75

remove a number of pairs in lines (4) to (7), say (x1 ; y1 ), (x2 ; y2 ), : : : , (xk ; yk ) for
k  0. These pairs form a hain, i.e., y1 = x2 ; y2 = x3 ; : : : ; yk 1 = xk , be ause if
yi = num (pfi ); x1 = num (pf0 ) then pfi must point to pfi+1 , 0  i < k, and p must
point to pf0 prior to line (4). See Figure 25. Thus K  D + e where D is the number
of pairs removed in lines (4) to (7). In a stage we add some pairs to S in line (9).
If (x; y) = (num (pf1 ); num (pf2)) is a pair added in line (9) then y = num (pf2 ) is a
\new" number. In parti ular, if pair (x; y0 ) was deleted at some previous stage and
pair (u; v) was deleted at the same stage then y > v.
pfk



pf1

p
Figure 25.

pf0

A hain of path fragments

Readers familiar with the Union-Find Problem, Se tion III.8.3, should see a
similarity at this point. Consider the Union-Find data stru ture with path ompression but without the weighted union rule. If one numbers nodes as they are
reated then upward links orrespond to pairs (x; y) with x < y. Also path ompression removes a hain of pairs and adds some new pairs with a \large" se ond
omponent.
Theorem 5. Let N and M be integers. Consider a pro ess operating on
the set S  f(x; y); 1  x < y  M g in N stages. Initially, S is a set
of pairs satisfying (x; y) 2 S; (x; y0 ) 2 S ) y = y0 . In ea h stage a hain
(x1 ; x2 ); (x2 ; x3 ); : : : ; (xk 1; xk ) of pairs is removed from S and some set of pairs
(x; y) is added to S . Let the added pairs (x; y) satisfy

(1) If (x; y) is added to S then no (x; y0 ) is urrently in S and (x; y) never belonged

(2)

to S previously.
If (x; y0 ) for some y0 was deleted at some previous (in luding the present) stage
and pair (u; v) was deleted at the same stage then y  v.

Under these assumptions at most (N + M )dlog M e pairs are removed from S .


Proof :

The proof is based on the following idea. If we delete a large hain


(x1 ; x2 ); : : : ; (xk 1; xk ) at some stage then all pairs (xi ; y) added later on must
satisfy y > xk . Therefore all these edges must have a large \rea h" y xi. But no
pair an have a \rea h" ex eeding M and hen e this annot happen very often. The
Version: 19.10.99

Time: 11:01

{75{

76

Chapter 4. Algorithms on Graphs

details are as follows. Let F be the set of pairs deleted. We divide F into lasses
a ording to the rea h of the edges in F , namely
Mi = f(x; y) 2 F ; 2i  y x < 2i+1 g for 0  i  dlog M e 1:
Furthermore, let
Li = f(x; y) 2 Mi ; no (u; v) 2 Mi with v > y is removed
from S at the same stage as (x; y)g:
Note that the de nitions make sense sin e no pair an be added twi e to S by
property (1).
Claim 1. jLi j  N .
Proof : Obvious, sin e the edges removed at a stage form a hain and sin e there
are only N stages.
Claim 2. For all x and i there is at most one y su h that (x; y) 2 Mi Li .
Proof : Assume (x; y1 ); (x; y2 ) 2 Mi Li where y1 < y2 . When (x; y1 ) is removed
from S a pair (u; v) 2 Mi with v > y1 is also removed from S at the same stage,
sin e (x; y1 ) 2= Li . Sin e the set of pairs removed at a stage form a hain we also
have y1  u. Next observe that y2  v by property (2). Thus
y2 x  v x  v u + y1 x  2i + 2i = 2i+1 ;
sin e (x; y1 ); (u; v) 2 Mi and hen e v u  2i and y1 x  2i. We on lude that
(x; y2 ) 2= Mi , ontradi tion.
The proof is now easily ompleted. Claims 1 and 2 yield
jF j =

dlogX
Me
i=0

jMi Lij + jLij  (M + N )dlog M e;

sin e jMi Lij  M by Claim 2 and jLij  N by Claim 1.


Theorem 6.
a) Let LN be a layered network with n nodes and e edges.
Then a blo king ow an be omputed in time O(e  (log n)2 ).
b) Let N = (V; E; ), s; t 2 V , be a network with n nodes and e edges.
Then a maximum (s; t)-legal ow an be omputed in time O(e  n  (log n)2 ).
Proof :

a) Theorem 5 (M = N = e) implies that the number of exe utions of lines (5)


and (6) is O(e log n). Thus the total running time is O(e (log n)2 ) by Lemma 6
and by the dis ussion following it.
b) Follows from part a) and Corollary 1.

Version: 19.10.99

Time: 11:01

{76{

4.9.2. (0,1)-Networks, Bipartite Mat hing and Graph Conne tivity

77

Theorem 5 an be used to show an O((n + m)  log(n + m)) bound on the ost of


n unions and m nds when path ompression is used but the weighted union rule

is not used ( f. Chapter III).


Theorem 6 an be slightly improved. Sleator/Tarjan ( f. Sleator (80)) have
shown that a lever use of dynami weighted trees ( f. III.6) instead of balan ed
trees redu es the ost of blo king ow omputations to O(e log n) and hen e the ost
of the maximum ow problem to O(e  n log n). Finally, the algorithm above an be
used to ompute the maximum ow in an (s; t)-planar network in time O(n log n);
see Exer ise 29 for a detailed dis ussion. The main additional insight required is
that a single blo king ow omputation su es to ompute a maximum ow.
4.9.2. (0,1)-Networks, Bipartite Mat hing and Graph Conne tivity

In this se tion we will spe ialize the network ow algorithms to (0,1)-networks, or


more generally bounded networks, and then apply it to ompute maximum mat hings in bipartite graphs and to ompute the vertex onne tivity of graphs.
De nition: Let d 2 N. A network N = (V; E; ) is d-bounded if (e) 2
f1; 2; : : : ; dg for all e 2 E . A 1-bounded network is ommonly alled (0,1)network.
If we apply any of our maximum ow algorithms to d-bounded networks then all
intermediate ows f are integral, i.e., f (e) 2 N0 for all e 2 E . In parti ular, the
maximum ow is integral. We already observed that a blo king ow in a (0,1)network an be omputed in linear time. More generally, we have
Lemma 8. Let N be a d-bounded network. Then a blo king ow an be omputed
in time O(d  e).
Proof :

Use the proof of Theorem 4 and observe that an edge an be used at most

d times in any path from s to t.

From Lemma 8 we on lude that maximum ows in d-bounded networks an be


omputed in time O(d  e  n). A tually, a mu h better time bound holds for small d
and an be shown by a more areful analysis of our network ow algorithms. More
pre isely, we show improved bounds on the number of phases exe uted by the
algorithm.
Let N = (V; E; ) be a network. Let s; t 2 V and let f be a legal ow. Let E1 =
f(v; w); f (v; w) < (v; w)g and let E2 = f(w; v); f (v; w) > 0g; f. the de nition
of layered network. Let AN = (V; E1 [_ E2 ; ) where (v; w) = (v; w) f (v; w) for
(v; w) 2 E1 and (w; v) = f (v; w) for (w; v) 2 E2 and E1 [_ E2 is the disjoint union of
E1 and E2 . Then onstru ting the layered network with respe t to N and f is the
same as onstru ting the layered network with respe t to AN and the ow fun tion
whi h is zero everywhere.
Version: 19.10.99

Time: 11:01

{77{

78

Chapter 4. Algorithms on Graphs

Lemma 9. Let N be a network and let fmax be the value of a maximum (s; t)- ow.
Let f be any legal (s; t)- ow, let AN be de ned as above and let fmax be the value
of a maximum (s; t)- ow in AN . Then
fmax

= fmax + val (f ):

Proof :

Let (S; V S ) be any (s; t)- ut. We use (S; V S ) and (S; V S ) to
denote the apa ity of ut (S; V S ) with respe t to N and AN , respe tively. We
have
X
(S; V S ) =
(v; w)
2

v S;w V

[( (v; w) f (v; w)) + f (w; v)

2 2 S
= (S; V S )
v S;w V

2
val (f ):

v V;w V

(f (v; w) f (w; v))


S

= (S; V S )
Therefrom we on lude that min = min val (f ) where min and min are the
minimum apa ities of any (s; t)- ut in N and AN , respe tively. An appli ation of
Theorem 3 (min ut = max ow) ompletes the proof.
Lemma 9 states that the augmenting network AN has the potential of in reasing the
ow to its maximum value. The layered network aptures all shortest (s; t)-paths
in AN .
Lemma 10. Let N be a d-bounded network. Then the number of phases is at
most 3  d1=3  n2=3 .
Proof :

Let fmax be the value of a maximum (s; t)- ow. If fmax < d1=3  n2=3 then
the laim holds true sin e every phase in reases the ow by at least one. So let us
assume fmax  d1=3  n2=3. Consider the phase, say the l-th phase, whi h in reases
the ow to at least fmax (d1=2 n)2=3 . Then there are at most d1=3 n2=3 phases after
phase l sin e every phase in reases the ow by at least one. We omplete the proof
by showing that l  2  d1=3  n2=3 , i.e., that ow value fmax d1=3  n2=3 is rea hed
in at most 2  d1=3  n2=3 phases. Sin e the depth of the layered network grows by at
least one in every phase (Lemma 3), it su es to show that kl , the depth of layered
network LN used in phase l, is at most 2dS1=3 n2=3 . Let LN = (V0 [V1 [  [Vk ; E; ),
where k = kl , V0 = fsg, t 2 Vk , and E  i(Vi  Vi+1) be the layered network used
in phase l. LN is onstru ted with respe t to ow f .
Let Wi = V0 [  [ Vi, 0  i < k. Then (Wi; V Wi) is an (s; t)- ut and hen e
(Wi ; V Wi)  fmax = fmax val (f )  d1=3  n2=3 by the proof of Lemma 9. Next
observe that all edges of AN whi h emanate in Wi and end in V Wi a tually start in
Version: 19.10.99

Time: 11:01

{78{

4.9.2. (0,1)-Networks, Bipartite Mat hing and Graph Conne tivity

79

Vi and end in Vi+1 by the de nition of layered network LN . Hen e (Wi ; V Wi ) 


2  d  jVij  jVi+1 j sin e there are at most 2  jVij  jVi+1 j edges from Vi to Vi+1.
(The 2 is due to the fa t that (v; w) 2 E1 and (v; w) 2 E2 is possible.) Thus
jVijjVi+1 j  (n=d)2=3 =2 and hen e jVij+jVi+1j  (n=d)1=3 for 0  i < k. Summing
this inequality for i, 0  i < k, we obtain
2  jV j  k  (n=d)1=3
or
k  2  d1=3  n2=3 :

Theorem 7. Let N be a d-bounded network. Then a maximum ow an be


omputed in time O(d4=3  n2=3  e).
Proof :

Obvious from Lemmas 8 and 10.


One restri ted form of (0,1)-networks is parti ularly important, namely simple (0,1)networks.
De nition: A network N = (V; E; ) is simple if indeg (v)  1 or outdeg (v)  1
for all v 2 V .
Theorem 8. Let N = (V; E; ) be a simple (0,1)-network. Then a maximum ow
an be omputed in time O(n1=2  e).
Proof : A phase of the network ow algorithm takes time O(e) by Theorem 4. It
therefore su es to show that the number of phases is O(n1=2 ). We use an argument
similar to Lemma 10.
Let fmax be the value of a maximum (s; t)- ow. If fmax < n1=2 then there
is nothing to show. If fmax  n1=2 then onsider the phase, say the l-th, whi h
in reases the ow to fmax n1=2 . Then there are at most n1=2 phases following
phase l. It remains to be shown that the layered network LN used in phase l has
depth at most n1=2 .
Let f be the legal (s; t)- ow obtained by our algorithm just prior to phase l
and let AN be the augmenting network with respe t to f . We laim that AN is
a simple network. This an be seen as follows. Let v 2 V be arbitrary. Assume
that indeg (v) = 1, the ase outdeg (v) = 1 is similar. If f (e) = 0 for in(v) = feg
and hen e f (e0) = 0 for all e0 2 out(v) then v ertainly has indegree one in AN . If
f (e) = 1 for in(v) = feg and hen e f (e0 ) = 1 for exa tly one e 2 out(v) then v has
also indegree at most one in AN . This follows from the fa t that the dire tion of e
and e0 is reversed for onstru ting the augmenting network. Thus AN is a simple
network.
Version: 19.10.99

Time: 11:01

{79{

80

Chapter 4. Algorithms on Graphs

By Lemma 9, AN permits an (s; t)- ow of fmax val (f )  n1=2. Consider a


maximum (s; t)- ow f in AN . We may assume that f is integral, i.e., f(e) 2 f0; 1g
for all edges of AN . Sin e AN is a simple network, f de nes val (f)  n1=2 paths
from s to t whi h have no ommon vertex other than s and t. Hen e any one of
these paths an have at most n1=2 intermediate nodes. This shows that the layered
network used in phase l has depth at most n1=2 .
We have thus shown that the number of phases is O(n1=2 ) and hen e the total
running time is O(n1=2  e).
We end this se tion with two appli ations of simple (0,1)-network ow: bipartite
mat hing and graph onne tivity.
Let G = (V; E ) be an undire ted graph. A mat hing M is a set of edges
M  E su h that no two edges e1 ; e2 2 M , e1 6= e2 , share an endpoint. A maximum
mat hing is a mat hing of maximum ardinality. An undire ted graph G = (V; E )
is bipartite if there is a partition V1 ; V2 of V su h that E  V1  V2 . In bipartite
graphs, the nodes of V1 (V2 ) are often alled girls (boys). Then (v; w) 2 E an
be interpreted as \girl v an go along with boy w". Mat hing in arbitrary graphs
allows for homosexuality.
Theorem 9. Let G = (V1 [ V2 ; E ), E  V1  V2 , be a bipartite graph. A maximum
mat hing an be omputed in time O(n1=2  e).
Proof : De ne a simple (0,1)-network N = (V1 [ V2 [fs; tg; E; ) as follows. Add two
nodes s and t, onne t s to all verti es in V1 , dire t all edges in E from V1 to V2 and
onne t all verti es in V2 to t. Also assign apa ity one to all edges. Then integervalued ows in N are in one-to-one orresponden e to mat hings in G. (Figure 26
shows a mat hing and the orresponding ow by wiggled edges.) By Theorem 8a)
maximum ow in N an be omputed in time O(n1=2  e).

Bipartite mat hing redu ed to a ow problem


We will next turn to vertex onne tivity of undire ted graphs. Let G = (V; E )
be an undire ted graph and let a; b 2 V be su h that (a; b) 2= E . Set S  V fa; bg
is an (a; b)-vertex separator if every path from a to b passes through a vertex
of S . In other words a and b belong to di erent onne ted omponents of G S .
The minimum ardinality of any (a; b)-vertex separator is denoted by N (a; b). If
(a; b) 2 E we set N (a; b) = +1.
Figure 26.

Version: 19.10.99

Time: 11:01

{80{

4.9.2. (0,1)-Networks, Bipartite Mat hing and Graph Conne tivity

81

Lemma 11. Let G = (V; E ) be an undire ted graph and let a; b 2 V be su h that
(a; b) 2= E . Then N (a; b) an be omputed in time O(n1=2  e).
Proof : Constru t a simple network N = (V ; E; ) as follows. Let V 0 = fv0 ; v 2 V g
and V 00 = fv00 ; v 2 V g; let V = V 0 [ V 00 and E = f(v0 ; v00 ); v 2 V g [
f(v00 ; w0 ); (w00 ; v0 ); (v; w) 2 E g. Finally, let (v0 ; v00 ) = 1 for v 2 V and let
(v00 ; w0 ) = (w00 ; v0 ) = 1 for (v; w) 2 E . The onstru tion is illustrated by Figure 27.

0
e

a0

e0

00
1 a

d0

d
Figure 27.

00
1

1 d00

00
1 e

b0

00
1 b

Computation of N (a; b)

Claim: N (a; b) is equal to the maximum ow from a00 , the sour e, to b0 , the sink,
in network N .
Proof : \": Let (A; V A) be a minimal (a00 ; b0 )- ut in network N . Let S = fv; v0 2
A; v00 2 V Ag. Then learly (A; V A)  jS j. Also, S is an (a; b)-vertex separator
and hen e jS j  N (a; b). This an be seen as follows. Let a = v0 ; v1 ; : : : ; vk = b be a
path from a to b in G. Consider path v000 ; v10 ; v100 ; v20 ; : : : ; vk00 1 ; vk0 from a00 to b0 in N .
At least one of its edges must go a ross the ut de ned by A. It annot be one of
the edges (vi00 ; vi0+1 ) be ause these edges have apa ity 1 and ut (A; V A) has
nite apa ity. Note that ut (A; V A) has nite apa ity be ause it is a minimal
ut and sin e there are uts, e.g., A = fa00 g [ (V 0 fb0 g), of nite apa ity.
\": Let S  V fa; bg be an (a; b)-vertex separator with jS j = N (a; b). De ne
A = fx 2 V ; x an be rea hed from a00 without using an edge (s0 ; s00 ) for s 2 S g.
Then b0 2= A and hen e (A; V A) is an (a00 ; b0 )- ut of network N . Also, v0 2 A
implies v00 2 A for v 2 V S . Hen e (A; V A)  jS j = N (a; b).

It is easy to see that the maximum ow from a00 to b0 does not hange if we hange
all apa ities to 1. In this way we obtain a simple (0,1)-network. A maximum
ow in this network and hen e also N (a; b) an be omputed in time O(n1=2  e) by
Theorem 8.
The vertex onne tivity of an undire ted graph G = (V; E ) is the minimal
onne tivity number of any pair of un onne ted verti es. More pre isely

n 1
if G is omplete;
= minf
N (a; b); (a; b) 2= E g otherwise.
Version: 19.10.99

Time: 11:01

{81{

82

Chapter 4. Algorithms on Graphs

= (V; E ) be an undire ted graph and let be its vertex


onne tivity.
a) an be omputed in time O(  n3=2  e) = O(n1=2  e2 ).
b) Let  > 0 and assume e  n2 =4. Then there is a randomized algorithm whi h
omputes in expe ted time O(( log )  n3=2  e) with probability of error at
most .

Theorem 10. Let G

Proof :

Both parts are based on the following simple observation.


Claim 1. Assume < n 1. Let = N (x; y) for some nodes x; y 2 V and let
S , jS j = , be an (x; y)-vertex separator. Then = minfN (a; b); b 2 V g for all
a 2 V S.
Proof : G S onsists of at least two omponents. Let b be a node whi h does
not belong to the same omponent as a. Then S separates a from b and hen e
N (a; b)  jS j = . Thus = N (a; b) by de nition of .
a) Claim 1 suggests Program 28. Let v1 ; v2 ; v3 ; : : : ; vn be some ordering of V .
Corre tness of Program 28 an be seen as follows. It is = minf 1 ; 2 ; : : : ; +1 g
by the laim above. Also C  and C = minf 1 ; : : : ; i g always. The algorithm
terminates with  C < i and hen e C = . Also C = whenever i  + 1. Thus
+ 1 iterations su e.
(1)
(2)
(3)
(4)
(5)

C 1; i 1;
while C  i
do i minfN (vi ; v); v 2 V g;
C min(C; i ); i i + 1
od.
Program 28

The running time is determined by line (3). A single exe ution of line (3) takes
time O(n3=2  e) by Lemma 11. Hen e the total runningPtime is O(  n3=2  e). Finally
observe that  minfdeg(v); v 2 V g  2  e=n sin e v2V deg(v) = 2  e.
b) Sin e  2  e=n, f. the proof of part a), and e  n2 =4 by the assumption we
have  n=2.
Claim 2. Choose a 2 V at random. Then
prob( > minfN (a; b); b 2 V g)  1=2:
Proof : This is almost obvious from Claim 1. Let S be de ned as in Claim 1. Then
jS j  n=2 and
prob( > minfN (a; b); b 2 V g)  prob(a 2 S )  1=2:
Version: 19.10.99

Time: 11:01

{82{

4.9.3. Weighted Network Flow and Weighted Bipartite Mat hing

83

Claim 2 suggests the randomized algorithm of Program 29 for omputing .


(1) k
log ; C 1;
(2) do k times
(3)
hoose a 2 V at random;
(4)
C min(C; minfN (a; b); b 2 V g)
(5) od.

Program 29

The running time of Program 29 is learly O(( log )  n3=2  e). Also, the
probability that C > upon termination is at most (1=2)k =  by Claim 2.
4.9.3. Weighted Network Flow and Weighted Bipartite Mat hing

A weighted network ow +problem is given by N = (V; E; ap ; ost ) and nodes


s; t 2 V . Here ap : E ! R gives the apa ity of an edge (we used instead of
ap so far) and ost : E ! R is the ost of transporting one unit of ow a ross an
edge. Throughout this se tion we assume that apa ities are integers. Let f be a
legal (s; t)- ow. Then the ost of f is given by
X
ost (f ) =
f (e)  ost (e):
2

e E

The weighted network ow problem is then to ompute a legal (s; t)- ow with
maximum ow value and (among these) minimal ost. More generally, we might
look for a ow fun tion f with val (f ) = v for some prede ned v and minimal
ost . In this se tion we will see that a minimal ost ow with ow value v an be
omputed in time O(v  e (log n)= log(e=n)). At the end of this se tion we will apply
this result to weighted bipartite mat hing and derive an O(n  e  (log n)= log(e=n))
algorithm for it.
The theory of weighted network ow is a natural extension of the theory of
ordinary network ow. Let N = (V; E; ap ; ost ) be a network, s; t 2 V , and let
f : E ! R be a legal (s; t)- ow. We de ne the augmenting network with respe t to
N and f as we did in the previous se tion. More pre isely, AN = (V; E; ap ; ost ),
where E = E1 [ E2 and E1 = fe 2 E ; f (e) < ap (e)g and E2 = f(w; v); (v; w) =
e 2 E and f (e) > 0g. For e 2 E we use ei to denote the edge orresponding to e in
Ei , i = 1; 2. Also

ap (e) f (e) if e = e1 ;
ap (e) =
f (e)
if e = e2
and

ost (e)
if e = e1 ;
ost (e) =
ost (e) if e = e2 .
Our rst lemma onne ts minimality in ost with the existen e of y les of negative
ost in the augmenting network.
Version: 19.10.99

Time: 11:01

{83{

84

Chapter 4. Algorithms on Graphs

Lemma 12. Let f be a legal (s; t)- ow with val (f ) = v and let AN be the augmenting network with respe t to f . Then f has minimal ost among all (s; t)- ows
with value v i there is no y le of negative ost in AN .
Proof :

\)": (Indire t.) It is lear that a negative ost y le an be used to de rease


the ost of f without hanging the ow value.
\(": (Indire t.) Assume that f does not have minimal ost, i.e., there is a legal
(s; t)- ow g with val (g) = val (f ) and ost (g) < ost(f ). Let AN = (V; E; ap ; ost )
be the augmenting network with respe t to f . Consider h : E ! R de ned by

max(0; g(e) f (e)) if e = e1 ;
h(e) = max(0
; f (e) g(e)) if e = e2 .
Claim 1. h is a legal (s; t)- ow in AN with val (h) = 0 and ost (h) < 0.
Proof : We show rst that h has negative ost. Note that for all e 2 E
h(e1 )  ost (e1 ) + h(e2 )  ost (e2 ) = (g(e) f (e))  ost (e) and hen e
ost (h) =


e
 E

we have

h(e)  ost (e)

[h(e1 )  ost (e1 ) + h(e2 )  ost (e2 )

e E

(g(e) f (e))  ost (e)

e E

= ost (g) ost (f ) < 0:


Next we show that h satis es the onservation laws. Note that for all e 2 E
h(e1 ) h(e2 ) = g(e) f (e) and hen e for all v 2 V ( out (v) is the set of edges
emanating from v in AN and similarly for in (v)):
X
X
h(e)
h(e)
2

e out(v )

=
=

e out(v )

e out(v )

=4

e in(v )

(g(e) f (e))

e out(v )

8
<

(h(e1 ) h(e2 ))

g(e)

e in(v )

e in(v )

(h(e1 ) h(e2 ))

(g(e) f (e))

e in(v )

g(e)5

0 0
if v 6= s; t;
= : val (g) val (f ) if v = s;
val (g) + val (f ) if v = t.
Version: 19.10.99

Time: 11:01

{84{

e out(v )

f (e)

e in(v )

f (e)5

4.9.3. Weighted Network Flow and Weighted Bipartite Mat hing

85

In any ase, this shows that h satis es the onservation laws and that val (h) = 0.
Finally, it is trivial to see that h satis es the apa ity onstraints.
Flow fun tion h has zero ow and negative ost. It is intuitively lear, that h is
ir ular in some sense. More pre isely, we show that h an be de omposed into a
set of ows around y les. It is then easy to on lude that one of the y les must
have negative ost.
Claim 2. There are h1 ; h2 ; : : : ; hm : E ! R+0 , m  e, su h that
1) h(e) = Pi hi(e) for all e 2 E .
2) For every i, 1  i  m, there is a dire ted y le w0 ; w1 ; : : : ; wk = w0 in AN
su h that hi (wj ; wj +1 ) = hi (wl ; wl+1 ) for 0  j < l < k, and hi (e) = 0 for
edges not on the y le.

(By indu tion on the number k of edges e 2 E with h(e) 6= 0.) If k = 0 then
there is nothing to prove. So let us assume k  0. Let
v0 be any node su h that there
is an edge (v0 ; v1 ) 2 E with h(v0 ; v1 ) 6= 0. Sin e Pe2out(v1 ) h(e) = Pe2in(v1 ) h(e)
and h(e)  0 for all e 2 E there must be v2 su h that (v1 ; v2 ) 2 E with h(v1 ; v2 ) 6= 0.
Continuing in this fashion we onstru t a path v0; v1 ; v2 ; : : : ; vr in AN with vr = vj
for some j < r.
We take vj ; vj+1; : : : ; vr as the desired y le and de ne h1 : E 2 R+0 by
h1 (vl ; vl+1 ) = minfh(vl ; vl+1 ); j  l < rg for j  l < r and h1 (e) = 0 for all
edges e not on the y le.
Finally, let h0 = h h1 . Then h0 is a legal (s; t)- ow with ow value 0. Also
there is at least one edge e less with h0 (e) 6= 0.
Proof :

The proof of Lemma 12 is now readily ompleted. It is ost (h) = Pi ost (hi )
and hen e ost (hi ) < 0 for some i, 1  i  m. Let C be the y le underlying hi and
let  be the ow along the edges of C . Then ost (hi ) =   ost (C ) where ost (C ) is
the ost of y le C interpreted as a path in network AN .
Lemma 12 an be used to design an algorithm for minimizing the ost without
hanging the ow value (Exer ise 32). What is more important, we an use
Lemma 12 to show that the augmenting along minimum ost paths does not destroy
the ost minimality.
Lemma 13. Let f be a minimal ost ow with val (f ) = v and let AN =
(V; E; ap ; ost ) be the augmenting network with respe t to f . Let p be a minimum ost path from s to t in AN , let f 0 be a legal (s; t)- ow in AN whi h is
non-zero only along p (i.e., f 0 sends some units of ow from s to t along p). Then
f 00 where
for all e 2 E
f 00 (e) = f (e) + f 0 (e1 ) f 0 (e2 )
is a minimum ost ow of value val (f ) + val (f 0 ).
Version: 19.10.99

Time: 11:01

{85{

86

Chapter 4. Algorithms on Graphs

Proof : f 00

is ertainly a legal (s; t)- ow with value val (f ) + val (f 0 ). A formal


proof an be given along the lines of Lemma 2b). It remains to be shown that
f 00 has minimal ost. Assume otherwise. Then there is a negative ost y le C
in the augmenting network AN 00 onstru ted with respe t to f 00. We will derive a
ontradi tion as follows.
If y le C exists in AN then f was not optimal, ontradi tion. So C annot
exist in AN , i.e., there is at least one edge (v; w) on path p su h that C uses this
edge in reverse dire tion. Let (v; w) be the rst su h edge. See Figure 28.
t
y

y
C

v
s

Figure 28.

w
p0

Constru tion for Lemma 13

Let path p0 from s to t be onstru ted as follows. Follow p from s to v, then


follow C until C interse ts p for the next time, say in point x, then follow p from
x to t. Let y le C 0 be onstru ted as follows. Follow p from w to y, where y
is the point following x on y le C , and then follow C from y to w. Note that
ost (p0 ) + ost (C 0 ) = ost (p) + ost (C ) sin e the ost of edge (v; w) is the negative
of the ost of edge (w; v). Continuing in this way we obtain an (s; t)-path p00 from
s to t in AN and a y le C 00 su h that ost (p00 ) + ost (C 00 ) = ost (p) + ost (C ) and
p00 and C 00 use no edge in reverse order. Thus C 00 is a y le in network AN . Sin e
ost (C ) < 0 we either have ost (C 00 ) < 0, a ontradi tion to the optimality of f , or
ost (p00 ) < ost (p), a ontradi tion to the fa t, that p is a least ost path from s to
t in AN .
Lemma 13 gives rise to a minimum ost ow algorithm formulated in Program 30.
(1)
(2)
(3)
(4)
(5)
(6)
(7)

onstru t a minimal ost ow with ow value 0 ( f. Exer ise 32);

while val (f ) < v


do let AN be the augmenting network with respe t to f ;
let p be a least ost path from s to t in AN ;
let  be the minimal apa ity of any edge in p;
in rease the ow along p by min(; v val (f ))
od.
Program 30

Version: 19.10.99

Time: 11:01

{86{

4.9.3. Weighted Network Flow and Weighted Bipartite Mat hing

87

Theorem 11. Let N = (V; E; ap ; ost ), s; t 2 V , be a network with integer


apa ities and let v 2 R+ . Then a minimum ost ow with value v (if it exists) an
be omputed in time O((1 + v)  n  e).
Proof : Corre tness of the algorithm above is obvious from Lemma 13. A single
exe ution of the loop body takes time O(e) for lines (3), (5) and (6), plus O(n  e)
for line (4). The time bound for line (4) follows from 4.7.3, Theorem 5. Finally,
sin e apa ities are integers, the ow is in reased by at least one in every iteration
(ex ept maybe the last). Thus the total running time is O((1 + v)  n  e).

In line (4) of Program 30 one has to solve single sour e least ost problems. In
Se tion 4.7.4 we saw that arbitrary edge osts an sometimes be transformed into
non-negative edge osts by means of a potential fun tion. More pre isely, we pro eeded as follows. Given a weighted graph (V; E; ost ) and s 2 V we omputed
(s; v), the ost of the least ost path from s to v. We used (s; v) as a potential
fun tion and turned all edge osts into non-negative edge osts by
g (v; w) = ost (v; w) + (s; v )
ost

(s; w):

A similar approa h works here. Let AN be the augmenting network with respe t
to minimal ost ow f and let p be a minimal ost path from s to t in AN . After
in reasing the ow along p we obtain ow f 0. Let AN 0 be the augmenting network
with respe t to f 0. Then AN and AN 0 are very similar. The only di eren e is that
some edges of path p are removed from, and the reverse of some edges of path p
are added to AN to obtain AN 0. Also if a reverse edge is added then its ost is the
negative of the ost of the edge. Let (s; v) be the ost of the least ost path from
s to v in AN . We laim that we an use (s; v) as a potential fun tion for least
ost path omputations in network AN 0.
Lemma 14. Let AN 0 = (V; E 0 ; ap 0 ; ost 0 ) and let (s; v) be the ost of a least
ost path from s to v in AN , v 2 V . Let
g (v; w) = ost 0 (v; w) + (s; v )
ost

(s; w)

g (v; w)  0 for all (v; w) 2 E 0 .


for all (v; w) 2 E 0 . Then ost

Proof :

We distinguish two ases: Edge (v; w) is the reverse of an edge of path


or it is not. If it is not then ost 0 (v; w) = ost (v; w) where ost is the ost
fun tion of network AN and the laim is true sin e the distan es satisfy the triangle
inequality. If (v; w) is the reverse of edge (w; v) and (w; v) belongs to p then (s; v) =
(s; w) + ost (w; v) sin e p is a least ost path and ost 0 (v; w) = ost (w; v) by
g (v; w) = 0.
de nition of AN 0 . Hen e ost
p

Version: 19.10.99

Time: 11:01

{87{

88

Chapter 4. Algorithms on Graphs

Lemma 14 almost implies that we only have to solve single sour e least ost path
problems with non-negative edge osts in line (4). However, there is still a small
problem to solve. If we transform edge osts as des ribed in Lemma 14, then we
ompute e(s; v), the least ost of a path from s to v in AN 0 with respe t to ost
g , in line (4). However, we need to know 0 (s; v ), the least ost of a
fun tion ost
path from s to v in AN 0 with respe t to ost fun tion ost 0 , in order to be able
to transform edge osts for the next iteration. This di ulty is easily surmounted.
Note that
e(s; v) = 0 (s; v) + (s; s) (s; v)
= 0(s; v) (s; v)
for all v 2 V . Hen e 0 (s; v) is easily omputed from e(s; v) and (s; v). We
summarize in
Theorem 12. Let N = (V; E; ap ; ost ), s; t 2 V , be a network with integer
apa ities and let v 2 R+ . Then a minimum ost ow from s to t with value v
an be omputed in time O(v  e  (log n)= max(1; log(e=n))).
Proof :

Follows immediately from the dis ussion above, and 4.7.2, Theorem 2.

We end this se tion with a short dis ussion of weighted bipartite mat hing. Let+
G = (V1 [ V2 ; E ), E  V1  V2 , be a bipartite (undire ted) graph. Let ost : E ! R
be a ost fun tion. If M  E is a mat hing then the ost of M is de ned by
ost (M ) =

ost (e):

e M

Theorem 13. Let G = (V1 [ V2 ; E ) be a weighted bipartite graph, let ost : E !


R+ be a ost fun tion and let v  n, v 2 N. Then a mat hing of ardinality v (if it
exists) and minimal ost an be omputed in time

O(n  e  (log n)= max(1; log(e=n))):


Proof : The proof is very similar to the proof of Theorem 9. De ne network N =
(fs; tg[V1 [V2 ; E; ap ; ost ) by E = (fsgV1 )[E [(V2 ftg), ap (e) = 1 for all e 2 E
and ost (e) = ost (e) for e 2 E and ost (e) = 0 for e 2 E E . Then mat hings
and ows are in one-to-one orresponden e and hen e a mat hing of ardinality v
and minimal ost an be omputed in time O(n  e  (log n)= max(1; log(e=n))) by
Theorem 12.

Version: 19.10.99

Time: 11:01

{88{

4.10. Planar Graphs

89

4.10. Planar Graphs

This se tion is devoted to planar graphs. We will treat ve topi s. We start with
a linear time algorithm for 5- oloring planar graphs and then show how to test
planarity and to onstru t a ombinatorial embedding in linear time. The third
topi is an O(n log n) algorithm for the onstru tion of a straight-line embedding
and the fourth topi is the planar separator theorem whi h makes the family of
planar graphs amenable to divide-and- onquer algorithms. The nal topi is one
parti ular algorithm based on the divide-and- onquer paradigm: a single sour e
least ost path algorithm for planar graphs.
A (topologi al) planar embedding of an undire ted graph2 G = (V; E ) is
a fun tion E that maps the verti es of G to distin t points in R and ea h edge
fu; vg 2 E to a Jordan urve in R2 from E(u) to E(v) su h that for all e = fu; vg 2
E , E(e) \ (E(V ) [ E(E nfeg)) = fE(u); E(v)g (i.e., edges do not ross). G is planar
if there exists a planar embedding of G.
Let E be a planar embedding of a planar graph G = (V; E ). The fa es of E
are the onne ted regions of R2nE(V [ E ). The boundary (indu ed by E) of a
fa e F of E is the subgraph of G onsisting of those elements  2 V [ E for whi h
there exist points in F arbitrarily lose to E(). If G is bi onne ted and jV j  3,
the boundary of ea h fa e of E is a simple y le. Let D be the set of darts of
the dire ted version of G, and for ea h dart e = (u; v) 2 D, let E (e) be the dart
e0 = (u; w) 2 D su h that E(fu; wg) is the rst urve in E(E ) with endpoint E(u)
en ountered after E(e) in a lo kwise s an around E(u). E is a permutation of D
known as the ombinatorial planar embedding orresponding to E or indu ed by E.
Note that E is equivalent to a y li ordering of the edges in ident to any vertex v.
A permutation  of D is alled a ombinatorial planar embedding if  = E
for some planar embedding E. The graph (D; f((u; v); E ((v; u))); (u; v) 2 Dg),
whi h is the union of vertex-disjoint dire ted simple y les, is alled the fa e y le
graph of E, and its y les the fa e y les of E. The intuitive meaning of a fa e
y le C of E is that it orresponds to a walk inside a parti ular fa e F of E along
the image R under E of the elements on the boundary of F , always keeping F to
the left and R to the right ( f. Fig. 101). The fa e y le C is said to be a fa e y le
of F . If G is onne ted, ea h fa e of E has pre isely one fa e y le. A fa e is said to
be in ident on the verti es and edges on its boundary, and the verti es and edges
on its boundary are said to border the fa e. Among the fa es there is exa tly one
whi h is unbounded. It is alled the outer or in nite fa e.
Lemma 1. Let E be any planar embedding of a graph G, let F be a fa e of E and
let B be the boundary of F . Then there is an embedding E 0 of G in whi h B is
the boundary of the outer fa e. Moreover, E and E 0 indu e the same ombinatorial

embedding.

Proof : Let E be any embedding of G and let F be any fa e of E, whi h is not the
outer fa e. Then there is a sequen e F0 ; F1 ; : : : ; Fk of fa es su h that F0 is the outer
fa e, Fk = F and Fi and Fi+1 have a ommon edge in their boundaries. Let e be
Version: 19.10.99

Time: 11:01

{89{

90

Chapter 4. Algorithms on Graphs

A planar embedding and its fa e y les

Figure 101.

an edge whi h is on the boundary of F0 and F1 . By hanging the embedding of e,


f. Figure 102, we an make the boundary of F1 the boundary of the outer fa e.
Continuing in this fashion we obtain the desired embedding E 0.

F0

F0

=)

F1 e

Figure 102.

F1

Changing the embedding of edge e

Lemma 2. Let G = (V; E ) be a planar graph. Then m  3n

6 for n  3.

Proof : It learly su es to prove the lemma for onne ted planar graphs. We
rst prove Euler's formula whi h relates the number of edges, nodes and fa es of a
onne ted planar graph.
Claim: Let E be a planar embedding of a onne ted planar graph G with n nodes
and m edges and let f be the number of fa es. Then n + f = m + 2.
Proof : We use indu tion on m. If m = 0 then n = f = 1 and the laim holds. If
m > 0 and there is a node of degree 1 then removal of this node yields an embedding
of a onne ted graph with one less node, one less edge and the same number of fa es
and the laim follows immediately from the indu tion hypothesis. If m > 0 and
there is no node of degree 1 then E ontains a y le. The removal of one of the
edges of the y le yields an embedding of a onne ted graph with the same number
Version: 19.10.99

Time: 11:01

{90{

4.10. Planar Graphs

91

of nodes, one less fa e and one less edge, and again the laim follows immediately
from the indu tion hypothesis.
Let E be any embedding of G. Then the fa e y le graph of E onsists of one y le
for ea h fa e and this y le onsists of at least three darts of G sin e G is onne ted
and n  3. Thus 3f  2m and hen e n + 2m=3  m + 2 or m  3n 6.
Lemma 2 is helpful in at least two respe ts. First, it implies that an O(n + m)
algorithm on planar graphs is really an O(n) algorithm and se ondly it implies that
a planar graph has a large fra tion of nodes
P pre isely, let nd
P of small degree. More
n

j
=
2
m
and
be
the
number
of
nodes
of
degree
d
.
Then
j
j nj = n and hen e
j
P
P
6
n
12
by
Lemma
2.
Thus
n

j

j
j
j j
(n1 +    + nd) 

d+1 (j

6)nj + 12

:
6
In parti ular, n1 +  + n5  2, i.e., there are at least two nodes of degree at most 5,
and n1 +  +n6  Pj7 nj =6 = (n (n1 +  +n6))=6, i.e., at least 14% of the nodes
have degree at most 6. The fa t that a large number of nodes have small degree an
sometimes be exploited to design divide-and- onquer algorithms for planar graphs.
One su h algorithm is treated in Se tion 8.3 on Voronoi diagrams and sear hing
planar subdivisions. Another onsequen e of Lemma 2 is the following
j

Lemma 3. Every planar graph is 5- olorable, i.e., if G = (V; E ) is planar then there
is a mapping : V ! f1; 2; 3; 4; 5g su h that (u) 6= (v) for all edges fu; vg 2 E .
Proof :

We use indu tion on the number of nodes of G. Let v 2 V be any node of


degree at most 5. If the degree of v is 4 or less then a 5- oloring of G v an learly
be extended to a 5- oloring of G. So let us assume that v has degree 5. Then there
must be neighbors x and y of v su h that fx; yg is not an edge of G. Otherwise,
the neighbors of v would form a K5 , i.e., a omplete graph on 5 nodes, and hen e
G were not planar sin e a K5 has 5 nodes and 10 edges and hen e is not planar by
Lemma 2. Consider the graph G0 obtained from G v by identifying the nodes x
and y, i.e., G0 has the vertex set V 0 = V fv; yg and the edge set
E 0 = ffu; wg; u; w 2 V 0 and fu; wg 2 E g [
ffx; wg; w 2 V 0 and fy; wg 2 E g:

The graph G0 is learly planar (Figure 103 indi ates how a planar embedding of G0
an be obtained from a planar embedding of G) and hen e G0 is 5- olorable by
indu tion hypothesis. Thus G v is 5- olorable with nodes x and y having the
same olor and hen e G is 5- olorable.
Version: 19.10.99

Time: 11:01

{91{

92

Chapter 4. Algorithms on Graphs

x
v

=)

A planar embedding of G yields a planar embedding of G0

Figure 103.

The proof of Lemma 3 dire tly yields a 5- oloring algorithm with O(n2) running
time. The key idea for a linear time algorithm is the observation that we an require
the nodes x and y to have small degree. Let us all a node v small if deg (v)  11,
and large otherwise and let us all a node v good, if either deg (v)  4 or deg (v) = 5
and v has at most one large neighbor.
Lemma 4.
a) A planar graph ontains at least one good node.
b) Let v be a good node of degree 5. Then there are small neighbors x, y of v
su h that fx; yg 2= E .
Proof : a) Let nd be the number of nodes of degree d. If n1 +    + n4 > 0 then we
are done. Otherwise
X
X
j  nj  6n 12 < 6  nj
j

by Lemma 2 and hen e


n5 >

X
j

6

5

(j 6)  nj 

X
j

5

X
(j 6)  nj  21 j  nj :

12

12

Suppose now that a good node does not exist. Then every node of degree 5 has at
least two large neighbors
and hen e the number of edges in ident to large nodes is
at least 2  n5, i.e., Pj12 j  nj  2  n5. This ontradi ts the inequality derived
above.
b) Let N be the set of small neighbors of v. Then jN j  4 and if fx; yg 2 E for all
x; y 2 N then N [ fvg ontains a K5 and hen e G is not planar, a ontradi tion.
Lemmas 3 and 4 suggest the following algorithm for 5- oloring a planar graph. We
start by determining the set M of good nodes. This takes time O(n). We keep the
set M as a doubly linked linear list L. For ea h node v 2 M we have a pointer
to the item on the list L whi h represents v. Then v an be deleted from M in
onstant time, an element of M an be sele ted in onstant time and elements an
be added to M in onstant time.
Version: 19.10.99

Time: 11:01

{92{

4.10. Planar Graphs

93

Let v be any node in M . If the degree of v is four or less, then we delete v


from M , update M ( f. below), olor G v by applying the algorithm re ursively,
and nally olor v. If v has degree 5, then let x and y be small neighbors of v with
fx; yg 2= E . The nodes x and y exist by Lemma 4 and an be found in time O(1).
We delete v from the graph, identify x and y (this takes time O(1), sin e v, x and
y are small) and update M ( f. below). We then olor G0 re ursively and nally
extend the oloring to G. We still need to explain how to update M . When v
is deleted from the graph some nodes may be ome small and hen e some of their
neighbors good. All of this an ertainly be he ked in time O(1). Similarly, when x
and y are identi ed, then x may be ome large and a ommon neighbor of x and y
may be ome small. Again, all of this and the e e t on M is easily determined in
onstant time. Thus time O(1) su es to olor an additional node and hen e the
algorithm runs in time O(n).
Theorem 1. A 5- oloring of a planar graph an be omputed in linear time.

Our next topi is a linear time planarity testing algorithm. Sin e a graph is planar
i its bi onne ted omponents are ( f. Se tion 4.6 for a linear time algorithm to
ompute the bi onne ted omponents of a graph) we an restri t our attention to
bi onne ted graphs. Also we an on ne ourselves to graphs with m  3n 6 by
Lemma 2. The planarity testing algorithm is an extension of depth- rst-sear h. In
the sequel we will always identify nodes with their DFS-number. A DFS on the
dire ted version of G = (V; E ) partitions the darts of G into the sets T , F and B .
For the planarity testing algorithm we onsider the dire ted graph (V; T [ F 1 ) and
all the edges in T tree edges and the edges in F 1 ba k edges. Also, we write
B instead of F 1 . Note that this notation di ers slightly from the one used in
Se tion 4.5. There, reversals of tree edges were also alled ba k edges.
We will now des ribe the idea underlying the planarity algorithm. Let C be
any y le starting in the root of the dfs -tree and onsisting of tree edges followed
by one ba k edge. Su h a y le exists sin e G is assumed to be bi onne ted. For
every edge e = (x; y) emanating from the y le, i.e., x lies on C but e is not an
edge of the y le we onsider the segment S (e) de ned as follows. If e is a ba k
edge then S (e) is the y le formed by the tree path from y to x together with the
edge e. If e is a tree edge then S (e) onsists of the subgraph spanned by the set
V (e) = fw; y ! wg of nodes rea hable from y by tree edges, all ba k edges starting
T
in a node in V (e) and ending in a node on y le C (whi h is then an an estor of x),
and the tree path from the lowest atta hment of S (e) to y le C to node y.
In Figure 104 the y le C onsists of the tree path from node 1 to
node 9 and the ba k edge (9; 1). The four edges (9; 10), (7; 5), (7; 13) and (6; 4)
emanate from the y le. The segment S ((9; 10)) onsists of the subgraph spanned
by f10; 11; 12g, the ba k edges (11; 8), (11; 7) and (12; 5), and the tree path from 5
to 10. The segment S ((9; 10)) is atta hed to the y le in the nodes 9, 8, 7 and 5.

Example:

Version: 19.10.99

Time: 11:01

{93{

94

Chapter 4. Algorithms on Graphs

10

11

12

8
13

7
6
5
4
3
2

1
Figure 104. A dfs -tree of a planar graph
We test the planarity of G in a two step pro ess. In the rst step we test
whether C + S (e), the graph onsisting of y le C and segment S (e), is planar
for every edge e emanating from y le C . This is equivalent to testing whether
the segment S (e) has a strongly planar embedding, i.e., an embedding where all
atta hments of S (e) to the y le C lie on the boundary of the outer fa e. In order
to test the strong planarity of S (e) we will use the algorithm re ursively. Suppose
now that the segments S (e) are all strongly planar. We then try in a se ond step to
merge the embeddings found in step one. The merging pro ess has to de ide for ea h
segment S (e) whether it should be pla ed inside or outside the y le C . For this
purpose, it only needs to take into a ount the set of atta hments of the di erent
segments emanating from C and their intera tion. In our example, the segments
S ((7; 5)) and S ((6; 4)) have to be embedded on di erent sides of C be ause these
segments \interla e".
We will next des ribe the theory behind both steps in detail. With an edge e =
(x; y) we asso iate a y le C (e) and a segment S (e) as follows. If e is a ba k edge
then C (e) and S (e) onsist of the tree
path from y to x and the edge e. If e is

a tree edge then let V (e) = fw; y !
wg be the set of tree su essors of y and let
T
lowpt [y = minfz ; (w; z ) is a ba k edge and w 2 V (e)g be the lowest endpoint of a
ba k edge starting in V (e). The y le C (e) onsists of a tree path from lowpt [y to w,
Version: 19.10.99

Time: 11:01

{94{

4.10. Planar Graphs

95

where w 2 V (e) and (w; lowpt [y) 2 B and su h a ba k edge. The segment S (e)
onsists of C (e), the subgraph spanned by V (e) and all ba k edges starting in a
node in V (e). Note that the segment S (e) is uniquely de ned but that there may be
several hoi es for the y le C (e). We divide the tree path underlying the y le C (e)
into two parts, its stem and its spine. The stem onsists of the part ending in x.
The spine is empty if e is a ba k edge and it is the part starting in y if e is a tree
edge.
In our example, the y le C ((9; 10)) onsists of the tree path from 5 to 12
followed by the ba k edge (12; 5). The stem is the tree path from 5 to 9 and the
spine is the tree path from 10 to 12. The y le C ((1; 2)) onsists of the tree path
from 1 to 9 and the ba k edge (9; 1). Its stem is the node 1.
A segment S (e) is alled strongly planar if there is an embedding of S (e)
su h that the stem of the y le C (e) borders the outer fa e. An embedding with
this property is alled a strongly planar embedding of S (e). Let w0; w1 ; : : : ; wr
with e = (wr ; y) be the stem of C (e). A strongly planar embedding of S (e) is alled
anoni al (reversed anoni al) if for all i, 0 < i < r, the edge fwi ; wi+1 g immediately follows (pre edes) the edge fwi ; wi 1g in the ounter lo kwise ordering of
edges in ident to wi. Note that every strongly planar embedding is either anoni al
or reversed anoni al.
In Figure 104 the embeddings of segments S ((9; 10)) and S ((7; 13)) are both
strongly planar, the embedding of S ((7; 13)) is anoni al and the embedding of
S ((9; 10)) is reversed anoni al.
Lemma 5. Let G be a bi onne ted graph and let e be the unique tree edge starting
in the root of the dfs -tree. Then S (e) = G and G is planar i S (e) is strongly planar.
Proof :

Let e = (1; 2) be the unique tree edge in ident to node 1. Then V (e) =
f2; : : : ; ng and hen e S (e) = G. Also, the stem of C (e) onsists only of vertex 1
and hen e S (e) is strongly planar i it is planar by Lemma 1.
Lemma 5 shows that we an on ne ourselves to a test of strong planarity. Now
let e0 be an edge and C = C (e0 ) be the y le asso iated with e0 . An edge e = (x; y)
is said to emanate from C if x lies on the spine of C but e does not belong to C .
Clearly, if e emanates from C (e0 ) then the stem of C (e) is part of the tree path
underlying C (e0 ) and S (e) is a subgraph of S (e0 ). Also, S (e0 ) is the union of C (e0 )
and the segments S (e), where e emanates from C (e0). The basis of step 1 of the
planarity algorithm is the following
Lemma 6. Let C = C (e0 ) be a y le and let e emanate from C . Then C + S (e)
is planar i S (e) is strongly planar.
Proof :

\)": Consider any embedding of C + S (e). The y le C divides the plane


into a bounded and an unbounded region. We may assume w.l.o.g. that the edge
e = (x; y) lies in the bounded region. Hen e all nodes in V (e) must lie in the
bounded region sin e every node in V (e) is rea hable from y without passing through
Version: 19.10.99

Time: 11:01

{95{

96

Chapter 4. Algorithms on Graphs

a node of C . If we remove the part of y le C between x and lowpt [y then we have


the desired strongly planar embedding of S (e).
\(": Given a strongly planar embedding of S (e) we an learly add the missing
part of C to obtain an embedding of C + S (e).
For step 2 of the algorithm we need the on epts of atta hments and interla ing.
Let C = C (e0 ) and let e = (x; y) emanate from C . The set A(e) of atta hments
of segment S (e) to y le C is de ned to be the set fx; yg if e is a ba k edge and
the set fxg [ fz; (w; z) is a ba k edge, w 2 V (e) and z 2= V (e)g if e is a tree edge.
Two segments S (e) and S (e0 ) where e and e0 emanate from C are said to interla e
if either there are nodes x < y < z < u on y le C su h that x; z 2 A(e) and
y; u 2 A(e0 ) or A(e) and A(e0 ) have three points in ommon ( f. Fig. 105; note that
the segments shown may have further atta hments).
u

S (e0 )
C

S (e 0 )

S (e)
y

S (e)

Interla ing segments


Clearly, interla ing segments annot be embedded on the same side of C . The
interla ing graph IG(C ) with respe t to y le C is de ned as follows: The nodes
of IG (C ) are the segments S (e) where e emanates from C . Also, S (e) and S (e0 )
are onne ted by an edge i S (e) and S (e0 ) interla e. The interla ing graph for the
y le C ((1; 2)) of Figure 104 is shown in Figure 106. This graph is bipartite with
segments S1 and S3 forming one of the sides of the bipartite graph. Note also that
the planar embedding of the graph of Figure 104 has S1 and S3 on one side of C
and S2 and S4 on the other side of C .
S1 = S ((9; 10))
S ((7; 13)) = S2
Figure 105.

S3 = S ((7; 5))

S ((6; 4)) = S4

Interla ing graph


let C = C (e0 ) = w0 ! w1 !    ! wk ! w0
T
T
T
B

Figure 106.

Lemma 7. Let e0 be a tree edge,


and let e0 = (wr ; wr+1 ). Let e1 ; : : : ; em be the edges leaving the spine of C , i.e.,
Version: 19.10.99

Time: 11:01

{96{

4.10. Planar Graphs

97

they leave the y le in nodes wj , r < j  k. Then S (e0 ) is planar i S (ei ) is


strongly planar for every i, 1  i  m, and IG (C ) is bipartite, i.e., there is a
partition L; R of fS (e1 ); : : : ; S (em )g su h that no two segments in L resp. R interla e. Moreover, segment S (e0 ) is strongly planar i in Saddition for every onne ted omponentSB of IG (C ): either fw1 ; : : : ; wr 1 g \ S(e)2B\L A(e) = ; or
fw1 ; : : : ; wr 1 g \ S(e)2B\R A(e) = ;.
Proof :

\)": Note rst that S (e0 ) = C + S (e1 ) +    + S (em ). Hen e, if S (e0 ) is


planar then C + S (ei ), 1  i  m, is planar and hen e S (ei ) is strongly planar
by Lemma 6. Consider any planar embedding of S (e0 ). Let L = fS (ei ); S (ei ) is
embedded inside y le C , 1  i  mg and let R be the remaining segments. Then
no two segments in L resp. R interla e be ause interla ing segments have to be
embedded on di erent sides of C . Hen e IG (C ) is bipartite. Finally, assume that
S (e0 ) is strongly planar. Consider any strongly planar embedding of S (e0 ), i.e.,
tree path w0 ! w1 ! w2 !    ! wr borders the outer fa e. Then no segment S (ei ),
1  i  m, whi h is embedded outside C an have an atta hment in fw1 ; : : : ; wr 1 g
and hen e fw1 ; : : : ; wr 1 g \ SS(e)2R A(e) = ;.
\(": The proof of this dire tion is postponed. It will be given in Lemma 9.
Lemma 7 suggests an algorithm for testing strong planarity. In order to test strong
planarity of a segment S (e0 ), test strong planarity of the segments S (ei ), 1  i  m,
onstru t the interla ing graph and test for the onditions stated in Lemma 7.
Unfortunately, the size of the interla ing graph might be quadrati and therefore
we annot a ord to onstru t the interla ing graph expli itly. Rather, we ompute
the onne ted omponents (and their partition into left and right side) of IG (C )
and an embedding of S (e0 ) = C + S (e1 ) +    + S (em ) by onsidering segment by
segment. We start with y le C and then try to add segment by segment. We will
onsider the segments S (e1 ); : : : ; S (em ) in an order su h that adding a anoni al
embedding of S (ei+1 ) to an embedding of C + S (e1 ) +    + S (ei ) an always be
a hieved (if at all) in a parti ularly simple way, namely by moving some of the
S (el ), l  i, to the other side of C and then adding S (ei+1 ) inside C and lose
to the tree path underlying C , f. Figure 111. In that gure the segment S (ei+1 )
emanates from wj , ei+1 = (wj ; y) and z = min A(ei+1 ) is the lowest atta hment
of S (ei+1 ). Also, there is a fa e F inside C su h that the tree path from z to wj
is on the boundary of F . Clearly, a anoni al embedding of S (ei+1 ) an be added
inside F to the embedding of C + S (e1 ) +    + S (ei ) in this ase.
In order to follow this embedding strategy we should rst onsider all segments
emanating from wk , then all segments emanating from wk 1, : : : . For any node
wj we onsider the segments emanating from wj in the order of lowest atta hment,
onsidering the segments with lower atta hment rst. Among the segments emanating from wj and having the same lowest atta hment, say wi with i < j , we
rst onsider the segments having only wi and wj as atta hments and then all the
others (there an be at most two segments of the latter kind be ause any two su h
segments interla e). We will now show how to ompute this ordering on the edges
Version: 19.10.99

Time: 11:01

{97{

98

Chapter 4. Algorithms on Graphs

emanating from C . We do so by showing how to reorder the adja en y list of ea h


node su h that the order of the adja en y list orresponds to the order de ned
above. For every node v let


lowpt [v = min(fvg [ fz ; v ! w ! z

for some w 2 V g); and


 w ! z for some w 2 V and z 6= lowpt [vg):
lowpt2 [v = min(fvg [ fz ; v !
T
B
T

lowpt [v is the lowest node rea hable from v by a sequen e of tree edges followed by
one ba k edge. Sin e G is assumed to be bi onne ted we have lowpt [v < v for all
v 6= 1. lowpt2 [v is the se ond lowest node rea hable from v in this way, if there is
one. The default value for both fun tions is v. The fun tions lowpt and lowpt2 are
easily omputed during dfs sin e

and

lowpt [v = min(fvg [ fz ; (v; z ) 2 B g [ flowpt [w; (v; w) 2 T g)


lowpt2 [v = min(fvg [ fz ; (v; z ) 2 B

and z 6= lowpt [vg


[ flowpt [w; (v; w) 2 T; lowpt [w 6= lowpt [vg
[ flowpt2 [w; (v; w) 2 T g):

These equations suggest to ompute lowpt and lowpt2 by two separate appli ations
of dfs . In the rst appli ation of dfs one omputes lowpt and in the se ond appli ation one omputes lowpt2 using lowpt . We leave it to the reader to show that one
dfs su es to ompute both fun tions. For an edge e = (wj ; y) let
lowpt [e = if e 2 B then y else lowpt [y fi:

Then lowpt [e = min A(e) and jA(e)j  3 i e 2 T and lowpt2 [y < wj for any
edge e = (wj ; y) emanating from the y le C . We want to reorder the adja en y
list of wj su h that an edge e = (wj ; y) is before an edge e0 = (wj ; y0 ) if either
lowpt [e < lowpt [e0 or lowpt [e = lowpt [e0 and jA(e)j = 2 and jA(e0 )j  3. Let
: E ! N be de ned by
8
if (v; w) 2 B ;
<2  w
((v; w)) = 2  lowpt [w
if (v; w) 2 T and lowpt2 [w  v;
:
2  lowpt [w + 1 if (v; w) 2 T and lowpt2 [w < v.
Then reordering an adja en y list a ording to non-de reasing values of yields
the desired ordering of outgoing edges. We an do the reordering in linear time
by bu ket sort. Have 2n initially empty bu kets. Step through the edges of G one
by one and throw edge (v; w) into bu ket ((v; w)). After having done so we go
through the bu kets in de reasing order. When edge (v; w) is en ountered we add
(v; w) to the front of v's adja en y list.
Version: 19.10.99

Time: 11:01

{98{

4.10. Planar Graphs

99

In our example, the edges out of node 7 are ordered (7; 8); (7; 13); (7; 5) and the
edges out of node 11 are ordered (11; 12); (11; 7); (11; 8).
From now on, we assume that adja en y lists are reordered in the way des ribed
above. The reordering has the additional property that a y le C (e0 ) for a tree edge
e0 = (x; y) is very easy to nd. We start at node y and onstru t a path by always
taking the rst edge out of ea h node until a ba k edge is en ountered. This path
is a spine of C (e0 ), as is easily veri ed.
We now resume the dis ussion of how to deal with the interla ing graph. As
in Lemma 7, C = C (e0 ),
C = w0 ! w1 !    ! wk ! w0
T

and e0 = (wr ; wr+1 ) for some r. Let e1 ; : : : ; em be the edges leaving the spine of C
in order , i.e., the edges leaving wk are onsidered rst and for ea h wj the edges
are ordered as des ribed above. Let IG i(C ) be the subgraph of IG (C ) spanned by
S (e1 ); : : : ; S (ei ). If IG i (C ) is non-bipartite then so is IG (C ) and hen e S (e0 ) is not
strongly planar. If IG i (C ) is bipartite then every onne ted omponent (= blo k)
of IG i(C ) is. If B is a blo k of IG i(C ) then we use LB , RB to denote the partition
of B indu ed by the bipartite graph.
Our next goal is to des ribe how the blo ks of IG i+1 (C ) an be obtained from
the blo ks of IG i(C ). Let ei+1 = (wj ; y). For every blo k B of IG i(C ) let
ALB = fwh ;

0  h < j and wh 2 A(e) for some S (e) 2 LB g

be the set of atta hments (below wj ) of segments in LB . ARB is de ned similarly.


Lemma 8. If IG i (C ) is bipartite, then:
a) There is some ordering of the blo ks of IG i (C ), say B1 ; B2 ; : : : ; Bh ; Bh+1 ; : : :
su h that

max(ALBl [ ARBl )  min(ALBl+1 [ ARBl+1 )


for 1  l < h and ALBl = ARBl = ; for l > h.
b) IG i+1 (C ) is bipartite i for all l, 1  l  h, either max ALBl  min A(ei+1 ) or
max ARBl  min A(ei+1 ).
) If IG i+1 (C ) is bipartite then the blo ks of IG i+1 (C ) an be obtained as follows: Assume w.l.o.g. that max ALBl  min A(ei+1 ) for all l. (This an

always be a hieved by inter hanging LB and RB for some blo ks B .) Let


d = min(fl; max ARBl > min A(ei+1 )g[fh +1g). Then the blo ks of IG i+1 (C )
are B1 ; : : : ; Bd 1 ; Bd [    [ Bh [ fS (ei+1 )g; Bh+1 ; : : : .
d) If IG i+1 (C ) is bipartite and S (el ), 1  l  i + 1, are strongly planar then
there
su h that all segments
S is a planar embedding of C + S (e1 )+  + S (ei+1 ) S
in l LB l are embedded inside C and all segments in l RB l are embedded
outside C .

Version: 19.10.99

Time: 11:01

{99{

100

Chapter 4. Algorithms on Graphs

Proof : We use indu tion on i. For i = 0 little remains to be shown. IG 0 (C ) is


empty and IG 1(C ) onsists of a single node. This shows a), b) and ). For part d)
we only have to observe that S (e1 ) an be embedded inside as well as outside C , if
S (e1 ) is strongly planar.
So let us turn to the ase i > 0. We will show parts b), ), a) and d) in this order.
b) \)": Note rst that it su es to show the following
Claim 1. If max ALBl > min A(ei+1 ) for some l then there is a segment S (e) 2 LB l
su h that S (e) and S (ei+1 ) interla e.
Suppose that we have shown Claim 1. If there were l, 1  l  h, su h that
max ALBl > min A(ei+1 ) and max ARBl > min A(ei+1 ) then S (ei+1 ) interla es
with a segment S (e) 2 LB l and a segment S (e0 ) 2 RB l by Claim 1. Sin e S (e)
and S (e0 ) belong to the same blo k there is a path from S (e) to S (e0 ) in IG i (C ).
Sin e IGi(C ) is bipartite this path ne essarily has odd length. Together with edges
fS (e); S (ei+1 )g and fS (ei+1 ); S (e0 )g we obtain an odd length y le in IG i+1 (C ).
Hen e IG i+1 (C ) is non-bipartite, a ontradi tion. We still have to show Claim 1.
Proof of Claim 1 : Let z = min A(ei+1 ). Sin e max ALBl > z there must be a
+
+
segment S (e) 2 LB l su h that w 2 A(e) for some w with z !
w!
wj . Edge e
T
T
emanates from node wp for some p  j .
Case 1 : p > j .
+
+
+
Then z !
w!
wj !
wp , z; wj 2 A(ei+1 ) and w; wp 2 A(e). Hen e segments S (e)
T
T
T
and S (ei+1 ) interla e ( f. Figure 107).

wp
wj S (e)
S (ei+1 )

w
z

Figure 107.
Case 2 : p = j .

Case 1

Let e = (wj ; u). Sin e e is onsidered before ei+1 and hen e min A(e)  z, edge
annot be a ba k edge. (If it were a ba k edge then min A(e) = u = w > z, a
ontradi tion.) Hen e e is a tree edge and min A(e) = lowpt [u.

Version: 19.10.99

Time: 11:01

{100{

4.10. Planar Graphs

101

Case 2.1 : lowpt [u < z .


+
+
+
Then lowpt [u !
z!
w!
wj , lowpt [u; w

2 A(e) and z; wj 2 A(ei+1 ). Hen e segments S (e) and S (ei+1 ) interla e ( f. Fig. 108).
T

wj

S (ei+1 )

w
S (e)

z
lowpt [u

Figure 108.

Case 2.1

Case 2.2 : lowpt [u = z .

Sin e w 2 A(e) we have lowpt2 [u < wj . Sin e e is onsidered before ei+1 we must
have jA(ei+1 )j  3, and hen e edge ei+1 annot be a ba k edge. Rather, it must
be a tree edge and we must have lowpt2 [y < wj . If lowpt2 [y 6= lowpt2 [u, say
+
+
+
+
lowpt2 [y ! lowpt2 [u, then we have z ! lowpt2 [y ! lowpt2 [u ! wj , z; lowpt2 [u 2
T
T
T
T
A(e), and lowpt2 [y; wj 2 A(ei+1 ). Hen e S (ei+1 ) and S (e) interla e ( f. Fig. 109).
If lowpt2 [y = lowpt2 [u then A(e) and A(ei+1 ) have three points in ommon and
hen e S (ei ) and S (ei+1 ) interla e ( f. Figure 110).
\(": Assume now that max ALBl  min A(ei+1 ) or max ARBl  min A(ei+1 ) for
all l, 1  l  h. By inter hanging LB l and RB l , if ne essary, we an a hieve that
max ALBl  min A(ei+1 ) for all l, 1  l  h.
S
Claim 2. Let S (e) 2 l LB l be arbitrary. Then S (e) and S (ei+1 ) do not interla e.
 w!
 w g and A(e)  fw; w !
 min A(e ) or
min A(ei+1 ) !
j
i+1
T
T
T

wj ! wg. Hen e S (e) and S (ei+1 ) do not interla e.
T
Proof : A(ei+1 )  fw;

The bipartiteness of IG i+1(C ) now follows from Claim 2 be ause it is safe to add
S (ei+1 ) to the \left side" of the interla ing graph.
Version: 19.10.99

Time: 11:01

{101{

102

Chapter 4. Algorithms on Graphs

y
wj
S (ei+1 )

lowpt2 [u
lowpt2 [y

S (e)

z lowpt [u
Figure 109.

Case 2.2, lowpt2 [y 6= lowpt2 [u

y
wj

S (ei+1 )
lowpt2 [y lowpt2 [u

S (e)

z lowpt [u
Figure 110.

Case 2.2, lowpt2 [y = lowpt2 [u

) Assume that IG i+1(C ) is bipartite. Then for all l, 1  l  h, max ALBl 


min A(ei+1 ) or max ARBl  min A(ei+1 ) by part b). By inter hanging LB l and
RB l , if ne essary, we an a hieve max ALBl  min A(ei+1 ) for all l, 1  l  h. Let
d = min(fl; max ARBl > min A(ei+1 )g [ fh + 1g).
Claim 3. For all l: There is a segment S (e) 2 RB l su h that S (e) and S (ei+1 )
interla e i d  l  h.
Version: 19.10.99

Time: 11:01

{102{

4.10. Planar Graphs


Proof :

103

\(": Let d  l  h. Then


min A(ei+1 ) < max ARBd
 max ARBl
< wj

[by de nition of d
[by indu tion hypothesis, part a) and d  l
[sin e l  h

and hen e there is a segment S (e) 2 RB l su h that S (e) and S (ei+1) interla e by
Claim 1.
 wg
\)": (Indire t.) Let l < d or l > h and let S (e) 2 RB l. Then A(e)  fw; wj !
 w or w !
 min A(e )g if l < d. TheT forif l > h and A(e)  fw; wj !
i+1
T
T
mer in lusion follows from the de nition of h, the latter in lusion follows from
the de nition of d, and
part a) of the indu tion hypothesis. Also A(e ) 
 w g and hen e S (e) and S (e ) do not interla e. i+1
fw; min A(ei+1 ) !
w!
j
i+1
T
T
We on lude from Claims 2 and 3 that S (ei+1 ) is onne ted to segments in blo ks
Bd ; : : : ; Bh . Hen e the blo ks of IG i+1 (C ) are B1 ; : : : ; Bd 1 ; Bd [    [ Bh [
fS (ei+1 )g; Bh+1 ; : : : . Let B = Bd [    [ Bh [ fS (ei+1 )g Sbe the new blo k. Then
B an be partitioned into LB and RB where LB = dlh LB l [ fS (ei+1 )g
and RB = Sdlh RB l . Moreover, if d  h, max ARBd  min ARBd+1 

max ARBd+1      min ARBh  max ARBh by part a) and max ALBd 
min ALBd+1  max ALBd+1      min ALBh  max ALBh  min A(ei+1 ) by
part a) and the assumption that max ALBl  min A(ei+1 ) for all l, 1  l  h.
a) Follows immediately from part ). The ordering of the blo ks of IG i+1 (C ) given
in part ) satis es the onditions required in part a). This follows immediately from
the dis ussion ompleting the proof of part ).
d) Assume that IG i+1 (C ) is bipartite and that S (el ), 1  l  i + 1, are strongly
planar. Let B10 , B20 , : : : be the blo ks of IG i+1(C ). By part ) we have B10 = B1 ,
: : : , Bd0 1 = Bd 1 , Bd0 = Bd [    [ Bh [ fS (ei+1 )g, Bd0 +1 = Bh+1 , : : : , where B1 ,
B2 , : : : are the blo ks of IG i (C ). Moreover, LB 0l = LB l ,SRB 0l = RB l for l < d,
LB 0d+l = LB h+l , RB 0d+l = RB h+l for l  1 and LB 0d = dlh LB l [ fS (ei+1 )g
and RB 0d = Sdlh RB l . By indu tion hypothesis Sthere is a planar embedding of
C + S (e1 ) +    + S (ei ) su h that all segments in l LB l are embedded inside C
and all segmentsSin Sl RB l are embedded outside C . By the proof of Claim 2 no
segment S (e) 2 l LB l has an atta hment w whi h lies stri tly between min A(ei+1 )
and wj . Thus there is a fa e F inside C su h that the tree path from min A(ei+1 ) to
wj is part of the boundary of F . All atta hments of S (ei+1 ) lie between min A(ei+1 )
and wj in lusively. Moreover, S (ei+1 ) is strongly planar and hen e there is a planar
embedding of S (ei+1 ) where the tree path from min A(ei+1 ) to wj borders the outer
fa e. We an add this embedding to the embedding of C + S (e1 ) +    + S (ei ) by
putting it inside fa e F . In this way we obtain a planar embedding of C + S (e1 ) +
   + S (ei+1 ) ( f. Fig. 111). This ompletes the proof of Lemma 8.
Version: 19.10.99

Time: 11:01

{103{

104

Chapter 4. Algorithms on Graphs

S (ei+1 )

wj
F

C
wj

wj

+
min A(ei+1 )

Figure 111.

min A(ei+1 )

min A(ei+1 )

Addition of S (ei+1 ) inside F

Lemma 9. The if-part of Lemma 7 holds.


Proof :

If IG (C ) is bipartite and S (ei ), 1  i  m, is strongly planar then by


Lemma 8d) thereSis an embedding of C + S (e1 ) +    + S (em) = S (e0S) su h that
all segments in i LB i are embedded inside C and all segments in i RB i are
embedded outside C . In parti ular, S (e0 ) is planar. Assume now that in addition
ALBl \ fw1 ; : : : ; wr 1 g = ; or ARBl \ fw1 ; : : : ; wr 1 g = ; for all l where ALBl
and ARBl are de ned with j = r + 1, i.e., when all edges e1 ; : : : ; em are embedded.
We may assume w.l.o.g. (by inter hanging L and R for some blo ks) that ARBl \
fw1 ; : : : ; wr 1 g = ; for all l. Thus outside C there are no atta hments to nodes
w1 ; : : : ; wr 1 and hen e there is a fa e F outside C su h that the stem w0 ; : : : ; wr
of S (e0 ) borders F . Lemma 1 allows us to turn F into the outer fa e and yields a
anoni al embedding of S (e0 ).
We illustrate Lemma 9 on our example. Let C be the y le whi h runs from node 1 to
node 9 along tree edges and then ba k to node 1. There are four segments emanating
from this y le: S1 = S ((9; 10)), S2 = S ((7; 13)), S3 = S ((7; 5)) and S4 = S ((6; 4)).
All four segments are strongly planar. When segment S2 = S ((7; 13)) is onsidered,
we have: IG 1 (C ) has one blo k B1 onsisting of segment S1 . Say S1 belongs to
RB 1 . Then ALB1 = ; and ARB1 = f5g. Lemma 8b) is satis ed and hen e IG 2 (C )
is bipartite. We have d = 1 in Lemma 8 ) and hen e IG 2(C ) has only blo k B1 ,
say LB 1 = fS2 g and RB 1 = fS1 g. Then ALB1 = f3; 4g and ARB1 = f5g when S3
is onsidered. IG 3(C ) is bipartite and has two blo ks B1 and B2, say LB 1 = fS2 g,
RB 1 = fS1 g, RB 2 = fS3 g. Then ALB1 = f3; 4g, ARB1 = f5g, ARB2 = f5g,
ALB2 = ; when S4 is onsidered. S4 for es us to merge blo ks B1 and B2 , i.e., d = 1
in Lemma 8 ), and hen e IG 4 (C ) has only one blo k B1. Moreover LB 1 = fS2 ; S4 g
and RB 1 = fS1 ; S3 g.
It is now easy to derive an e ient way of dealing with the interla ing graph
from Lemma 8. Suppose that we pro essed edges e1 ; : : : ; ei already and want to
pro ess edge ei+1 next. At this point we keep blo ks B1 ; : : : ; Bh in a sta k S where
Version: 19.10.99

Time: 11:01

{104{

4.10. Planar Graphs

105

h is de ned as in Lemma 8a).

Also for ea h l, 1  l  h, we maintain the multi-sets


and ARBl in a doubly linked list. The lists ALBl and ARBl are ordered
a ording to DFS-numbers. From the sta k position orresponding to Bl we have
pointers to the front and ba k end of lists ALBl and ARBl . The test for bipartiteness
of IG i+1(C ) given in Lemma 8b) is now easily implemented by Program 31.

ALBl

l h + 1;
while max(ALBl 1 [ ARBl 1 ) > lowpt [ei+1
do if ALBl 1 is non-empty and max ALBl 1 > lowpt [ei+1
then inter hange LB l 1 and RB l 1 fi;
if ALBl 1 is non-empty and max ALBl 1 > lowpt [ei+1
then IG i+1 (C ) is not bipartite and hen e
the graph an be de lared non-planar fi;
l l 1
od;
d l.
Program 31

The running time of Program 31 is learly O(h d + 2). Also, it orre tly
omputes d as de ned in Lemma 8 ). The new blo ks of IG i+1 (C ) are now easily
formed by Program 32.
ALB

ARB

;;

for l from d to h
do ALB ALB on atenated with ALBl ;
od;

ARB

ARB on atenated

with ARBl

with (A(ei+1 ) fwj g);


pop Bh; : : : ; Bd from sta k S ;
add B to sta k S .
ALB

ALB on atenated

Program 32

Again the running time of Program 32 is learly O(h d + 2) provided we are


given (A(ei+1 ) fwj g). Also, it orre tly omputes lists ALB and ARB. Note that
these lists are ordered a ording to the remark at the end of the proof of Lemma 9 ).
We an now give the omplete planarity testing algorithm, see Program 33.
Lemma 10. Program 33 tests strong planarity in linear time and spa e.
Proof :

Observe rst that line (1) determines the spine of y le C (e0 ) in time proportional to the length of the spine. The stem w0 ; : : : ; wr is not expli itly onstru ted; we only mention it in order to keep the same notation as in Lemmas 7
Version: 19.10.99

Time: 11:01

{105{

106
(0)
(1)

(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)
(20)
(21)
(22)
(23)
(24)
(25)

Chapter 4. Algorithms on Graphs

pro edure stronglyplanar (e0 : edge);


o tests whether segment S (e0 ), e0 = (x; y), is strongly planar.

If so, it returns the ordered (a ording to dfsnum ) list of


atta hments of S (e0 ) ex luding x o
nd the spine of y le C (e0 ) by starting in node y and always
taking the rst edge on every adja en y list until a ba k edge is
en ountered. This ba k edge leads to node w0 = lowpt [y.
Let w0 ; : : : ; wr be the tree path from w0 to x = wr and
and let wr+1 = y; : : : ; wk be the spine onstru ted above;
let S be an empty sta k of blo ks;
for j from k downto r + 1
do for all edges e0 (ex ept the rst) emanating from wj
do stronglyplanar (e0 );
let A(e0 ) be the ordered list of atta hments of S (e0 )
as returned by the su essful all stronglyplanar (e0 );
update sta k S as des ribed in Programs 31 and 32
od;
let Bh be the top entry in sta k S ;
while max(ALBh [ ARBh ) = wj 1
do remove node wj 1 from ALBh and ARBh ;
if ALBh and ARBh be ome empty
then pop Bh from the sta k; h h 1 fi
od
od;
o if ontrol rea hes this point then IG (C ) is bipartite.
We will now test for strong planarity and ompute A(e0 ) o
L ;;
o an empty list o
for l from 1 to h
do if max ALBl  w1 and max ARBl  w1
then de lare S (e0 ) not strongly planar and stop fi;
if ALBl 6= ; and max ALBl  w1
then L L on ARBl on ALBl
else L L on ALBl on ARBl fi
od;
return L
end.
Program 33

and 9. Next we argue that bipartiteness of IG (C ) is tested orre tly. The orre tness of loop (4){(8) is obvious from the dis ussions above. Suppose now that
we pro essed all edges emanating from wj . In order to prepare for pro essing the
edges emanating from wj 1 we only have to delete all o urren es of wj 1 on lists
ALBl and ARBl . This is done in lines (9){(14). Note that all o urren es of wj 1
Version: 19.10.99

Time: 11:01

{106{

4.10. Planar Graphs

107

must be in the top entries of sta k S by Lemma 8a). Hen e lines (9){(14) work
orre tly. When ontrol rea hes line (16) the interla ing graph IG (C ) is bipartite
and hen e S (e0 ) is planar. Moreover, for every blo k B in the sta k S the lists
ALB and ARB ontain exa tly the atta hments below wr of segments in the blo k.
In line (18) we now test the ondition for strong planarity given
S in Lemma 7. It
states that for all blo ks
B
of
IG (C ) either fw1 ; : : : ; wr 1 g \ S(e)2LB A(e) = ;
or fw1 ; : : : ; wr 1g \ SS(e)2RB A(e) = ;. Of ourse, we an always inter hange L
and R su h that the former is the ase. It remains to argue that lines (20)
to (22) orre tly ompute the ordered set A(e0 ) fxg of atta hments. Let l0
be minimal su h that max(ALBl0 [ ARBl0 )  w1 . Then ALBl [ ARBl  fw0 g
for l < l0 and either ALBl0  fw0 g or ARBl0  fw0 g by line (18). Also
min(ALBl [ ARBl )  max(ALBl 1 [ ARBl 1 )  max(ARBl0 [ ALBl0 ) for l > l0 by
Lemma 8a) and hen e either ALBl = ; or ARBl = ; for l > l0 by line (18). Thus
lines (20) to (22) work orre tly and the orre tness proof is omplete.
We still have to analyze the running time. Note rst that stronglyplanar is
alled at most on e for ea h edge. Also, ea h tree edge belongs to exa tly one
spine. Hen e the total time spent in lines (1), (2), (3), (4), (5) (without ounting
the time spent within re ursive alls), (6), (8), (9) and (16) is O(m). Let us look at
line (7) next. Observe that line (7) is exe uted at most on e for ea h edge. Also,
at most one blo k is pushed on sta k S in one exe ution of line (7), and exe ution
time of line (7) is proportional to the number of entries removed from sta k S .
Sin e only m elements are added to sta ks S altogether, only m elements an be
removed and hen e the total time spent in line (7) is O(m). The same argument
shows that the total time spent in lines (17){(23) is O(m), be ause the time spent
in these lines is proportional to the number of elements removed from sta ks S in
these lines. Lines (10){(14) still remain to be onsidered. Only endpoints of ba k
edges are pla ed on lists ALB and ARB. No ba k edge is pla ed twi e on a list and
ea h ba k edge is removed at most on e. Hen e the total ost of lines (10){(14)
is O(m).
Theorem 2. Let G
time O(n).

= (V; E ) be a graph.

Then planarity of G an be tested in

Proof :

If m > 3n 6 then G is non-planar. If m  3n 6 then we an divide


G into its bi onne ted omponents in time O(m) = O(n). For ea h bi onne ted
omponent we an test its planarity in linear time. Also, a graph is planar i its
bi onne ted omponents are planar.
At this point we have developed an O(n) algorithm for testing planarity. Suppose
now that G = (V; E ) is a planar graph. Does a su essful planarity test also tell
us something about a planar embedding? We show that it does; more spe i ally,
we show how to extend the planarity test su h that it omputes a ombinatorial
embedding. Re all that a graph G = (V; E ) together with a y li ordering  of
Version: 19.10.99

Time: 11:01

{107{

108

Chapter 4. Algorithms on Graphs

the edges in ident to any node v 2 V is alled a planar map (or ombinatorial
if there is a planar embedding of G su h that the y li ordering 
agrees with the lo kwise ordering of the edges in the embedding. Figure 112 shows
a planar map (think of the adja en y lists as ir ular lists) and a orresponding
embedding.
4

embedding)

1: (1,2), (1,3), (1,4)


2: (2,4), (2,3), (2,1)
3: (3,1), (3,2), (3,4)
4: (4,2), (4,1), (4,3)

2
1

3
Figure 112. Planar map and its embedding
We now show how the planarity testing algorithm an be used to turn a planar graph into a planar map. Let G = (V; E ) be a planar graph. Consider an
appli ation of the planarity testing algorithm to graph G. Let C = C (e0 ) be
the y le asso iated with some edge e0 and let e1 ; : : : ; em be the edges emanating from the spine path. The planarity testing algorithm omputes the blo ks
(and their partition into sides) of IG (C 0). More pre isely, it omputes a mapping
: fS (e1 ); : : : ; S (em )g ! fL; Rg su h that no two segments with the same label
interla e and su h that ( f. Lemmas 8d and 9) there is a anoni al embedding of
S (e0 ) with pre isely the segments S (ei ) with (S (ei )) = L, embedded inside C .
The mapping an be omputed as follows. Let B be the blo k of IG (C ) whi h
ontains S (e). Our pro edure stronglyplanar omputes B iteratively. The onstru tion of B is ertainly ompleted when B is popped from sta k S . Let (S (e)) = R
if S (e) 2 RB at that moment and let (S (e)) = L otherwise. With this extension,
algorithm stronglyplanar omputes mapping in linear time.
Suppose now that we have omputed the mapping and run algorithm stronglyplanar
again. We an then avoid all ipping of sides by embedding the segments S (ei) as
pres ribed by . More pre isely, when (S (ei )) = L then we add a anoni al embedding of S (ei ) inside C and when (S (ei )) = R then we add a reversed anoni al
embedding of S (ei ) outside C to the embedding of C + S (e1 )+  + S (ei 1 ). Finally,
we turn the obtained embedding of S (e0 ) = C + S (e1 )+  + S (em) into a anoni al
embedding of S (e0 ) by the onstru tion of Lemma 1 ( f. the proof of Lemma 10).
Note that the onstru tion of Lemma 1 does not hange the ombinatorial embedding.
Similarly, a reversed anoni al embedding of S (e0 ) an be determined by inter hanging the roles of L and R, i.e., if (S (ei )) = L then a reversed anoni al
embedding of S (ei ) is added outside C and if (S (ei )) = R then a anoni al embedding of S (ei ) is added inside C .
In summary, we have shown that a anoni al or reversed anoni al embedding
of S (e0 ) an be onstru ted by adding appropriate embeddings of the segments S (ei )
Version: 19.10.99

Time: 11:01

{108{

4.10. Planar Graphs

109

as dire ted by the mapping . The remaining question to be addressed is whether


a anoni al or reversed anoni al embedding of S (e0 ) is needed in the embedding
of G. Let S be the set of edges for whi h stronglyplanar (e) is alled. De ne the
type t(e) 2 f anoni al ; reversed anoni al g as follows. If e = (1; 2) then t(e) =
anoni al . If e 2 S; e 6= (1; 2), let e0 2 S be su h that stronglyplanar (e) is alled by
stronglyplanar (e0 ). Then t(e) = anoni al if either t(e0 ) = anoni al and (S (e)) =
L or t(e0 ) = reversed anoni al and (S (e)) = R, and t(e) = reversed anoni al
otherwise. The signi an e of the type t(e) of an edge e lies in the fa t that the
indu ed embedding of the subgraph S (e) in a anoni al embedding of S ((1; 2)) = G
is a t(e) embedding for all edges e 2 S . It is lear that the types t(e) for e 2 S an
be omputed in linear time.
We an no extend pro edure stronglyplanar su h that it omputes a planar
map orresponding to a anoni al embedding of S ((1; 2)) as follows. We start with
an embedding of the y le C ((1; 2)). A all stronglyplanar (e), e 2 S f(1; 2)g
adds the spine of C (e) and the ba k edge belonging to C (e) to the embedding.
Let wr ; wr+1 ; : : : ; wk ; w0 ) be the spine followed by the ba k edge (wk ; w0 ). If
t(e) = anoni al (reversed anoni al ) then the dart (wr ; wr+1 ) is inserted immediately after (before) the dart (wr ; parent [wr ) and into the lo kwise ordering of
edges around wr and the dart (w0 ; wk ) is insert immediately before (after) the dart
(w0 ; a tive edge [w0 ) into the lo kwise ordering of darts around w0 ; also the nodes
wi , r + 1  i  k, and the two in ident edges are added to the planar map. Here,
parent is a pre omputed array whi h ontains for ea h node w 6= 1 the parent of w
in the tree T , i.e., parent [w = v i (v; w) 2 T , and a tive edge is an array with
a tive edge [w = e if e = (v; w) 2 T and the edge e0 2 S for whi h stronglyplanar (e0 )
is urrently a tive starts in V (e), and a tive edge [v = nil otherwise. It is lear that
the array parent an be pre omputed in linear time and the array a tive edge an be
maintained in linear time. It is also lear, that adding the path wr ; wr+1 ; : : : ; wk ; w0
to the planar map takes time proportional to the number of edges added and hen e
a planar map an be omputed in linear time. we summarize in
Theorem 3. Let G = (V; E ) be a planar graph. Then G an be turned into a
planar map (G; ) in linear time.

In our example we have S = f(1; 2); (9; 10); (12; 10); (11; 7); (11; 8); (7; 13); (13; 4);
(7; 5); (6; 4)g, ((6; 4)) = ((7; 13)) = L, ((9; 10)) = ((7; 5)) = R, ((12; 10)) =
R, (11; 8)) = ((11; 7)) = L (13; 4)) = L, t((1; 2)) = t((6; 4)) = t((7; 13)) =
t((13; 4)) = t((12; 10)) = anoni al and t((9; 10)) = t((7; 5)) = t((11; 8)) =
t((11; 7)) = reversed anoni al . When edge (7; 5) is added to the embedding, we
have a tive edge [5 = (5; 6) and hen e (7; 5) is inserted before (7; 6) in the order
around 7 and (5; 7) is inserted after the dart (5; 6) in the order around 5. Altogether the planar map shown in Figure 113 is onstru ted.
A planar map is still a ombinatorial obje t, it is not yet a drawing of a graph.
We will now show how to produ e drawings, in fa t we show how to ompute
drawings where nodes are mapped into grid points and edges are mapped into
Version: 19.10.99

Time: 11:01

{109{

110

Chapter 4. Algorithms on Graphs

9
8
13

7
6
5

10
11
12

4
3
2
1
Figure 113. The planar map onstru ted for the graph of Figure 106

straight-line segments, ( f. Fig. 114). Su h an embedding is alled a straight-line


or Fary embedding.
Version: 19.10.99

Time: 11:01

{110{

4.10. Planar Graphs

5
12
10
9

1
Figure 114.

111

3
4
6

11
7

13

A straight-line embedding of the graph of Figure 115

Theorem 4. Any planar graph with n nodes has a straight-line embedding into
the 2n 4 by n 2 grid, i.e., verti es are mapped into elements of f0; : : : ; 2n
4g  f0; : : : ; n 2g and edges are mapped into straight-line segments. Also, su h
an embedding an be onstru ted in time O(n log n).

Let G be a planar graph. We may assume w.l.o.g. that we have a ombinatorial


embedding of G and that G is triangulated, i.e., every fa e is a triangle. Note that a
ombinatorial embedding an be omputed in linear time by Theorem 3 and that the
obtained planar map an be triangulated in linear time by subdividing fa es with
more than three verti es ( f. Exer ise 33?). Figure 115 shows a triangulation of the
planar map of Figure 113. Our strategy for omputing a straight-line embedding
works iteratively, i.e., we start with a single triangle and then add node after node.
The basis for the iteration is the following Lemma 11.
Lemma 11. Let (G; ) be a triangulated planar map with outer fa e u; v; w. Then
there is a labelling v1 = u; v2 = v; v3 ; v4 ; : : : ; vn = w of the nodes meeting the
following requirements for every k, 4  k  n.
(1) The subgraph Gk 1  G indu ed by v1 ; v2 ; : : : ; vk 1 is bi onne ted, and the
boundary of its outer fa e is a y le Ck 1 ontaining the edge fu; vg.
(2) The nodes of G lying inside or on the y le Ck 1 are exa tly the verti es
v1 ; : : : ; vk 1 .
(3) vk is in the outer fa e of Gk 1, and its neighbors in Gk 1 form an (at least
2-element) subinterval of the path Ck 1 fu; vg ( f. Fig. 116).
Version: 19.10.99

Time: 11:01

{111{

112

Chapter 4. Algorithms on Graphs

9
8

10
11

13

12

5
4
3
2

1
Figure 115. A triangulation of the planar map of Figure 113. The
additional edges are drawn light.
Moreover the labelling an be omputed in linear time.

vk

Gk
u

The vertex vk is atta hed to a subinterval of the path Ck 1 fu; vg


Proof : Let v3 be the unique vertex su h that the triangle u; v; v3 is a bounded fa e
of G. Then (1) and (2) hold for k  4. Assume indu tively that v1 ; : : : ; vk 1 have

Figure 116.

Version: 19.10.99

Time: 11:01

{112{

4.10. Planar Graphs

113

been de ned and that (1) and (2) hold for the subgraph Gk 1. Let w1; : : : ; wr ; w1
with w1 = u, wr = v be the y le Gk 1 and let K = fz; z 2= fv1 ; : : : ; vk 1 g
and z is adja ent to at least two verti es on the y le Ck 1g. For z 2 K let
min(z) = minfi; wi is a neighbor of zg, max(z) = maxfi; wi is a neighbor of
z g and let C (z ) be the y le z; wmin(z) ; wmin(z)+1 ; : : : ; wmax(z) ; z . Let z0 2 K be
su h that the y le C (z0 ) ontains a minimal number of nodes in its interior. We
laim that withe vk = z0 requirement (3) is met and that (1) and (2) hold for the
subgraph Gk .
Note rst that K 6= ; sin e for every edge ei = fwi ; wi+1 g, 1  i < r, there
is a unique node z(ei ) outside Ck 1 su h that the triangle wi; wi+1 ; z(ei ) is a fa e
of G. The nodes z(ei ) belong to K , 1  i < r, and hen e K 6= ;. So z0 exists. Note
next that the y le C (z0 ) ontains no node in its interior be ause otherwise there
would be a node z 2 K in its interior su h that C (z) ontains even fewer nodes
in its interior. This also implies z0 6= w of k < n sin e min(w) = 1, max(w) = r
and hen e C (w) has non-empty interior if k < n. Finally, sin e G is triangulated
z0 must be onne ted to all nodes wi , min(z0 )  i  max(z0 ). This proves (3) and
also that (1) and (2) hold for Gk . Thus the disired labelling exists.
It an be omputed in linear time as follows. We maintain a partition of the
unlabelled nodes 6= w into lasses:
A: no neighbor labelled yet
B : exa tly one neighbor labelled
i: more than one neighbor labelled and the labelled neighbors form i intervals in
the y li ordering of edges around the node, i  1.
We start with only nodes u and v labelled and an initial partition whi h an ertainly be omputed in linear time. Assume now that we have determined nodes
v1 ; : : : ; vk 1 already. We then hoose any node in lass 1 and label it vk . Note that
lass 1 is never empty by the argument given in the existen e proof of the labelling.
Conversely, let z be any node in lass 1. Then ertainly z 2 K . Also, z is adja ent
to all nodes wi, min(z)  i  max(z), sin e z belongs to lass 1. Assume now
that one of the triangles z; wi ; wi+1 where min(z)  i  max(z) is not a fa e of G
and hen e ontains a node in its interior. Any node in the interior of this triangle
is unlabelled and one of the nodes must be adja ent to z sin e G is triangulated.
Thus z dose not belong to lass 1, a ontradi tion. After labelling vk we onsider
all unlabelled neighbors of vk and update their lass membership as follows. Let
z be any su h neighbor. If z belongs to lass A then it is moved to lass B . If z
belongs to lass B then it is moved to either lass 1 or lass 2 and if v belongs to
lass i then it is moved to either lass i 1 or i + 1 or stays in lass i. For ea h
neighbor z this de ision takes onstant time end hen e the time required to label vk
is proportional to the degree of vk . Thus the entire labelling is omputed in linear
time.
In the example of Figure 115, we may hoose u = 1, v = 2, w = 9 and use the
labelling v1 = 1, v2 = 2, v3 = 3, v4 = 4, v5 = 5, v6 = 6, v7 = 13, v8 = 7, v9 = 11,
v10 = 8, v11 = 12, v12 = 10 and v13 = 9.
Version: 19.10.99

Time: 11:01

{113{

114

Chapter 4. Algorithms on Graphs

P (wp ; wq )

wp+1
wp
w3
w2
w1 = v1 = u

Gk

wq
wp+2

wq

wq

wr = v2 = v
Figure 117. Gk

We an now des ribe the iteration in more detail. We start by pla ing v1 at
(0; 0), v2 at (2; 0) and v3 at (1; 1). Assume indu tively, that we embedded Gk as a
mountain range ( f. Fig. 118) with base fu; vg, i.e.,
(1) v1 is at (0; 0), v2 is at (2k 4; 0) and all points of Gk are embedded onto latti e
points of the rst quadrant;
(2) If w1 = v1 ; w2 ; : : : ; wr = v2 denote the verti es on the outer fa e of Gk (in the
order of their appearan e), denotes the x- oordinate of wi, then
x(w1 ) < x(w2 ) <    < x(wr );

(3) The line segments L(wi; wi+1 ), 1  i < r, all have slope +1 or 1.
Note that (3) implies that the Manhattan distan e between any two nodes wi and wj
of the outer fa e is even . (The Manhattan distan e of (x; y) and (x0 ; y0 ) is jx x0 j+
jy y0 j. Hen e, if i < j , then the interse tion of the line with slope +1 through wi
and the line with slope 1 through wj is a latti e point P (wi ; wj ).
Let wp ; wp+1 ; : : : ; wq be the neighbor of vk+1 in Gk+1 (1  p < q  r),
( f. part (3) of Lemma 11). The idea is to pla e the node vk at the latti e
point P (wp ; wq ). This may fail be ause e.g. wq may not be visible from that latti e point; f. Figure 117. To make sure that all nodes wp ; wp+1 ; : : : ; wq are visible
from P (wp ; wq ) we deform the embedding su h that the slope of the line segment
L(wp ; wp+1 be omes less than 1, the slope of the line segment L(wq+1 ; wq ) be omes larger than 1, and the slopes of all other line segments on the boundary
of the outer fa e remain the same. One way to a hieve this is to rst move nodes
wp+1 ; wp + 2; : : : ; wr one unit to the right and then to move nodes wq ; wq+1 ; : : : ; wr
one further unit to the right. However, in order to not destroy the straightline embedding of Gk 1 we may also have to move some other nodes of Gk .
For this purpose, we maintain for ea h node wi on the outer fa e of Gk a set
M (k; wi )  fv1 ; : : : ; vk g su h that
(4) wj 2 M (k; wi ) i j  i
Version: 19.10.99

Time: 11:01

{114{

4.10. Planar Graphs

115

(5) M (k; w1 )  M (k; w2 )      M (k; wr )


(6) For any sequen e 1 ; : : : ; r of non-negative numbers, if we sequentially translate all verti es in M (k; wi ) with distan e i to the right (i = 1; 2; : : : ; r),
then the embedding of Gk remains a Fary embedding. (Note that many verti es will move several times; e.g., all points in M (k; wi ) n M (k; wi+1 ) will be
translated by 1 + 2 +    + i.) For k = 3 these onditions are met by
straight-line embedding v1 7! (0; 0), v2 7! (2; 0), v3 7! (1; 1) and by the sets
M (3; v1 ) = fv1 ; v2 ; v3 g, M (3; v2 ) = fv2 ; v3 g, M (3; v3 ) = fv3 g. We an now des ribe how to embed node vk . We apply (6) with p+1 = q = 1 and all other
i = 0 to nd a new straight-line embedding of Gk . This assumes p +1 < q. If
p + 1 = q then we apply (6) with q = 2 and all other i = 0. The Manhattan
distan e between wp and the new lo ation of wq is still even, thus we an pla e
vk+1 at the interse tion of the lines with slope +1 and 1 through wq and
the new lo ation of wq , respe tively. Conditions (1), (2) and (3) will trivially
remain true for this new embedding of Gk+1. ( f. Fig. 118)
P (wp ; wq )

wp+1

wp
w3
w2
w1 = v1 = u

Gk+1

wq
wp+2

wq

wq

wr = v2 = v
Figure 118. Gk+1

The nodes of the outer fa e of Gk+1 are u = w1 ; w2 ; : : : ; wp ; vk+1; wq ; : : : ; wr =


v. For ea h member z of this sequen e we have to de ne a set M (k + 1; z ) 
V (Gk+1 ). Let
M (k + 1; wi ) = M (k; wi ) [ fvk+1 g for i  p;
M (k + 1; vk+1 ) = M (k; wp+1 ) [ fvk+1 g;
M (k + 1; wj ) = M (k; wj ) for j  q:

It is obvious that these sets have properties (4) and (5). To he k that property (6)
remains true as well onsider any sequen e of nonnegative numbers (w1 ); : : : ; (wp ); (vk+1 ); (
For all z on the outer fa e of Gk+1 translate the set M (k + 1; z) with distan e (z)
to the right. Observe that after this motion the part of Gk+1 below the polygon
w1 w2 : : : wm (i.e., Gk ) remains straight-line embedded (by ondition (6) applied
to Gk with 1 = (w1 ), : : : , p = 1 + (wp ), p+1 = 1 + (vk+1 ), q = 1 + (wq ),
Version: 19.10.99

Time: 11:01

{115{

116

Chapter 4. Algorithms on Graphs

q+1 = 1 + (wq+1 ), : : : , m = 1 + (wm ) and every other i = 0 if p + 1 < q and


1 = (w1 ), : : : , p = (wp ), q = 2 + (wk+1 ) + (wq ), q+1 = (wq+1 ), : : : ,
(wm ) if p +1 = q). On the other hand, it is easy to see that the part of Gk+1 above
w1 w2 : : : wm (i.e., vk+1 and the upper ontour of Gk ) remains straight-line embedded too, sin e during the motion the subgraph indu ed by wp+1 ; wp+2 ; : : : ; wq 1
and vk+1 moves rigidly (to a distan e (w1 ) +    + (wp ) + (vk+1 )). The nal
output of our algorithm is a straight-line embedding E of Gn = G satisfying onditions (1), (2) and (3) with k = n. This immediately implies that every point ov G is
embedded in some latti e pont of the triangle determined by E(v1 ) = E(u) = (0; 0),
E(v2 ) = E(v) = (2n 4; 0) and E(vn ) = E(w) = (n 2; n 2). This proves the

existen e of the desired embedding.


It is easy to derive an O(n2) algorithm from the onstru tive existen e proof.
A less dire t implementation whi h avoids the expli it onstru tion of embeddings
for the intermediate graphs a hieves a running time of O(n log n). We des ribe this
implementation next.
Let pk (l) = (xk (l); yk (l)) be the position of node vk in the embedding of Gl , k 
l. We are interested in pk (n) but in order to make the onstru tion work we deal with
the more general problem of omputing pk (l). learly, xk (l) = xk (k)+(xk (l) xk (k))
and yk (l) = yk (k), i.e., the position of vk in Gl is given by the position pk (k) of vk
in Gk and the shift distan e shift k (l) := xk (l) xk (k) of vk when passing from Gk
to Gl.
Lemma 12. The positions pk (k), pk (n), k = 1; 2; : : : ; n an be omputed in
time O(n) plus the time to ompute O(n) shift distan es.
Proof :

Clearly, knowing pk (k) and shift k (n) we an ompute pk (n). So we only


have to deal with the omputation of pk (k), k = 1; 2; : : : ; n. For node vk let
vi1 ; vi2 ; : : : ; vij be the neighbors of vk in Gk 1 in ounter lo kwise order; this nodes
these nodes were where alled wp ; wp+1 ; : : : ; wq in the existen e proof. Set rst k = i1 , se ond k = i2
and last k = ij and observe that the fun tions rst , se ond and last an be omputed
with no extra e ort when omputing the labelling; f. Lemma 11. Then pk (k) is
given by the interse tion of the line with slope +1 through p rst k (k) and the line
with slope 1 through plast k (k). Also, p rst k (k) is determined by p rst k ( rst k )
and shift rst k (k) and similarly for plast k (k). We on lude that the sequen e pk (k),
k = 1; 2; : : : ; n, an be omputed in linear time if the quantities shift rst k (k) and
shift last k (k), 4  k  n, are known.
For the omputation of shift distan es we rst derive an e onomi al en oding of the
sets M (l; vk ). De ne sequen es 2 ; 3 ; : : : ; n as follows. Let 2 = (1; 2) and obtain
k+1 from k by inserting k + 1 just to the left of se ond k and n + k + 1 just to the
left of last k where se ond k and last k are de ned as in the proof of Lemma 12.
Lemma 13.
Version: 19.10.99

Time: 11:01

{116{

4.10. Planar Graphs

117

a) Let vk be a vertex on the outer fa e of Gl . Then


vi 2 M (l; vk ) i i  l and k pre edes i in n
or k = i:
b)

shift k (l) = jfj ; k < j

 l or k < j n  l
and j pre edes k in n gj

Proof : a) We use indu tion on max(k; i). For max(k; i) = 2 the laim is obvious. So
let us suppose max(k; i) > 2 and k 6= i. Assume rst that i < k. Then vi 2 M (l; vk )
i vi 2 M (k 1; vse ond k ) by the de nition of M (k; vk ), and k pre edes i in n i
se ond k pre edes i in n by the de nition of n . So the laim follows dire tly
from the indu tion hypothesis. Assume next that i > k. Then i 2 M (i; vk ) i vk
pre edes vi on the boundary of Gi i vk pre edes vse ond i on the boundary of Gi 1
i k pre edes i in n and the laim is shown.
b) Consider the addition of a node vj , k < j  l. Then node vk is moved two
units to the right if vk 2 M (j 1; vlast j ) and vk is moved one unit to the right if
vk 2 M (j 1; vse ond j ) M (j 1; vlast j ). By part a) and the onstru tion of n
this is equivalent to j + n and j pre ede k in n and j but not j + n pre edes k
in n. This proves part b).
It is now easy to translate the omputation of shift distan es into a range query
problem. Let S be the following set of points
S = f(1; 1); (2; 2n 3)g [ f(k; n 1 (k)); (k; n 1 (n + k)); 3  k  ng
and let R(k; l) be the re tangle
R(k; l) = f(j; y); k < j  l and y  n 1 (k)g:
Then
jR(k; l) \ S j = jfj ; k < j  l or k < j n  l
and j pre edes k in ngj
= shift k (l):
A query of the form \determine the ardinality of the interse tion of a re tangle R
and a set S of points" is alled a re tangular range ounting query. Our re tangles
are 3-sided be ause there is no restri tion on y from below in the de nition of R(k; l).
In the se tion on segment trees in Chapter 8 it is shown that 3-sided re tangular
range ounting queries an be pre essed in time O(log N ) with a prepro essing time
of O(N log N ), where N = jS j. (In the rst edition, only O((log N )2 ) is shown).
We summarize in:
Version: 19.10.99

Time: 11:01

{117{

118

Chapter 4. Algorithms on Graphs

Theorem 5. A straight-line embedding of a planar graph of n nodes an be omputed in time O(n + P (n)+ n  Q(n)) where P (n) is the prepro essing time of 3-sided
re tangular range ounting and Q(n) is the query time.

With the results of Chapter 8 the proof of Theorem 5 is now ompleted.


Theorem 6. Let G = (V; E ) be a planar graph. Then G an be turned into a
planar map (G; ) in linear time.

Planar graphs have more stru ture than general graphs and are therefore in many
respe ts omputationally simpler than general graphs. The planar separator theorem (Theorem 3 below) makes planar graphs amenable to divide and onquer algorithms. It states that a planar graph an be split into about equal sized subgraphs
by the removal of only O(pn) nodes.
+
Theorem 7. Let G = (V; E ) be a planar
P graph and let w : V ! R0 be a weight
fun tion on the verti es of G. Let W = v2V w(v) be the total weight of G. Then
there is a partition A; S; B of V su h that
P
1) W (A) = v2A w(v)  2W=3, W (B )  2W=3;
p
2) jS j  4 n;
3) S separates A from B , i.e., E \ (A  B ) = ;;
4) Partition A; S; B an be onstru ted in time O(n).

Proof :

Assume rst that G is onne ted. Let s 2 V be arbitrary and let L(t) =
fv; v 2 V and the shortest path from s to v has length tg for t  0. Then L(0) =
fsg. Let r be maximal su h that L(r) 6= ;. Add empty levels L( 1) = L(r +1) = ;,
for onvenien e. Let t1 be su h that
W (L(0) [    [ L(t1

1))  W=2  W (L(0) [    [ L(t1 ));


let t0  t1 be su h that pjL(t0 )j + (t1 t0 ) < 2pn and let t2 > t1 be su h that
jL(t2 )j + (t2 t1 1) < 2 n.
Claim 1. t0 , t1 and t2 exist.
Proofp: The existen e p
of t1 is obvious. If t1 < pn then we an hoose
p t0 = 1. If
n)j +    + jL(t1 )j  npand hen e jL(t0 )j < n for some t0 ,
t1  p n then jL(t1
n  t0  t1 . Thus jL(t0 )j + t1 t0 < 2 n. In either ase we have shown the
t1
existen e of t0 . The existen e of t2 is shown similarly.

Let us take a loser look at W (L(t0 + 1) [    [ L(t2 1)). If this weight is at


most 2W=3 then let S = L(t0 ) [ L(t2 ), let A be the heaviest of the three sets
L(0) [    L(t0 1), L(t0 + 1) [    [ L(t2 1), L(t2 + 1) [    [ L(r) and let B be
the union of the remaining two sets. Then W (A)  2W=3 and W (B )  2W=3.
Version: 19.10.99

Time: 11:01

{118{

4.10. Planar Graphs

119

Let us assume now that W (L(t0 + 1) [    [ L(t2 1)) > 2W=3. Constru t
planar graph G0 as follows. Delete levels t2 and above from the graph and shrink
all nodes in level t0 and below to a single node, i.e., repla e all nodes in level t0 and
below by a single node and onne t this node to all nodes in L(t0 +1). The planarity
of G0 an be seen as follows. Consider a planar embedding of G and identify a tree
of paths from s to all nodes in L(t0 + 1). Then delete all nodes in level t0 and
below, make s the new node and draw the new edges along the tree paths. Note
that graph G0 has a spanning tree with radius t2 t0 1, i.e., the newly onstru ted
node is the root and all other nodes have distan e at most t2 t0 1 from the root.
Claim 2. Let G = (V; E ) be a onne ted planar graph having a spanning tree of
radius r and let w : V ! R+
0 be a weight fun tion. Then there is a partition A; S; B
of V su h that W (A)  2W=3, W (B )  2W=3, jS j  2r + 1, S ontains the root of
the spanning tree, and S separates A from B . Moreover, partition A; S; B an be
found in time O(n).

Suppose that we have shown Claim 2. Clearly, all steps of the proof pre eding Claim 2 an be arried out in linear time, i.e., the onstru tion of levels
L(0); L(1); : : : ; L(r), determination of t0 , t1 and t2 , and onstru tion of G0 . By
Claim 2 we an nd a partition A0; S 0 ; B 0 of the nodes of G0 su h that S 0 ontains
at most 2(t2 t0 1) + 1 nodes one of whi h is the node whi h repla ed levels t0
and below. Let S = L(t0) [ L(t2 ) [ (S 0 fnew nodeg). Then
jS j  jL(t0 )j + jL(t2 )j + 2(t2 t0)
= jL(t0 )j + 2(t1 t0) + jL(t2 )j + 2(t2 t1 1) + 2
 2p n 1 + 2p n 1 + 2
= 4pn:

The removal of S from G splits G into sets L(0)[   [ L(t0 1), A0; B 0 , L(t2 +1)[
   [ L(r) none of whi h has weight ex eeding 2W=3. It is easy to form sets A and
B from these four sets su h that W (A)  2W=3 and W (B )  2W=3. Moreover,
partition A0; S 0 ; B 0 and hen e partition A; S; B an be found in linear time.
Proof of Claim 2: If there is v 2 V with w(v)  W=3 then let S onsist of v and the
root of the spanning tree, let A = ; and let B = V S . Clearly, partition A; S; B
has all desired properties.
So let us assume next that w(v) < W=3 for all v 2 V . Extend G to a planar
map G^ ; this an be done in time O(n) by Theorem 6. Add edges to G^ su h that
every fa e be omes a triangle, f. Exer ise 3302. Let T be a spanning tree of G
of radius at most r. Every non-tree edge of G forms a simple y le with some of
the tree edges. This y le has length at most 2r + 1 if the root belongs to the
y le and has length at most 2r 1 otherwise. Every su h y le separates its inside
from its outside. It therefore su es to show that there is one su h y le su h that
neither the inside nor the outside has weight ex eeding 2W=3. More pre isely, if e
is a non-tree edge, let C (e) by the y le de ned by e, let WC (e) be the weight of
Version: 19.10.99

Time: 11:01

{119{

120

Chapter 4. Algorithms on Graphs

y le C (e), i.e., WC (e) = Pv2C(e) w(v) and let WI (e) be the weight of the nodes
in the interior of C (e).
In Figure 119 (tree edges are drawn solid, non-tree edges are drawn dashed) we
have for e = (2; 6), C (e) = (2; 1; 5; 7; 6), WC (e) = w(2)+ w(1)+ w(5)+ w(7)+ w(6)
and WI (e) = w(3) + w(4).
7
5
1

8
6

Figure 119.

Example for Claim 2

We have to show that there is a non-tree edge e su h that WI (e)  2W=3 and
 W=3. Programs 34 and 35 ompute WI (e), WC (e), and C (e)
for (all) non-tree edges e.

WI (e) + WC (e)

begin for all non-tree edges e do WC (e) unde ned od;


for all non-tree edges e
do if WC (e) is unde ned then y le (e) fi od
end
Program 34

Program 34 makes use of pro edure y le (see Program 35) whi h omputes
C (e), WI (e) and WC (e) for non-tree edge e. The body of y le is basi ally a ase
distin tion a ording to the type of edges in the triangle inside C (e) with edge e on
its boundary. This ase distin tion is illustrated in Figure 120. The main program
y le is alled at most on e for every non-tree edge e.
pro edure y le (e: non-tree edge);
o omputes C (e) as a doubly linked list and weights WI (e) and WC (e);
stops omputation if C (e) is desired y le o
let e = fx; zg, and let y be the third node of the triangle inside C (e)
whi h has e as an edge;
Version: 19.10.99

Time: 11:01

{120{

4.10. Planar Graphs

121

y
z

z
x

z
Figure 120.

The 4 ases of y le

Case 1: fx; yg and fy; z g are tree edges.


Then triangle (x; y; z) is a y le C (e) and hen e C (e) (x; y; z), WI (e) 0 and
WC (e) s(x) + w(y) + w(z ).
Case 2: fx; yg is a tree edge, and fy; z g is not, and edge fx; yg lies on y le C (e),
i.e., y is loser to the root of the spanning tree than x.
y le (fy; z g);
C (e) x on atenated with C (fy; z g);
WC (e) WC (fy; z g) + w(x);
WI (e) WI (fy; z g).
Case 3: fx; yg is a tree edge, fy; z g is not, and edge fx; yg does not lie on y le
C (e), i.e., y is farther away from the root of the spanning tree than x.
y le (fy; z g);
C (e) C (fy; z g) minus node y;
WC (e) WC (fy; z g) w(y);
WI (e) WI (fy; z g) + w(y).
Case 4: Neither fx; yg nor fy; z g are tree edges.
y le (fx; yg);
y le (fy; z g);
let p be the tree path from y to C (e) in luding y and ex luding v where v is the
node on C (e) where p meets C (e). The node v an be found in time O(jpj) where
jpj is the number of nodes of p as follows. The alls y le (fx; yg) and y le (fy; zg)
return the y les C (fx; yg) and C (fy; zg). We walk along these y les starting in
node y and away from nodes x and z respe tively. Then v is the last ommon node
and p is the path of nodes pre eding v.
Version: 19.10.99

Time: 11:01

{121{

122

Chapter 4. Algorithms on Graphs

C (e)

(C (fx; yg) minus p) on atenated with (C (fy; zg) minus p);


WC (fx; yg) + WC (fy; z g) 2W (p) w(v);
WI (fx; yg) + WI (fy; z g) + W (p);
end of ase-distin tion;
if WI (e)  2W=3 and WC (e) + WI (e)  W=3
then stop and exhibit C (e) as the desired y le fi
end.

WC (e)
WI (e)

Program 35

We still have to show that some all of y le nds a y le with the desired
properties and to analyze the running time. We show rst that the running time is
linear. Note rst that y le is alled at most on e for ea h non-tree edge e. Also the
ost of a all of y le is O(1) if Case 1, 2 or 3 is taken in the body and it is O(jpj)
in Case 4 where jpj is the number of nodes on path p. Also, 2jpj nodes are deleted
from y les C (fx; yg) and C (fy; zg) when y le C (e) is formed in the latter ase.
Sin e at most two tree edges are added to y les in a single exe ution of Cases 1
to 3 the total number of nodes deleted in Case 4 must be O(n). Thus the total ost
of either ase is O(n) and hen e the total running time is O(n).
Finally, we have to show that a y le with the desired properties is found.
We will show rst that there is a non-tree edge e with WC (e) + WI (e)  W=3.
Sin e every fa e of G^ is a triangle, so is the outer fa e. Let e1 ; e2 ; e3 be the edges
bordering the outer fa e. At least one of themPis ia non-tree edge, say e1 ; : : : ; ei are
non-tree edges for some i, 1  i  3. Then j=1 WC (ej ) + WI (ej )  W sin e
ea h node of G lies inside or on at least one of the y les C (ej ), j = 1; : : : ; i. Thus
WC (ek ) + WI (ek )  W=3 for some k, 1  k  i.
We an now exhibit edge e su h that C (e) has the desired properties. Let e be
a non-tree edge su h that WC (e) + WI (e)  W=3 and either Case 1 is taken for
e or WC (e0 ) + WI (e0 ) < W=3 for all non-tree edges e0 su h that y le (e0 ) is alled
by y le (e). Edge e exists sin e there are edges with WC (e) + WI (e)  W=3 and
sin e Case 1 is taken for at least one edge. It remains to show that edge e has the
property WI (e) < 2W=3. If Case 1 is taken then WI (e) = 0 and we are done. If
Case 2 is taken then WI (e) = WI (e0 ) < W=3 and we are done. If Case 3 is taken
then WI (e) + WC (e) = WI (e0 ) + WC (e0 ) whi h is impossible. If Case 4 is taken,
let e1 and e2 be the two non-tree edges for whi h y le is alled. We have
WI (e) = WI (e1 ) + WI (e2 ) + W (p)

 WI (e1 ) + W (p) + WI (e2 ) + W (p)


 WI (e1 ) + WC (e1 ) + WI (e2 ) + WC (e2 )
 2W=3;

and hen e C (e) is the desired y le.


Version: 19.10.99

Time: 11:01

{122{

4.10. Planar Graphs

123

We have now proved Theorem 7 for onne ted graphs. If G is un onne ted, let
G1 ; G2 ; : : : Gk be the onne ted omponents. If W (Gi )  2W=3 for all i, 1  i  k,
then a partition with S = ; is possible. If W (Gi)  2W=3 for some i then split Gi
as des ribed above and pro eed as in the former ase.
An important orollary of Theorem 3 is obtained in the unit ost ase.
Corollary 1. Let G = (V; E ) be a planar graph. Then there is a partition A; S; B
of V su h that
1) jAj  2n=3, jB j  2n=3;
p
2) jS j  4 n;
3) S separates A from B ;
4) Partition A; S; B an be found in time O(n).
Proof :

Obvious from Theorem 3 with w(v) = 1 for all v 2 V .

We end this se tion with an appli ation of the planar separator theorem. Let
N = (V; E; ) with : E ! R be a dire ted planar network and let s 2 V be a
designated node. As in Se tion 4.7 we will study the problem of omputing (s; v),
the ost of a least ost path from s to v, for any node v 2 V . In Se tion 4.7.3 we saw
how to solve this problem in time O(n  e) = O(n2 ) for planar networks. A better
algorithm an be obtained by applying the separator property of planar graphs.
Theorem 8. The single sour e least ost path problem on planar networks an be
solved in time O(n1:5 log n)

Let N = (V; E; ) with : E ! R be a planar dire ted network and let


s 2 V be a designated node. We want to ompute (s; v) for all nodes v 2 V . The
algorithm is as follows.
(1) Compute a partition V1 ; S; V2 as given by the planar separator theorem. Let
S S [ fsg and let Ni be the subnetwork indu ed by Vi [ S for i = 1; 2.
(2) Compute 1(t; v) for all t 2 S and v 2 V1 [ S . Here 1(t; v) is the ost of a
least ost path from t to v in subnetwork N1 . Similarly, ompute 2(t; v) for
all t 2 S and v 2 V2 [ S . The details of this step are spelled out below.
(3) De ne network N = (S; S  S; ) by (r; t) = minf1; 1 (r; t); 2 (r; t)g. Compute (s; t) for all t 2 S where (s; t) is the ost of a least ost path from s to
t in network N .
(4) For v 2 Vi [ S , i = 1; 2, output (s; v) = minf(s; t) + i(t; v); t 2 S g.
Proof :

The orre tness of this algorithm an be seen fairly easily. It follows from
Version: 19.10.99

Time: 11:01

{123{

124

Chapter 4. Algorithms on Graphs

Claim 1.
a) (s; t) = (s; t) for all t 2 S .
b) (s; v) = minf(s; t) + i (t; v); t 2 S g for v 2 Vi , i = 1; 2.
Proof : a) Edges of N orrespond to least ost paths in subnetworks Ni , i = 1; 2.
Thus (s; t)  (s; t) sin e every path in N gives rise to a path in N by repla ing
edges of N by paths in N . Also (s; t)  (s; t) sin e a least ost path from s to t
in N an be de omposed into subpaths running ompletely within Ni , i = 1; 2.

b) Follows immediately from part a).

It remains to des ribe the details of the implementation. For step (1) we use the
algorithm des ribed above in p
Corollary 1; it yields partition V1 ; S; V2 with jV1 j 
2n=3, jV2 j  2n=3 and jS j  5 n. (We use 5 instead of 4 be ause node s is added
to S ). For step (3) we use the algorithm des ribed in Se tion 4.7.3; it runs in time
O(jS jjS j2 ) = O(n1:5 ). Step (4) is also easily done in time O(n1:5 ). Step (2) remains
to be des ribed in detail. We do so for subnetwork N1.
(2.1) Compute 1 (s; v) for all v 2 V1 [ S using the algorithm re ursively. This takes
time T (jV1 [ S j) where T (n) is the running time of the algorithm on a n node
graph.
(2.2) Use the solution of step (2.1) to make all edge osts non-negative as des ribed
in Se tion 4.7.4. Compute 1 (t; v) for all t 2 S , v 2 V1 [ S in time O(jS j  n1 
log n1 ) = O(n1:5 log n) using the methods des ribed in Se tions 4.7.4 and 4.7.2.
Here n1 = jV1 [ S j.
We on lude that the ost of step (2) is T (n1) + T (n2 ) + O(n1:5 log n) where ni =
jVi [ S j, i = 1; 2. Altogether, we have the following re urren e for T (n):
T (n) 

 n1:5 log n
max n1+n2 n+5pn fT (n1 ) + T (n2) + d  n1:5 log ng
n1 ;n2 4n=5

for n < 1500;


for n  1500.

Here ; d are appropriate onstants, n1 = jV1 [ S j  2n=p3 + 5pn  4n=5 for


n  1500, N2 = jV2 [ S j and n1 + n2  n + jS j  n + 5 n. T (n) is learly a
non-de reasing fun tion. Let U (n) = T (n)=n. Then
U (n)   n0:5 log n

for n  1500

and
U (n) 

f(n1 =n)  U (n1) + (n2 =n)  U (n2) + d  n0:5 log ng


max
 p

n1 +n2 n+5 n
n1 ;n2 4n=5

f((
n1 + n2 )=n)  U (4n=5) + d  n0:5 log ng
 n +nmax
p
1 2 n+5 n
 (1 + 5=pn)  U (4n=5) + dpn log n
Version: 19.10.99

Time: 11:01

{124{

4.10. Planar Graphs

125

for n  1500. Let k = k(n) = dlog(n=1500)= log(5=4)e and f (n) = dpn log n. Then
U (n) 

for n  1500.
Claim 2.
Proof :

Qi 1

j =0

We have

k
Y1
j =0

"i 1
k
Y
X
i=0 j =0

1 + 5= (4=5)j n  f ((4=5)i n)


1 + 5= (4=5)j n  a for all i  k and some onstant a.




1 + 5= (4=5) n = e
j

Pk 1
ln(1+5= (4=5)j n)
j =0

Pk 1
j
e j=0 5= (4=5)Pn
k

 p
 e 5= (4=5)k n
a
p

(
j =1

p4=5)j

(sin e ln(1 + x)  x)

4=5 j onverges and sin e 1500  (4=5)k  n  (4=5)1500.


for some a sin e P1
j =1
Constant a an be hosen as 3.


Substituting into the upper bound for U (n) we obtain


U (n) 

k
X
i=0
k
X
i=0

a  f ((4=5)i n)
p

ad

(4=5)i n log n

= O(pn log n)

This proves that T (n) = n  U (n) = O(n1:5 log n).


Other appli ations of the planar separator theorem an be found in Exer ises 34
to 41.

Version: 19.10.99

Time: 11:01

{125{

126

Chapter 4. Algorithms on Graphs

4.11. Exer ises

Let G = (V; E ) be a digraph. Let Grev = (V; E rev ) be obtained from G by


reversing all edges, i.e., E rev = f(w; v); (v; w) 2 E g. Show: Given the adja en y
list representation of G one an ompute the adja en y list representation of Grev
in time O(n + e).

1)

2) A multi-graph is given by a set V of nodes, a set K of edges and fun tions,


a; b : K ! V . An edge k 2 K runs from a(k) to b(k). The underlying graph G =
(V; E ) is de ned by E = f(a(k); b(k)); k 2 K g, i.e., parallel edges are eliminated.

Show: Given the adja en y list representation of a multi-graph, i.e., given a linear
list for every i ontaining multi-set fb(k); k 2 K and a(k) = ig, one an ompute
the adja en y list representation of G in time O(jV j + jK j). [Hint: Use bu ket sort
to sort multi-set f(a(k); b(k)); k 2 K g in lexi ographi order.
Let G = (V; E ) be an a y li digraph and let G = (V; E ) be any a y li digraph
with the same transitive losure as G, i.e., G = G . Show:
a) Ered  E where Ered is de ned in Se tion 4.3.
b) Con lude from part a) that Gred is the minimal graph (with respe t to set
in lusion) with a xed transitive losure.
4) Let G = (V; E ) be an a y li digraph. Show that one an ompute Gred in time
O(n  ered ).
3)

5) Show that one an use pro edure explorefrom of Se tion 4.4 to ompute the
transitive losure of an arbitrary digraph in time O(n  e).
6) Let G be a ontext-free grammar. For sentential form let First 1 ( ) be the set
of terminal symbols a su h that ! a for some .
a) Show how to use pro edure explorefrom to ompute First 1 ( ) if G ontains no
-rules.
b) Modify your solution to part a) su h that -rules an also be handled.
7) Is the algorithm for strongly onne ted omponents still orre t if line (24) is
hanged to
then lowpt [v min(lowpt [v; lowpt [w)?

be an undire ted graph. G0 = (V; E 0 ) is a minimal bi onof G if E  E 0 , G0 is bi onne ted, and jE 0 j is as small as
possible. Develop an algorithm to ompute minimal bi onne ted extensions. [Hint:
Solve the problem for trees rst. Extend to general graphs as follows. Let V1; : : : ; Vk
be the b. . .'s of G. De ne a graph with node set V1 ; : : : ; Vk and edges (Vi ; Vj ) i
Vi \ Vj 6= ;. This graph is a tree.
8) Let G = (V; E )
ne ted extension

Version: 19.10.99

Time: 11:01

{126{

4.11. Exer ises

127

Derive a bound g(n) on the maximal number of iterations of the basi least ost
path algorithm ( f. the beginning of Se tion 4.7) on a network of n nodes. Design
networks where the algorithm might a tually need approximately g(n) iterations.

9)

Extend all least ost path algorithms so that they not only ompute the least
ost of a path but also the path itself. Running time should not hange. [Hint:
Have array Pred [1 : : n; whenever ost [v is hanged when onsidering edge (u; v)
set Pred [v to u. Then array Pred stores a tree of least ost paths after termination.
10)

Let lp(s; v) = maxf (p); p is a path from s to vg. Derive algorithms for omputing lp(s; v) for all v 2 V on various assumptions about the underlying network.

11)

(Extension of 4.7.2, Theorem 4.) Let g1 ; g2 be estimators and let g1 (v)  g2 (v)
for all v 2 V . Let Ri be the set of nodes removed from U when estimator gi is used.
Then R1 R2  fv; (s; v) + g1 (v) = (s; t)g provided that g1 is onsistent.
12)

13) Constru t an instan e of a least ost path problem and an estimator g su h


that some nodes are removed from U more than on e.
14) Consider the following well-known \15-puzzle". The board onsists of a 3 by 3
square with eight 1 by 1 tokens numbered 1 to 8 arranged on the board. One square
of the board is empty. The goal is to arrange the tokens in as ending order.

7 3 1
2 8
4 6 5
a) Formulate this puzzle as a path nding problem. What are the nodes and what
are the edges of the graph?
b) Use the path- nding algorithm of Se tion 4.7.2 to nd a solution. Use the
following three estimators: onstant zero, number of tokens out of pla e, total
distan e of tokens from their nal position.
15) Show that the algorithm of Se tion 4.7.3 has running time O(kmax  e) where
kmax is the length (number of edges) of the longest least ost path from s to any
v 2V.
16) For v 2 V let ost i [v = minf (p); p is a path from s to v of length at most ig,
i > 0. Show how to ompute array ost i [1 : : n from array ost i 1 [1 : : n in time
O(e). Con lude that the single sour e least ost path problem an be solved in
time O(n  e). Relate this algorithm to the algorithm des ribed in Se tion 4.7.3.

Relate the algorithm of this exer ise to dynami programming in general.


Version: 19.10.99

Time: 11:01

{127{

128

Chapter 4. Algorithms on Graphs

Is it a good idea to realize set U as a sta k instead of a queue in the algorithm


of Se tion 4.7.3? Is it a good idea to repla e the array ount [1 : : n of ounters by
a single ounter ount whi h ounts iterations of the loop?

17)

Let N = (V; E; ), : E ! R, be a network. Let Ep = f(v; w) 2 E ; (v; w) 


0g and let En = f(v; w) 2 E ; (v; w) < 0g. If N does not have any negative y les
then (V; En ) is a y li . Show that one an solve a single sour e least ost path
problem by repeatedly (at most n times) solving the problem for Np = (V; Ep ; )
and Nn = (V; En ; ). Here fun tion ost as omputed by one algorithm is used
as input for the other algorithm. Show that this idea leads to an O(min(n  e +
n2+1=k ; (n2 + n  e) log n)) algorithm for arbitrary integer k.

18)

Consider the following algorithm for solving the single sour e least ost path
problem. Let E = fe1 ; : : : ; em g. Use the basi algorithm of the beginning of
Se tion 4.7. Go through the elements of E in y li order and he k for the triangle
inequality. Prove that this algorithm runs in time O(n  e).
19)

20)

Design and analyze algorithms for maximum ost spanning trees.

21) Let N = (V; E; ) be a network and let s; t 2 V . Let f be a legal ow fun tion.
Show that
X
X
val (f ) =
f (e)
f (e):
2

e in(t)

e out(t)

Let f be a legal (s; t)- ow in network N . De ne the augmenting network


AN with respe t to f by AN = (V; E1 [ E2 ; ) where E1 ; E2 are de ned as in the
de nition of the layered network. Note that AN aptures all augmenting paths
while LN aptures only the minimum length augmenting path.
a) Constru t AN for the example at the beginning of Se tion 4.9.1.
b) Show that an analog of Lemma 2 is true with AN instead of LN .
) De ne the on ept of blo king ow and depth for augmenting networks. Does
Lemma 3 still hold true? [Hint: he k part b) of the laim in Lemma 3 arefully.
22)

Let N = (V; E; ) be a network with integral apa ities, i.e., : V ! N.


Let vmax be the maximal value of any legal (s; t)- ow in N . Show that vmax
augmentations su e to onstru t a maximal ow, where an augmentation an be
arried out along any augmenting path.

23)

24) Show that O(log vmax ) augmentations su e under the assumptions of Exer ise 23 if the augmentation is always arried out along an augmenting path of
maximal apa ity.
Version: 19.10.99

Time: 11:01

{128{

4.11. Exer ises

129

Design e ient algorithms for ea h of the following versions of the max- ow


problem by redu ing it to the standard version:
a) The nodes, as well as the ar s, have apa ities.
b) There are many sour es and sinks.
) The network is undire ted.
d) There are both upper and lower bounds on the value of the ow through ea h
ar .

25)

In the O(n2 ) algorithm for omputing a blo king


ow in a layered network we
rst determined a node v with PO (v) = PO  and then \forwarded" and \ba kwarded" the ow starting at v.
a) Show that the algorithm stays orre t if we only forward the ow, but start at
node s.
b) Can you still prove the O(n2 ) time bound?
26)

27)

Des ribe an algorithm for pro edure simplify in detail.

Adapt the O(n2) blo king ow algorithm to (0,1)-networks. Avoid the re omputation of PO [v for all v 2 V in line (5). Instead, ompute PO [v on e and update
it as edges in ident to v are removed in pro edures forward , su k and simplify . Also,
have an array L[1 : : e of linear lists. In list L[1 store all nodes v with PO [v = 1.
Keep a pointer in this list pointing to the leftmost non-empty list. Move this pointer
to the right in order to nd minfPO [v; v 2 V g in line (5), move it to the left when
potentials are updated. Show that the total number of moves of the pointer is O(e).
Con lude that a blo king ow in a (0,1)-network an be omputed in time O(e).
28)

A network N = (V; E; ), s; t 2 V , is (s; t)-planar if (V; E ) is a planar graph


and if s and t border the same fa e of the planar graph. Consider an embedding
of (V; E ) where s and t border the outer fa e. In this situation there is a natural
order on the set of paths from s to t. (Path p1 is above path p2 if p1 = p0e1 p00 and
p2 = p0 e2 p000 and e1 is \above" e2 ; f. Figure 121.) Let p1 ; p2 ; p3 ; : : : ; pm be the set
of paths from s to t ordered a ording to the property of being above another path.

29)

Figure 121.
Version: 19.10.99

Time: 11:01

Ordering of paths from s to t

{129{

130

Chapter 4. Algorithms on Graphs

a) Constru t a blo king ow by rst saturating an edge of p1, then an edge of p2 ,


et . Show that the onstru ted ow is maximal. [Hint: Let 1 be the apa ity
of p1; show that there is a maximal ow whi h sends 1 units a ross every
edge of p1. Assume otherwise. Let p1 onsist of edges e1 ; e2 ; : : : ; ek . Let f
be a maximal ow fun tion su h that f (e1 ); : : : ; f (ei )  1 , f (ei+1) < 1 and
no maximal ow f 0 satis es f 0(e1 ); : : : ; f 0 (ei )  1 , f 0(ei+1 ) > f (ei+1). Then
1 f (ei+1 ) units must be transported from v to t along some path p0 (see
Figure 122). Let j > i + 1 be minimal su h that f (ej )  1 . (If j does not
exist the argument be omes simpler.) Then f (ej ) f (ej 1) > 0 units of ow
are transported from s to w along some path p00. Sin e the network is assumed
to be planar p0 and p00 must onverge
in some node, say x. It is now easy to
divert some ow from the path v ! x ! w to path p1 thus ontradi ting the
existen e of f . This proves that there is a maximal ow whi h sends 1 units
along ea h edge of p1 . The orre tness proof is now ompleted by indu tion.

Figure 122.

Exer ise 29a)

b) Show how to implement the algorithm outlined in part a) in time O(e  log n).
[Hint: Use the blo king ow algorithm des ribed in the text without hange.
Explain how edges around nodes have to be ordered. Show that lines (5)
and (6) are exe uted at most e times. This follows from the observation that p
always points into pf from below in lines (4){(6) and hen e pf 0 an be dis arded
be ause it will never be the ase that p points into pf 0. This will dis ard at
least one edge ex ept when pf 0 is trivial, i.e., last (p) = rst (pf ). However, this
an happen only if line (3) is exe uted immediately before.

Figure 123.

Exer ise 29b)

30) A network ow problem with upper and lower bounds is given by a dire ted
graph G = (V; E ), sour e s, sink t and two apa ity fun tions low : E ! R and
Version: 19.10.99

Time: 11:01

{130{

4.11. Exer ises

131

: E ! R. A legal (s; t)- ow f must satisfy the onservation laws and the
apa ity onstraints: low (e)  f (e)  high (e) for all e 2 E .
a) Show that the problem whether a legal ow exists an be redu ed to an ordinary
network ow problem. [Hint: Let V =+V [fs; tg, let E = E [(fsg V )[(V 
ftg) [ f(s; t); (t; s)g and
let  : E ! R be de ned by (e) = high (e) low (e)
for e 2 E , (s; v) = Pe2in(v) low (e), (v; t) = Pe2out(v) low (e), and (s; t) =
(t; s) = 1. Show that there is a legal ow i the maximum ow in the
auxiliary network N saturates all edges emanating from s.
b) Show how to ompute a maximal ow in a network with upper and lower
bounds [Hint: Start with a legal ow as onstru ted in a) and use augmentation.

high

31) Let G = (V1 [ V2 ; E ), E  V1  V2 , be a bipartite graph with jV1 j  jV2 j.


Show: G has a omplete mat hing M , i.e., jM j = jV1 j, if for all S  V1 holds:
jfw 2 V2 ; (v; w) 2 E for some v 2 S gj  jS j.

Let N = (V; E; ap ; ost ) be a weighted network and let f be a legal (s; t)- ow.
Show how to ompute a legal (s; t)- ow g from f with val (g) = val (f ) and minimal
ost. Running time?

32)

33) Let T be an undire ted tree where every node has degree at most d. Show
that there is a node v of T su h that the removal of v splits T into subtrees of at
most (d 1)  n=d nodes ea h.
34) Let G = (V; E ) be a planar graph. Show that there is a partition A; S; B of V
su h that jAj  n=2, jB j  n=2, S = O(pn), and S separates A from B . Moreover
A; S; B an be found in linear time.
35) Let A be a symmetri , positive de nite matrix. Show: If A is the adja en y
matrix of a planar graph G = (V; E ), i.e., (i; j ) 2 E i aij 6= 0, then the linear
system A  x = b an be solved in time O(n3=2 ). [Hint: Let V1 ; S; V2 be a partition
of V as given by the planar separator theorem; let P be a permutation matrix su h
that P  A  P 1 has the form
n0
V1 B
nB
V2 B
B
nB


A1
A
V1

3
|{z}

A3
A2

A4

A
V2

5
|{z}

4
|{z}

A
S

1
C
C
C
C
C
A

Apply a similar reordering to submatri es A1 ; A2 . Use Gaussian elimination on the


reordered matrix. Study arefully, whi h entries of the matrix be ome non-zero
during Gaussian elimination.
Version: 19.10.99

Time: 11:01

{131{

132

Chapter 4. Algorithms on Graphs

Let G = (V; E ) be a dire ted planar graph. Show that one an onstru t a
transitive redu tion of G, i.e., a smallest graph with the same transitive losure, in
time O(n3=2 ). [Hint: Use the planar separator theorem.

36)

Let G = P(V; E ) be a planar graph, let w : V ! R+0 be a weight fun tion,


and let Wp = v2V w(v). Show that there is a partition A; S; B of V su h that
jS j  8  n, jAj  2  n=3, W (A)  2  W=3, jB j  2  n=3, W (B )  2  W=3 and S
separates A from B . [Hint: Apply Theorem 3 to G, then apply Corollary 1 to the
heavier part.

37)

and
Let G = (V; E ) be a planar graph, let w : V ! R+0 be a weight fun tion
p
let 0 <   1=2. Show that there is a subset S  V su h that jS j = O( n=) and
su h that no onne ted omponent of G S has weight ex eeding   W . [Hint: Use
Exer ise 37 repeatedly.
38)

39) Let G = (V; E ) be a planar graph. A subset V 0  V is independent if (V 0 


V 0 )\ E = ;. The problem of de iding whether there is an independent set of size m

is NP- omplete ( f. Chapter VI). Show how to nd a nearly maximal independent


set e iently in planar graphs. [Hint: Use Exer ise 38 with w(v) = 1 for all
v 2 V and  = (log log n)=n. Find maximal independent sets of all omponents
of G S by exhaustive
sear h and output the union of these sets. Show that
(jI j jI  j)=jI j = O(1=plog log n) where I is the independent set omputed by the
algorithm and where I  is a maximum independent set. Observe that jI  j =
(n)
sin e a planar graph has a large number of nodes of small degree.
40)p Show that a maximum independent set of a planar graph an be found in time
2O( n) . [Hint: Split V into V1 ; S; V2 as given by the planar separator theorem. For
every S 0  S nd a maximal independent set I of the subgraph indu ed by V1 [ S
(V2 [ S ) su h that I \ S = S 0 by re ursive appli ation of the algorithm.
p

Show how to nd the hromati number of a planar graph in time 2O( n) .


[Hint: Pro eed as in the pre eding exer ise.
41)

Version: 19.10.99

Time: 11:01

{132{

4.12. Bibliographi Notes

133

4.12. Bibliographi Notes

The algorithm for topologi al sorting is due to Kahn (62) and Knuth (68). A detailed analysis of the representation problem an be found in Rivest/Vuillemin (75).
The O(n  ered) algorithm for the omputation of the transitive losure of digraphs
is by Goral ikova/Koubek (79). The analysis for random a y li digraphs and the
improved losure algorithm have not appeared before; they were done jointly with
K. Simon (Simon (83)). A linear expe ted time algorithm for random digraphs is
des ribed in S hnorr (78). Algorithms for the systemati exploration of a graph
(maze) are very old and date ba k to the 19th entury at least. Depth- rst-sear h
was made popular by Tarjan (72) and Se tions 4.5 and 4.6 are adopted from his
paper.
The presentation of the basi algorithm for least ost paths follows Johnson (77); Theorem 2 ) is also due to him. Theorem 2a) is taken over from Dijkstra (59). The dis ussion on the use of estimators for solving one pair least ost
path problems is based on Hart/Nilsson/Raphael. An algorithm whi h solves
the
all pairs problem on nonnegative networks in expe ted time O(n2  log n  log n) is
dis ussed in Bloniarz (80). The treatment of the general ase follows Bellmann (58),
Floyd (62) for Theorem 5 and Exer ise 16, Edmonds/Karp (72) for Lemma 4, and
Johnson (77) for Theorem 7 and Exer ise 18.
The se tion on minimum spanning trees ombines the work of Kruskal (56)
(Theorem 1), Prim (57) and Dijkstra (59) (Theorem 2), Yao (75) (Theorems 3 and 4)
and Cheriton/Tarjan (76) (Theorems 3 and 4). The paper by Cheriton/Tarjan
ontains even better algorithms than the ones des ribed in the text.
Many fundamental results on network ow, in parti ular Theorem 3, are due
to Ford/Fulkerson (62). The O(n3 ) algorithm is from Malhotra et al. (78) who
re ne an algorithm due to Karzanov (74). The O(n2  e) algorithm underlying
Theorem 4 was invented by Dini (70) and then re ned to an O(n  e  (log n)2) algorithm by Galil/Naamad (79). An O(e  n log n) algorithm was re ently des ribed
by Sleator/Tarjan (Sleator (79)). Theorem 7 is also due to Galil/Naamad (79).
Se tion 4.9.2 on (0,1)-Networks ombines work of Even/Tarjan (75) (Theorems 7,
8 and 10a)), Hop roft/Karp (75) (Theorem 9) and Be ker et al. (82) (Theorem 10b)). Weighted network ow was treated by Jewel (58), Busa ker/Gowen (61)
(Lemma 13) and Edmonds/Karp (72) (Lemma 14). Exer ise 29 is from Itai/Shiloa h (79) and Galil/Naamad (79). The linear time planarity testing algorithm is
due to Hop roft/Tarjan (72). The planar separator theorem and many of its appli ations (Exer ises 35, 37{41) are from Lipton/Tarjan (77,77). The appli ation
to least ost path omputations is taken over from Mehlhorn/S hmidt (83). An
O(n3=2 ) algorithm for least ost path omputations in planar graphs was des ribed
by Tarjan (81). Exer ise 36 was proposed by Th. Lengauer.

Version: 19.10.99

Time: 11:01

{133{

S-ar putea să vă placă și