An $tilde{O}(n^{2.5})$-Time Algorithm for Online Topological Ordering

We present an $\tilde{O}(n^{2.5})$-time algorithm for maintaining the topological order of a directed acyclic graph with $n$ vertices while inserting $m$ edges.

Authors: Hsiao-Fei Liu, Kun-Mao Chao

An ˜ O ( n 2 . 5 )-Time Algorithm for Online T op ological Ordering Hsiao-F ei Liu 1 and Kun-Mao Chao 1 , 2 , 3 1 Departmen t of Computer Science a nd Information Enginee ring 2 Graduate Institute of Biomedical Electronics and Bio informatics 3 Graduate Institute of Net w orking and Multimedia National T aiw an Univ ersit y , T aip ei, T aiwan 106 Marc h 1, 2022 Abstract W e present an ˜ O ( n 2 . 5 )-time algo rithm for maintaining the top ologica l order of a directed ac yclic graph with n vertices while inserting m edges. This is an improvemen t ov er the previous result o f O ( n 2 . 75 ) by Ajw ani, F riedrich, and Meyer. Key w ords. online a lg orithm, directed ac y clic graph, top ological orde r ing AMS sub ject classification. 68W01, 68 W40 1 In tro duction A top ological order T of a d irecte d acyclic graph (D A G) G = ( V , E ) is a linear order of all its vertice s suc h that if G con tains an edge ( u, v ), then T ( u ) < T ( v ). In this p aper we study an online v arian t of the top ological ord er in g problem in wh ic h the edges of the D A G are giv en one at a time and we hav e to up date the ord er T eac h time an edge is added. It s p r act ical applications can b e found in [2, 5, 7]. In this pap er, w e giv e an ˜ O ( n 2 . 5 )-time 1 algorithm f or online top ologic al ordering. 1.1 Related W or k Alp ern et al. [2] ga v e an algorithm w hic h tak es O ( || δ || log || δ || ) time for eac h edge insertion, where || δ || is a measur e of the c hange. (F or a formal defin itio n of || δ || , please see [2, 7, 8].) P earce and Kelly [7] p roposed a different algorithm w hic h n ee ds sligh tly more time to pro cess an ed ge insertion in the w orst case th an the algorithm giv en by Alp ern et al. [2], but sho we d exp erimenta lly their algorithm p erform well on sparse graphs. Marc hetti-Spaccamela et al. [6] ga ve an algorithm whic h take s O ( mn ) time for inserting m edges. Katriel [3] show ed that the a nalysis is tigh t. Katriel and Bo dlaender [4] m odified the alg orithm prop osed by Alp ern et al. [2], whic h is referred to as the Katriel-Bo dlaender algorithm. Katriel and Bo dlaender pro v ed that their algorithm has b oth an O (min { m 3 / 2 log n, m 3 / 2 + n 2 log n } ) upp er b ound and an Ω( m 3 / 2 ) lo wer b ound on runtime for m edge insertions. Katriel and Bo dlaender also analyzed the complexit y of their algorithm on structured graphs . They sho w ed that the Katriel-Bo dlaender algorithm run s in time O ( mk log 2 n ) w here k is the tr eewidth of the u nderlying undir ec ted graph and 1 The symbol ˜ O m eans O with log factors ignored. Dep ending on the implementa tion, t he runtime may v ary from O ( n 2 . 5 log 2 n ) to O ( n 2 . 5 log n ). 1 Online T op olog ical O rdering 2 can b e implement ed to run in O ( n log n ) time on trees. In [9], w e prov ed that the Katriel-Bod lae nder algorithm tak es Θ( m 3 / 2 + mn 1 / 2 log n ) time for inserting m edges. Rece nt ly , Ajw ani et al. [1] prop osed an O ( n 2 . 75 )-time algorithm, indep endent of the num b er of edges m inserted. T o the b est of our kno wledge, it is the b est result for d en se DA Gs. 2 Algorithm W e k eep the curr en t top olog ical order as a bijectiv e f u nction T : V → [1 ..n ]. Let d ( u, v ) denote | T ( v ) − T ( u ) | , u → v express that there is a n edge from u to v , u v exp r ess that there is a path from u to v and u < v b e a short form of T ( u ) < T ( v ). Let n 0 . 5 < t 0 < t 1 < t 2 < . . . < t p − 1 < t p < t p +1 = n , where p = O (log n ) is a nonnegativ e in teger. In S ection 6, w e sh all sh o w ho w to determine the v alues of these p aramet ers. Figure 1 giv es the pseudo co de of our algorithm. T is initialized with the top olog ical order of the starting graph. Whenev er an edge ( u, v ) is inserted in to the graph, Inser t ( u, v ) is calle d. If u < v , then In ser t ( u, v ) do es n ot c hange T and simp ly insert the edge int o th e graph. If u > v , then Inser t ( u, v ) calls Reord er ( v, u, 0 , 0) to up date T suc h that T is s till a v alid top ologica l order and T ( u ) < T ( v ). After the call to Reord er ( v, u, 0 , 0), I nser t ( u, v ) can safely insert the edge into the graph. It remains to explain ho w the p rocedur e R eorder ( u, v , f 1 , f 2 ) w orks. The du ty of the pro cedure Reorder ( u, v , f 1 , f 2 ) is to u p date T suc h that T is still a v alid top ologic al order and T ( u ) > T ( v ). The flag f 1 = 1 ind ica tes that th e set A ′ = { w : u → w and w ≤ v } has b een kno wn to b e empty . The flag f 2 = 1 ind ica tes that the set B ′ = { w : w → v an d w ≥ u } h as b een kn own to b e empty . If T ( u ) > T ( v ), then we dir ectly exit. Otherw ise, th er e are tw o cases to consider: 1: t i < d ( u, v ) ≤ t i +1 for some i = 0 , . . . , p . In this case, we fi rst ha v e to compute ˆ A i = { w : u → w , d ( u, w ) ≤ t i , and w < v } and ˆ B i = { w : w → v , d ( w , v ) ≤ t i , and w > u } . If ˆ A i = ∅ and f 1 = 0, then we still h a v e to compute ˆ A i +1 = { w : u → w , d ( u, w ) ≤ t i +1 , and w < v } and set A = ˆ A i +1 ; otherwise, w e directly set A = ˆ A i . Similarly , if ˆ B i = ∅ and f 2 = 0, then we still h av e to compu te ˆ B i +1 = { w : u → w , d ( u, w ) ≤ t i +1 , and w < v } and set B = ˆ B i +1 ; otherwise, we directly set B = ˆ B i . 2: d ( u, v ) ≤ t 0 . In this case we directly s et A = ˆ A 0 = { w : u → w , d ( u, w ) ≤ t 0 , and w < v } and B = ˆ B 0 = { w : w → v , d ( w, v ) ≤ t 0 , and w > u } . If b oth A and B are emp t y , then we directly sw ap u and v and exit the pro cedure. O therwise, let T or ig ianl b e the top olo gical order at the start of the execution of the pr ocedure. F or eac h u ′ ∈ { u } ∪ A , considered in order of d ecrea sing T or ig inal ( u ′ ), w e do the follo wing. F or ea c h v ′ ∈ { v ′ : v ′ ∈ B ∪ { v } and T or ig inal ( v ′ ) > T or g inal ( u ′ ) } , co nsidered in order of in cr easing T or ig inal ( v ′ ), recursiv ely call Reorder ( u ′ , v ′ , f ′ 1 , f ′ 2 ). T he fir st flag f ′ 1 is set to 1 if and only if u ′ = u and A = ∅ , and the second flag f ′ 2 is set to 1 if and only if v ′ = v and B = ∅ . The ide a b ehind the algorithm. Our algorithm br oadly follo ws the algorithm by Ajw ani et al. [1]. The main difference is that Ajwani et al. alw a ys set A to ˆ A i +1 and B to ˆ B i +1 during the execution of Reorder b ut we set A to ˆ A i +1 only if ˆ A i = ∅ and B to ˆ B i +1 only if ˆ B i = ∅ . W e shall pro v e that the total n umber of calls to Re order won’t increase (b ounded ab o ve by O ( n 2 )) by in tro ducing this mo dification. Th us intuitiv ely , our algorithm should run f aste r b ecause in eac h call to Reord er we migh t only n eed to compute ˆ A i and ˆ B i instead of ˆ A i +1 and ˆ B i +1 . Online T op olog ical O rdering 3 Inser t ( u, v ) (* I n sert edge ( u, v ) and calculate new top ologic al ordering *) 1 if v ≤ u then R eorder ( v , u, 0 , 0) 2 in sert edge ( u, v ) in graph Reorder ( u, v , f 1 , f 2 ) (* Reord er v ertices b et ween u and v such that v ≤ u *) 1 if v ≤ u then exit 2 if t i < d ( u, v ) ≤ t i +1 3 then 4 ˆ A i := { w : u → w , d ( u, w ) ≤ t i , and w < v } 5 ˆ B i := { w : w → v , d ( w, v ) ≤ t i , and w > u } 6 A := ˆ A i if A i 6 = ∅ or f 1 = 1; otherwise, A := ˆ A i +1 := { w : u → w , d ( u, w ) ≤ t i +1 , and w < v } 7 B := ˆ B i if B i 6 = ∅ or f 2 = 1; otherwise, B := ˆ B i +1 := { w : w → v , d ( w , v ) ≤ t i +1 , and w > u } 8 else 9 A := ˆ A 0 := { w : u → w , d ( u, w ) ≤ t 0 , and w < v } 10 B := ˆ B 0 := { w : w → v , d ( w, v ) ≤ t 0 , and w > u } 11 if A = ∅ and B = ∅ 12 then 13 sw ap u and v 14 else 15 for u ′ ∈ { u } ∪ A in d ecreasing top ological order 16 for v ′ ∈ B ∪ { v } ∧ v ′ > u ′ in in creasing top ologic al order 17 f ′ 1 := 1 if ( u = u ′ and A = ∅ ); otherwise, f ′ 1 := 0 18 f ′ 2 := 1 if ( v = v ′ and B = ∅ ); otherwise, f ′ 2 := 0 19 Reorder ( u ′ , v ′ , f ′ 1 , f ′ 2 ) Figure 1: Ou r algorithm. 3 Data Struc tures 3.1 Main Data Structures In the follo wing, w e shall describ e the main data stru ct ures used in our algorithm. The current top ological order T and its inv erse T − 1 are stored as arra ys. Th us findin g T ( i ) and T − 1 ( u ) can b e done in constan t time. The D A G G = ( V , E ) is stored as an arr a y of vertices. F or eac h v ertex u w e main tain t wo adjacency lists I nList ( u ) and O utList ( u ). The b ac kward adjacency list I nList [ u ] con tains all v ertices v with ( v , u ) ∈ E . The forward adjacency list O utList ( u ) con tains all v ertices v with ( u, v ) ∈ E . Adjacency lists are implemen ted b y usin g n -bit arra ys and supp ort the f ollo wing op erations. 1. List-In ser t : Given a vertex and a list, add th e vertex to th e list. 2. List-S ear ch : Giv en a ve rtex and a list, determine if the ve rtex is in th e list. If y es, return 1. Else, return 0. Since the adjacency lists are im p lemen ted by us in g n -bit arra ys, it take s O (1) time p er List-Inse r t or List-Se ar ch op eration. Online T op olog ical O rdering 4 3.2 Auxiliary Data Structures In the follo wing w e describ e some auxiliary d at a structures whic h are used in our algorithm to improv e the time complexit y . F or eac h verte x u , w e main tain tw o arra ys of pails: I nP ail s ( u )[0 · · · p + 1] and O utP ail s ( u )[0 · · · p + 1]. I nP ail s ( u )[ i ] con tains all v ertices v with 0 < d ( v , u ) ≤ t i and ( v , u ) ∈ E . O utP ail s ( u )[ i ] con tains all v er tices v with 0 < d ( u, v ) ≤ t i and ( u, v ) ∈ E . A v ertex v in a pail is stored w ith its ve rtex ind ex (and not T ( v )) as its k ey . Pails are implemente d by using b ala nced binary searc h trees and supp ort the follo wing op erations. 1. P ail-Inser t : Giv en a v ertex and a pail, add the vertex to th e p ail. 2. P ail-Delete : Giv en a v ertex and a pail, delete the vertex from the pail. 3. P ail-Collect-All : Giv en a pail, rep ort all v ertices in the pail. It tak es O (log n ) time p er P ail-Inser t or P ail-Delete and O (1 + γ ) time p er P ail-Collect- All , where γ is th e num b er of v ertices in the p ail. 3.3 Instructions for Data Structures Giv en a DA G G with a v alid top olo gical order and t w o v ertices u and v with u 6 v , defin e s orted v ertex sets ˆ A i and ˆ B i , i = 0 , . . . , p + 1, as follo w s: ˆ A i = { w : u → w and d ( u, w ) ≤ t i and w < v } sorted by the top ological order. ˆ B i = { w : w → v and d ( u, w ) ≤ t i and w > u } sorted by th e top ologic al order. In the follo win g we discus s ho w to insert an edge, compu te vertex sets ˆ A i , and ˆ B i , and sw ap tw o v ertices in terms of the ab o ve five basic op erations. a. In serting an edge ( u, v ): This means inserting v ertex v to the forward ad j ac ency list of u and u to the b ac kw ard adjacency list of v . This r equires t w o List-Inser t op erations and at most 2( p + 2) P ail-Inser t op erations. Th u s inserting an edge ( u, v ) can b e d one in O ( p log n ) = ˜ O (1) time. b. C omp uting ˆ A i and ˆ B i : ˆ A i can b e computed by sorting the v ertices in O utP ail ( u )[ i ] and choosing all w with w < v . This can b e done by fi rst calling P ail-Collect-All to collect all the vertic es in O utP ail ( u )[ i ] in O ( t i ) time. Note th at for all these ve rtices w , we h av e 0 < T ( w ) − T ( u ) ≤ t i . Th us we can sort these v ertices in O ( t i ) time by counti ng sort and then c ho ose all w with w < v in O ( | ˆ A i | + 1) time. The total time required to compute ˆ A i is O ( t i + | ˆ A i | ) = O ( t i ). Similarly , the time required to compu te ˆ B i is O ( t i ). c. C omputing ˆ A i and ˆ B i when t i < d ( u, v ) ≤ t i +1 : ˆ A i can b e computed by sorting the v ertices in O utP ail ( u )[ i ] . This can b e d one b y first calling P ail-Collect-All to collect all the ve rtices in O utP ail ( u )[ i ] in O ( | ˆ A i | + 1) time, and then sorting these vertic es in O (( | ˆ A i | + 1) log n ) time. T hus the total time required to compute ˆ A i is ˜ O ( | ˆ A i | + 1). Similarly , the time required to compute ˆ B i is ˜ O ( | ˆ B i | + 1). d. S w apping u and v : Without loss of generalit y assume u < v . When swa ppin g u and v , we n ee d to up date the p ails, T , and T − 1 . W e n o w show h o w to u p date the pails. F or all vertic es w with T ( u ) − t i ≤ T ( w ) < min { T ( u ) , T ( u ) − t i + d ( u, v ) } , w e delete w from I nP ail ( u )[ i ] and delete u from O utP ail ( w )[ i ]. F or all v ertices w w ith max { T ( v ) + t i − d ( u, v ) , T ( v ) } < T ( w ) ≤ T ( v ) + t i } , w e d ele te w f r om O utP ail ( v )[ i ] and delete v from I nP ail ( w )[ i ]. It requires total O ( d ( u, v )) P ail- Delete op erations for eac h i . F or all w with max { T ( v ) , T ( u ) + t i } < T ( w ) ≤ T ( u ) + t i + d ( u, v ) , if w is in the forw ard adjacency list of u , then insert w in to OutP ail ( u )[ i ] and insert u in to I nP ail ( w )[ i ]. F or all w with T ( v ) − t i − d ( u, v ) ≤ T ( w ) < min { T ( u ) , T ( v ) − t i + d ( u, v ) } , if w is in Online T op olog ical O rdering 5 the bac kw ard adjacency list of v , then insert w in to I nP ail ( v )[ i ] and insert v into O utP ail ( w )[ i ]. It requires total O ( d ( u, v )) List-Search and P ail-Inser t op erations for eac h i . In total, we need O ( p · d ( u, v )) List-Search , P ail-Inser t , and P ail-Delete op erations. Up dating T and T − 1 is trivial and can b e done in constan t time. Th us the total time is O ( p · d ( u, v ) log n ) = ˜ O ( d ( u, v )). 4 Correctness In th is section, we shall argue that our algorithm is correct. W e sa y a call of a recurs iv e pr ocedure leads to an op erati on “b y itself ” if and only if this operation is executed dur ing the execution of this call and n ot d u ring the executio n of su bsequen t r ecursiv e calls. Given a D AG G with a v alid top olog ical order T and t w o v ertices u, v of G with u < v , let A ′ = { w : u → w and w ≤ v } and B ′ = { w : w → v and w ≥ u } . W e sa y the flag f 1 of the call to Reorder ( u, v, f 1 , f 2 ) is corr ectly set only if ( f 1 ⇒ ( A ′ = ∅ )) = 1. Th at is, if f = 1, then A ′ is empt y . W e say the fl ag f 2 of the call to Reorder ( u, v , f 1 , f 2 ) is correctly set only if f 2 ⇒ ( B ′ = ∅ ) = 1. That is, if f 2 = 1, then B ′ is emp ty . Lemma 1: Giv en a D A G G with a v alid top ological order and tw o v ertices u, v of G with u < v , let A ′ = { w : u → w and w < v } and B ′ = { w : w → v and w > u } . If the flags are correctly set, then in th e call of R eorder ( u, v , f 1 , f 2 ), A := ∅ if and only if A ′ = ∅ . Similarly , if the flags are correctly set, th en in the call of Reorder ( u, v, f 1 , f 2 ), B := ∅ if and only if B ′ = ∅ . Pro of: W e shall only pro v e that A := ∅ if and only if A ′ = ∅ . It can b e prov ed in a similar wa y th at B := ∅ if and only if B ′ = ∅ . There are tw o cases to consider. Case 1: t i < d ( u, v ) ≤ t i +1 for s ome i with 0 < i ≤ p . By the algorithm, A := ∅ if and only if ˆ A i = { w : u → w and d ( u, w ) ≤ t i and w < v } = ∅ and ( ˆ A i +1 = { w : u → w and d ( u, w ) ≤ t i +1 and w < v } = ∅ ∨ f 1 = 1) . By t i < d ( u, v ) ≤ t i +1 , w e h av e ˆ A i ⊆ ˆ A i +1 = A ′ . F rom ˆ A i ⊆ ˆ A i +1 = A ′ and ( f 1 ⇒ ( A ′ = ∅ )) = 1, w e conclude that A := ∅ if and only if A ′ = ∅ . Case 2: d ( u, v ) ≤ t 0 . By the algorithm, A := ∅ if and only if ˆ A 0 = { w : u → w and d ( u, w ) ≤ t 0 and w < v } = ∅ . By d ( u, v ) ≤ t 0 , we hav e ˆ A 0 = A ′ . F r om ˆ A 0 = A ′ , w e conclud e that A := ∅ if and only if A ′ = ∅ . Lemma 2: Giv en a D A G G with a v alid top ological order and tw o v ertices u, v of G with u < v , let A ′ = { w : u → w and w < v } and B ′ = { w : w → v and w > u } . I f the fl ags are correctly set, then Reorder ( u, v , f 1 , f 2 ) leads to a swap by itself if and only if A ′ = ∅ and B ′ = ∅ . Pro of: By the algorithm, the call to Reo rder ( u, v , f 1 , f 2 ) leads to a swap by itself if and only if in this call A := ∅ and B := ∅ . By L emm a 1 , A := ∅ and B := ∅ if and only if A ′ = ∅ and B ′ = ∅ . Giv en a DA G G with a v alid top ologica l order, Reo rder ( u, v , f 1 , f 2 ) is said to b e l ocal if and only if the execution of R eorder ( u, v ) will n ot affect T ( w ) for all w with w > v or w < u . Lemma 3 : Giv en a D A G G w ith a v alid top ologic al order and t wo v er tices u, v with u 6 v , if the flags are correctly set, then Re order ( u, v , f 1 , f 2 ) mainta ins a v alid top olog ical ord er and stop with v < u and is lo cal. Online T op olog ical O rdering 6 Pro of: W e pr o v e the lemma by ind uction on T ( v ) − T ( u ). When T ( u ) − T ( v ) ≤ 0, th e lemma is trivially correct. Assume the lemma to b e true when T ( v ) − T ( u ) < k , wh ere k > 0. W e shall pro v e that the lemma is true wh en T ( v ) − T ( u ) = k . If A ′ = { w : u → w and w < v } = ∅ an d B ′ = { w : w → v and w > u } = ∅ , then by Lemma 2, line 13 is executed. Th us, Reo rder ( u, v , f 1 , f 2 ) m ain tains a v alid top ologica l order, stops w ith v < u , and only T ( u ) and T ( v ) are up dated, so the lemma follo ws. If A ′ 6 = ∅ or B ′ 6 = ∅ , by Lemma 2, the for -loops are executed. Let T ′ b e the initial topological order. By our induction h yp othesis and L emma 1 , the follo w in g lo op in v arian ts hold: 1. T is a v alid top ologic al order. 2. A t the start of the execution of line 19, T ( v ′ ) − T ( u ′ ) < k and T ′ ( u ) ≤ T ( u ′ ) < T ( v ′ ) ≤ T ′ ( v ). 3. A t the start of the execution of line 19, u ′ 6 v ′ . 4. The flags are correctly set for the recursive call. By the lo op in v arian ts and our induction hyp othesis, eac h recur s iv e call Reorder ( u ′ , v ′ , f ′ 1 , f ′ 2 ) in the for -loops stops with v ′ < u ′ and is local. Since the last recursiv e call is Reorder ( u, v ), th e en tire pro cedure stops with v < u . Since eac h recursiv e call Reorde r ( u ′ , v ′ ) is lo cal and starts with T ′ ( u ) ≤ T ( u ′ ) < T ( v ′ ) ≤ T ′ ( v ), th e top olog ical order of ve rtices w with T ′ ( w ) > T ′ ( v ) or T ′ ( w ) < T ′ ( u ) is not affected. Thus the entire pr ocedur e m aintains a v alid top ologic al order, stops with v < u , and is lo cal. Theorem 1: Giv en a DA G G with a v alid top ologic al order and t wo v ertices u, v of G with u 6 v , if the flags are correctl y set, then Inser t ( u, v ) w ill add an ed ge ( u, v ) to G and mainta in a v alid top olog ical ord er. Pro of: Bec ause u 6 v , we kn o w th at u and v are t w o different vertic es and either u < v or u > v . If u < v th en the theorem is trivially correct. Assume that v > u . By Lemma 3, Reorder ( v, u, 0 , 0) will stop with u < v and main tain a v alid top ologic al order. Thus when line 2 of Inser t is ready to b e executed, we will hav e a v alid top ologi cal order an d u < v , and ad d ing an edge ( u, v ) to G wo n’t affect the v alidness of the top olog ical ord er. In addition to the correctness of the algorithm, w e also wan t to prov e that the flags are alw a ys correctly set. Lemma 4 : Giv en a D AG G with a v alid top ologica l order and t w o v ertices u, v of G with u < v , consider a call to Reor der ( u, v , f 1 , f 2 ). If the flags f 1 and f 2 are correctly set, then while executing this call, all su bsequen t calls to Reo rder will also own correct flags. Pro of: W e prov e the lemma by ind uction on the depth of the r ec ursion tree. By Lemma 3, the call to Reorder ( u, v , f 1 , f 2 ) will stop, so the depth of the r ecursion tree is finite. If the d ep th is zero, then no r ec ursive calls are made and the lemma follo w s . Assume th e lemma to b e tru e w hen the depth of the recursion tree is less than k , where k > 0. W e shall prov e that the lemma is true w h en the dep th of the r ec ursion tree is k . Since k > 0, there is at least one recursiv e call. Th us th e for -lo ops are executed. By Lemma 1 and Lemma 2, th e follo wing lo op in v arian ts h old: 1. T is a v alid top ologic al order. 2. A t the start of th e execution of line 19, T ( u ′ ) < T ( v ′ ). 3. A t the start of th e execution of line 19, u ′ 6 v ′ . 4. The flags are correctly set for th e r ecursiv e call. Online T op olog ical O rdering 7 By the lo op inv arian ts and our in d uction hyp othesis, eac h recursive call to Reorde r ( u ′ , v ′ , f ′ 1 , f ′ 2 ) in the for -lo ops, together with all subsequent calls to R eorder in it, own correct flags, and the lemma follo ws. Theorem 2: Giv en a DA G G with a v alid top ologic al order and t wo v ertices u, v of G with u 6 v , while executing Ins er t ( u, v ), the fl ag s are correctly set for all calls to Reord er . Pro of: If u < v then there are no calls to Re order made wh ile executing Inser t ( u, v ), and the lemma follo ws. If u > v then I nser t ( u, v ) will call Reorder ( v, u, 0 , 0). By Lemma 4, the call to Reorder ( v, u, 0 , 0), together with all s ubsequen t calls to Reorde r in it, own correct flags, and the lemma follo ws. 5 Run time In this section, we analyze the time required to insert a s equ ence of edges. B y Theorem 2, the fl ag s are alw ays correctly set. T o a v oid u nnecessary d iscussion, eac h lemma, theorem, corollary , and p roof in this section is state un der the assumption that the flags are correctly set. T o a void notatio nal o verloa d, sometimes we shall just write Reord er ( u, v ) and ignore the fl ag s. 5.1 Prop erties Lemma 5 : Giv en a DA G G with a v alid top ological order and t w o vertic es u and v with u < v , then during the execution of Reorder ( u, v ), we ha v e that (1) T ( x ) is n on d ecrea sing if u x ; (2) T ( y ) is nonincreasing if y v ; and (3) T ( z ) d o esn’t c h ange if u 6 z and z 6 v . Pro of: W e prov e the lemma by ind uction on the depth of the recursion tree. By L emm a 3 , the call to Re order ( u, v ) will stop, so the depth of the r ecur sion tree is fin ite. If the dep th is zero, then no recursiv e calls are made. It follo ws that line 13 is executed, so th e lemma follo w s. Assume th e lemma to b e tru e w hen the depth of the recursion tree is less than k , where k > 0. W e shall prov e that the lemma is true w h en the dep th of the r ec ursion tree is k . Since k > 0, there is at least one recursiv e call. Th us th e for -lo ops are executed. By Lemma 1 and Lemma 2, th e follo wing lo op in v arian ts h old: 1. T is a v alid top ologic al order. 2. A t the start of th e execution of line 19, T ( u ′ ) < T ( v ′ ). 3. A t the start of th e execution of line 19, u ′ 6 v ′ . 4. The flags are correctly set for th e r ecursiv e call. Note that for eac h recursiv e call R eorder ( u ′ , v ′ ) in the f or -lo ops, we h a v e u u ′ and v ′ v . Le t u x . Then we ha v e either u ′ x or ( u ′ 6 x and x 6 v ′ ). By the indu ct ion hyp othesis, T ( x ) is nondecreasing or do esn’t c hange d uring the execution of the recurs ive call. Thus T ( x ) is nond ecrea s- ing if u x . L et y v . Then w e ha ve either y v ′ or ( u ′ 6 y and y 6 v ′ ). By the induction h yp othesis, T ( y ) is nonin crea sing or do esn’t c hange d uring the execution of the recursive call. Th u s T ( y ) is n onincreasing if y v . Let u 6 z and z 6 v . Then u ′ 6 z and z 6 v ′ . By the induction h yp othesis, T ( z ) do esn’t c hange du ring the execution of the r ecur siv e call. Thus T ( z ) do esn’t c hange if u 6 z and z 6 v . Online T op olog ical O rdering 8 Lemma 6: Give n a DA G G w ith a v alid top ological order and t wo v ertices u and v with u < v , for all x and y , Re order ( u, v ) leads to at most one sw ap of x and y . Pro of: Supp ose that Re order ( u, v ) leads to at least one swa p of x and y . Without loss of generalit y w e assu me that x < y b efore the the fi r st swap o ccurs. Then the fir st swap of x and y leads to increase of T ( x ) and decrease of T ( y ). Thus by Lemma 5, T ( x ) is n ondecreasing and T ( y ) is nonincreasing during the execution of Re order ( u, v ). After the fir st s wap, w e hav e x > y . Since T ( x ) is n onde- creasing an d T ( y ) is n onincreasing, we kn o w there are no more swaps. Theorem 3 : While inserting a sequ ence of edges, for all vertice s x an d y , after the fi r st sw ap of x and y , the r ela tiv e order of x and y won’t change. Pro of: Supp ose that the v ertex pair ( x, y ) is s w app ed at least once while inserting a sequence of edges. By Lemma 6 and the algorithm Inser t , eac h edge insertion leads to at most one swap of x and y . Let ( u, v ) b e the first edge wh ose insertion leads to a swap of x an d y . Wit hout loss of generalit y w e assume that x < y b efore the the fir st sw ap o ccurs. W e shall prov e that x > y will hold after the fir s t sw ap of x an d y . Consider the execution pro cess of In ser t ( u, v ). Th e fi rst sw ap o ccurs d u ring the execution of Re order ( v , u ). By Lemma 5, we ha v e v x and y u . Since T ( x ) is nond ecrea sing and T ( y ) is nonincreasing, after the first swap of x and y , x > y will hold u n til Reo rder ( v , u ) return s. After Reord er ( v, u ) r et urn s, the edge ( u, v ) w ill b e add ed to th e graph. T h us we will hav e y x an d x > y j u st after Inser t ( u, v ) returns. By Lemma 3, calls to Reor der maint ain a v alid top ological order, so x > y will hold hereafter. Corollary 1: While inserting a sequ en ce of edges, f or all v ertices x and y , th ere is at most one swap of x an d y . Lemma 7 : Giv en a D A G G with a v alid topological order and t w o v ertices u and v with u < v , Reorder ( u, v ) leads to a swap of u and v . Pro of: W e prov e the lemma by ind uction on the depth of the recursion tree. By L emm a 3 , the call to Re order ( u, v ) will stop, so the depth of the r ecur sion tree is fin ite. If the dep th is zero, then no recursiv e calls are made. It follo ws that line 13 is executed, so th e lemma follo w s. Assume th e lemma to b e tru e w hen the depth of the recursion tree is less than k , where k > 0. W e shall prov e that the lemma is true w h en the dep th of the r ec ursion tree is k . Since k > 0, there is at least one recursiv e call. Th us th e for -lo ops are executed. By Lemma 1 and Lemma 2, th e follo wing lo op in v arian ts h old: 1. T is a v alid top ologic al order. 2. A t the start of th e execution of line 19, T ( u ′ ) < T ( v ′ ). 3. A t the start of th e execution of line 19, u ′ 6 v ′ . 4. The flags are correctly set for th e r ecursiv e call. Note that when executing the last recursive call R eorder ( u ′ , v ′ ) in the for -lo ops, we hav e u ′ = u and v ′ = v . By our induction hyp othesis and th e lo op in v arian ts, the last r ec ursive call leads to a swap of u and v , and the lemma follo w s. Online T op olog ical O rdering 9 Theorem 4 : While inserting a sequence of edges, the summation of | A | + | B | o v er all calls of Reorder is O ( n 2 ). Pro of: Consider arbitrary v ertices u and ˆ v . W e shall p ro v e that ˆ v ∈ B o ccurs at most once ov er all calls of Re order ( u, · ). This pr ov es that the su m matio n of | B | o ve r all calls of Re order ( u, · ) is less than or equal to n . T h erefore the su mmation of | B | o v er all calls of Reorder ( · , · ) is less th an or equal to n 2 . Consider the execution pro cess of the firs t call of Reor der ( u, · ) for which ˆ v ∈ B . By the algorithm, a recur siv e call to R eorder ( u, ˆ v ) is made in the for -lo ops. Before th e recursive call to Reorde r ( u, ˆ v ) in the for -lo ops, at the start of the execution of eac h recursive call to Reorder ( u ′ , v ′ ) in the for - lo ops, we ha v e u < ˆ v and ( u < u ′ < v ′ or u = u ′ < v ′ < ˆ v ). This follo ws from the order in whic h w e mak e the recursive calls and the lo cal prop ert y (Lemma 3). Since u < ˆ v and ( u < u ′ < v ′ or u = u ′ < v ′ < ˆ v ), by the lo ca l prop ert y , u < ˆ v will hold during the execution of the call to Reorder ( u ′ , v ′ ). T h us b efore the recursive call to Re order ( u, ˆ v ) in the for -loop s , all r ecursiv e calls to Reor der ( u ′ , v ′ ) in the for -lo ops w on’t lead to a call to Reord er ( u, · ) f or wh ic h ˆ v ∈ B ; otherwise, the call to Reo rder ( u, · ) for whic h ˆ v ∈ B will lead to a call to Reord er ( u, ˆ v ) which will further lead to ˆ v > u by Lemma 3, leading to a con tradiction. Supp ose for the con tradiction that the r ec ursive call to Reorde r ( u, ˆ v ) in the for -lo ops leads to a call to Reorder ( u, v ′′ ) f or which ˆ v ∈ B . By the order in whic h we mak e the recursive calls and the lo cal pr operty , at the s tart of the execution of the recursiv e call to R eorder ( u, ˆ v ) in the for -lo ops, we ha v e u < ˆ v . Since ˆ v v ′′ , at the start of th e execution of th e r ecursiv e call to R eorder ( u, ˆ v ) in the for -lo ops, we hav e u < ˆ v < v ′′ . Th us by the lo ca l prop ert y , v ′′ > u will h old du r ing the executio n of the recursive call to Reor der ( u, ˆ v ) in the for -loops . How ev er, by L emm a 3, Re order ( u, v ′′ ) stops with v ′′ < u , wh ic h is a cont radiction. After the recur siv e call to R eorder ( u, ˆ v ) in the for -lo ops, w e ha v e ˆ v < u by Lemma 3. Since ˆ v > u b efore the recur siv e call to R eorder ( u, ˆ v ) in the for -lo ops, by Lemma 7, th is recurs ive call leads to a swap of u and ˆ v . Thus after th e recursiv e call to Re order ( u, ˆ v ) in th e for -lo ops, we will ha v e ˆ v < u and , b y Lemma 3, the relativ e order of u and ˆ v wo n’t c hange h ereafte r. Since ˆ v < u holds hereafter, there will b e no more calls of Reorder ( u, · ) for which ˆ v ∈ B . Putting all things together, it follo ws that ˆ v ∈ B o ccurs at most once ov er all calls of R eorder ( u, · ). Similarly , we can pr o v e that for arb itrary vertice s ˆ u and v , ˆ u ∈ A o ccurs at most once ov er all calls of Reorder ( · , v ). It f ol lo ws that the summation of | A | ov er all calls of Reor der ( · , · ) is less than or equal to n 2 . Corollary 2: While in s erting a s equence of edges, Reor der is called O ( n 2 ) times. Pro of: By the algorithm, a call to Re order for whic h | A | + | B | = 0 leads to a s wap by itself. By Corollary 1 , there are at most n 2 sw aps. Thus there are at most n 2 calls to Reorde r f or which | A | + | B | = 0. By Theorem 4, there are O ( n 2 ) calls to Re order for wh ic h | A | + | B | > 0. Therefore, there are O ( n 2 ) calls to Reorde r in total. Let S = { ( u, v ) : there is a swa p of u and v such that u < v w h ile inserting the edges } . Define D ( u, v ) =  the v alue of d ( u, v ) while sw apping u and v ∀ ( u, v ) ∈ S ; 0 ∀ ( u, v ) 6∈ S. Since by Corollary 1 eac h v ertex pair is swapp ed at most once, D ( u, v ) is we ll defined . Let k b e a n umber with 1 ≤ k ≤ n . Define D k ( u, v ) =  D ( u, v ) if D ( u, v ) ≤ k ; k otherwise . Online T op olog ical O rdering 10 The follo wing theorem is the key to our ru n time analysis. Theorem 5: F or all k ∈ [1 , n ] with k ≥ n 0 . 5 , w e ha v e P D k ( u, v ) = O ( n 2 · √ k ). Pro of: Let k = n r . Let T ∗ denote the final top ologica l order. Define x ( T ∗ ( u ) , T ∗ ( v )) = D ( u, v ) and z ( T ∗ ( u ) , T ∗ ( v )) = D k ( u, v ) for all vertic es u and v . Th e follo wing linear inequalities are p ro v ed to b e true by Ajwani et al. [1]. (1) x ( i, j ) ≤ 0 for all 1 ≤ i ≤ n and 1 ≤ j ≤ i . (2) x ( i, j ) ≤ n for all 1 ≤ i ≤ n and i < j ≤ n . (3) P j >i x ( i, j ) − P j i x ( i, j ) − P j i (3) Z i · n + j + W i · n + j ≥ 1 for all 0 ≤ i < n and 0 ≤ j < n (4) Y i ≥ 0 for all 0 ≤ i < n 2 + n (5) Z i ≥ 0 for all 0 ≤ i < n 2 (6) W i ≥ 0 for all 0 ≤ i < n 2 . Let c b e a large enough constan t, e.g. 120, su c h that ( i + c · n r / 2 ) r / 2 ≥ ( i r / 2 + 1) for any 1 ≤ i ≤ n . The follo wing is a feasible solution to the dual problem. Online T op olog ical O rdering 11                                  Y i · n + j = 1 for all 0 ≤ i < n and 0 ≤ j ≤ i Z i · n + j = 0 for all 0 ≤ i < n and 0 ≤ j ≤ i W i · n + j = 1 for all 0 ≤ i < n and 0 ≤ j ≤ i Y i · n + j = 0 for all 0 ≤ i < n and i < j ≤ i + c · n r / 2 Z i · n + j = 1 for all 0 ≤ i < n and i < j ≤ i + c · n r / 2 W i · n + j = 0 for all 0 ≤ i < n and i < j ≤ i + c · n r / 2 Y i · n + j = 0 for all 0 ≤ i < n and i + c · n r / 2 < j < n Z i · n + j = 0 for all 0 ≤ i < n and i + c · n r / 2 < j < n W i · n + j = 1 for all 0 ≤ i < n and i + c · n r / 2 < j < n Y n 2 + i = ( n − i ) r / 2 for all 0 ≤ i < n This feasible solution to the du al problem has an ob jectiv e v alue of O ( n P n i =1 i r / 2 + n · n r · c · n r / 2 ) = O ( n 2+ r/ 2 + n 1+ r + r/ 2 ) = O ( n 2+ r/ 2 ) = O ( n 2 · √ k ), whic h by the prim al-du al theorem is an upp er b ound on the ob jectiv e v alues of the original linear p r og ram. Lemma 8 : Giv en a D AG G with a v alid topological order and t w o v ertices u and v with u < v , consider a call to Reor der ( u, v ). If A ′ = { w : u → w an d w < v } = ∅ , then w hen executing this call, w e shall h a v e the fi rst fl ag f 1 = 1 for all subs equen t calls to Re order ( u, · ). Pro of: W e prov e the lemma by ind uction on the depth of the recursion tree. By L emm a 3 , the call to Re order ( u, v ) will stop, so the depth of the r ecur sion tree is fin ite. If the dep th is zero, then no subsequent recursive calls are made, so the lemma follo w s. Assume th e lemma to b e tru e w hen the depth of the recursion tree is less than k , where k > 0. W e shall pro v e that the lemma is tru e when the depth of the recursion tr ee is k . S ince k > 0, there is at least one recursiv e call. Thus the for -loops are executed. By Lemm a 1, A = ∅ . By the lo cal prop ert y (Lemma 2) and the order in wh ic h w e make the r ecursiv e calls, an y subs equ en t call to R e- order ( u, · ) must o ccur du ring the execution of last iteration of th e ou ter for -lo op. C onsider an y first lev el recursiv e call Re order ( u ′ , v ′ , f ′ 1 , f ′ 2 ) in the last iteratio n of th e outer for -lo op. Note that we ha v e u ′ = u < v ′ and { w : u → w and w < v ′ ≤ v } ⊆ A ′ = ∅ w hen this call is ready to b e executed. Since u ′ = u and A = ∅ , we also h a v e f ′ 1 = 1. By the induction hyp othesis, we also hav e the fi rst fl ag f 1 = 1 for all subs equen t calls to Re order ( u, · ) wh ile executing this call. It completes the p roof. Lemma 9 : Giv en a D AG G with a v alid topological order and t w o v ertices u and v with u < v , let A ′ = { w : u → w and w < v } = ∅ . Then a call to Reorde r ( u, v ) will stop with u at the initial p osition of v . That is, letting T bef or e b e the top ological order jus t b efore th e call to R eorder ( u, v ), then the call to R eorder ( u, v ) will return a top ologica l order T af ter suc h that T af ter ( u ) = T bef or e ( v ). Pro of: W e prov e the lemma by ind uction on the depth of the recursion tree. By L emm a 3 , the call to Re order ( u, v ) will stop, so the depth of the r ecur sion tree is fin ite. If the dep th is zero, then no recursiv e calls are made. It follo ws that line 13 is executed, so th e lemma follo w s. Assume th e lemma to b e tru e w hen the depth of the recursion tree is less than k , where k > 0. W e shall pro v e that the lemma is tru e when the depth of the recursion tr ee is k . S ince k > 0, there is at least one r ec ursive call. Thus the for -lo ops are executed. Let T bef or e b e the in itia l top ological order. By Lemma 1, Lemma 2, and the induction hypothesis, the follo wing lo op in v arian ts h old: Online T op olog ical O rdering 12 1. T is a v alid top ologic al order. 2. A t the start of th e execution of line 19, T ( u ) = T ( u ′ ) < T ( v ′ ) = T bef or e ( v ′ ) . 3. A t the start of th e execution of line 19, u ′ 6 v ′ . 4. After the execution of lin e 19, T ( u ) = T bef or e ( v ′ ) . 5. The flags are correctly set for the recur s iv e call. Note th at for th e last recurs ive call R eorder ( u ′ , v ′ ) in the for -lo ops, we ha v e v ′ = v . T h us b y th e lo op in v arian ts, we hav e T ( u ) = T bef or e ( v ) after the last r ecur siv e call in the for -lo ops. Lemma 10 : Giv en a D A G G w ith a v alid top ologic al order and t wo vertices u and v with u < v , when executing a call of Reorder ( u, v ) in w h ic h b oth ˆ A i and ˆ A i +1 are computed, u will b e mov ed righ t with distance at least t i . Pro of: Since b oth ˆ A i and ˆ A i +1 are computed, b y th e algorithm, we h a v e t i < d ( u, v ) ≤ t i +1 and ˆ A i = ∅ . Th ere are t wo cases to consider. Case 1: ˆ A i +1 = ∅ . I t follo w s th at A ′ = { w : u → w and w < v } = ∅ . By Lemma 9, u will b e mo ved r igh t to the in itial p osition of v . Since initially d ( u, v ) > t i , u will b e mo ved r igh t with distance at least t i . Case 2: ˆ A i +1 6 = ∅ . By the algorithm, th e for -loops are executed. Let ˆ u b e the ve rtex with low est top olog ical order in ˆ A i +1 . Let T initial b e the initial top ological ord er. Sin ce ˆ A i = ∅ , we ha v e initially d ( u, ˆ u ) > t i , i.e., T initial ( ˆ u ) − T initial ( v ) > t i . By Lemma 3 and th e order in w hic h we make th e recursiv e calls, b efore the last iteration of the outer for -lo op, T ( v ) ≥ T initial ( ˆ u ) will h old. Consider the execution of the last iteration of the outer for -lo op. Let T start b e the top olog ical order at the start of this iteration. Then we ha v e T start ( v ) − T initial ( u ) ≥ T initial ( ˆ u ) − T initial ( u ) > t i . By Lemma 3 and Lemma 9 , the follo wing lo op inv arian ts hold. 1. T is a v alid top ologic al order. 2. A t the start of the execution of the last iteration of line 19, T ( u ) = T ( u ′ ) < T ( v ′ ) = T start ( v ′ ). 3. A t the start of the execution of line 19, u ′ 6 v ′ . 4. A t the start of the execution of line 19, { w : u → w and w < v ′ ≤ v } = ∅ . 5. After the execution of line 19, T ( u ) = T start ( v ′ ) . 6. The flags are correctly set for the recursive call. Th us after this iteration, we w ill ha v e T ( u ) = T start ( v ) > T initial ( u ) + t i , and the lemma follo w s. Theorem 6 : While inserting a s equ ence of edges, there are at most O ( n 2 √ t i +1 t i ) calls of Reor der for whic h b oth ˆ A i and ˆ A i +1 are computed. Similarly , th ere are at most O ( n 2 √ t i +1 t i ) calls of Reorder f or whic h b oth ˆ B i and ˆ B i +1 are computed. Pro of: W e shall only p ro v e that there are at most O ( n 2 √ t i +1 t i ) calls of Reor der f or w hic h b oth ˆ A i and ˆ A i +1 are computed. It can b e pr o v ed in a similar w a y that there are at most O ( n 2 √ t i +1 t i ) calls of Reorder for whic h b oth ˆ B i and ˆ B i +1 are computed. Let C 1 ( u, v ) , C 2 ( u, v ) , . . . , C m ( u,v ) ( u, v ) b e the calls to Reo rder ( u, v ) for which b oth ˆ A i and ˆ A i +1 are computed for al l v ertices u and v . Let S i ( u, v ) = { w : C i ( u, v ) leads to a sw ap of u and w } Online T op olog ical O rdering 13 for i = 1 , . . . , m ( u, v ). W e shall pro ve that P u,v P m ( u,v ) i =1 P w ∈ S i ( u,v ) D ( u, w ) = O ( n 2 √ t i +1 ). By Lemma 10, P w ∈ S i ( u,v ) D ( u, w ) ≥ t i for all v ertices u and v and i = 1 , . . . , m ( u, v ). It follo ws that P u,v P m ( u,v ) i =1 1 = O ( n 2 √ t i +1 t i ). In a call to Re order ( u, v ), ˆ A i and ˆ A i +1 are b oth computed only if t i < d ( u, v ) ≤ t i +1 . Thus by the lo cal prop ert y (Lemma 3), we h a v e D ( u, w ) ≤ t i +1 for all w ∈ S i ( u, v ), i = 1 , . . . , m ( u, v ). By Theorem 5, w e ha v e P u,v D t ( u, v ) = O ( n 2 √ t i +1 ). Th us it suffices to show that in the summ at ion P u,v P m ( u,v ) i =1 P w ∈ S i ( u,v ) D ( u, w ), D ( u, w ) is coun ted at most t wice for eac h vertex pair ( u, w ). T o s ho w th at in the summation P u,v P m ( u,v ) i =1 P w ∈ S i ( u,v ) D ( u, w ), D ( u, w ) is counted at most twice for eac h v ertex pair ( u, v ), we only ha v e to prov e that S i ( u, v ) ∩ S j ( u, v ′ ) ∩ S k ( u, v ′′ ) = ∅ if C i ( u, v ), C j ( u, v ′ ), and C k ( u, v ′′ ) are thr ee different calls. Supp ose for the con tradiction that w ∈ S i ( u, v ) ∩ S j ( u, v ′ ) ∩ S k ( u, v ′′ ) and C i ( u, v ), C j ( u, v ′ ), and C k ( u, v ′′ ) are three differen t calls. Without loss of generalit y , w e assume C i ( u, v ) o ccurs b e- fore C j ( u, v ′ ) and C j ( u, v ′ ) o ccurs b efore C k ( u, v ′′ ). By Corollary 1, there is only one sw ap of u and w , so C j ( u, v ′ ) m ust b e a subsequent r ec ursive call whic h o ccur s during the execution of C i ( u, v ) and C k ( u, v ′′ ) m ust be a sub sequen t recursiv e call which o ccurs d uring the exe cution of C j ( u, v ′ ). Consider the execution of C i ( u, v ). By Lemma 3 and the order in whic h we mak e the recursiv e calls in the for -lo ops, C j ( u, v ′ ) m ust o ccur during the last iteration of the outer for - lo op. Note that b y Lemm a 3, b efore th e last iterat ion of the outer for -lo op b egins, all vertice s in ˆ A i +1 = { w : u → w , d ( u, w ) ≤ t i +1 and w < v } = { w : u → w and w < v } are mov ed to the righ t of v . Thus w h en the last iteration of the outer for -lo op b egins, there are not any v ertices w b et ween u and v with u → w . Therefore, by the lo cal prop erty of Reorde r , dur ing the last iteration of the outer for -loop, for eac h call to Reorder ( u, v ′ ), w e h a v e { w : u → w and w < v ′ ≤ v } = ∅ . By Lemma 8, we ha v e the fi rst fl ag f 1 = 1 for eac h sub sequen t call to Reorde r ( u, · ) d u ring the execution of C j ( u, v ′ ). It follo ws that the first flag f 1 of the call C k ( u, v ′′ ) is 1. Th us by the algo rithm, we d on’t compute ˆ A i +1 in th e call C k ( u, v ′′ ), whic h is a contradictio n. 5.2 Run t ime Analysis Lemma 11: While inserting a sequence of ed ge s, the total time sp en t on executing lin e 2 of Inser t is ˜ O ( n 2 ). Pro of: As discussed in Section 3.3, eac h execution of line 2 of Inser t can b e done in ˜ O (1) time. Since th er e are at m ost n ( n − 1) / 2 edge insertions, the lemma follo ws. Lemma 12 : While inserting a sequence of edges, the total time sp ent on computing ˆ A i and ˆ B i , i = 1 , . . . , p , o ver all calls of Reorder ( u, v ) with t i < d ( u, v ) ≤ t i +1 is ˜ O ( n 2 ). Pro of: As discussed in Section 3.3, it needs ˜ O ( | ˆ A i | + | ˆ B i | + 1) time to compute ˆ A i and ˆ B i in a call of Reor der ( u, v ) if d ( u, v ) < t i +1 . By T heorem 4, the s ummation of | ˆ A i | + | ˆ B i | , i = 1 , . . . , p , o ver all calls of Re order is O ( n 2 ). By Corollary 2, R eorder is called O ( n 2 ) times. Th us the sum matio n of ( | ˆ A i | + | ˆ B i | + 1) o ver all calls of Reorder is O ( n 2 ), and the lemma follo ws. Lemma 13: F or eac h i with 1 ≤ i ≤ p + 1, while inserting a sequ ence of ed ge s, the total time s pent on compu ting ˆ A i and ˆ B i , o ver all calls of R eorder ( u, v ) with d ( u, v ) ≤ t i is O ( n 2 t 3 / 2 i t i − 1 ). Online T op olog ical O rdering 14 Pro of: I f d ( u, v ) ≤ t i , then by the algorithm, ˆ A i is computed only if ˆ A i − 1 is also computed and is empt y . Thus b y Th eorem 6, there are at most O ( n 2 t 1 / 2 i t i − 1 ) su c h calls. As discu s sed in Section 3.3, it needs O ( t i ) time to compute ˆ A i in eac h of su c h calls. Thus the total time sp ent on compu ting ˆ A i o ver all calls of Re order ( u, v ) with d ( u, v ) ≤ t i is O ( n 2 t 3 / 2 i t i − 1 ). Similarly , the total time sp ent on computing ˆ B i o ver all calls of Reorder ( u, v ) with d ( u, v ) ≤ t i is O ( n 2 t 3 / 2 i t i − 1 ). Lemma 14: While inserting a sequen ce of edges, th e total time sp en t on computing ˆ A 0 and ˆ B 0 o ver all calls of Reorder is O ( n 2 · t 0 ). Pro of: As discussed in Section 3.3, it needs O ( t 0 ) time to compute ˆ A 0 and ˆ B 0 in a call of Re order . By Corollary 2, Reorder is called O ( n 2 ) times, and the lemma f ollo ws. Lemma 15 : While inserting a sequen ce of edges, th e total time sp ent on swa pping v ertices is ˜ O ( n 2 . 5 ). Pro of: As discuss ed in Section 3.3, eac h swap of v ertices u and v with d ( u, v ) < t can b e done in ˜ O ( d ( u, v )) time. By Theorem 5, P D n ( u, v ) = O ( n 2 . 5 ). Th us the total time is ˜ O ( n 2 . 5 ). Theorem 7: While in serting a sequence of edges, th e total time r equired is ˜ O ( P p +1 i =1 n 2 t 3 / 2 i t i − 1 + n 2 · t 0 ). Pro of: It follo ws d irectly from the ab o ve lemmas. 6 F urther Discussion Let n 0 . 5 < t 0 < t 1 < . . . < t p < t p +1 = n and p = O (lo g n ). W e ha v e kn o wn that the runtime is ˜ O ( P p +1 i =1 n 2 t 3 / 2 i t i − 1 + n 2 · t 0 ). In this section, we sh o w ho w to d et ermine the v alues of these parameters. By letting n 3 √ t p +1 = n 2 t 3 / 2 p +1 t p = n 2 t 3 / 2 p t p − 1 = n 2 t 3 / 2 p − 1 t p − 2 , . . . , = n 2 t 3 / 2 1 t 0 = n 2 · t 0 , w e ha v e t 1 = t 4 / 3 0 , t i = t 5 / 3 i − 1 t 2 / 3 i − 2 for all i = 2 , . . . , p + 1 . Let t i = t x i 0 for i = 0 , . . . , p + 1. w e ha v e x 0 = 1 , x 1 = 4 / 3 , and x i = 5 x i − 1 3 − 2 x i − 2 3 for all i = 2 , . . . , p + 1 . By solving this linear second order recurren ce relation, we get x i = 2 − (2 / 3) i for all i = 0 , 1 , 2 , . . . , p + 1. It follo ws that t p +1 = t 2 − (2 / 3) ( p +1) 0 . Since t p +1 = n , we ha v e t 0 = n f ( p ) , where f ( p ) = 3 p +1 (2 · 3 p +1 − 2 p +1 ) . Corollary 3: While inserting a sequence of edges, the total time required is ˜ O ( n 2+ f ( p ) ) if t 0 = n f ( p ) and t i = t 2 − (2 / 3) i 0 for all i = 1 , . . . , p + 1, w h ere f ( p ) = 3 p +1 (2 · 3 p +1 − 2 p +1 ) . Online T op olog ical O rdering 15 Note that f ( p ) = 3 p +1 (2 · 3 p +1 − 2 p +1 ) = 0 . 5 + 2 p (2 · 3 p +1 − 2 p +1 ) . By letting ǫ ( p ) = 2 p (2 · 3 p +1 − 2 p +1 ) , we ha ve ǫ ( p ) < 2 p 3 p +1 < (3 / 2) − p . By letting p = log 3 / 2 n , we ha v e 1 < n ǫ ( p ) < n 1 /n < 2 when n > 2. Thus ˜ O ( n 2+ f ( p ) ) = ˜ O ( n 2 . 5+ ǫ ( p ) ) = ˜ O ( n 2 . 5 ) if w e c ho ose p = ⌈ log 3 / 2 n ⌉ . The follo wing theorem su mmarizes our discussion. Theorem 8: There exists an ˜ O ( n 2 . 5 )-time algorithm for online top ologi cal ordering. 7 Concluding Remarks W e p rop ose an ˜ O ( n 2 . 5 )-time algorithm for mainta ining the top olog ical order of a DA G with n v er- tices while inserting m edges. By com bin ing this with the result in [4], w e get an u p p er b ound of ˜ O (min { m 3 / 2 , n 2 . 5 } ) f or online top ologica l ordering. The only non-trivial lo wer b oun d is d ue to Rama- lingam and Rep s [8], wh o show th at an y algorithm need Ω ( n log n ) time while inser tin g n − 1 edges in the worst case if all lab els are main tained explicitly . Precisely , our algorithm runs in O ( n 2 . 5 log 2 n ) time. Ch oosing a b etter im p lemen tation for the pails, like data structures d iscussed in S ec tion 5 of [1], can further drop on e log factor from the r u n time. Ac kn o wledgmen ts W e thank Chen g-W ei Luo f or helpful discussion. References [1] Dee pak Ajwa ni, T obias F r iedric h, and Ulrich Mey er. An O ( n 2 . 75 ) Algorithm for Onlin e T op ological Ordering. In P r o c e e dings of the 10th Sc andinavian Workshop on Algorithm The ory ( SW A T ), 53- 64, 2006. [2] Bo wen Alp ern, Roger Ho o ver, Barry K. Rosen, Pete r F. Sw eeney , and F. Kenn eth Zadec k. In- cremen tal E v aluation of Compu tat ional Circuits. In Pr o c e e dings of the First Annual ACM-SIAM Symp osium on D iscr ete Algorithms ( SODA ), 32-42, 1990. [3] Irit Katriel. On Algorithms f or Online T op ological Or dering and Sorting. Researc h Rep ort MPI- I-2004- 1-003, Max-Planc k-In stitut f ¨ u r Inf ormati k, Saarbr ¨ u c k en , Germ any , 2004. [4] Irit Katriel and Hans L. Bo dlaender. On line T op ological Or dering. ACM T r ansactions on Algo- rithms , 2(3): 364- 379, 2006. [5] Alberto Marc h ett i-Spaccamela, Umberto Nanni, and Hans Rohnert. On-Line Graph Algorithms for Incremental Compilation. In Pr o c e e ding of International Workshop on Gr aph-The or etic Con- c epts in Computer Scienc e ( WG ), 70-86, 1993. [6] Alberto Marc hetti-Spaccamela, Um b erto Nanni, and Hans Rohnert. Main taining a T op ological Order Under Ed ge Insertions. Information Pr o c essing L etters 59(1): 53-58 , 1996. [7] Da v id J. P earce and Paul H. J. K elly . A Dyn amic Algorithm for T op ologic ally Sorting Directed Acyclic Graphs. ACM J ourna l of Exp erimental Algorithms , 11(1.7 ): 1-24, 2007. [8] Ganesa n Ramalingam and Th omas W. Reps. On C ompetitiv e On-Line Algorithms for the Dy- namic Pr iority-Ordering Problem. Information Pr o c essing L etters , 51(3): 155-161, 1994. [9] Hsiao -F ei Liu and Kun-Mao Chao. A Tigh t An alysis of the Katriel-Bo dlaender Algorithm for Online T op olog ical O rdering. The or etic al Computer Scienc e , to app ear.

Original Paper

Loading high-quality paper...

Comments & Academic Discussion

Loading comments...

Leave a Comment