200 likes | 304 Views
A dynamic algorithm for topologically sorting directed acyclic graphs. David J. Pearce and Paul H.J. Kelly Imperial College, London, UK d.pearce@doc.ic.ac.uk www.doc.ic.ac.uk/~djp1/. Introduction. Topologically sorting a directed acyclic graph G=(V,E). U. W. U. Y. Z. W. V. T. X. S.
E N D
A dynamic algorithm for topologically sorting directed acyclic graphs David J. Pearce and Paul H.J. Kelly Imperial College, London, UK d.pearce@doc.ic.ac.uk www.doc.ic.ac.uk/~djp1/
Introduction • Topologically sorting a directed acyclic graph G=(V,E) U W U Y Z W V T X S Z Y X T V • Sort nodes so X beforeY if XY E, x,yV • Well-known algorithms taking O(v + e) time • E.g. using depth-first search S
Problem Definition • How to update topological sort after edge insertion? U W U Y Z W V T X S Z Y X T V • Invalidating or non-invalidating? • Adding YV does not invalidate sort, but XY does • How to deal with invalidating edges? • Re-sorting entire graph takes O(v+e) time again S
Problem Definition • How to update topological sort after edge insertion? U W U Y Z W V T X S Z Y X T V • Invalidating or non-invalidating? • Adding YV does not invalidate sort, but XY does • How to deal with invalidating edges? • Re-sorting entire graph takes O(v+e) time again S
Problem Definition • How to update topological sort after edge insertion? U W U Y Z W V T X S Z Y X T V • Invalidating or non-invalidating? • Adding YV does not invalidate sort, but XY does • How to deal with invalidating edges? • Re-sorting entire graph takes O(v+e) time again S
Performing less work • How to avoid resorting entire graph? U W U Y Z W V T X S Z Y X Affected region T V Definition: affected region, ARxy, is set of nodes between X and Y for some edge insertion X Y S Lemma: only affected region needs reordering to obtain valid sort Proof: by Marchetti-Spaccamela et al. [MNR96]
Performing less work (continued) • How to re-sort affected region? U W U Y Z W V T X S Before Z Y X T V U Z W V T X Y S After S • Could just move Y to right of X! • But, V now incorrectly prioritised with respect to Y • Problem, cannot move Y past nodes it reaches in affected region
Algorithm MNR • Algorithm MNR due to Marchetti-Spaccamela et al. [MNR96] U W U Y Z W V T X S Before Z Y X T V U Z W T X Y V S After • Depth-First Search from Y identifies reachable set • Only visits those in affected region • Shift other nodes to the left • Every node in affected region moved – so at least O(ARXY) time S
Algorithm PK • Algorithm PK - main contribution of this work U W U Y Z W V T X S Before Z Y X T V U W Z X Y T V S After S • Key insight: can avoid resorting entire affected region • Only set xy = {Y,W,V,X} needs resorting • {Z,T} remain untouched • This saves work compared with MNR
Algorithm PK • Algorithm PK – how does it work? U W U Y Z W V T X S Z Y X W X Y V T V RB RF S • Observation: nodes in RB must come before those in RF • RB = all nodes reaching X (including X) • RF = all nodes reachable from Y (including Y)
Algorithm PK • Algorithm PK – how does it work? U W U Y Z W V T X S Before Z Y X Forward DFS T V Backward DFS S • Use forward and backward Depth-First Search • Forward search to determine RF (same as MNR) • Backward search to determine RB
Algorithm PK • Algorithm PK – how does it work? W X Y V U W RB RF Z Y X T V U ? Z ? ? T ? S S • Place RB and RF into slots previously held by RB RF • RB goes into leftmost slots, RF into rightmost slots • Thus, Z and T do not need repositioning
Algorithm PK • Algorithm PK – how does it work? U Y Z W V T X S U W Z Y X W X Y V T V RB RF S U W Z X Y T V S
Algorithm PK – Complexity ? Definition: Let XY = RF RB U W • Here, xy = {Y,V,W,X} Definition: E(K) = { XYE | XK YK } Z Y X • e.g., E(XY) = { UX, UY, WX, XY, YV } T V Definition: Let ||K|| = |K| + E(K) Conclusion: complexity is ~O(||XY||) time S • MNR actually needs O(|ARXY| + ||XY||) time • Thus, PK has tighter bound than MNR
Is XY minimal ? • The answer is no. For example: U W X Y U W X Y W U X Y • Key point: U not repositioned • But, under PK it would be since U XY • Scope for an even better algorithm ?
Algorithm AHRSZ Definition: A set K is a cover if XK YK, for every incorrectly ordered pair X,Y. A cover, Kmin, is minimal no smaller cover is possible. • Algorithm AHRSZ due to Alpern et al. [AHRSZ90] • Worst-case time complexity O(||Kmin||log ||Kmin||) • So, tighter bound than algorithm PK – but is it practical? • Issues with algorithm AHRSZ • Employs Deitz and Sleator ordered list structure [DS87] • Permits new priorities values to be created in O(1) time • But, is complex to implement and has relatively high overheads Lemma: For invalidating edge insertion XY it holds that KminXY
Experimental Study • Experiment • Measure cost per insertion over 5000 edges insertions • Invalidating and non-invalidating included to reflect actual performance • Observations • MNR has linear performance in V, but optimal on dense graphs • AHRSZ similar behaviour to PK, but constant factor slower
Conclusion • Stuff • Batch algorithm and Cycles • Links • Work of Irit