• Keine Ergebnisse gefunden

Drawing algorithms for parse trees

Im Dokument Diploma Thesis (Seite 45-50)

Chapter 3 Feature Visualization

3.4 Drawing algorithms for parse trees

This section introduces specialized algorithms that are suitable to produce tidy drawings of parse trees received from WebLicht. A natural way of visualizing these rooted trees is by constructing downward planar drawings.

In the first step of a drawing process a layer assignment is performed in which each vertex of a tree is assigned to a layer such that an edge with and goes from layer to a layer below, with .

In the general case of acyclic graphs, the goals during the layer assignment are to simultaneously produce a small number of layers, as few edges as possible that span large numbers of layers, and a balanced assignment of vertices to layers [Battista].

In the case of trees the layer assignment is simple: The number of layers is given by the maximum depth of a vertex in the tree. Therefore, a vertex with depth is directly placed into layer , thus each vertex can be assigned the y-coordinate . The drawing starts with by assigning to and y-coordinate , which is the top of the drawing.

If has more vertices than the root, the assignment continues with its children. Each child vertex with depth is then assigned to a new layer below.

The edges of a parse tree can be drawn without crossings by ensuring that the left-to-right relative order of any two vertices and in layer is the same order of their parents and

in layer . As mentioned in Section 2.6.3, each vertex of a parse tree is indicated by a unique number (id) and the order of vertices is given by an increasing numbering.

Due to the layer assignment, all vertices have prescribed y-coordinates so that an algorithm for constructing a drawing only needs to compute the x-coordinates. An intuitive requirement is to position the x-coordinate of a parent vertex within the horizontal span of its children.

3.4.1 The "Layered-Tree-Draw" Algorithm

In order to construct a layered drawing of an n-ary rooted tree, the 'Layered-Tree-Draw' Algorithm is present first. It is a basic recursive approach that makes use of a divide-and-conquer strategy to draw subtrees (divide) and their children (divide-and-conquer) in the tree. The Algorithm 3-1 to this approach is defined as follows:

Algorithm 3-1: The "Layered-Tree-Draw" Algorithm

The term bounding box (mentioned in the conquer step of Algorithm 3-1) refers to a rectangle that embraces the drawing of a subtree. According to this approach, an exemplary rooted tree is presented in the next Figure 3-5.

The properties of a drawing produced by the 'Layered-Tree-Draw' Algorithm are given as follows:

 clearly encodes the depth levels by using a layered layout in which each vertex with depth is placed on a layer with y-coordinate .

 is planar, and consists of strictly downward straight lines.

 contains no crossings since the left-to-right order of the children of each vertex is preserved.

 Minimum horizontal and vertical distance of at least 1 unit between any two vertices.

 The area covered by is .

 Every parent vertex is placed horizontally in the center of a subtree.

Input: Rooted n-ary tree Output: Layered drawing of

1. Trivial case: If consists of only one vertex, output the trivial drawing.

2. Divide: Apply the algorithm recursively on each subtree (e.g. in a left-to-right order).

3. Conquer: Draw each subtree for separately. Then, place the drawing of to the right of the drawing of so that their bounding boxes are not overlapping. Now, shift to the left until the leftmost node in has a distance of units to the rightmost node in . Finally, the root is positioned vertically one unit above and horizontally in the center of the drawing of a new subtree. If only has one subtree, then place the root directly above that subtree.

Figure 3-5: An exemplary rooted tree whose nodes are placed by the Layered-Tree-Draw Algorithm along the x-axis and by the layer assignment along the y-axis. The steps beneath the drawing describe the construction of subtree .

As we can see in Figure 3-5, a major disadvantage of Algorithm 3-1 is that the produced drawing spans a large breadth, and even larger trees lead to an exponential growth in the width of the drawing.

Let us briefly describe the steps of constructing the subtree : Due to the recursive nature of the divide-and-conquer approach, the algorithm traverses down to the lowest level (1) and positions the nodes along the x-axis with a distance of towards each other. Then, in the same conquer step, the parent vertex is centered above its children at the x-coordinate (2).

In the conquer step of the previous step during the recursive run, the leaf nodes on layer are drawn (4). In (5) the subtree can be added and properly shifted to the right by so many units that the leftmost node in has a distance of units to the rightmost node that is drawn at the x-coordinate . Note, that the vertex at could equally contain a subtree to which T' would be placed with to the rightmost node in that subtree. Finally, in (6) the parent of the nodes on is positioned in the center of the resulting drawing which spans from the x-coordinate to . After that Algorithm 3-1 continues with analogously.

3.4.2 The "Reingold & Tilford" Algorithm

Reingold and Tilford modified the Layered-Tree-Draw Algorithm to produce a layout that makes smarter use of space, maximizes the density and still displays symmetries in the drawing [Reingold&Tilford]. Since their original algorithm processes binary trees only, [Walker] extended it to draw n-ary rooted trees as well. In order to perform the drawing in linear time, [Buchheim et al.] further improved Walker's algorithm.

The 'Reingold & Tilford' Algorithm follows the same divide-and-conquer strategy as the 'Layered-Tree-Draw' Algorithm (Section 3.4.1). However, at each conquer step it makes use of a local optimization heuristic in order to reduce the width, and centers a parent vertex horizontally with regards to its children. The modified Algorithm 2 is defined as follows:

Algorithm 3-2: The "Reingold & Tilford" Algorithm

The modification in Algorithm 3-2 compared to the "Layered-Tree-Draw" Algorithm is the positioning of subtrees according to their contours. A left contour of a tree with height is defined as the sequence of vertices such that each is the leftmost vertex with depth in . The right contour is defined analogously. In the conquer step the right contour of the left subtree and the left contour of the right subtree need to be followed simultaneously while ensuring that both contours keep the minimum distance of two units. This compacting step is visualized in the following Figure 3-6:

Figure 3-6: Compacting subtrees along their contours during the conquer step in the "Reingold&Tilford"-Algorithm

The basic steps of the implementation of the "Reingold & Tilford" Algorithm consists of two traversals of the input tree : In the first traversal the horizontal shifts of each child vertex relative to its parent vertex are determined. Then, in the second traversal the x-coordinates of the

Input: Rooted n-ary tree

Output: Compact layered drawing of

1. Trivial case: If consists of only one vertex, output the trivial drawing.

2. Divide: Apply the algorithm recursively on each subtree (e.g. in a left-to-right order).

3. Conquer: First, draw each subtree for separately. Then, place the drawing of to the right of the drawing of . Now, shift to the left until its left contour has a horizontal distance of to the right contour of . Finally, the root is positioned vertically one unit above and horizontally in the center of its children. If only has one subtree, then position the root directly above that subtree.

vertices are computed by accumulating the shifts on the path from each vertex to the root.

Finally, the same graph as in Figure 3-6 is obtained according to the "Reingold & Tilford"

Algorithm:

Figure 3-7: The exemplary tree from Figure 3-5 drawn by the "Reingold&Tilford" Algorithm.

Figure 3-7 shows an aesthetically pleasing drawing of a rooted tree constructed by the "Reingold

& Tilford" Algorithm. Note, that the -coordinates are calculated by the layer assignment as described in the introduction of Section 3.4.

To conclude this section, the properties of a drawing according to the "Reingold & Tilford"

layout are given as follows:

 clearly encodes the depth level

 is planar with strictly downward straight lines.

 contains no crossings since the left-to-right order of the children of each vertex is preserved.

 is compact

 A minimum distance of 1 unit between any two vertices

 covers an area of

 preserves symmetry by producing simply isomorphic structures that have congruent drawings, up to a translation in the drawing.

Im Dokument Diploma Thesis (Seite 45-50)