Divide and Conquer Rooted Trees Introduction Rooted trees

  • Slides: 42
Download presentation
Divide and Conquer: Rooted Trees

Divide and Conquer: Rooted Trees

Introduction Rooted trees: l acyclic graphs (no cycles) l all edges directed away from

Introduction Rooted trees: l acyclic graphs (no cycles) l all edges directed away from root. l downward planar drawing (child placed no higher than parent) In this lecture, 4 divide-and-conquer techniques for constructing downward planar drawings: 1. Layering 2. Radial drawing 3. HV-drawing 4. Recursive winding 2

Tree Drawing Algorithms 1. 2. 3. 4. Layered Drawing Radial Drawing HV-Drawing Recursive Winding

Tree Drawing Algorithms 1. 2. 3. 4. Layered Drawing Radial Drawing HV-Drawing Recursive Winding 3

Layering A simple method for constructing downward planar drawing: • • • Vertex with

Layering A simple method for constructing downward planar drawing: • • • Vertex with depth i is placed into layer Li Root placed in the top layer L 0 Layered drawing is strictly downward. Vertex v of depth i has y(v) = - i 4

Layering Ordering of vertices in each layer: Avoid crossings – left-right order of two

Layering Ordering of vertices in each layer: Avoid crossings – left-right order of two vertices is same as their parents. Computing x-coordinate: l l Place parent in horizontal span of its children (possibly in a central position). Ordering of children might be fixed 5

Tree Traversals – short review In Order: Pre Order: Post Order: A, B, C,

Tree Traversals – short review In Order: Pre Order: Post Order: A, B, C, D, E, F, G, H, I F, B, A, D, C, E, G, I, H A, C, E, D, B, H, I, G, F 6

Layering In-Order Traversal: Vertex v is i-th vertex encountered in inorder traversal of the

Layering In-Order Traversal: Vertex v is i-th vertex encountered in inorder traversal of the tree, then x(v) = i Problems: • • Much wider than necessary Parent not centered with respect to children 7

Layered-Tree-Draw Algorithm input = binary tree T output = layered drawing of T l

Layered-Tree-Draw Algorithm input = binary tree T output = layered drawing of T l Divide and conquer l Reduce width l Horizontally centers parent vertex l Yields aesthetically pleasing drawings 8

Layered-Tree-Draw Algorithm Base Case: l If T has only 1 vertex, drawing is trivially

Layered-Tree-Draw Algorithm Base Case: l If T has only 1 vertex, drawing is trivially defined Divide: l recursively apply algorithm to draw left and right subtrees of T. Conquer: l move drawings of subtrees until horizontal distance = 2. l place the root r vertically one level above and horizontally half way between its children. l If only one child, place root at horizontal distance 1 from child. 9

Layered-Tree-Draw Algorithm Implementation - Two Traversals of T: 1. Post-order traversal For each vertex

Layered-Tree-Draw Algorithm Implementation - Two Traversals of T: 1. Post-order traversal For each vertex v, recursively compute horizontal displacement of left & right children of v with respect to v. 2. Pre-order traversal Compute x-coordinates of vertices by accumulating displacements on path from each vertex to root. Compute y-coordinates of vertices by determining depth of each vertex 10

Layered-Tree-Draw Algorithm left contour: Sequence of vertices v 0… vh such that vi is

Layered-Tree-Draw Algorithm left contour: Sequence of vertices v 0… vh such that vi is the leftmost vertex of T with depth i (right contour is defined similarly) In the conquer step, follow right contour of left subtree and left contour of right subtree 11

Layered-Tree-Draw Algorithm Compute left and right contour of vertex v: l scan right contour

Layered-Tree-Draw Algorithm Compute left and right contour of vertex v: l scan right contour of left subtree (T’) and left contour of right subtree (T’’ ) l accumulate displacements of vertices on the left & right contour l keep the max cumulative displacement at any depth 12

Layered-Tree-Draw Algorithm L(T) = left contour list R(T) = right contour list case 1:

Layered-Tree-Draw Algorithm L(T) = left contour list R(T) = right contour list case 1: height(T’) = height(T’’) l l L(T) = L(T’) + v R(T) = R(T’’) + v case 2: height(T’) < height(T’’) l l R(T) = R(T’’) + v L(T) = v + L(T’) + {part of L(T’’) starting from w} h’: depth of T’ w: the vertex on L(T’’) whose depth = h’+1 case 3: height(T’) > height(T’’) : similar to case 2 13

Layered-Tree-Draw Algorithm Efficiency: Necessary to travel down countours of T’ and T’’ only as

Layered-Tree-Draw Algorithm Efficiency: Necessary to travel down countours of T’ and T’’ only as far as height of shorter subtree. Thus, postorder traversal is proportional to minimum of the heights of T’ and T’’. The sum is no more than # of vertices of the tree Hence, linear time. 14

Layering Summary Time Complexity: Pre order: Post order: linear Hence, algorithm runs in linear

Layering Summary Time Complexity: Pre order: Post order: linear Hence, algorithm runs in linear time. 15

Tree Drawing Algorithms 1. 2. 3. 4. Layered Drawing Radial Drawing HV-Drawing Recursive Winding

Tree Drawing Algorithms 1. 2. 3. 4. Layered Drawing Radial Drawing HV-Drawing Recursive Winding 16

Radial Drawing l A variation of layered drawing l Root at the origin l

Radial Drawing l A variation of layered drawing l Root at the origin l Layers are concentric circles centered at the origin l Vertices of depth i placed on circle Ci 17

Radial Drawing: Wedge Angle l Subtree rooted at vertex v is drawn within annulus

Radial Drawing: Wedge Angle l Subtree rooted at vertex v is drawn within annulus wedge Wv. l l(v) = # of leaves in subtree rooted at v l It may seem reasonable to choose wedge angle to be proportional to l(v) l This can lead to edge crossings, because edge with endpoints within Wv can extend outside Wv and intersect other edges. 18

Radial Drawing: Wedge Angle Prevent edge crossings - restrict vertices to convex subset of

Radial Drawing: Wedge Angle Prevent edge crossings - restrict vertices to convex subset of wedge. Suppose: vertex v lies on Ci, tangent to Ci through v meets Ci+1 at a and b. Region Fv is convex. We restrict subtree rooted at v to lie within Fv Children of v arranged on Ci+1 according to # of leaves in their respective subtrees. More precisely - for each child u of v, the angle βu of wedge Wu is βu = min( [ βv * l(u) / l(v)], τ ) βv = angle of Wv, τ = angle formed by Fv child u is placed on Ci at the center of Wv. 19

Radial Drawing Used for free trees (tree without a root) l l Fictitious root

Radial Drawing Used for free trees (tree without a root) l l Fictitious root is selected to be center of tree Fictitious root should also minimize tree height Center found in linear time using simple recursive leaf pruning algorithm: If tree has at most 2 vertices: We have found the center(s) Else: We remove all the leaves. Do this recursively until find center(s). If Center is unique: Place center at origin 2 Centers: Edge which joins them is drawn as a horizontal line of length 1 with midpoint at origin. 20

Tree Drawing Algorithms 1. 2. 3. 4. Layered Drawing Radial Drawing HV-Drawing Recursive Winding

Tree Drawing Algorithms 1. 2. 3. 4. Layered Drawing Radial Drawing HV-Drawing Recursive Winding 21

HV Drawing Horizontal-Vertical drawing of binary tree. T: l Straight-line grid drawing. l For

HV Drawing Horizontal-Vertical drawing of binary tree. T: l Straight-line grid drawing. l For every vertex u, a child of u is either: 1. 2. l horizontally aligned to the right of u, or vertically aligned below u The bounding rectangles of subtrees of u do not intersect HV drawings are: Planar (no edges intersect) Straight-lined Orthogonal (perpendicular, 90 degree angles) Downward 22

HV Drawing Divide and Conquer scheme: Divide: Recursively construct hv-drawings for the left and

HV Drawing Divide and Conquer scheme: Divide: Recursively construct hv-drawings for the left and right subtrees Conquer: perform either l a horizontal combination or l a vertical combination 23

HV Drawing Order of children (embedding) is preserved only if l l Left subtree

HV Drawing Order of children (embedding) is preserved only if l l Left subtree placed to the left in horizontal combination Left subtree placed below in vertical combination Height and width are each at most n-1 (n is # of vertices of the tree) 24

HV Drawing Right-Heavy-HV-Tree-Draw Algorithm: Input: Output: binary tree T hv-drawing of T 1. Recursively

HV Drawing Right-Heavy-HV-Tree-Draw Algorithm: Input: Output: binary tree T hv-drawing of T 1. Recursively construct drawing of the left and right subtrees of T 2. Using horizontal combination, place subtree with largest number of vertices to right of other subtree. 25

HV Drawing Binary tree T with n vertices. (Lemma 3. 1) Proof: Height of

HV Drawing Binary tree T with n vertices. (Lemma 3. 1) Proof: Height of the drawing of T constructed by RHHTD is at most logn. let w = lowest vertex in T horizontal combinations: larger subtree placed to the right of smaller one Each vertical edge has unit length Height of T = # of vertical edges encountered when traversing path from w to root 26

HV Drawing (Lemma 3. 1) Height of the drawing of T constructed by RHHTD

HV Drawing (Lemma 3. 1) Height of the drawing of T constructed by RHHTD is at most logn. Proof: Traverse vertical edges from w to root such that: size of parent subtree is at least twice the size of vertical child subtree Hence, # of vertical edges traversed is at most logn 27

HV Drawing: Algorithm Right-Heavy-HV-Tree-Draw l Good area bound, but bad aspect ratio (width/height) l

HV Drawing: Algorithm Right-Heavy-HV-Tree-Draw l Good area bound, but bad aspect ratio (width/height) l Better aspect ratio: use both vertical and horizontal combinations ex: odd level - horizontal even level - vertical l O(n) area and constant aspect ratio 28

HV Drawing Summary l l Downward, planar, grid, straight-line, orthogonal Area is O(nlogn) Width

HV Drawing Summary l l Downward, planar, grid, straight-line, orthogonal Area is O(nlogn) Width is at most n-1 Height is at most logn 29

Tree Drawing Algorithms 1. 2. 3. 4. Layered Drawing Radial Drawing HV-Drawing Recursive Winding

Tree Drawing Algorithms 1. 2. 3. 4. Layered Drawing Radial Drawing HV-Drawing Recursive Winding 30

Recursive Winding Recursive-Wind-Tree-Draw Algorithm: l Planar (no edges intersect) l Downward l Straight-lined drawings

Recursive Winding Recursive-Wind-Tree-Draw Algorithm: l Planar (no edges intersect) l Downward l Straight-lined drawings of binary trees 31

Recursive Winding Recursive-Wind-Tree-Draw Algorithm: Input: binary tree T with n vertices and l leaves.

Recursive Winding Recursive-Wind-Tree-Draw Algorithm: Input: binary tree T with n vertices and l leaves. § n = 2*l - 1 Assume for each vertex v: § § § Children = left(v), right(v) Subtree rooted at v = T(v) # of leaves in T(v) = l(v) Recursive winding tree drawing § § § H(l) = height of T with l leaves W(l) = width of T with l leaves t(l) = running time 32

Recursive Wind Tree Draw Algorithm 1. Arrange T so that l(left(v)) ≤ l(right(v)) at

Recursive Wind Tree Draw Algorithm 1. Arrange T so that l(left(v)) ≤ l(right(v)) at every vertex v 2. Fix a parameter A > 1 3. If #of leaves l ≤ A, draw T using Right-Heavy-HV-Tree-Draw This provides Base Case: H(l) ≤ log 2 l, W(l) ≤ A, t(l) = O(A) if l ≤ A 33

Recursive Wind Tree Draw Algorithm Suppose l>A. Define a sequence {vi}: v 1 is

Recursive Wind Tree Draw Algorithm Suppose l>A. Define a sequence {vi}: v 1 is the root vi+1 = right(vi) for i=1, 2, … Let k≥ 1 be an index with l(vk)>l-A # of leaves at vertex k > # of leaves - A and l(vk+1)≤ l-A # of leaves at k+1 ≤ # of leaves - A Such index can be found in O(k) time, since l(v 1), l(v 2), … is in decreasing order 34

Recursive Wind Tree Draw Algorithm Let Ti=T(left(vi)) and li=l(left(vi)) for i=1, …, k-1 Note

Recursive Wind Tree Draw Algorithm Let Ti=T(left(vi)) and li=l(left(vi)) for i=1, …, k-1 Note that l l’≤ l’’, since T is right heavy l l 1 + … + lk-1 = l - l(vk) < A l max{l’, l’’} = l(vk+1) ≤ l – A v 2 T 1 T 2 … Let T’=T(left(vk)), T’’=T(right(vk)), l’=l(left(vk)), and l’’=l(right(vk)) v 1 Vk-2 Vk-1 Tk-2 vk Tk-1 T’ T’’ 35

Recursive Wind Tree Draw Algorithm 3 Cases: 1. If k=1, T’ and T’’ are

Recursive Wind Tree Draw Algorithm 3 Cases: 1. If k=1, T’ and T’’ are drawn recursively below v 1 2. If k=2, T 1 is drawn with Right-Heavy-HV-Tree-Draw, while T’ and T’’ are drawn recursively 3. If k>2, T 1, …Tk-2 are drawn from left to right with Right-Heavy-HV-Tree-Draw. Tk-1 is drawn according to RHHTD and then reflected around y-axis and rotated by π/2 clockwise. T’ and T’’ are drawn recursively below and then reflected around the y-axis so that their roots are placed at upper right-hand corners. (This is the “recursive winding”) 36

Recursive Wind Tree Draw Algorithm Make Tree Right-Heavy 37

Recursive Wind Tree Draw Algorithm Make Tree Right-Heavy 37

Recursive Wind Tree Draw Algorithm 38

Recursive Wind Tree Draw Algorithm 38

Recursive Wind Tree Draw Algorithm 39

Recursive Wind Tree Draw Algorithm 39

Recursive Wind Tree Draw Algorithm Bounds on Height and Width of drawing, and running

Recursive Wind Tree Draw Algorithm Bounds on Height and Width of drawing, and running time of algorithm: l l l H(l) ≤ max{H(l’) + H(l’’) + log 2 A + 3, lk-1 -1} W(l) ≤ max{W(l’) + 1, W(l’’), l 1+…+lk-2} + log 2 lk-1 + 1 t(l) ≤ t(l’) + t(l’’) + O(l 1+ … + lk-1 + 1) Because l 1 + … + lk-1 = l-l(vk) < A, l l l H(l) ≤ max{H(l’) + H(l’’) + O(log. A), A} W(l) ≤ max{W(l’), W(l’’), A} + O(log 2 A) t(l) ≤ t(l’) + t(l’’) + O(A) Because Max{l’, l’’}= l(vk+1)≤ l-A, l W(l) = O( l/A log. A + A ) 40

Recursive Winding Summary: l Downward, planar, grid, straight-line, orthogonal Running time is O(n) Area

Recursive Winding Summary: l Downward, planar, grid, straight-line, orthogonal Running time is O(n) Area is O(nlogn) By setting A = √(l·log 2 l), l Height and Width = O( √(nlogn) ) Aspect Ratio (width/height) = O(1) l l l 41

42

42