Sei sulla pagina 1di 42

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:

1. Layering
2. Radial drawing
3. HV-drawing
4. Recursive winding

2
Tree Drawing Algorithms

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

3
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

4
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

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

 Divide and conquer

 Reduce width

 Horizontally centers
parent vertex

 Yields aesthetically
pleasing drawings

8
Layered-Tree-Draw Algorithm
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.
9
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 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

11
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

12
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
13
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: linear


Post order: linear

Hence, algorithm runs


in linear time.

15
Tree Drawing Algorithms

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

16
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 Ci

17
Radial Drawing: Wedge Angle
 Subtree rooted at vertex v
is drawn within annulus wedge Wv.

 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
Wv can extend outside Wv and
intersect other edges.

18
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)
 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. Layered Drawing
2. Radial Drawing
3. HV-Drawing
4. Recursive Winding

21
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
22
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

23
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)

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

25
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
26
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

 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

28
HV Drawing Summary

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

30
Recursive Winding
Recursive-Wind-Tree-Draw Algorithm:

 Planar (no edges intersect)

 Downward

 Straight-lined drawings of
binary trees

31
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 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) ≤ log2l,
W(l) ≤ A,
t(l) = O(A)
if l ≤ A
33
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,


since l(v1), l(v2), … is in decreasing order

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

Let T’=T(left(vk)), T’’=T(right(vk)), T2


l’=l(left(vk)), and l’’=l(right(vk)) Vk-2

Note that Vk-1


Tk-2
 l’≤ l’’, since T is right heavy
vk
 l1 + … + lk-1 = l - l(vk) < A Tk-1
 max{l’,l’’} = l(vk+1) ≤ l – A

T’ T’’

35
Recursive Wind Tree Draw Algorithm
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 π/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

38
Recursive Wind Tree Draw Algorithm

39
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 )
40
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)
41
42

Potrebbero piacerti anche