I NTRODUCTION TO G RAPH DRAWING Fall 2010 Battista, G. D., Eades, P., Tamassia, R., and Tollis, I. G Graph Drawing: Algorithms for the Visualization of Graphs. 1st. Prentice Hall PTR.
D IVIDE AND CONQUER Rooted tree Layered Drawing : vertices are placed on horizontal layers Radial Drawing : layers are mapped on concentric circles HV-Drawing : edges are drawn as rightward horizontal or downward vertical segment. Recursive Winding : drawings with constant aspect ratio and small area
T ERMINOLOGY Tree: Acyclic graph Rooted tree Root: a distinguished vertex in tree Usually treated as a directed graph: all edges oriented away from the root Direct edge u --> v : u is the parent of v and v is the child of u Leaf: no child Ordered tree: rooted tree with an ordering for the children of every vertex
T ERMINOLOGY Binary tree: rooted tree with every node has at most two children Left and right child One child, either left or right Subtree rooted at v : the subgraph induced by all vertices that have v as their “ancestor” Binary tree: left and right subtree Depth of a vertex: number of edges between it and the root Height of a tree: maximum depth
T REE D RAWING A LGORITHM Layered Drawing Radial Drawing HV-Drawing Recursive Winding
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 L AYERING
In-Order Traversal: Vertex v is j -th vertex encountered in inorder traversal of the tree, then x(v) = j Problems: Much wider than necessary Parent not centered with respect to children
T REE T RAVERSALS – 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
input= binary tree T output= layered drawing of T Divide and conquer Reduce width Horizontally centers parent vertex Yields aesthetically pleasing drawings L AYERED -T REE -D RAW A LGORITHM
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. L AYERED -T REE -D RAW A LGORITHM
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.
L AYERED -T REE -D RAW A LGORITHM 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
L AYERED -T REE -D RAW A LGORITHM Compute left and right contour of vertex v : scan right contour of left subtree ( T’ ) and left contour of right subtree ( T’’ ) gather displacements of vertices on the left & right contour keep the max increasing displacement at any depth
L AYERED -T REE -D RAW A LGORITHM 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
L AYERING S UMMARY Time Complexity: Pre order: linear Post order: linear Hence, algorithm runs in linear time.
T REE D RAWING A LGORITHM Layered Drawing Radial Drawing HV-Drawing Recursive Winding
17 R ADIAL D RAWING 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 R ADIAL D RAWING : W EDGE A NGLE Subtree rooted at vertex v is drawn within annulus wedge W v. It may seem reasonable to choose wedge angle to be proportional to l(v) = number of leavs in the subtree rooted at v This can lead to edge crossings, because edge with endpoints within W v can extend outside W v and intersect other edges.
19 R ADIAL D RAWING : W EDGE A NGLE 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.
T REE D RAWING A LGORITHM Layered Drawing Radial Drawing HV-Drawing Recursive Winding
HV-D RAWING – B INARY T REE HV-drawing of a binary tree T : straight-line grid drawing such that for each vertex u, a child of u is either horizontally aligned with and to the right of u, or vertically aligned with and below u the bounding rectangles of the subtrees of u do not intersect Planar, straight-line, orthogonal, and downward
D IVIDE & C ONQUER M ETHOD Divide: recursively construct hv-drawings for the left & right subtrees Conquer: perform either a horizontal combination or a vertical combination The height & width are each at most n-1
R IGHT -H EAVY -HV-T REE -D RAWING ALGORITHM *** 1. Recursively construct drawing of the left & right subtrees 2. Using only horizontal combination, place the subtree with the largest number of vertices to the right of the other one.
L EMMAS AND THEOREMS Let T be a binary tree with n vertices, the height of the drawing of T constructed by the Right-Heavy-HV- Tree-Drawing Algorithm is at most logn. Let T be a binary tree with n vertices, Right-Heavy- HV-Tree-Drawing Algorithm construct a drawing Γ of T in O( n ) time, such that: Γ is an hv-drawing The area of Γ is O( nlogn ) The width of Γ is at most n-1 The height of Γ is at most logn
R IGHT -H EAVY -HV-T REE -D RAWING HV-drawing (downward, planar, grid, straight-line and orthogonal) Width is at most n-1 Height is at most logn The larger subtree is always placed to the right The size of parent subtree is at least twice the size of vertical child subtree area O(nlogn)
A REA -A SPECT R ATIO Right-Heavy-HV-Tree-Draw Good area bound, but bad aspect ratio Better aspect ratio: use both vertical and horizontal combinations Alternating the combination Odd level: horizontal, even level: vertical O(n) area and constant aspect ratio
It is possible to construct an HV-drawing of a binary tree that is optimal with respect to area or perimeter in O(n 2 ) time. Right-Heavy-HV-Tree-Drawing can be extended to general rooted tree Downward, planar, grid, straight-line Area O(nlogn) Width is at most n-1 Height is at most logn O PTIMIZATION AND E XTENSION Largest subtree
T REE D RAWING A LGORITHM Layered Drawing Radial Drawing HV-Drawing Recursive Winding
R ECURSIVE W INDING Similar to HV- drawing For binary tree Produce planar downward straight- line grid drawing Constant aspect ratio Almost linear area T1T1 T k-2 T k-1 T’ T’’ …
R ECURSIVE W INDING Input: a binary tree with n vertices and l leaves. n=2 l -1 For a vertex v : left(v) : left child; right(v) : right child T(v) : subtree rooted at v l (v): number of leaves in T(v) Recursive winding tree drawing H( l ): height of the drawing of T with l leaves W( l ): width of the drawing of T with l leaves t( l ) : running time
R ECURSIVE W INDING T REE D RAWING Arrange the tree so that l (left(v)) ≤ l (right(v)) at every vertex v ; Give a parameter A>1,if l ≤A, then draw the tree using right-heavy-HV-tree; H( l ) ≤log 2 l, W( l )≤A, and t( l )=O(A) ; If l >A, define a sequence { v i }: v 1 is the root and v i +1=right(v i ) for i=1,2,… ; Let k≥1 be an index with l (v k )> l -A and l (v k+1 )≤ l -A. Such a k can be found in O(k) time, since l (v 1 ), l (v 2 ), … is a strictly decreasing order
R ECURSIVE W INDING T REE D RAWING 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’’
R ECURSIVE W INDING T REE D RAWING If k=1, T’ and T’’ are drawn recursively below v 1 ; If k=2, T 1 is drawn with right-heavy-HV-tree, while T’ and T’’ are drawn recursively; If k=3, T 1,…T k-2 are drawn from left to right with right-heavy-HV-tree. T k-1 is drawn right-heavy-HV- tree and then reflected around y-axis and rotated by π/2. T’ and T’’ are drawn recursively below and then reflected around y-axis so that their roots are placed at upper right- hand corners. (This is the “recursive winding”) v1v1 T’ T’’ k=1 v1v1 T1T1 T’’ T’ k=2 T1T1 T k-2 T k-1 T’ T’’ … k>2
R ECURSIVE W INDING T REE D RAWING Bounds: 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)
R ECURSIVE W INDING T REE D RAWING The running time is O(n) ; By setting A as A = √( l ·log 2 l ), the height and width of the drawing are both O(√nlogn). An example