Incremental topological ordering with Bernhard Haeupler, Sid Sen • Two problems on digraphs: Cycle detection Topological ordering: a total order O such that v w O(v) < O(w) Incremental topological ordering • A digraph is acyclic if it has at least one topological ordering 3 5 1 2 4 • Static: O(n + m) time, n = # vertices, m = # arcs Assume m ≥ n (simplicity) Dynamic: on-line, incremental • Start with vertex set, no arcs • Add one arc at a time Maintain a topological order Report a cycle as soon as one is created How to maintain ordering? • 1-1 mapping: V {1,2,…,n} via 1 or 2 arrays (inverse?) • Dynamic ordered list query: v < w ? update: delete v insert v (before or after) w a, b, c, d, e, f, g, h move c after f O(1) time per operation (Dietz, Sleator 1987; Bender, Cole, Demaine, Farach-Colton, Zito 2002) Adding an arc • Given an existing order, how to handle a new arc v w? If v < w: do nothing If w < v: search for a cycle or a set of vertices to reorder to restore topological order • Affected region: all vertices x : w ≤ x ≤ v Adding an arc • Search forward from w, but not from vertices > v w v x • Vertex states during search: U = unlabeled L = labeled, initially {w} S = scanned Adding an arc • scan(x): for x y if y = v stop (cycle) if y < v and y U add y to L • while L ≠ Ø: delete some x from L, scan(x), add x to S • reorder: move S after v (in order) Adding an arc • Running time? Count related pairs: some paths contain more than one type vertex, vertex pairs – arc, vertex pairs – nm m arc, arc pairs – 2 n 2 • One-way search: x y traversed v, x y newly related O(m) amortized time per arc addition; O(nm) total (vs. O(m2)) • Marchetti-Spaccamela, Nanni, Rohnert 1996 Two-way (bidirectional) search • Forward from w, backward from v concurrently v w ? w v ? When to stop searching? When to pay for a search step? Two-way (bidirectional) search • Stop when x with: no forward labeled vertices < x no backward labeled vertices > x • Reorder: Move forward scanned vertices (≠ x) after x Move backward scanned vertices (≠ x) before x v w zU x v w x z How to pay? • Traverse arcs in pairs: x y forward, z u backward allowed if x < u • Adding v w relates x y, z u (unless cycle) • Search time = O(1) per arc traversal + overhead • k traversal steps k2/4 new related arc pairs m (out of 2 ) k ≤ m1/2 O(m1/2) time per arc addition k > m1/2 (km1/2)/4 new pairs k = O(m3/2) O(m1/2) amortized per arc addition How to implement? • Ordered search: Scan smallest forward labeled vertex or largest backward labeled vertex Stop when next forward vertex > next backward vertex All forward traversed arcs form pairs with all backward traversed arcs • But: need a heap (priority queue) for: F = forward labeled vertices R = backward labeled vertices • Logarithmic (?) overhead History of two-way search method • Idea: Alpern, Hoover, Rosen, Sweeney, Zadeck 1990 Incremental bound (per arc addition) • Katriel, Bodlaender 2005 O(min{m3/2 logn, m3/2 + n2 logn}) • Liu, Chao 2007 O(m3/2 + mn1/2 logn) • Kavitha, Mathew 2007 O(m3/2 + nm1/2 logn) • Improvable, but… All bounds for m arc additions Avoid heaps? • Can we eliminate the heap altogether? Get O(m1/2) per arc amortized time? • Balanced safe search F=A∪B bypassed (temporarily) R=C∪D Choose x A, u C If x < u, traverse x y, z u If x > u, bypass x (move to B) or u (move to D) Split: • Which? What if A or C is empty? Soft threshold • Maintain a tentative threshold t, initially w or v If x > u, bypass x if x > t, u if u < t (if both, choose one) if A is empty, replace A by B, choose new t uniformly at random from A (or median) Similarly if C is empty Amortized O(1) bypassed vertices per search step (expected or worst-case) O(1) overhead per search step O(m1/2) amortized time per arc addition forward vertex unreached vertex backward vertex scanned vertex (v,w) a w l,sl b c d s A = {w}, B = {}, D = {}, C = {v} e v f h a forward vertex unreached vertex backward vertex scanned vertex w l b l,ssl c d s A = {}, B = {b,d}, D = {}, C = {c,e} e v f h a forward vertex unreached vertex backward vertex scanned vertex w b l c s d s e hh,s A = {}, B = {d}, D = {c}, C = {} v f h a forward vertex unreached vertex backward vertex scanned vertex w b l c s ds e h A = {}, B = {}, D = {c}, C = {} v f a w forward vertex unreached vertex backward vertex scanned vertex b X c s e v Y f a c forward vertex unreached vertex backward vertex scanned vertex e v w b s f • Lower bounds (n logn) vertex reorderings for any algorithm Ramalingam and Reps 1994 (n2) vertex reorderings if order = 1-1 mapping reordering only within affected region (nm) vertex reorderings if reordering only within affected region • Dense case O(n2.75) Ajwani, Friedrich, Meyer 2006 Õ(n2.5) Liu, Chao 2007 O(n2.5) Kavitha, Mathew 2007