Sunteți pe pagina 1din 6

Chapter 2: Mathematical Preliminaries

49

And v3e4v4e5v~ is a path from 1'3 to v~. We call 1'3e41'4e5v~ a directed path since
the edges e4 and es have the forward direction. (But 1'je~V3e3v~ is not a directed
path as e~ is in the forward direction and e3 is in the backward direction.)

Defmition 2.15 A graph (directed or undirected) is connected if there is a


path bet\veen every pair of vertices.
The graphs given by Figs. 2.3 and 2.4. for example, are connected.
Definition 2;16 A circuit in a graph is an alternating sequence 1'lelv2e~ ...
en-lVI of vertices and edges starting and ending in the same vertex such that
ei has Vi and Vi+l as the end vertices and no edge or vertex other than VI is
repeated.
In Fig. 2.3. for example. V3e3 v~e5v4e4v3' ~'1 e~1'3e4V4e5v~el VI are circuits. In
Fig. 2.4. l'je2v3e31'2ejvl and v2e3v3e4V4eSl'~ are circuits.

2.2.2

TREES

Definition 2.17 A graph (directed or undirected) is called a tree if it is


connected and has no circuits.
The graphs given in Figs. 2.6 and 2.7, for example, are trees. The graphs
given in Figs. 2.3 and 2.4 are not trees,
Note: A directed graph G is a tree iff the corresponding undirected graph
is a tree.

Fig. 2.6

Fig. 2.7

A tree with four vertices.

A tree with seven vertices.

We no",,- discuss some properties of trees (both directed and undirected)


used in developing transition systems and studying grammar rules.

50

;!

Theory of Computer Science

Property 1

A tree is a connected graph with no circuits or loops.

Property 2
vertices.

In a tree there is one and only one path between every pair of

Property 3 If in a graph there is a unique (i.e. one and only one) path
between every pair of vertices, then the graph is a tree.
Property 4

A tree with n vertices has

Property 5
a tree.
Property 6
it is a tree.

If a connected graph with n vertices has

11 -

1 edges.
11 -

If a graph with no circuits has n vertices and

1 edges, then it is
11 -

1 edges,tben

A leaf in a tree can be defined as a vertex of degree one. The vertices


other than leaves are called internal ve11ices.
In Fig. 2.6. for example, 1'1, "'3, "'4 are leaves and "'2 is an internal vertex.
In Fig. 2.7. "'2, 1'5. V6' Vi are leaves and 1'1, 1'3' 1'4 are internal vertices.
The following definition of ordered trees will be used for representing
derivations in context-free grammars.
Defmition 2.18
conditions:

An ordered directed tree is a digraph satisfying the following

T 1: There is one vertex called the root of the tree which is distinguished
from all the other vertices and the root has no predecessors.
T:: There is a directed path from the root to every other vertex.
T3 : Every ve11ex except the root has exactly one predecessor.
T4 : The successors of each vertex are ordered 'from the left'.

The condition T 4 of the definition becomes evident once we have the


diagram of the graph.

Note:

Figure 2.7 is an ordered tree with VI as the root. Figure 2.8 also gives an
ordered directed tree with V1 as the root. In this figure the successors of 1'1 are
ordered as 1':1'3' The successors of 1'3 are ordered as 1'51'6'

!\!\

v,
v4

Fig. 2.8

An ordered directed tree.

Chapter 2: Mathematical Preliminaries

J;1

51

By adopting the following convention, we can simplify Fig. 2.8. The root
is at the top. The directed edges are represented by arrows pointing downwards.
As all the arrows point downwards, the directed edges can be simply
represented by lines sloping downwards, as illustrated in Fig. 2.9.

Fig. 2.9

Representation of an ordered directed tree.

Note: An ordered directed tree is connected (which follows from T2). It has
no circuits (because of T3 ). Hence an ordered directed tree is a tree (see
Definition 2.17).
As we use only the ordered directed trees in applications to grammars, we
refer to ordered directed trees as simply trees.
Defmition 2.19 A binary tree is a tree in which the degree of the root is 2 and
the remaining vertices are of degree 1 or 3-

Note: In a binary tree any vertex has at most two successors. For example, the
trees given by Figs. 2.11 and 2.12 are binary trees. The tree given by Fig. 2.9
is not a binary tree.
Theorem 2.5

The number of vertices in a binary tree is odd.

Proof Let n be the number of vertices. The root is of degree 2 and the
remaining n - 1 vertices are of odd degree (by Definition 2.19). By
Theorem 2.4, n - 1 is even and hence 11 is odd. I

We now introduce some more terminology regarding trees:


(i) A son of a vertex v is a successor of 1'.
(ii) The father of v is the predecessor of 1'.
(iii) If there is a directed path from v] to 1'2> VI is called an ancestor of V.:,
and V2 is called a descendant of V1' (Convention: v] is an ancestor of
itself and also a descendant of itself.)
(iv) The number of edges in a path is called the length of the path.
(v) The height of a tree is the length of a longest path from the root. For
example, for the tree given by Fig. 2.9, the height is 2. (Actually there
are three longest paths, 1'1 -+ 1'2 -+ V.., 1'1 -+ 1'3 -+ VS, VI -+ V2 -+ V6'
Each is of length 2.)
(vi) A vertex V in a tree is at level k if there is a path of length k from the
root to the vertex V (the maximum possible level in a tree is the height
of the tree).

52

Theory of Computer Science

Figure 2.10. for example. gives a tree where the levels of vertices are
indicated.
Root

r--,

Level 0

\L""'~
Level :2

-0

cf-

Level 2 . : 0

Level 3

Fig. 2.10

illustration of levels of vertices.

EXAMPLE 2.15
For a binary tree T with n vertices. shO\v that the minimum possible height
is rlog=(n + 1) - n where r k 1 is the smallest integer 2 k. and the maximum
possible height is (n - 1)12.

Solution
In a binacy tree the root is at level O. As every vertex can have at most t\vo
successors. vve have at most two vertices at level 1. at most 4 vertices at level
2. etc. So the maximum number of vertices in a binary tree of height k is
1 + 2 + 2= + ... + i'. As T has n vertices. 1 + 2 + 2= + ... + 2k 2 11, i.e.
(2 k+1 - 1)/(2 -1) 2': 11. so k 2': log=(n + 1) - 1. As k is an integer, the smallest
possible value for k is log=(n + 1) - n Thus the minimum possible height
is r log=(n + 1) To get the maximum possible height. we proceed in a similar way. In
a binary tree we have the root at zero level and at least two vertices at level
1. 2, .... When T is of height k. we have at least 1 + 2 + ... + 2 (2 repeated
k times) vertices. So. 1 + 2k ~ n, i.e. k ~ (n - 1)/2. But, n is odd by
Theorem 2.4. So (n - 1)/2 is an integer. Hence the maximum possible value
for k is (11 - 1)/2.

EXAMPLE 2.16
When 11 = 9. the trees \vith minimum and maximum height are shown
in Figs. 2.11 and 2.12 respectively. The height of the tree in Fig. 2.11 is
!log.:'(9 + 1) - 11 = 3. For the tree in Fig. 2.12. the height = (9 - 1)/2 = 4.

Chapter 2: Mathematical Preliminaries

);l,

53

+ 1)/2, where

/1 IS

Fig. 2.11

Binary tree of minimum height with 9 vertices.

Fig. 2.12

Binary tree of maximum height with 9 vertices.

EXAMPLE 2.1 7
Prove that the number of leaves in a binary tree Tis
the number of vertices.

(/1

Solution
Let in be the number of leaves in a tree with /1 vertices. The root is of degree
2 and the remaining /1 - in - 1 vertices are of degree 3. As T has /1 vertices,
it has /1 - 1 edges (by Property 4). As each edge is counted twice while
calculating the degrees of its end vertices. 2(/1 - 1) = the sum of degrees of all
vertices = 2 + m + 3(11 - In - 1). Solving for in. we get in = (/1 + 1)12.

EXAMPLE 2.18
For the tree shown in Fig. 2.13, answer the following questions:
(a) Which vertices are leaves and \vhich internal vertices?

S4

g,

Theory ofComputer Science

(b)
(c)
(d)
(e)

Which vertices are the sons of 57


Which vertex is the father of 57
\Xlhat is the length of the path from 1 to 97
What is the left-right order of leaves?
(f) What is the height of the tree?

10

Fig. 2.13

The directed tree for Example 2.18.

Solutions
(a) 10, 4, 9, 8, 6 are leaves. 1, 2. 3, 5, 7 are internal vertices.
(bi 7 and 8 are the sons of 5.

(c) 3 is the father of 5.


(d) Four (the path is 1 ~ 3 ~ 5 ~ 7 ~ 9).
(e) 10 - 4 - 9 - 8 - 6.
(f) Four (1 ~ 3 ~ 5 ~ 7 ~ 9 is the longest path).

2.3

STRINGS AND THEIR PROPERTIES

A string over an alphabet set 2: is a finite sequence of symbols from 2:.

2:* denotes the set of all strings (including A, the empty string)
over the alphabet set 2:. That is, 2:+ = 2:* - {A}.

NOTATION:

2.3.1

OPERATIONS ON STRINGS

The basic operation for strings is the binary concatenation operation. We


define this operation as follows: Let x and y be two strings in 2:*. Let us form
a new string :: by placing y after x, i.e. z = xy. The string z is said to be
obtained by concatenation of x and y.

S-ar putea să vă placă și