Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer: Rooted Trees. 2 Introduction Rooted trees: acyclic graphs (no cycles) all edges directed away from root. downward planar drawing (child.

Similar presentations


Presentation on theme: "Divide and Conquer: Rooted Trees. 2 Introduction Rooted trees: acyclic graphs (no cycles) all edges directed away from root. downward planar drawing (child."— Presentation transcript:

1 Divide and Conquer: Rooted Trees

2 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 3 Tree Drawing Algorithms 1. Layered Drawing 2. Radial Drawing 3. HV-Drawing 4. Recursive Winding

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

5 5 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 Layering

6 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 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 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 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 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 11 Layered-Tree-Draw Algorithm left contour: Sequence of vertices v 0 … v h such that v i 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 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 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 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 15 Layering Summary Time Complexity: Pre order:linear Post order:linear Hence, algorithm runs in linear time.

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

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

18 18 Radial Drawing: Wedge Angle Subtree rooted at vertex v is drawn within annulus wedge W v. l(v) = # of leaves in subtree rooted at v 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 W v can extend outside W v and intersect other edges.

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

20 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 21 Tree Drawing Algorithms 1. Layered Drawing 2. Radial Drawing 3. HV-Drawing 4. Recursive Winding

22 22 HV Drawing Horizontal-Vertical drawing of binary treeT: Straight-line grid drawing. For every vertex u, a child of u is either: 1. horizontally aligned to the right of u, 2. 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 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 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 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 26 HV Drawing Binary tree T with n vertices. (Lemma 3.1) Height of the drawing of T constructed by RHHTD is at most logn. 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 27 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

28 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 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 30 Tree Drawing Algorithms 1. Layered Drawing 2. Radial Drawing 3. HV-Drawing 4. Recursive Winding

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

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

33 33 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 Recursive Wind Tree Draw Algorithm

34 34 Suppose l >A. Define a sequence {v i }: v 1 is the root v i +1 = right(v i ) for i=1,2,… Let k≥1 be an index with l (v k )> l -A# of leaves at vertex k > # of leaves - A and l (v k+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 Recursive Wind Tree Draw Algorithm

35 35 Recursive Wind Tree Draw Algorithm Let T i =T(left(v i )) and l i = l (left(v i )) for i=1,…,k-1 Let T’=T(left(v k )), T’’=T(right(v k )), l ’= l (left(v k )), and l ’’= l (right(v k )) Note that l ’≤ l ’’, since T is right heavy l 1 + … + l k-1 = l - l (v k ) < A max{ l ’, l ’’} = l (v k +1) ≤ l – A v1v1 T1T1 v2v2 T2T2 … V k-2 T k-2 V k-1 T k-1 vkvk T’T’’

36 36 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,…T k-2 are drawn from left to right with Right-Heavy-HV-Tree-Draw. T k-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”)

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

38 38 Recursive Wind Tree Draw Algorithm

39 39 Recursive Wind Tree Draw Algorithm

40 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 ’’) + log 2 A + 3, l k-1 -1} W( l ) ≤ max{W( l ’) + 1, W( l ’’), l 1 +…+ l k-2 } + log 2 l k t( l ) ≤ t( l ’) + t( l ’’) + O( l 1 + … + l k-1 + 1) Because l 1 + … + l k-1 = l - l (v k ) < A, H( l ) ≤ max{H( l ’) + H( l ’’) + O(logA), A} W( l ) ≤ max{W( l ’), W( l ’’), A} + O(log 2A ) t( l ) ≤ t( l ’) + t( l ’’) + O(A) Because Max{ l ’, l ’’}= l (v k +1)≤ l -A, W( l ) = O( l /A logA + A )

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

42 42


Download ppt "Divide and Conquer: Rooted Trees. 2 Introduction Rooted trees: acyclic graphs (no cycles) all edges directed away from root. downward planar drawing (child."

Similar presentations


Ads by Google