Documente Academic
Documente Profesional
Documente Cultură
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{
Time: 11:01
{2{
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
Figure 3.
Time: 11:01
{3{
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{
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
Time: 11:01
{5{
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
ount + 1;
ount ;
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
ied. On this set the
following operations are performed: insertion of an arbitrary and deletion of an
unspe
ied 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{
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 :
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 :
(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{
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)
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.
Denition: 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{
Time: 11:01
{9{
10
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
denitions 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{
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
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
log 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
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
n
X1
l=1
Time: 11:01
13
n
X1
=1
l=1
= 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 ):
Time: 11:01
{13{
1;l 1
14
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
E('(outdeg n(1))) = n 1
for all n.
Proof :
'(l) p1;l
= '(1) p1;1
=0
and for n 2
X
E('(outdeg n(1)) = '(l) pn;l
l
'(l) (l
pn
1;l 1
X
l
X
l
'(l + 1) l pn
X
1;l
'(l) pn
'(l) l pn
1;l
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{
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
Time: 11:01
{15{
[Lemma 6
[sin
e '
16
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{
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
Version: 19.10.99
Time: 11:01
{17{
18
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)
Lines (6) to (8) of Program 10 need to be rened 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{
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.
Denition: 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
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{
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)
(20)
(21)
(22)
(23)
21
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 dierent 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 depthrst-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
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
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).
Version: 19.10.99
Time: 11:01
{22{
23
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 denition 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
Cross or
ba
k edge
Figure 5.
Time: 11:01
{24{
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 undened
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 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.
Denition:
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
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
Yet another one-pass algorithm is des
ribed in Exer
ise 8. Both one-pass algorithms
an be modied for
omputing the bi
onne
ted
omponents of an undire
ted graph.
i
e
Figure 6.
i
e
b
Figure 7.
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{
27
C3
C2
C4
C1
C5
Figure 8.
Proof :
Time: 11:01
{27{
28
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{
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 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
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 ee
t of
exploring the
edge (v; w)
Let unnished 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,
unnished = (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 unnished .
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 unnished . In our
example, roots = (b;
; f; h). With these denitions we
an reformulate (and rene)
invariants I2 and I3 as follows:
Let unnished = (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{
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 unnished 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 unnished
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 unnished are realized as pushdown stores; in addition, unnished
is also represented as a boolean array in unnished .
Exploration of a non-tree edge (v; w):
We have to distinguish two
ases: either w belongs to a
ompleted
omponent
(in unnished [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 unnished [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 unnished [w = true .
Let unnished = (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)
to S ;
fi
end;
begin
main program o
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{
33
dfsnum [vk
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 unnished 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.
Denition:
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
Time: 11:01
{33{
34
d
b
Figure 11.
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 dene 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 modied to
ompute the b.
.
.'s of undire
ted graphs,
Version: 19.10.99
Time: 11:01
{34{
d
b
2
a
Figure 12.
35
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
Lemma 3. Let G
[
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
Time: 11:01
{36{
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)
(20)
(21)
(22)
(23)
ount 1 + 1; dfsnum [v
add v to S ;
37
ount 1;
lowpt [v
dfsnum [v;
push v on unnished ;
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 (unnished );
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 ;; unnished 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 unnished before v. Conversely, if w was not added to
unnished not before v and is on unnished 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
unnished 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).
lowpt [
Version: 19.10.99
Time: 11:01
{37{
38
exe
ution of line (15) in dfs (f ) the
ontent of unnished is a; b;
; f . In line (15) f
is deleted. Then d; e and g are added to unnished and so prior to the exe
ution of
line (15) in dfs (g) the
ontent of unnished 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 dened 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.
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 dene 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 inmum over the empty set being 1.
Version: 19.10.99
Time: 11:01
{38{
39
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
Version: 19.10.99
Time: 11:01
{39{
40
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
0; path [s (s);
all v 2 V , v 6= s do
ost [v +1;
path [v undened
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
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 dened 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{
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 :
Time: 11:01
{41{
42
(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 renement 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
denition 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
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
(1)
(2)
(3)
(4)
(5)
(6)
(7)
ost [1
0;
There is a generalization of a
y
li
graphs whi
h still allows for a linear time shortest
path algorithm: shortest-path-orderable graphs.
Denition: 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
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 satises the triangle inequality, a fa
t that
an be
Time: 11:01
{44{
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
( ost [vj+1
ost [vj
(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
Time: 11:01
{45{
46
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. Dierent implementations of priority queues, whi
h we treat in the se
ond
part, yield
p dierent 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
Time: 11:01
{46{
(k; inf )
pq (j )
dom (pq )
47
be any
if j = i;
if j 6= i.
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
do
od.
Findmin (PQ );
get inf (PQ ; i);
Delete (PQ ; i);
i
u
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 renes 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.
Dierent 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{
49
Denition:
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
ied (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
Part a) follows from the dis
ussion above and Part b) follows from Part a)
and Theorem 3.
Time: 11:01
{50{
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.
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
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{
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
Time: 11:01
{53{
54
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
ied 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
od
fi
fi
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
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 modied
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
k
X1
i=0
(vi ; vi+1 )
k
X1
i=0
= (v0 ) +
k
X1
i=0
Version: 19.10.99
Time: 11:01
{56{
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)
for all i 2 V do Vi
do n 1 times
od.
fig; Ti
; od;
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 dened 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 renement 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{
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 denition
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
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 dened
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 :
Time: 11:01
{60{
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 :
O(ei ) = O
log n 1
X
i=0
ni
=O
log n 1
X
i=0
n=2
= O(n):
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 satises
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)
Time: 11:01
{61{
62
10=1
10=0
1=1
10=0
10=1
2
Figure 19. Graph with
apa
ity/
ow
Denition: 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{
63
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 dened 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
9
10
10
9
v0
v1
Figure 22. LN
v2
= (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 )
b) We have to show that f1 satises 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
Time: 11:01
{64{
65
i.e., f 0 satises 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 .
Denition:
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.
Denition: 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
(1)
(2)
(3)
(4)
(5)
(6)
(7)
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.
1
1
to vj in LNi
to vj in LNi
Proof :
1
1
then dj
then dj
= dj 1 + 1.
dj 1 .
Version: 19.10.99
Time: 11:01
{66{
67
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 suer.) 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
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)
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{
(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
(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 3. Let N
Version: 19.10.99
Time: 11:01
{69{
70
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.
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
71
Figure 24.
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)
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
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 ae
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 dened 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 )
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 :
Time: 11:01
{73{
74
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)
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).
Time: 11:01
{74{
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
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.
Time: 11:01
{75{
76
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 denitions 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
Version: 19.10.99
Time: 11:01
{76{
77
Use the proof of Theorem 4 and observe that an edge an be used at most
Time: 11:01
{77{
78
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 dened 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
2 2 S
=
(S; V S )
v S;w V
2
val (f ):
v V;w V
=
(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{
79
Time: 11:01
{79{
80
Version: 19.10.99
Time: 11:01
{80{
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 dened 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). Dene
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
Proof :
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 dened 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{
83
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
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 predened 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 dene 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
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 :
e
E
we have
e E
e E
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
85
In any
ase, this shows that h satises the
onservation laws and that val (h) = 0.
Finally, it is trivial to see that h satises 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 dene 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
Proof : f 00
y
C
v
s
Figure 28.
w
p0
Version: 19.10.99
Time: 11:01
{86{
87
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 dieren
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)
Proof :
Version: 19.10.99
Time: 11:01
{87{
88
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 dened by
ost (M ) =
ost (e):
e M
Version: 19.10.99
Time: 11:01
{88{
89
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 innite 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
Figure 101.
F0
F0
=)
F1 e
Figure 102.
F1
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{
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 :
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
x
v
=)
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
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{
93
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
onne 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 diers 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) dened 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
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 dierent
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 dierent 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{
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 dened 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
onne 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 :
Time: 11:01
{95{
96
S (e0 )
C
S (e 0 )
S (e)
y
S (e)
S3 = S ((7; 5))
S ((6; 4)) = S4
Figure 106.
Time: 11:01
{96{
97
Time: 11:01
{97{
98
lowpt [v = min(fvg [ fz ; v ! w ! z
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
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 dened 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{
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 veried.
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 ;
Version: 19.10.99
Time: 11:01
{99{
100
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{
101
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
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
y
wj
S (ei+1 )
lowpt2 [u
lowpt2 [y
S (e)
z lowpt [u
Figure 109.
y
wj
S (ei+1 )
lowpt2 [y lowpt2 [u
S (e)
z lowpt [u
Figure 110.
Time: 11:01
{102{
103
[by denition 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 denition of h, the latter in
lusion follows from
the denition 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
) satises 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
S (ei+1 )
wj
F
C
wj
wj
+
min A(ei+1 )
Figure 111.
min A(ei+1 )
min A(ei+1 )
Time: 11:01
{104{
105
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 dened 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
ALB on atenated
Program 32
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)
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{
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.
Proof :
Time: 11:01
{107{
108
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)
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{
109
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
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
Time: 11:01
{110{
5
12
10
9
1
Figure 114.
111
3
4
6
11
7
13
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).
Time: 11:01
{111{
112
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
Figure 116.
Version: 19.10.99
Time: 11:01
{112{
113
been dened 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
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{
115
wp+1
wp
w3
w2
w1 = v1 = u
Gk+1
wq
wp+2
wq
wq
wr = v2 = v
Figure 118. Gk+1
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
Time: 11:01
{116{
117
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 denition of M (k; vk ), and k pre
edes i in n i
se
ond k pre
edes i in n by the denition 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 denition 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
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.
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
Time: 11:01
{118{
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 dened by e, let WC (e) be the weight of
Version: 19.10.99
Time: 11:01
{119{
120
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.
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)
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{
121
y
z
z
x
z
Figure 120.
The 4 ases of y le
Time: 11:01
{121{
122
C (e)
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)
Time: 11:01
{122{
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 :
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)
The
orre
tness of this algorithm
an be seen fairly easily. It follows from
Version: 19.10.99
Time: 11:01
{123{
124
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.
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
and
U (n)
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{
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) 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)
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)
Version: 19.10.99
Time: 11:01
{125{
126
1)
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 dened 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. Dene 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{
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)
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.
Time: 11:01
{127{
128
17)
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)
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)
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{
129
25)
27)
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)
29)
Figure 121.
Version: 19.10.99
Time: 11:01
{129{
130
Figure 122.
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.
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{
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 dened 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
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 denite 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
Time: 11:01
{131{
132
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)
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)
Version: 19.10.99
Time: 11:01
{132{
133
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
rene an algorithm due to Karzanov (74). The O(n2 e) algorithm underlying
Theorem 4 was invented by Dini
(70) and then rened 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{