Sie sind auf Seite 1von 6

Information Processing Letters 21 (1985) 253-258 North-Holland

18 November 1985

AN EFFICIENT DIJKSTRA-LIKE LABELING METHOD FOR COMPUTING SHORTEST O D D / E V E N PATHS Ulrich DERIGS *
Institut fiar Okonometrie und Operations Research, Abteilung Operations Research, Rheinische Friedrich-Wilhelms-Unwersiti~t Bonn, Nassestrasse 2, D-5300 Bonn 1, Fed. Rep. Germany
Communicated by K. Mehlhorn Received 14 April 1984 Revised 28 January 1985

We show how the problem of determining shortest paths of even or odd length between two specified vertices in a graph G = (V, E) can be reduced to the problem of finding a shortest alternating path with respect to a specific matching in a related graph H. This problem can be solved by a Dijkstra-like labeling procedure of complexity O( IV 12) respectively O( IElloglV I). Interpreting this procedure appropriately the method can then be applied directly on the given graph G.

Keywords: Shortest odd/even path, matching, labeling algorithm

1. Introduction
In [6], Grrtschel and Pulleyblank describe a simple reduction--which they attribute to Waterloo-folklore--for the problem of computing shortest paths with an even (or odd) number of edges. Here the length of a path is simply the sum over the edgeweights of edges in the path. In their approach, the path-problem is reduced to the problem of finding a min-cost perfect matching in a related graph. To solve the matching problem, for instance, the well-known blossom-algorithm (cf. [5]) can be used. This procedure can be implemented in O(n 3) steps where n is the number of nodes of the underlying graph. Yet, a closer look at Gr/Stschel and Pulleyblank's approach shows that only one of the usually n iterations of the blossom-algorithm is needed which reduces the complexity to O(n2). In this paper we show using basically the same
* Supported by Sonderforschungsbereich 21 (DFG), Institut fiir Okonometrie und Operations Research, Universit~t Bonn, Fed. Rep. Germany.

ideas as Grrtschel and Pulleyblank that the shortest odd/even path problem in G is equivalent to finding a shortest alternating path in a related graph. This problem can be solved by a Dijkstralike labeling method which has been used. by the present author to solve the min-cost perfect matching problem (cf. [3]). Due to the special structure of the underlying graph, the labeling procedure can be significantly simplified--from a computational as well as logical point of view--which leads to a procedure of complexity O(m-log n) where m is the number of edges in the given graph. In the following we will first develop the basic theory and the transformations before formulating the labeling procedures for finding the shortest alternating path. From that it will be immediately clear how this labeling procedure can be implemented for finding the shortest odd/even path on the original graph directly.

2. Shortest alternating paths and odd/even paths


Let G = (V, E) be a graph with nonnegative edgeweights cij for (i, j ) ~ E (w.l.o.g., V =
253

0020-0190/85/$3.30 1985, Elsevier Science Publishers B.V. (North-Holland)

Volume 21, Number 5

INFORMATION PROCESSING LETTERS

18 November 1985

( 1 , 2 , 3 ..... n}). A matching M in a graph is a subset of the edges such that no two are adjacent. A matching is called perfect if every node is incident to exactly one matching edge. Then the cost of a matching is given by

c(M):=

Y', %.
(i,j)~M

With respect to a matching M an alternating path (cycle) is a path (cycle) the edges of which are alternately in M and not. An alternating path is called augmenting path if its two endnodes are not incident to a matching edge. With alternating cycles and augmenting paths P we can modify the matching by reversing the role of matching and nonmatching along P. We will denote this operation and the new matching by M P, i.e., M P:= ( M \ P ) U ( P \ M ) . With alternating cycles and augmenting paths P we can associate a 'length'
e ( e ) := E
(i,j)~ P\M

c,j -

E
(i,j)~MnP

c,j.

This at first sight rather unusual length-definition is motivated by the following relation: c(M P ) = c(M) + Y(P). An augmenting path P is called a shortest augmenting path with respect to a matching M if E(P) ~< Z(P') holds for all augmenting paths P' with respect to M and an alternating cycle Q with E(Q) < 0 is called a negative alternating cycle. The importance of negative alternating cycles and shortest augmenting paths is given by the following properties. (i) A perfect matching is of minimal cost if it does not allow negative alternating cycles. (ii) If M does not allow a negative alternating cycle and P is a shortest augmenting path with respect to M, then M ~ P does not allow negative alternating cycles. Thus, the min-cost perfect matching problem can be solved by successively augmenting along shortest augmenting paths. In [4], the present
254

author has given a labeling technique for determining shortest augmenting paths provided the matching does not allow a negative alternating cycle. We will now discuss how a method for finding shortest augmenting paths can be used to find shortest odd/even paths in a graph. Suppose we want to find the shortest path from i to j with an even number of edges in G. Then let G~ respectively Gj be the graph obtained from G by removing node i respectively j. Let H~j be the graph of the node-disjoint union of G~ and Gj where in addition every node k in G~, k c j, is linked to its copy in Gj. To distinguish G i and Gj we will denote the vertices in Gj by k' for k = 1. . . . . n. The edges in G~ respectively Gj keep their original weight and the weights of the 'new' edges (k, k') are set to zero. Now, consider the matching M in H,j consisting of the new edges only. This matching leaves only i in Gj and j in G, unmatched. Now it is evident that there is a one-toone correspondence between the set of even paths between i and j in G and alternating (augmenting) paths between i' and j in H,j. Given an augmenting path P in H~j we get the associated even path Q by simply removing the matching edges of P and identifying vertices k' in P by their image k ~ V. Moreover, since the weight of the matching edges is zero, the paths P and Q have the same length. Thus, finding the shortest augmenting path in H~j solves the shortest even path problem in G and since M does not allow any negative cycle in H 0 this shortest augmenting path can be found using the labeling method given by Derigs [3,4]. The shortest path with an odd number of edges between i and j in G can be determined by a similar reduction. Now the graph H,j is given by the node-disjoint union of G and Gij, the graph obtained from G by removing nodes i and j and again adding 'new' edges (k, k') for k 4 i, j with weight zero which again define a matching M in G. Now, every augmenting path from i to j in H~j corresponds to an odd path joining i and j in G and the shortest path with an odd number of edges joining i and j can be determined by the shortest augmenting path method. In the next section we will introduce this labeling procedure.

Volume 21, Number 5

INFORMATION PROCESSING LETTERS

18 November1985

3. The shortest augmenting path labeling method


For the general shortest augmenting path method used for finding min-cost perfect matchings at the beginning of an iteration there may be several subgraphs shrunk to pseudonodes. This is due to the fact that their dual weight is positive. Then the method will start in the so-called surface graph and it will eventually have to expand certain pseudonodes again. Such an expansion is the most complicated and expensive step in this method. Ball and Derigs [2] developed labeling procedures of complexity O(I V I 2 ) and O ( I E I l o g l V [ ) + 13(O(IE I)) respectively where 13 is the number of expansions. 13 may theoretically be of order O( IV I) which would yield a total complexity of O( IV I t E I), yet empirically 13 is of order O(1). We will not go inte detail here and for more information on the shortest augmenting path method for the general matching problem we refer to [41. For our purposes we can work immediately on H ij, and no subgraph has to be shrunk. Thus, 13 is zero and we immediately get a bound of O( IE Ilog IV I) operations. For the following, let H = (V(H), E(H)) be the graph H~j coming from the even path problem in which we are searching for shortest augmenting path. We do not treat the odd-path case explicitly here. In our labeling procedure we will grow a (shortest) alternating path tree T = (V(T), E(T)) rooted at the unmatched vertex i'. For handling alternating trees and tracing back alternating paths on a computer, several efficient data structures have been developed. We will not discuss these structures here and refer to [7]. In the following we will focus on the computation of the shortest path length only. During the procedure, the nodes d e V(H) receive (tentative) labels d~- respectively d e indicating the shortest path length from i' to ~ (found so far) for alternating paths ending in E with a matching edge respectively ending in ~' with a nonmatching edge. These labels are set to infinity at the beginning of the procedure. Alternating paths ending in E with a matching edge (nonmatching edge) always have an even number of

edges. Vertices with a (finite) d +-label, i.e., vertices for which an alternating path with an even number of edges has been detected, are called outer vertices. Now, let k E V(H) be an outer vertex; thus there exists an alternating path with an even number of edges in H joining i' and k. It is easy to see that if k ~ G,, this alternating path P induces an odd path joining i and k in G, and if k ~ G j, then P induces an even path joining i and k in G. To enable the backtracking of alternating paths we define for vertices f ~ V(H) with (finite) d-label by PRED(E) the predecessor of t on the alternating path from i' to I which has defined d 7 . During the process we successively fix the minimal tentative d--label and use it to update the other tentative d--labels. The shortest augmenting path has been found when the minimal d--value is attained for vertex j in which case the procedure stops. The updating of the d--labels is done in the following GROw-step: Let d j be the smallest tentative d--value, f:~j, and let k be the vertex which is matched with E; then we introduce the vertices E and k and the edges (f, PRED(f)) and (f, k) into the tree and we set d ~- = d j . The vertex k becomes an outer vertex of the tree and it is scanned, i.e., for all edges (k, r) where r is not a non-outer vertex of the tree we set d r .'= min{ d ~ , d~- + Ckr } and

PRED(r) .'= k

if d ~- has changed.

Now, let l ~ V(H) be an outer vertex with finite d--label and k = PRED(g). Then there exist two alternating paths from f to i ' - P 1 with an even number of edges and P2 with an odd number of edges. Since both paths end in i', there exists a maximal alternating path P with an even number of edges starting at i' which is contained in 1:'1 and P2. Let b be the other outer endnode of P (possibly b = i' in which case P reduces to a single vertex). Now the set (E(P1)U E(Pz))\E(P ) induces an odd cycle C with the following property: For all non-outer vertices q in C there exists an alternating path Pq joining q and i' which has an even number of edges, i.e., ending in a matching edge. 255

Volume 21, Number 5

INFORMATION PROCESSINGLETTERS

18 November 1985

blossom C' with base b'4: b. Then we have to update BASIS(i) -'= b' Fig. 1. Blossom with respect to M. Due to Edmonds [5] such a structure is called a blossom with respect to M and b is called the base of this blossom (see Fig. 1). The length of Pq can be calculated as follows: d(Pq)=d; +d;-dq. for all i with BASIS(i) in C'.

For a blossom C we define ICI as the number of BAsis-vertices in C, i.e., vertices i with BASIS(i)= i. In the following sections we will specialize and formalize the above ideas to the problem of finding a shortest even path in G joining two specific nodes i and j.

Now let (t, q) ~ E, t not in C. Then there exists an alternating path P~ from t to i' of length d ( P t ) = d(Pq) + Cq,Which has an odd number of edges. The length d(Pt) may be smaller than the present (tentative) label d~- and the minimal d--label, 81 say. In this case we have to update d~- to guarantee the detection of the shortest even (i - j)-path in O. Since Cqt >~ 0, this is possible only if d(Pq) -- d ; + d r - d~ < 8~. Since dq ~< 81 this implies d ; + d7 < 281. Thus, during the algorithm we compute 82 = min{(d~ + d T ) l d outer vertex}. If 82 < 81 holds, we determine the odd cycle C and we set, for all non-outer vertices in C, dq := d(Pq) = O f + d 7 - d q
+

4. An O( IV 12)-labeling method for shortest even paths


In this section we will apply the labeling procedure developed for determining shortest alternating paths to obtain shortest even paths. We do not treat the problem of determining shortest odd paths explicitly--from the above and what follows it should be clear how this problem can be handled analogously. We assume that the graph G = (V, E) is given in form of neighborlists, i.e., for all k E V we have N(k)=(d~Vl(k , g)~E},

=282-d q .

Then all these vertices become outer and are scanned in the same way as the new outer vertex k is scanned in a normal GROw-step. Yet, to ensure correctness of the labels, i.e., to ensure that the associated paths are simple, we are not allowed to check edges joining two vertices contained in C. For that purpose we keep the nodes of the graph as a collection of disjoint sets using a pointer BASIS(i) for i ~ V(H) with the following meaning: At the beginning we set BASIS(k)= k for all k ~ V(H). Whenever 82 < 81 holds, we determine the associated cycle C with base b and we set BAsis(k) = b for all vertices k in C and we call b the canonial element of C. When scanning an outer vertex q we will then discuss only edges (q, r) with BASIS(r)4= BASIS(q). Thus, we can think of the blossom C being shrunk to a pseudonode with name b. In a later step this pseudonode may become the member of another 256

the set of neighbors of vertex k. We do not construct the graph H~j explicitly; instead we associate with every vertex k ~ V a 'fictitious' vertex k' = n + k and define N(n + k) := (n + d I E ~ N(k), d4:j} N(k):= N(k)\(i} for k 4 : i . for k 4:j,

The matching is then represented by the following swapping function m : ( 1 , . . . , 2n } ---, ( 1. . . . ,2n }: re(k)= n+k k-n if k..< n, k 4: i, j, ifk>n,k-n4:i,j.

The central subroutine in our labeling procedure is scanning an outer vertex k of the alternating tree T--(V(T), E(T)) which can be formalized as follows: SCAN(k): for all j ~ N(k) set dj- := rnin{d~-, d~- + ckj )

Volume 21, Number 5 if d~- has c h a n g e d set PRED(j):---- k

INFORMATION PROCESSING LETTERS

18 November 1985

N o w the complete labeling procedure can be formalized as follows:

Procedure for finding shortest even (i -j)-path


INITIALIZATION: Set d k '= d k '= ~ for all k = 'l, ... ,2n + BASIS(k) := k for all k = 1 , . . . , 2n + dn+i:=0 for all k ~ N ( n + i) set d k := % PRED(k)-'= n + i goto CONTROL CONTROL: Set 81 "= min{d~- IJ ~ V(T)} 82 .'= min((d ~- + d ~ ) l j outer vertex) set 8 := min{ 81, 82 ) if 8 = 00, STOP, there is no even (i - j ) - p a t h in G. If 8 = 81, set f equal to the vertex defining 81 if f = j, STOP, shortest even ( i - j ) - p a t h has been f o u n d else goto GROW(f) Otherwise, ( 8 - - 8 2 ) , set f equal to the vertex defining 82 a n d let k := PRED(f). T h e n goto BLOSSOM(f, k) GROW(f): Let k "= m ( f ) , set V(T)-'= V ( T ) o {k, f } with k b e c o m i n g an outer vertex of T Set d k := 8 a n d SCAN(k) return to C O N T R O L BLOSSOM(E, k): D e t e r m i n e b l o s s o m C with base b d e t e r m i n e d by edge ( f , k). Set BASIS(j) := b for all j with BASIS(j) in C for each j ~ C n o t outer set d~- := 28 - d~- a n d SCAN(j) (j is n o w an outer vertex of the tree) return to CONTROL F r o m our discussion in Section 3 it follows that

the above labeling m e t h o d wil find the shortest even ( i - j)-path or stop with the message that no such path exists. It is easy to see that the above p r o c e d u r e requires at most O( IVl 2) time. Each SCAN-operation is of complexity O ( I N ( k ) I). Since each vertex is scanned at most once, the total a m o u n t for scanning is O( IE I)- At most O( ]V I) calls to BLOSSOM are possible, each of which can be p e r f o r m e d in O( IV l) time. The CONTROL-routine requires the c o m p u t a t i o n of a m i n i m u m which is of complexity O(IVl). Since at m o s t O(IVl) GROW- and BLOSSOM-Steps are possible, CONTROL requires O( IV ] 2) time. Hence, the overall complexity is O ( ] V ] 2 ) , too.

5. An O( IE l log IV [)-labeling method for shortest even paths


We now show h o w the procedure can be implem e n t e d in O ( I E l l o g l V ] ) time via the use of special data-structures. To obtain the required b o u n d we first have to decrease the a m o u n t spent on the CONTROL-operation. T o enable an efficient determination of 8 = min{ 81, 82 } and u p d a t i n g of the vertex-labels we define for all edges (j, k ) ~ E with j, k outer vertices and k = PRED(j): dj, k = ( d ; + d~ + Ckj), a n d we store these edges in a priority-queue P Q E ordered by these d-values. Also, we store the non-tree vertices with a finite (tentative) d--label in a priority queue PQV ordered by the d--values. By priority queue we m e a n a list structure which allows the operations of finding the m i n i m u m in constant time and a d d i n g / d e l e t i n g an element or changing the value of an element to be p e r f o r m e d in O(log K) time where K is the size of the list (cf.

[11).
Then, the SCAN- and CONTROL-Operations are modified as follows: SCAN(k): for all j ~ N(k) if j ff V(T) set d~- .-= min{d~-, d~ + Ckj } if d j- has changed 257

Volume 21, Number 5

INFORMATION PROCESSING LETYERS

18 November 1985

set PRED(j).'= k adjust position of j on P Q V respectively p u t j o n t o PQV else if j outer vertex of T and BASIS(J)4: BASIS(k) set djk := (d;- + d~- + Ckj ) place (j, k) on PQE. CONTROL: Set 81 := m i n ( d ; - ]j on P Q V ) 8 2 := OO If P Q E n o t e m p t y take topedges (k, j) from P Q E until BASIS(k):/: BASIS(j) holds for topedge (k, j) or P Q E runs empty. If P Q E n o t empty, set 8 2 := d kj set 8 = m i n ( 8a, 8 2 }. If 8 = oo, STOP, there is no even ( i - j)-path in G. If 8 = 8~ set g equal to the vertex o n top of PQV if g = j, STOP, shortest even ( i - j ) - p a t h has been f o u n d else r e m o v e g from PQV a n d goto GROW(g) Otherwise, (8 = 8 2 ), let (E, k) be the topedge on PQE remove (g,k) from PQE and goto BLOSSOM(g, k). Additionally, in INITIALIZATION all nodes k N ( n + i) are p u t on PQV. A n edge or vertex can be taken off the appropriate priority queue at m o s t once or twice so that all operations p e r f o r m e d by CONTROL use no m o r e t h a n O( IE l) time while the total a m o u n t s p e n t on SCAN-operations increases to O( [E Ilog I E l) = O( IE Ilog IV I) due to the use of P Q V a n d PQE. Second, we have to reduce the a m o u n t spent on the BLOSSOM-subroutine. There are two different operations to be p e r f o r m e d in BLOSSOM: d e t e r m i n e the vertices spanning C, and link all nodes spanning C using the BASISlabel. (Note t h a t the a m o u n t spent on scanning the newly o u t e r vertices in C is not attributed to BLOSSOM.) T h e d e t e r m i n a t i o n of the m e m b e r s of C can be p e r f o r m e d b y simultaneously backtracking f r o m 258

vertices E and k towards the ' r o o t ' n + i using the PRED-label. Thereby, after at m o s t 2 IC I steps the base b of C is determined. Each node except the canonial element b of C will not contribute to the cardinality I C'I of a b l o s s o m C' which is detected in a later stage of the procedure. Since at m o s t O( IV I)calls to BLOSSOM are possible, determination of m e m b e r s h i p in blossoms a m o u n t s to O( IV I) steps overall. If in addition the canonial element, i.e., pseudon o d e name, of a newly s h r u n k blossom C is chosen to be the canonial element of the largest blossom inside, then the BASiS-values need not be changed for nodes in the largest p s e u d o n o d e contained in C. In this case, whenever BASIS(i) changes the cardinality of the set B(i) = {j IBASIS(j)= BASIS(i) } at least doubles and thus for any node i ~ V(H) the pointer BASIS(i) can change at most logIV[ times and thus the total a m o u n t required for-updating BAsis-pointers is O( IV ]log IV I). Alltogether, the a m o u n t spent on BLoSSOM-Operations is at most O( IV Ilog I v I ), which enables a total complexity of O ( I E Ilog IV I) for the whole procedure. Thus, the use of priority queues for storing path labels improves the r u n n i n g time of the general ' a u g m e n t i n g path a p p r o a c h ' for sparse graphs.

R e f e r e n c e s

[1] A.V. Aho, J.E. Hopcroft and J.D. Ullman, The Design and Analysis of Computer Algorithms (Addison~Wesley, Reading, MA, 1974). [2] M.O. Ball and U. Derigs, An analysis of alternate strategies for implementing matching algorithms, Networks 13 (1983) 517-549. [3] U. Derigs, A shortest augmenting path method for solving minimal perfect matching problems, Networks 11 (1981) 379- 390. [4] U. Derigs, Solving matching problems via shortest paths, WP 83263-OR, Institut ft~r Okonometrie und Operations Research, Universit~tt Bonn, 1983. [5] J. Edmonds, Maximum matching and a polyhedron with 0, 1-vertices, J. Res. Nat. Bur. Standards 68B (1965) 125-130. [6] M. Grrtschel and W.R. Pulleyblank, Weakly bipartite graphs and the max-cut problem, OR Lett. 1 (1981) 23-27. [7] E. Lawler, Combinatorial Optimization: Networks and Matroids (Rinehart & Winston, New York, 1976).