Divide and Conquer Algorithms for Trees and Series
Transcription
Divide and Conquer Algorithms for Trees and Series
Algorithms for Graph Visualization Summer Semester 2016 Lecture #4 Divide-and-Conquer Algorithms: Trees and Series-Parallel Graphs (based on slides from Martin Nöllenburg and Robert Görke, KIT) 1 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Uses of Divide & Conquer Well suited for inductively/recursively defined Graph Classes 1. draw the left subtree 2. draw the right subtree 3. combine together + draw root Terminology Rooted Binary Trees: r root Subtree T (v) v right child left child depth(v): distance from the root traversal • preorder • inorder • postorder left subtree Tl (v) right subtree Tr (v) 2 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Overview Grid Size balanced drawings of binary trees O(nh) radial drawings of trees O(nh) compact drawings of trees O(n log n) upward drawings of series parallel graphs exp. 3 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Algorithm of Reingold and Tilford (’81) Trivial: Divide: If T = {v}, draw v (e.g., as a small disk). Run Alg. recursively on the left and right subtrees. Conquer: shift the partial drawings to up to 2 units apart place the root r one above and centrally btw. children. or 3! Grid Drawing? 2 4-7 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Algorithm of Reingold and Tilford (’81) Implementation in 2 Phases: 1. postorder (bottom-up): contours and x-offsets gather the predecessors 2 2. preorder (top-down): calculate absolute coordinates Contour: linked list of vertices (-coordinates) 4-9 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Algorithm of Reingold and Tilford (’81) Phase 1: 1. compute T` (v) und Tr (v) 2. trace the right contour of T` (v) and left of Tr (v) 3. Find dv = min. horiz. distance between v` und vr 4. x-offset(v` ) = −ddv /2e, x-offset(vr ) = ddv /2e v 5. Build left contour of Tv from: v, v` left contour of T` (v), left contour of any low hanging part of Tr (v) 6. Symmetrically for right contour. vr 5-3 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Algorithm of Reingold and Tilford (’81) Phase 1: 1. compute T` (v) und Tr (v) 2. trace the right contour of T` (v) and left of Tr (v) 3. Find dv = min. horiz. distance between v` und vr 4. x-offset(v` ) = −ddv /2e, x-offset(vr ) = ddv /2e v 5. Build left contour of Tv from: v, v` left contour of T` (v), left contour of any low hanging part of Tr (v) 6. Symmetrically for right contour. vr 5-10 Runtime? Steven Chaplick P v (1 + min{h` (v), hr (v)}) = n + · Lehrstuhl für Informatik I P · v min{. . . } ≤ n + n Universität Würzburg Algorithm of Reingold und Tilford (’81) Phase 2: Set y-coordinate y(v) = −depth(v) for each vertex v. Set x(w) := 0 for the root w, then in preorder for v ∈ V : x(v` ) := x(v) + x-offset(v` ) and x(vr ) := x(v) + x-offset(vr ). Runtime? O(n) 6 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Summary for Balanced Drawings of Binary Trees Theorem [Reingold & Tilford ’81] For a binary tree with n vertices, in O(n) time we can produce a drawing Γ such that: Easily generalizes to arbitrary trees! Γ is layered, i.e., y ≡ − depth, Γ is planar, straightline, and strictly downward, Γ matches the embedding (i.e., right children on the right), all vertices: horiz. & vert. distances ≥ 1, and on the grid, example? the area is O(n2 ), parent always centered above children. Min. width (but without the grid): by LP! Min. width and on the grid: NP-hard! [Supowit & Reingold ’83] 7 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Example of width variation Output of the Algorithm: Optimal Drawing: 8 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg 2. Radial Drawings of Trees Grid Size balanced drawings of binary trees O(nh) radial drawings of trees O(nh) compact drawings of trees O(n log n) upward drawings of series parallel graphs exp. 9 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Example: Radial Tree Layouts 10 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg An Algorithm for Radial Layout? 9 10 11 · 7 8 1 6 · 1 9 1 9 10 1 8 1 7 1 · 1 10 5 1 1 3 1 11 9 1 1 7 5 11 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Restricting to Smaller Sectors αv ρi αmin τ ρi+1 v cos τ = 12 ⇒ αmax Steven Chaplick · Lehrstuhl für Informatik I ρi ρi+1 i αmin = αv − arccos ρρi+1 i αmax = αv + arccos ρρi+1 · Universität Würzburg Pseudocode for radial tree layout RadialTreeLayout(tree T , root r ∈ T , radii ρ1 < · · · < ρk ) preorder(vertex v, t, αmin , αmax ) begin postorder (r) d v ← ρt { output } preorder (r, 0, 0, 2π) αv ← (αmin + αmax )/2 return (dv , αv )v∈V (T ) if t > 0 then t {vertex pos./ polar coord.} αmin ← max{αmin , αv −arccos ρρt+1 } ρt α ← min{α , α +arccos } max max v postorder(vertex v) ρt+1 nv ← 1 foreach child w von v do postorder (w) nv ← nv + nw 13 size of the subtree T (v) Steven Chaplick · left ← αmin foreach child w von v do ·(αmax − αmin ) right ← left + nnvw −1 preorder (w, t + 1, left, right) left ← right Runtime? O(n). Lehrstuhl für Informatik I · Correctness? X Universität Würzburg Overview Grid Size balanced drawings of binary trees O(nh) radial drawings of trees O(nh) compact drawings of trees O(n log n) upward drawings of series parallel graphs exp. 14 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg hv-Drawings Definition. An hv-drawing of a binary tree is a straight line drawing, so that for each vertex v: each child of v is either directly right or directly below v. the smallest axis-parallel rectangle enclosing the subtrees of the children of v are disjoint. 15 Steven Chaplick horizontal combination · vertical combination Lehrstuhl für Informatik I · Universität Würzburg Algorithm RightHeavyHVTreeDraw Recursively construct drawings of the left and right subtrees from the root. Place the larger subtree on the right using the horizontal combination, and the smaller on the left Size of a subtree := number of vertices at least ·2 at least ·2 at least ·2 16 Obs. The drawing has width ≤ n , Steven Chaplick · Lehrstuhl für Informatik I height ≤ log2 n. · Universität Würzburg Overview Grid Size balanced drawings of binary trees O(nh) radial drawings of trees O(nh) compact drawings of trees O(n log n) upward drawings of series parallel graphs exp. 17 Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg Series Parallel Graphs t simple series parallel graph s Induction: combining two series parallel graphs G1 , G2 . . . t1 t2 G2 G1 s2 s1 - . . . series . . . t1 = s2 18 s1 - . . . or parallel. G2 G1 Steven Chaplick t1 = t2 t2 G1 s1 = s2 · Lehrstuhl für Informatik I G2 · Universität Würzburg Decomposition Tree for SP-graphs P P S S S Q Q 19 Q P S Q · Q Lehrstuhl für Informatik I Q S Q Generalization: SPQR-Tree Steven Chaplick S Q Q · Q Universität Würzburg SP-Graphs: applications PERT-Diagrams Flow Charts Provides: 20 Steven Chaplick (Program Evaluation and Review Technique) Linear time algorithms for NP-complete problems (e.g., Maximum Independent Set) · Lehrstuhl für Informatik I · Universität Würzburg Grid Size Theorem [Bertolazzi et al. ’92] There is a family (Gn )n∈N of embedded SP-graphs where Gn has 2n vertices and every upward planar drawing of Gn requires tn+1 Ω(4n ) area. Proof: tn+1 ∆2 tn t0 tn Gn Gn sn sn+1 s0 21 G0 sn−1 ∆1 sn+1 Π Gn+1 sn a(Gn+1 ) ≥ a(Π) + a(∆1 ) + a(∆2 ) ≥ 2 · a(Π) ≥ 4 · a(Gn ) Steven Chaplick · Lehrstuhl für Informatik I · Universität Würzburg