A Data Structure for Dynamic Trees

Embed Size (px)

Citation preview

  • 8/9/2019 A Data Structure for Dynamic Trees

    1/30

    Re intcd from JOURNAL OP COMPUTERND Smmm SCIENCESAI Ki gh ts Reserved by Academic Press, New York and London Vol. 26, No. 3, June 1983Printed in Belgium

    A Data Structure for Dynamic Trees

    DANIELD. SLEATORAND ROBERT NDRETARJANBell Laboratories, Murray Hill, New Jersey 07974

    Received May 8, 1982; revised October 18, 1982

    A data structure is proposed to maintain a collection of vertex-disjoint trees under asequence of two kinds of operations: alink operation that combines two trees into one byadding an edge, and acut operation that divides one tree into two by deleting an edge. Eachoperation requiresO(log n) time. Using this data structure, new fast algorithms are obtainedfor the following problems:

    (1) Computing nearest common ancestors.

    (2) Solving various network flow problems including finding maximum flows, blocking

    (3) Computing certain kindsof constrained minimum spanning trees.(4 ) Implementing the network simplex algorithm for minimum-cost flows.

    flows, and acyclic flows.

    The most significant application is (2); anO(mn log n)-time algorithm is obtained to find amaximum flow in a networkof n vertices andm edges, beating by a factor oflog n the fastestalgorithm previously known for sparse graphs.

    1. INTRODUCTION

    In this paper we consider the following problem: We are givena collection ofvertex-disjoint rooted trees. We want to represent the trees bya data structure thatallows us to easily extract certain information about the trees and to easily update the

    structure to reflect changes in the trees caused by three kindsof operations:link(v, w ) : If v is a tree root and w is a vertex in another tree, link the trees

    containing v and w by adding the edge(v,w), making w the parent ofv.cuf (v ) : If node u is not a tree root, divide the tree containingv into two trees by

    deleting the edge fromu to its parent.everf(v): Turn the tree containing vertexu inside out by makingv the root of

    the tree.

    We propose a data structure that solves this dynamic trees problem. We give two

    versions of the data structure. The first has a time bound of O(1ogn) per operationwhen the time is amortized over a worst-case sequenceof operations; the second,362

    0022-oooO /83 $3.00Copyright 0 1983 by Academic Press, Inc.All rights of reproduction in any form r e ~ ~ e d .

  • 8/9/2019 A Data Structure for Dynamic Trees

    2/30

    A DATA STRUCTURE FOR DYNAMIC TREES 363

    slightly more complicated, has a worst-case per-operation time boundof O(log n). We use our dat a structure t o devise- new fast algorithm s for the following graph-theoretic problems:

    (1 ) Computing nearest common ancestors in O(logn) time per operation.(2 ) Finding various kinds of network flows, including maximum flows in

    O(nm log n) ime,* blocking flows inO(m og n) ime, and acyclic flows inO(m og n)time.

    (3) Computing certain kinds of constrained minimum spanning trees inO(m og n ) ime.

    (4 ) Implementing the network simplex algorithm for the transportationproblem so that updatinga feasible tree solution takes O(1ogn) time per pivot step.

    The paper consists of six sections. In Section2 we formulate a precise versionof

    the dynamic trees problem and briefly discuss variations of the problem. In Section3we present a high-level description of the first versionof our data structure and carryout some preliminary running time analysis. The key idea presented in this section isthe partitioning of each tree intoa collection of vertex-disjoint paths. In Section4 wediscuss how to represent individual paths a s biased trees and complete the descriptionand analysis of the data structure. In Section5 we develop the second versionof ourdata structure. Section6 contains applications, related work, and additional remarks.Our results extend and improve the preliminary work of Sleator and Tarjan[181.

    2. THE DYNAMIC REESPROBLEM

    We shall consider the following version of the dynamic trees problem. We wish tomaintain a forest of vertex-disjoint rooted trees,3 each of whose edges hasa real-valued cost, under a sequence of eight kinds of operations, which can be intermixed inany order (see Fig.1):

    purent(vertexu): Return the parent ofu. If u has no parent (it is a tree root),return a special valuenull.

    root(vertex u ) :

    cost(vertexu ) :

    mincost(vertex u ) :

    Return the rootof the tree containingu.Return the cost of the edge(u,parenf(u)) .This operation assumes

    Return the vertex w closest to root(u) such that the edgethat u is not a tree root.

    I IfJand g are functions of x, the notation f(x) is O(g(x)) means there are positive constants c , and

    When discussing graph problems we denote by n the number of vertices and by rn the number of

    Our tree terminology is the same as that of Bent el al. [2],except that we use the term external node

    cz such that J ( x )

  • 8/9/2019 A Data Structure for Dynamic Trees

    3/30

    364 SLEATOR A N D TARJAN

    a

    t C )

    S

    uf 4

    W Y 2

    FIG. 1. Operations on dynamic trees. (a) Three trees. Operation parenr(n) returns m , root(n) returnsa , cost(n) returns 6, mincost(n) returns g. (b) Tree containing n after update(n, 1) . (c) Tree formed bylink@, t , 7) . (d) Trees formed by cut@) on tree in part (b). Value returned is 4. (e) Tree formed byeverr(n) on tree in part (d). ,

    (w,parent(w)) has minimum cost among edgeson the tree path fromu to root(u).This operation assumes thatv is not a tree root.

    update(vertex v , real x):Modify the costs

    ofall edges on the tree path from

    utoroof(u)by addingx to the costof each edge.

    link(vertex u, w, eal x): Combine the trees containingu and w by adding the edge(u, w ) f cost x, making w the parent ofv . This operation assumes thatu and w are indifferent trees andv is a tree root.

    cuf(vertex0): Divide the tree containing vertexv into two trees by deleting theedge (v,purenf(v) ) ;eturn the costof this edge. This operation assumes thatu is nota tree root.

    everf(vertexu) : Modify the tree containing vertexv by making u the root. (This

    operation canbe regarded a s reversing the direction of every edge on the path fromuto the original root.)

  • 8/9/2019 A Data Structure for Dynamic Trees

    4/30

    A DATA STRUCTURE FOR DYNAMIC TREES 365

    The operationsparent, root, cost, and mincost extract information from the forestwithout altering it. The operationupdate changes edge costs but not the structureofthe forest. The operationslink, cut, and evert change the forest. These eightoperations allowus to solve a number of graph-theoretic problems, as we shall see inSection6.

    The d ata structure we shall develop to support these operations can be m odified tohandle slightly different operations as well. Some possible variations are thefollowing:

    We can drop the operationevert, allowing some simplification of the datastructure. We have included theevert operation to allow representation of free(unrooted) trees: We represent each free tree by a rooted tree and applyeuert asnecessary to change tree roots. In applications involving rooted trees directly, thisoperation is generally unnecessary.

    (2) We can add the operationupdate edge(v,x), which addsx to the cost ofthe edge (u,parent(v)) .Note that if we allowevert, the operationupdate edge(u,x)can be simulated by the sequencew := root(v), evert(parent(v)), update(v,x ) ,evert(w).

    (3) We can add the operationupdate all(v ,x) , which addsx to the cost of alledges in the tree with rootu.

    (4) We can associate costs with the vertices rather than with the edges.( 5 ) Instead of real-valued costs combined by minimization and updated by

    addition, we can allow the costs to the elements of an arbitrary (but fixed) semigroup,with the operations redefined approp riately.For a discussion of this generalization inthe case thatlink is allowed but neithercut nor evert see Tarjan [20].

    In our discussion of the dynamic trees problem we shall assume that the initialforest consists ofn single-vertex trees; we shall usem to denote the total number ofoperations of the eight types. In stating time bounds we assumen 2 2.

    Before considering sophisticated solutions to the dynamic trees problem, it isworthwhile to examine the obvious solution: with each vertex0, we store its parentp ( v ) and the cost of the edge(v,p(u)). Using this representation we can carry ou t a

    parent,Cost,

    link, or cut operation in0(1)

    time. The time for each of the other fouroperations is proportional to the length of the tree path fromu to root(v), which isO ( n ) in the worst case.

    By using an implicit representation of the structure of the forest, we can reduce thetime for root, min, update, and evert to O(1ogn ) , at the cost of increasing the time forthe other operations to O(1ogn). In the next three sections we develop two suchimplicit representations.

    (1 )

  • 8/9/2019 A Data Structure for Dynamic Trees

    5/30

    366 SLEATOR AND TARJAN

    3. DYNAMIC REESAS SETS OF PATHS

    We shall present our solution to the dyn amic trees problem ina top-down fashion.We begin by assuming that we know how to solvea version of the problem for thespecial case in which the trees are paths. More precisely, suppose we know how tocarry out an intermixed sequence of the following11 kinds of operations on acollection of vertex-disjoint paths, each of whose edges hasa real-valued cost:

    puth(vertex u ) :

    heud(pathp) :

    tui l (pathp):

    before(vertexu):path, returnnull.

    ufter(vertex u ) :return null.

    pcost(vertex u) :

    prnincost(pathp):

    Return the path containingu. (We assume each path hasa uniqueidentifier.)

    Return the head (first vertex) ofp .

    Return the tail (last vertex) ofp .

    Return the vertex beforeu on puth(u) . If u is the head of the

    Return the vertex afteru on puth(u) . If u is the tail of the path,

    Return the cost of the edge(u , uffer (u) ) .This operation assumesthat u is not the tail ofputh(u) .

    Return the vertexu closest to t u i l (p ) such that (u, uffer (u) ) asminimum cost among edges onp . This operation assumes thatp contains more than

    one vertex.pup dute (path p, real x):

    reuerse(pathp):

    Add x to the cost of every edge onp.

    Reverse the direction ofp , making the head the tail and viceversa.

    concutenute(pathp, q, real x):

    split(vertexu ) :

    Combine p and q by adding the edge( tu i l (p ) ,heud(q))of cost x. Return the combined path.

    Divide puth(u) into (up to) three parts by deleting the edgesincident tou. Return a list[p , q, x, y ] , wherep is the subpath consisting of all verticesfrom head(puth(u) ) to before(u), q is the subpath consisting of all vertices fromufter(v) to tui l (puth(u)) ,x is the cost of the deleted edge(before(u),u) , and y is thecost of the deleted edge(u,ufter(u)). f u is originally the head ofputh(u) ,p is null andx is undefined; ifu is originally the tail ofputh(u) , q is null and y is undefined.

    Using these path operations as primitives, we can develop a solution to thedynamic trees problem. We partition each tree intoa collection of vertex-disjointpaths and carry out each tree operation by means of one or more path operations.We shall present two variations of this approach:naive partitioning, which gives an

    O(logn ) amortized time bound per tree operation, andpartitioning by size, whichgives an O(1ogn) worst-case time bound per tree operation. In this and the next

  • 8/9/2019 A Data Structure for Dynamic Trees

    6/30

    A DATA STRUCTURE FOR DYNAMIC TREES 367

    section we develop the naive p artitioning m ethod; in Section5 we discuss partitioningby size.

    Nu iue Par it ioning

    In the naive partitioning method, the pa rtition of each tree into path s is determinednot by the structure of the tree but by the sequence of tree operationsso fa rperformed. We partition the edges of each tree into two kinds,solid and dashed, withthe property that at most one solid edge enters any vertex. (See Fig.2.) Thus the solidedges definea collection ofsolid paths that partition the vertices.(A vertex with noincident solid edge isa one-vertex solid path.) The head of a path is its bottommostvertex; the tail is its topmost vertex.

    We represent the dashed edges using the obvious method discussed at the endofSection2: with each vertexu that is the tail of a solid path, we storedpurenf(u) , heparent of u (via the outgoing dashed edge), anddcosf(u) , the cost of the edge(u,purenf(u)) . f u is a tree root, dpurenf(u)=null and dcosf(u) s undefined. Wemanipulate the solid paths using the eleven path operations defined. In addition weneed two composite operations (see Fig.3):

    splice(pathp): Extend the solid pathp by converting the dashed edge leavingtu i l (p) to solid and converting the original solid edge enteringpurent ( fu i l (p) ) if any)to dashed. Return the extended path. This operation assumes thatf u i l ( p ) s not a treeroot (that is, there is a dashed edge leavingf u i l ( p ) ) .

    Create a single solid path with headu and tail roof(u) byconverting dashed edges to solid a long the tree path fromu to root(u) and convertingsolid edges incident to this path to dashed. Return the resulting solid path.

    expose(vertexu ) :

    We implementsplice and expose as follows, where our algorithmic notation is aversion of Dijkstras guarded command language[ S ] augmented with functions andprocedures and with the vertical bar1 used in place of the box0:

    function splice(path p ) :vertex u ; path q, r; real x , y ,u := dpurent( tui l (p)) ;[q , r , x ,y ] := sp l i f (u) ;ifq # null -+ dpurent(fui l (q)) , c osf(fu i l (q)= u, x ti;

    return if r = null -, p

    1.

    2 . p := concutenute(p,puth(u), dc ost( tuil(p )));

    3 .

    end splice;

    [ r # null -, concutenute(p, r , y )fi

    Note. Line 1 of splice converts to dashed the solid edges (if any) incident tou =purenf ( fu i l (p ) ) .Line 2 converts to solid the dashed edge leavingfu i l (p ) .Line 3

  • 8/9/2019 A Data Structure for Dynamic Trees

    7/30

    368 SLEATOR AND TARJAN

    FIG. . A tree partitioned into solid paths. Path It , 9 ,p . , i, dl has head t and tail d .

    FIG. . Splice and expose. (a) The effect of splice(p). The letters 9, r, and u refer to thecorresponding variables in the program for splice. (b) The effect of expose(v).

  • 8/9/2019 A Data Structure for Dynamic Trees

    8/30

    A DAT A STRUCTURE FOR DYNAMIC TREES 369

    converts to solid the dashed edge leaving u. To make this program robust, an errorcheck should be added to ensure that on entry dparent(tail(p)) z ull. I

    function expose(vertex u ) ;

    pathp, q, r ; real x, Y ;1. [q, r, x, y ] := spZit(u);

    if q # null + dprent(tail(q)), dcost(tail(q)) := u, x ti;i f r=nul l+p :=purh(u);

    2.fi,

    3. do dpurent(taiZ(p)) # null + p := spZice(p) od;returnp

    end expose;

    Note. Line 1 of expose converts to dashed the solid edges (if any) incident to u.Line 2 restores to solid the edge out of u if it has just become dashed. Line 3, themain part of expose, is a do loop that extends the path containing u by splicing untilits tail is the tree root.

    I r # null + p := concatenute(path(v), ,y )

    I

    We implement the eight tree operations as follows:

    functionparent(vertex u);return ifo = tuiZ(path(u)) 4 parent(u)

    I u # tail(path(u)) + Llfter(u)ti

    end parent;

    function root(vertex u ) ;

    end root;

    function cost(vertex u ) ; .

    return tail(expose(u))

    return if u = tail(path(u)) -t dcost(u)1u # taiZ(path(u)) +pcost(u)

    ti

    end cost;function mincost(vertex u ) ;

    returnpmincost(expose(u))end min;

    procedure update(vertex u, real x ) ;

    end update;

    procedure link(vertex u, w, real x) ;

    end link;

    pupdate(expose(u), x )

    concatenate(path(v), expose(w), x )

  • 8/9/2019 A Data Structure for Dynamic Trees

    9/30

    370 SLEATOR AND TARJAN

    function cut(vertex 0);path p , q ; real x , Y ;expose(v);[P , 4, x, v ] = S p W v ) ;dpurent(v) := null;return y

    end cut;

    procedure euert(v);

    end evert;

    We allow a functionto be used as a procedure. In such a use the valuereturned is ignored. Functionconcatenate is so used in link, and expose is so used incut. There are simpler ways to implementlink and cut; we have chosen these methodsfor technical reasons discussed in Section4.

    Analysis of Expose

    Having specifiedan implementation of the dynamic tree operations in termsofpath operations, we can begin a running time analysis. At this levelof detail, the onlynontrivial task is to count the number of splice operations per expose. In theremainder of this section we shall derive anO(n +m log n ) bound on the numberofsplices caused by a sequence ofm tree operations. This bound implies tha t there areO(1ogn ) splices per expose amortized over the sequence.

    The O(n +m log n ) bound on splices is implicit in the work of Galil and Naamad[8 ] and S hiloach [16 ], although they did not consider evert operations. Galil andNaamad obtained the bound by applying an upper bound for path comparison 1191;Shiloach gavea somewhat obscure direct proof. We shall give a simple direct proofthat accommodates evert.

    To carry out the proof we need one new concept. We define thesize of a vertexuin the forest, denoted bysize(v) ,to be the number of descendants ofu, including uitself. We define a tree edge(u ,parent (v) ) o be heavy if 2 size(u)> size(parent(u))and light, otherwise. The following result is obvious:

    reuerse(expose(v));dparent(v):= null

    Remark.

    LEMMA1. Let u be any vertex. Then 1

  • 8/9/2019 A Data Structure for Dynamic Trees

    10/30

    A DATA STRUCTURE FOR DYNAMICTREES 37 1

    splice increases the number of solid edges by one; a normal splice leaves the numberof solid edges unchanged.

    THEOREM. There are at most m special splices.

    Proof. Let #solids, #exposes, #specials, #links,and #cuts be the number of solidedges, expose operations, special splices, links, and cuts, respectively, all as afunction of time. There are#links - #cuts tree edges; thus 'solids < #links - #cuts.Lines 1 and 2 of expose decrease #solids by at most one; line3 of expose increases#solids by at most one per special splice. Outside ofexpose, link increases #solids byone and cut decreases #solids by at most one. Thus'solids 2 #specials + 'links -xexposes- cuts, which means 'specials< "exposes + 'solids - Sinks + 'cuts r ank(p) and rank(s)2 ank(p ) .

    Let w t ( v )be the weight ofv before the splice andwt (u ) he weight ofv after thesplice; similarly for rank(v) and rank(v) . Then rank(s)> rank(v)+ 1 = 1 +[Ig wt(v)J2 + [Igw t ( p ) J> rank(p) . (Since the splice is normal,s # v . Since v is anexternal node, it has rank strictly less than that of its parent by property (ii). Theremaining inequalities follow from property (i) and the definition of weights.) Thesame argument shows thatrank(s)> r ank(p ) .

    The amortized time for the split in line1 of splice is O ( r a n k ( s ) - r a n k ( v ) ) =O(rank(s ) - rank(p) ) . Since the split is normal, it leaves an excess ofr ank(s ) -rank(q)+ 1>0 credits, whereq is as defined in the implementation of splice. Wehave rank(s)2 ank(o) + 1 >/ 1 + [lg wt(u)J2 + [lg wt(q)J> rank(q). Further-more, r a n k ( s ) > r a n k ( p ) by Lemma3. The number of credits needed for the

    Proox

    FIG. . A normal splice.

  • 8/9/2019 A Data Structure for Dynamic Trees

    18/30

    A DATA STRUCTURE FOR DYNAMIC TREES 379

    concatenation in line 2 of splice isI rank(p) - ank(u)l + 1 6 2 rank(s) -r ank(p )- ank(u)+ 1 6 rank(s) - ank(p )+ runk(s) - ank(q)+ 2, since prop-erty (i) impliesrank(u)> runk(q) - 1. Since we haverank(s) - ank(q) + 1 creditson hand, the number of new credits we need isrank@)- a n k ( p )+ runk(s) -rank(s)+ 1 Q 2(rank(s)- a n k ( p ) )+ 1.The last operation is the concatenation in line3 of splice. If it takes place, thereare rank(s) - unk(r) + 1 >0 credits still available from the split in line1, where r isas defined in the implementation of splice. The number of credits needed for theconcatenation is at most 2 rank@)- unk(p )- ank(r) + 1 since rank(s)>max(rank(p),rank(r)} by Lemma3. Thus the number of new credits needed is atmost 2(rank(s)- ank(p ) )by the same argument.

    Combining the estimates for the split and the two concatenates, we obtain theclaimed boundof O(rank(s)- ank(p ) + rank(s)- a n k ( p ) )on the amortized timeof the splice. Note thatrank(s)> r ank(p) and rank(s )> r ank(p ) . If we sum thisestimate over all the normal splices that take place during a single expose, the sumtelescopes, and Lemma5 gives a bound ofO(k + og n ) or the amortized time takenfor the entire expose, wherek is the number of normal splices. From this andTheorem2 we obtain anO (m log n) bound on the timefor the normal splices duringall m dynamic tree operations, and hence anO(m og n) bound on the total time forthe tree operations. 1

    5. PARTITIONINGY SIZE

    The data structure describedin Sections 3 and 4 is a good one if amortizedrunning time is the relevant complexity measure, which is the case in most of theapplications known to the authors. If worst-case per-operation running time isimportant, we can modify the data structureso that each individual dynamic treeoperation takes O(logn) time. In this section we present the required modifications.The modified structure is more complicated than the original, meaning that theoriginal is likely to perform better in practice. Thus the new structure is mainly oftheoretical interest. We shall omit some of the details, concentrating on the mainideas.

    As in Section3, we decompose each tree into vertex-disjoint solid paths, but wedefine the solid edges according to the tree structure rather than according to thesequence of previously performed tree operations. Specifically, we call an edge(u, w )solid if and only if it is heavy, namely, if and onlyif 2 size(u) > size(w). Thus,whereas in Section3 we used light and heavy edges only as an analytical tool, herethey become part of the data structure. With this definitiona tree path from a vertexu to root(u) contains at most [lgnj dashed edges by Lemma1 and thus intersects atmost [Ign ] + 1 solid paths.

    We implement the dynamic tree operations usingexpose as we did in Section3. A

    new problem arises: an expose can convert light edges to solid and heavy edges todashed, violating the definition of solid edges. Thus we must conclude each tree

  • 8/9/2019 A Data Structure for Dynamic Trees

    19/30

    380 SLEATOR AND TARJAN

    operation by repairing the damage caused by theexpose. For this purpose we use thefollowing operation:

    conceul(pathp): Convert to dashed every light edge on pathp , and to solid everyheavy edge incident top , including the heavy edge,if any, incident toheud(p).

    We implement the eight tree operations as follows:

    functionpurent(vertex 0);(same as in Section3)

    function root(vertexu ) ;Pa& P ;p := expose(u);u := tu i l (p) ;conceal(p )

    return uend root;function cost(vertex0);

    (sameas in Section3)function m incost(vertexu ) ;

    pathp; real x ;p := e x p s e ( u ) ;x :=pmincost(p);conceal(p )

    return xend min;function updute(ver texu, real x ) ;

    Pa& P;p := expose(0);Pupdatdp , x ) ;conceul(p )

    end update;procedure link(vertexu, w, eal x ) ;

    end link;

    functioncut(vertexu ) ;Path P, 9, r; real x ,y ;p := e x p s e ( u ) ;[q, r, x ,y ] := split(u);dpurent(u) := null;conceal(r );conceuZ( path (u ) ) ;

    conceul(concutenute( uth(u),expose(w, x ) )

    end cut;

  • 8/9/2019 A Data Structure for Dynamic Trees

    20/30

    A DATA STRUCTURE F OR DYNAMIC TREES 38 1

    Note. In the program forcut, the exposed pathp is broken by the cut into twoparts, the path consisting only of vertexv and the path r. Both parts must beconcealed. I

    procedure evert(v);pabp;p := expose(v);reverse( p )2iparent(v) := null;conceal( p )

    end evert;

    New Features of the Data Structure

    In order to implementconceal efficiently, we must make three changes in the datastructure. The first change is to add two fields to each internal node in a binary tree.(See Fig.9.) If u is an internal node corresponding t o an edge(v, w ) n the solid pathp, we definelefttilt(u) to be the sum of weights of vertices on the subpath o f p fromhead(p ) to v minus the weight ofw. The condition tha t(v, w ) s heavy is equivalentto lefttilt(u) > 0 . We define leftmin(u) to be the minimum .oflefttilt(t)for t an internaldescendant ofu. We need leftmin to locate light edges duringconceal operations. Tohandle reverse, we also need the symmetric valuesrighttilt(u), defined to be the sumof weights of vertices on the subpath ofp from w to t a i l ( p ) minus the weight ofu,and rightmin(u), defined to be the minimum ofrighttilt(t)fo r t an internal descendant

    of u.To represent these values, we use two fields for each internal nodeu :

    netleftmin(u) = leftmin(u) if u is a binary tree root;

    leftmin(u) - efrmin(bparent(u)),otherwise.

    netrightmin(u) = rightmin(u) if u is a binary tree root;

    rightmin ( u ) - ightmin (bpare nt(u )), therwise.

    As with bleft and bright, and bhead and btail, the values of netleftmin(u) andnetrightmin(u) are interchanged if the reversal stateof u is true.

    Starting from the binary tree root corresponding toa solid pathp and proceedingdownward in the tree, we can computeleftmin and rightmin for each internal nodevisited in O( ) time per node. We can also computelefttilt and righttilt for each nodevisited in 0 ( 1 ) time per node, using the following observations: Ifu is an internalnode representing an edge(v, w), oth v and w are accessible fromu in 0 ( 1 ) time viableft, bright, bhead, and btail. Thus w t ( v ) and w t ( w ) are available in 0 ( 1 ) time.Furthermore the sum of weights of vertices beforew (after v ) on p is the sum ofweights of internal nodest such that t is the left (right) child of an ancestor ofu but t

    is not an ancestor ofu. We can compute these two sums for each internal nodeu inO( ) time per node while descending from the tree root. Henceforth we shall assume

  • 8/9/2019 A Data Structure for Dynamic Trees

    21/30

    382 SLEATOR A N D TA R J A N

    ( 0 )

    FIG. . Fields to represent lefttilt and righttilt. (a) Format of internal nodes, omitting external bits

    and fields shown in Fig. 5. (b ) Values of fields representing a path whose vertices have weights 6, 3, 2, 4,9, assuming all reversed bits are false.

    that leftmin(u), rightmin(u), lefttilt@),and righttil t@)are available whenever we visitan internal nodeu.

    It is easy to augmentconstruct, destroy, rotateleft, and' rotateright so that theyupdate the netleftmin and netrightmin fields and still take0(1) time. We need oneadditional path operation:

    Return the vertexw closest to t a i l ( p ) such that (before(u),u ) islight; returnnull if there is no such vertex.

    l ight(pathp):

    We implement light in the same way aspmincost. Starting from the root of thebinary tree representingp , we proceed downward, usingleftmin and lefttilt to guidethe descent, until reaching the internal nodeu last in symmetric order such thatlefttilt@)

  • 8/9/2019 A Data Structure for Dynamic Trees

    22/30

    A DATA STRUCTURE FOR DYNAMIC TREES 383

    The third change is to add an extra part to our data structure.For each vertexv ,we maintaina path set containing each pathp such that ( ta i l (p) ,v ) is a dashed edge.We manipulate path sets by means of three operations:

    maxwt(vertex v): Return the path of maximum weight in the path set ofv ; returnnull if the path set is empty. (Recall that the weight of a path is the sum of theweights of its vertices.)

    insert(pathp, vertex 0):

    delete (path p, vertex 0):

    We represent the path set ofa vertex by a globally biased binary tree, with thepaths appearing as external nodes in left-to-right order by decreasing weight. Theweight of a path is also used to determine its rank in the tree.For each internal nodewe maintain a pointer to its leftmost leaf descendant. With this representation thetime to perform amaxwt operation is0(1), and the timeto perform insert(p, v ) ordelete(p,u ) is O(log(W/wt(p))), where W is the sum of the weights of the paths inthe path setof u before the operation. The bounds forinsert and delete follow fromLemmas 6 and 7 and the ordering of paths in the path set by weight.

    Insert pathp into the path set ofv.Delete pathp from the path set ofv.

    Implementation and Analysis of Expose and Conceal

    Since each path operation takes O(1ogn) time,so does each dynamic treeoperation, not counting the time forexpose and conceal operations. We haveimplemented the dynamic tree operationsso that the only necessary manipulation ofweights and path sets takes place insideexpose and conceal. Thus it remains for us toimplement and analyzeexpose and conceal. We implementexpose as in Section4,with additional statements to update the path sets. Here are the details, with theadditions indicated:

    function splicebath p ) ;vertex u ; path q, r ; real x , y ;v := dparent(tail(p));[ q ,r, x , y ] := split(u);w t ( v ) := w t ( v )- w t ( p ) ;

    => delete(p,v);if q # null +

    dparent(tail(q)), cost(tail(q)) = u, x ;

    insert(q, v )w t ( v ):= wt(u ) + wt(q) ;

    tip := concatenate(p, pat h(u ), dcost(tail(p)));return if r = null + p

    I r # null + concatenate( p , r , y )ti

    end splice;

  • 8/9/2019 A Data Structure for Dynamic Trees

    23/30

    384 SLEATOR AND TARJAN

    function exp se(ve rtexu ) ;pathp, q, r ; real x, y ;[q , r, x, y ] = split(u);

    . i f q # n u l l +

    dpurent(tuil(q)), dcost(tuil(q)):= u, x;wt(u ) := wt (u ) + w t ( q ) ;insert(q,u )

    fi;i f r = n u l l+ p : =p ut h( u)I r # null- ip := concutenate(puth(u),r , y )

    fi ;do dpurent(tuil( p ))# null -+ p := splice( p )od;returnp

    end expose;

    THEOREM. A n expose operation takes O(logn) time.

    Proox The argument is much like that in the proof of Theorem5. An exposeoperation takes O(1ogn) time plus time for O(1ogn) splice operations (one per lightedge on the dynamic tree path from the exposed vertex to the root). Consider the ithsplice. Letu be the tail of the path being spliced,p the path containingu before thefirst splice,p the path containingu just before the ith splice,u the parent ofu, s thepath containingu before the first splice, ands the path containingu (and u ) after theith splice. (See Fig.8.)

    The sp lit operation in splice takesO(runk(s)- unk(u)) ime. The delete operationtakes O (log(wt(u)/wt(p))) time. The insert operation, if performed, takes O (logwt(u) /wt(q)) = O( ) time, whereq is the inserted path, since the edge (tuil(q),u ) is heavy. Itfollows from Lemmas 1 and6 that the one or two concatenate operations needed tocomplete the splice takeO(runk(s)- unk(u)) ime, whererunk(u) is the rank ofuafter the ith splice.

    We also have r u n k ( p )< 1 + [lg wt(p )JQ 1 + [Igw t ( u ) J < 1 + runk(u). Thus thesplit takes O (ru nk (s) -ru nk (p )) time, and summed over all splices the total splittime is O(logn). Similarly sincew t ( u )Q w t ( s ) the delete takes O(log(wt(s)/wt(p)))

    time, and summed over all splices the delete time is O(logn). Finally, runk(p)Q 1 +[lg wt(p )J< 1 + [Igw t ( u ) J< runk(u), where wt (u ) is the weight ofu after the ithsplice, since wt(p)= wt(p)< size(u)/2, which means wt(p)Q size(u)- wt(p)Qw t ( u ) . Thu s the concatenates takeO(runk(s) - un k(p )) time, and summed overallsplices the concatenate time is O(1ogn). Combining these estimates we obtain thetheorem. I

    An operation conceul(p) is just like an expose run backwards. The onlycomplication is that we must find the light edges onp, which we do using pmintilt,and the heavy edges incident top, which we do using the path sets. The main loop of

    conceal proceeds backward (downward) alongp, performing the inverse of a splice,called a slice, each time it encoun ters a light edge. The behavior of slice is as follows:

  • 8/9/2019 A Data Structure for Dynamic Trees

    24/30

    A DAT A STRUCTURE FOR DYNAMIC TREES 385

    function slice(pathp): Convert the edge light(p) to dashed, dividing p into twopaths, say q and r. If any edge entering head(r) is heavy, make it solid. Return thepath q. This operation assumes that p contains at least one light edge, i.e., l ight (p)#null.

    The programs below implement slice and conceal. Their correctness is easy toverify.

    function slice (p a th p );vertex u; path q, r, s; real x , y ;u := l ight (p) ;[p , r, x, y ] = split(u);s := i f r = null -+pa th@) r # null + conca tenafe(path(u), ,y ) i;dpurent(tail(p)),dcost( tai l(p)).-- v , x;wt(u) := wt(u ) + w t ( p ) ;q := maxwt(o);if 2 . w t ( q )> w t ( u ) +

    w t ( v ):= wt(v)- wt(q);delete(q,0);concatenate(q,s, dcost(tail(q)))

    fi ;insert(p , 0);re-p

    end slice;

    procedure conceal(pathp ) ;vertex u ; path q, r, s; eal x , y ;do light(p)# null + p := slice(p)od;u := head(p)s := maxwt(u);i f s # null and 2 . wt(s) > w t ( u ) +

    [q, r , x , y ] := split(u);

    delete@,u ) ;s := concatenate(s, ath(u), dcost( tai l(s))) ;i fr f null --t concutenate(s,r, y ) i

    wt (u ) := w t ( u ) - wt( s ) ;

    fiend conceal;

    THEOREM . A conceal operation takes O(1og n ) time.

    Proof. The proof is like that of Theorem 6. A conceal operation takes O(1og n )time plus time for O(log n ) slice operations, since the light operation preceding each

    splice is repeated inside the slice. Consider a typical slice. Let p be the path sliced, letu= l igh t (p ) , let p be the path containing u after the slice (and thus after the

  • 8/9/2019 A Data Structure for Dynamic Trees

    25/30

    386 SLEATOR A N D TA R J A N

    FIG. 10. A slice.

    conceal), lets be the path returned by the slice, and lets' be the path containingtai l@)after the conceal. (See Fig. 10.)

    The light and split operations take O ( r a n k ( p )- unk(v)) time. The deleteoperation, if performed, takesO(log(wt(v) /wt(q)) )= 0(1) time, whereq is the deletedpath, since the edge(tail(q),v ) is heavy. The one or twoconcatenate operations takeO(runk(p ' ) - runk ' (v ) ) time, where rank'(v) is the rank sf v after the slice. Theinsert operation takesO(log(wt ' (v ) /wt ( s ) ) ) ,here wt ' (v) is the weight ofv after theslice.

    Since ( tai l@), ) is light, wt(s )

  • 8/9/2019 A Data Structure for Dynamic Trees

    26/30

    A DATA STRUCTURE FOR DYNAMIC TREES 381

    Finding Roots

    We obtain a primitive version of the dynamic trees problem if we allow only theoperations ofroot, link, cut, and evert. For the even more restricted problem with

    only root and link, the compressed tree data structure for disjoint set union[19,211 gives anO (n + ma(m +n, n))-time algorithm, wherea is a functional inverseof Ackermanns function. The version of the disjoint set union algorithm that usesunion by rankor size [21 ] but not path compression can be adap ted to handleroot,link, and cut in O(1ogn) time per operation. Although this method is relativelysimple, it does not seem to extend to alloweuert, nor to maintain information aboutthe paths in the trees.

    No Cuts or Everts

    Tarjan [201 has studied a version of the dynamic trees problem in which links are

    allowed but neither cuts nor everts, obtaining anO (n + ma(m + n, n))-time algorithmwith a number of applications. Tarjan uses the same idea we have used, ofpartitioning the tree edges into heavy and light.

    Nearest Common Ancestors

    Another useful operation on dynamic trees is the following:

    nca(vertex v, w ) : Return the nearest common ancestor ofu and w.

    The problem of implementing this operation along with some combination oflink,cut, and euert is the nearest common ancestor problem [1, 10, 131. Our data structureeasily adapts to this problem.To find the nearest common ancestor ofu and w, weexpose v and then exposew , during the second expose noting the first vertex encoun-tered on the previously exposed path fromv ; this is the nearest common ancestor.(With the data structure of Section5 we must finish the operation by concealingwand then u . ) The previous result closest to this is by Ahoet al. [11, who proposed amethod that allows links but neither cuts nor everts and runs inO ( ( m+ n) og n ) timeand O (n log n ) space. Maier [131 has given a more complicated method that reducesthe space required and allows cuts, though the time bound for a cutis O ( n ) . Ourmethod allows both cuts and everts, usesO ( n ) space, and runs in U(logn ) ime per

    operation.Network Flow

    A classic problem in network optimization is to finda maximum flow in a network(a directed graph with nonnegative edge capacities) from a given source vertexs to agiven sink vertext. A sequence of faster and faster algorithms have been devised forthis problem.For dense graphs, the best time bound known isO ( n 3 ) , ttained by thealgorithms of Karzanov[111 and Malhotraet al. [141. For sparse graphs, the bestpreviously known time bound isO(nm(1oga) ) , attained by the algorithmof Galiland Naamad [8], rediscovered by Shiloach [16]. Here n is the numberof vertices and

    m is the number of edges in the problem graph.Our data structure for dynamic trees gives an algorithm for this problem whose

  • 8/9/2019 A Data Structure for Dynamic Trees

    27/30

    388 SLEATOR AND TARJAN

    running time is O(nm log n). Our algorithm (as are all the fast network flowalgorithms) is based on the fundamental workof Dinits [ 6 ] . Dinits reduced themaximum network flow problem to the solutionof n blocking flow problems. Theblocking flow problem is to find a flow in an acyclic network froma source s to a

    sink t such that every path froms to t contains a saturated edge (an edge whose flowequals its capacity). Dinits algorithm for findinga blocking flow consists ofbeginning with the zero flow, discarding all zero-capacity edges, and repeating thefollowing step until there is no path froms to t :

    Augmenting Step. Find a path from s to t . Let c be the capacity of a minimum-capacity edge on the path. Sendc units of flow through the path, reducing the(residual) capacity of each edge on the path byc. Delete all edges whose capacity isnow zero.

    Since each execution of the augmenting step saturates at least one edge, there areat most m executions of the step. The hard part of the method is to find augmentingpaths and update edge capacities. The straightforward implementation of Dinitsalgorithm takesO ( n ) time per augmenting path plusO ( m ) time amortized over allpaths, fora total time ofO ( n m ) .Galil and Naamad[8] and Shiloach[161 discoveredan O(m(1og n))-time method that uses almost the same structure a s described inSections3 and 4, with balanced trees in place of biased trees representing solid paths.We have obtained our result by introducing biased trees and doing the requiredrunning time analysis.

    Our data structure gives anO (m

    logn)

    time bound for the blocking flow problem.We use dynamic treesto find augmenting paths and keep track of edge capacities.The algorithm maintainsa collection of trees consisting of at most one unsaturatededge leaving each vertex; these edges are the current candidates to form augmentingpaths. Initially each vertex is ina separate tree. We execute Dinits algorithm asfollows:

    Step 1.Step 2 (v # t ; extend path).

    Step 3 (all paths from0 to t are blocked).

    Let v = root(s).If v = , go to Step4; otherwise, go to Step2.If no edges leave vertexv, go to Step3. Otherwise,

    select an edge(v , w) eaving v and performl ink(v,w, upucity((v,w))). G o to Step1.If u = s, compute the unused capacity

    of every tree edge usingcost and stop. Otherwise, delete from the graph every edgeentering v. For each such edge(u , v ) that is a tree edge, performcut(u) ,recording theunused capacity. G o to Step1.

    Let u = mincost(s). (Edge(v,purent (v) ) s a minimum-capacity edge on the augmenting path.) Letc = cost(v) .Perform updute(s, -c). G o to Step5 .

    Step 5 (delete edges with no remaining capacity). Letv = mincost(s). If

    cost(v)= 0, delete (v,purent(u)) from the graph, performcut (v) ,recording an unusedcapacityof zero, and repeat Step5 . Otherwise,(cos t (v)> 0), go to Step 1.

    Step 4 ( 0 = t ; the tree path froms to t is augmenting).

  • 8/9/2019 A Data Structure for Dynamic Trees

    28/30

    A DATA STRUCTURE FOR DYNAMIC TREES 389

    When the algorithm stops, we can compute the final flow from the original andunused capacities. The algorithm requiresO ( m ) dynamic tree operations and thustakes O (m log n ) time. Using this method for finding blocking flows we obtain anO(nm log n ) time bound for the maximum flow problem.

    Another flow problem amenable to this approach is theacyclicjlow problem:givena flow from s to t in an arbitrary network, reduce it to an acyclic flow by repeatedlyfinding a cycle of flow and reducing the flow around the cycle to zero. Anappropriate version of the blocking flow algorithm will solve this problem inO (m log n ) time. We begin with each vertex ina separate tree, delete all zero-capacityedges, and carry out the following steps:

    Step 1. Let v = root(s).If no edges leave vertexv , go to Step 2. Otherwise, selectan edge ( v , w ) leaving v. If roo t (w)=u , go to Step3. Otherwise, performl ink(v, w,flow ((v, w ) ) )and repeat Step1.

    If v = s, compute the current flow ofevery tree edge usingcost and stop. Otherwise, delete from the graph every edgeentering v . For each such edge(u , v ) that is a tree edge, perform recording thecurrent flow. Go to Step1.

    Let u = mincost(w). Let c =min{flow((v,w ) ) ,cost(u)}. Reducef l ow ( ( v, w ) )by c and perform update(w, -c) . G oto Step4.

    Step 4 (delete tree edges with no remaining, flow). Letu =mincost(w). Ifcost@) = 0, delete (u,parent(u)) rom the graph, performc u t @ ) ,recording a currentflow of zero, and repeat Step4. Otherwise (cost(u) > 0), go to Step1.

    Step 2 (all paths fromv to t are acyclic).

    Step 3 (a cycle of positive flow has been found).

    Constrained Minimum Spanning Trees

    Gabow and Tarjan [ 7 ] consider the following problem: Given a connectedundirected graph whose edges are of two colors, say white and black, and have realvalued costs, find a spanning tree of minimum total edge cost containing exactlykblack edges for some integerk . They describe an algorithm for this problem whosecritical par t consists of repeating the following stepn - 1 times: givena spanning treeT and a non-tree edge{ v ,w } , ind the maximum-cost edge, say( x , y } ,on the (unique)tree path joiningv and w ; form a new treeT by deleting(x, y ) and adding ( v , w ) .Wecan use our data structure to carry out this process in O(1ogn ) time per swap foratotal of O(n log n ) time.

    The Network Simplex Algorithm

    An edge-swapping process almost identical to the one described above occurs inthe network simplex algorithm for the transportation problem[4] . Operationsresearchers have proposed various data structures to represent the so-called feasibletree solutions in this problem, but they all takeO ( n ) time per swap in the worst

    case. With our data structure the time per swap isO(1ogn). Although theoreticallythe network simplex algorithm needs exponential time in the worst case, it is heavily

    #

  • 8/9/2019 A Data Structure for Dynamic Trees

    29/30

    390 SLEATOR AN D TA R J A N

    used in practice, and our result offers hope of further improving its practical perfor-mance.

    Remarks and Further Research

    Much work remainsto be done on the dynamic trees problem. One direction forfuture work is to implement our da ta structures and perform em pirical experimentstodetermine their practical value, if any. Sleator has implemented a dynamic treesalgorithm for maximum network flow. It is slower by a small constant factor (abouttwo) than the algorithmof Dinits except on specially constructed worst-case examplegraphs. This merely reflects the fact thatof randomly generated graphs onlya smallfraction make Dinitss algorithm perform poorly. Some experiments with our methodapplied to the network simplex algorithm m ight be rewarding.

    The amortized-time versionof our data structure uses locally biased binary trees,

    whereas the worst-case version uses globally biased trees. Globally biased trees willwork in the amortized-time version but locally biased trees will not work in theworst-case version. Further work to improve and simplify the data structure may bevaluable. Recently the authors have discovered a new kind of search tree, called aself-adjusting search tree[22] , which can be substituted for biased binary trees in thedata structure of Sections3 and 4. This produces considerable simplification. (SeeTarjan [23] . )

    REFERENCES

    1. A. V. AHO , J. E. HOPCROFT,N D J. D. ULLMAN,On finding lowest common ancestors in trees,

    2. S. W. BENT,D. D. SLEATOR, ND R. E. TARJAN, iased2-3 trees, in Proc. Twenty-First Annual

    3. S. W. BENT, D. D. SLEATO R,N D R. E. TARJAN, Biased Sea rch Trees,SIAM J. Cornput., to

    4. C. V. CHVATAL,Linear Programming, Freeman, San Francisco,1983, to appear.5 . E. W. DIJKSTRA, A Discipline of Programming, Prentice -Hall, Englewood Cliffs, N.J.,1976.6. E. A. DINITS, An algorithm for the solution of a problem of maxim al flow in a network w ith power

    7. H. N. GABOW N D R. E. TARJAN,Efficient algorithms for a family of matroid problems,J.

    8. Z. GALILA N D A. NAAMAD, n Q (EV log*V) algorithm for the maximal flow problem,J. Comput.

    9. L. G. GUIBASAND R. SEDGEWICK, dichromatic framework for balanced trees,in Proc.

    10. D. HARELAND R. E. TARJAN, ast algorithms for finding nearest common ancestors,SIAM J.

    1 1 . A. V. KARZANOV,etermining the maximal flow in a network by the method of preflows,Sovief

    12. D. E. KNUTH, The Art of Computer Programming,Vol. 3: Sorting and Searching, Addison-

    13. D. MAIER,An efficient method for storing ancestor information in trees,SIAM J. Comput. 8 (1979),

    S I A M J . Comput. 5 (1975), 115-132.

    IEEE Symp.on Fou nda tion s of Compute r Science, pp.248-254, 1980.

    appear. .:i J

    estimation, Soviet Math. Dokl. I 1 (1970), 1277-1280.

    Algorithms, to appear.

    System Sci. 21 (1980), 203-217.

    Nineteenth Annual IEEE Symp.on Fou nda tions of Compu ter Science, pp.8-21, 1978.

    Cornput., to appear.

    Math. Dokl. 15 (1974), 434437.

    Wesley, Reading,Mass., 974.

    599-618.

  • 8/9/2019 A Data Structure for Dynamic Trees

    30/30

    A DATA STRUCTURE FOR DYNAMIC TREES 39 1

    14. V. M. MALHOTRA,. . KUMAR,AND S. N. MAHESHWARI,n O(lV13)algorithm for maximum

    15. J. NIEVERGELTND F. M. EINGOLD, inary searchtrees of bounded balance,SIAM J. Cornput. 2

    16. Y. SHILOACH, An O(nZ log2 I ) Maximum-Flow Algorithm, Tech. Rep. STAN-CS-78-702,

    17. D. D. SLEATOR, An O(nm log n) Algorithm for Maximum NetworkFlow, Tech. Rep. STAN-CS-

    18. D. D. SLEAT~R ND R. E. TARIAN, data structure for dynamic trees,in Proc. Thirteenth Annual

    19. R. E. TARJAN, fficiency ofa good but not linear set union algorithm,J. Assoc. Cornput. Mach. 22

    20. R. E. TARJAN,Applications of path compression on balanced trees,1. Assoc. Cornput. Mach. 26

    21. R. E. TARJANN D J. VA N LEEUWEN, orst-case analysis of set union algorithms,J . Assoc. Cornpuf.

    22. D. D. SLEATORND R. E. TARIAN, elf-adjusting binary trees,in Proc. Fifteenth Annual ACM

    23. R. E. TARIAN, Data Structures and Network Algorithms, Society for Industrial and Applied

    flows in networks,Idorrn. Proc. Lett. 7 (1978), 277-278.

    (1973), 33-43.

    Computer Science Department, Stanford University, Stanford, Calif.,1978.

    80-83 1, Computer Science Department, Stanford University, Stanford, Calif.,1980.

    ACM Symp. on Theory of Computing, pp.114-122, 1981.

    (1975), 215-225.

    (1979), 690-715.

    Mach., to appear.

    Symp. on Theory of Computing, pp.235-245, 1983.

    Mathematics, Philadelphia, Penn., to appear.

    Printed by the St. Catherine Press Ltd., Tempelhof41, Bruges, Belgium