370 likes | 732 Views
Introduction to Graph drawing. Fall 2010 Battista, G. D., Eades , P., Tamassia , R., and Tollis , I. G. 1998 Graph Drawing: Algorithms for the Visualization of Graphs . 1st. Prentice Hall PTR. Divide and conquer. Rooted tree Layered Drawing : vertices are placed on horizontal layers
E N D
Introduction to Graph drawing Fall 2010 Battista, G. D., Eades, P., Tamassia, R., and Tollis, I. G. 1998 Graph Drawing: Algorithms for the Visualization of Graphs. 1st. Prentice Hall PTR.
Divide 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
Terminology • 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: uis 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
Terminology • 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
Tree Drawing Algorithm • Layered Drawing • Radial Drawing • HV-Drawing • Recursive Winding
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
Layering 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
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
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
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
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.
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
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’’ ) • gather displacements of vertices on the left & right contour • keep the max increasing displacement at any depth
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
Layering Summary Time Complexity: Pre order: linear Post order: linear Hence, algorithm runs in linear time.
Tree Drawing Algorithm • Layered Drawing • Radial Drawing • HV-Drawing • Recursive Winding
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
Subtree rooted at vertex v is drawn within annulus wedge Wv. 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 Wv can extend outside Wv and intersect other edges. Radial Drawing: Wedge Angle
Radial Drawing: Wedge Angle Prevent edge crossings - restrict vertices to convex subset of wedge. Suppose: vertex v lies onCi, 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.
Tree Drawing Algorithm • Layered Drawing • Radial Drawing • HV-Drawing • Recursive Winding
HV-Drawing – Binary Tree • 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
Divide & Conquer Method • 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
Right-Heavy-HV-Tree-Drawing 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.
Lemmas 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
Right-Heavy-HV-Tree-Drawing • 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)
Area-Aspect Ratio • 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
Largest subtree Optimization and Extension • It is possible to construct an HV-drawing of a binary tree that is optimal with respect to area or perimeter in O(n2)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
Tree Drawing Algorithm • Layered Drawing • Radial Drawing • HV-Drawing • Recursive Winding
Similar to HV-drawing For binary tree Produce planar downward straight-line grid drawing Constant aspect ratio Almost linear area … T1 Tk-2 Tk-1 T’ T’’ Recursive Winding
Recursive Winding • Input: a binary tree with n vertices and l leaves. • n=2l-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 withlleaves • W(l): width of the drawing of T withlleaves • t(l): running time
Recursive Winding Tree Drawing • 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) ≤log2l, W(l)≤A, and t(l)=O(A); • If l>A, define a sequence {vi}: v1 is the root and vi+1=right(vi) for i=1,2,…; • Let k≥1 be an index with l(vk)>l-A and l(vk+1)≤ l-A. • Such a k can be found in O(k) time, sincel(v1), l(v2), … is a strictly decreasing order
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 Winding Tree Drawing … vk T’ T’’
If k=1, T’ and T’’ are drawn recursively below v1; If k=2, T1 is drawn with right-heavy-HV-tree, while T’ and T’’ are drawn recursively; If k=3, T1,…Tk-2 are drawn from left to right with right-heavy-HV-tree. Tk-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”) k=1 k=2 v1 v1 T’ T1 T’ T’’ T’’ … T1 Tk-2 Tk-1 T’ k>2 T’’ Recursive Winding Tree Drawing
Recursive Winding Tree Drawing • Bounds: • 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)
The running time is O(n); By setting A as A = √(l·log2l), the height and width of the drawing are both O(√nlogn). An example Recursive Winding Tree Drawing