1 / 42

Divide and Conquer: Rooted Trees

Divide and Conquer: Rooted Trees. Introduction. Rooted trees: acyclic graphs (no cycles) all edges directed away from root. downward planar drawing (child placed no higher than parent) In this lecture, 4 divide-and-conquer techniques for constructing downward planar drawings:

albany
Download Presentation

Divide and Conquer: Rooted Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Divide and Conquer:Rooted Trees

  2. Introduction Rooted trees: • acyclic graphs (no cycles) • all edges directed away from root. • 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

  3. Tree Drawing Algorithms • Layered Drawing • Radial Drawing • HV-Drawing • Recursive Winding

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

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

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

  7. 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

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

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

  10. 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

  11. Layered-Tree-Draw Algorithm left contour: Sequence of vertices v0… 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

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

  13. Layered-Tree-Draw Algorithm L(T) = left contour list R(T) = right contour list case 1: height(T’) = height(T’’) • L(T) = L(T’) + v • R(T) = R(T’’) + v case 2: height(T’) < height(T’’) • 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 case2

  14. 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.

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

  16. Tree Drawing Algorithms • Layered Drawing • Radial Drawing • HV-Drawing • Recursive Winding

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

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

  19. 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.

  20. Radial Drawing Used for free trees (tree without a root) • 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.

  21. Tree Drawing Algorithms • Layered Drawing • Radial Drawing • HV-Drawing • Recursive Winding

  22. HV Drawing Horizontal-Vertical drawing of binary treeT: • Straight-line grid drawing. • For every vertex u, a child of u is either: • 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

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

  24. HV Drawing Order of children (embedding) is preserved only if • 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)

  25. HV Drawing Right-Heavy-HV-Tree-Draw Algorithm: Input: binary tree T Output: 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.

  26. HV Drawing Binary tree T with n vertices. (Lemma 3.1) Height of the drawing of T constructed by RHHTD is at mostlogn. Proof: 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

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

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

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

  30. Tree Drawing Algorithms • Layered Drawing • Radial Drawing • HV-Drawing • Recursive Winding

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

  32. 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 inT(v)=l(v) Recursive winding tree drawing • H(l) = height of Twithlleaves • W(l) = width of Twithlleaves • t(l) = running time

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

  34. Recursive Wind Tree Draw Algorithm Suppose l>A. Define a sequence {vi}: v1 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, sincel(v1), l(v2), … is in decreasing order

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

  36. 3 Cases: 1. If k=1, T’ and T’’ are drawn recursively below v1 2. If k=2, T1 is drawn with Right-Heavy-HV-Tree-Draw, while T’ and T’’are drawn recursively 3. If k>2, T1,…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 π/2clockwise. 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”) Recursive Wind Tree Draw Algorithm

  37. Recursive Wind Tree Draw Algorithm Make Tree Right-Heavy

  38. Recursive Wind Tree Draw Algorithm

  39. Recursive Wind Tree Draw Algorithm

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

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

More Related