4 Trees and Forests Trees, actually acyclic connected simple graphs, belong among the simplest graph types. Despite their simplicity, they still have rich structure and many practical application, such as in data structures. Brief outline of this lecture • Definition and basic properties of trees (acyclic connected graphs). • Rooted trees, ordered trees, and isomorphism of trees. • Spanning trees in a graph, enumeration of spanning trees. _________:r Hliněný, Fl MU Brn : MAOIO: Trees and Forests ^ 4.1 Basic properties of trees Definition 4.1. A tree is a connected graph T without cycles. A graph whose connected components are trees is called a forest. Notice that a tree must be simple since a loop is a cycle of length one and parallel edges form a cycle of length two. More basic properties of trees follow.. . Lemma 4.2. A tree on more than one vertex contains a vertex of degree 1 (a "leaf").c Proof: We consider a longest possible path in a tree T. Since there are no cycles in T, the endvertices of this path must have no other edges incident with them. (Hence the degree one.) □ D Theorem 4.3. An n-vertex tree has exactly n — 1 edges for n> 1. Proof: By induction on n.. . c A one-vertex tree has n — 1 = 0 edges, OK. Let a tree T have n > 1 vertices. By Lemma 4.2, T has a vertex v of degree 1. Denote by T' = T — v the subgraph obtained from T by deleting v. Then T' is also connected and acyclic, and hence T' is a tree on n — 1 vertices. By induction assumption, T' has n — 1 — 1 edges, and so T has n — 1 — 1 + 1 = n — 1 edges. D • Hlinený. Fl Ml MAOIO: Trees and Forests r Theorem 4.4. Any two vertices of a tree are connected via exactly one path, c Proof: Since a tree T is connected by the definition, any two vertices u,v are connected via a path. If there were two distinct paths P\,P2 between u,v, then their symmetric difference (w.r.t. edge sets) — a subgraph H = P1AP2 of nonempty edge set — has all degrees even and not all zero. On the other hand, if as a subgraph of a tree is acyclic, and hence its components are trees (not all single-vertex), and by Lemma 4.2 there has to be a vertex of degree 1 in if, a contradiction. D Corollary 4.5. If a single new edge is added to a tree, this results in a creation of exactly one cycle. Proof: If u,v are not neighbours in a tree T, then the new edge uv forms one cycle with the unique u — v path from Theorem 4.4. c D Theorem 4.6. A tree is a minimal connected graph (on a given vertex set). -Proof: We prove, from the previous properties, the two directions of this claim: • If T is a tree, then removal of any edge from T disconnects it. • Conversely, if a connected graph had a cycle, then it would not be minimal among its connected subgraphs. □ s_ Petr Hliněný, Fl MU Brno Fl: MAOIO: Trees and Forests 4.2 Rooted trees Introduction of rooted trees is motivated primarily by practical applications in which a "tree data structure" has to be grasped at some vertex (the root), and also by historical use of family-trees. Actually, the traditional family-trees motivated much terminology of graph trees, c Definition 4.7. A rooted tree is a tree T together with a highlighted root r G V (T), i.e. shortly a pair T,r. Interestingly, trees in computer science grow top-to-bottom. etr Hliněný, Fl MU Br -I: MAOIO: Trees and Forests Definition: Consider a rooted tree T,r and its vertex v. Let u denote the neighbour of v on the unique path towards the root r. Then u is the parent of v, and v is a child (descendant) of u. c The root has no "ancestors" children root Definition: A vertex of degree lina tree is called a leaf. Strictly saying, a root of degree 1 is also a leaf, but we shall use this term carefully for roots to avoid confusion. Notice that a leaf (which is not a root) has no descendants. Petr Hliněný, Fl MU Brm MAOIO: Trees and Forests Definition: The center of a (nonempty) tree T is the vertex or an edge found by the following recursive procedure: • If T is a single vertex or a single edge, then this (T) is the center. • Otherwise, we create a smaller tree T' C T by removing all leaves of T at the same time. This T' is nonempty, and we determine the center of T' recursively. The center of T is identical to this center of T', c Example 4.8. The definition of a tree center is illustrated by the two examples: S ^S «^ ® Fact. If one needs to determine a unique root of a tree, the center is a good choice. (If the center is an edge, then the root may be a new vertex subdividing this edge.) etr Hlinený, hl MU Brr -I: MA010: Trees and Forests Definition: A rooted tree T, r is ordered if the orderings of the children of each its vertex are prescribed. A rooted ordered tree is also called a planted tree, c A rooted ordered tree can be visualized as a tree drawn in the plane without "crossings" and with a marked root. The cyclic ordering of the child edges (against the parent edge / the root mark) then gives the tree ordering of the children. root children: 1 parent Detr Hliněný, Fl MU Br -I: MAOIO: Trees and Forests 4.3 Isomorfismus stromů The isomorphism of trees, as a special instance of graphs, has already been defined and studied. Tree isomorphism problem, however, has one additional interesting aspect — that there is an efficient and straightforward solution to it. Definition: Two rooted trees T, r and T', r' are isomorphic if there is an isomorphism between T and T' such that r is mapped onto r'. Ž Definition: Two rooted ordered trees are isomorphic if there exists a rooted isomorphism between them which preserves the orderings of children at each vertex. 5* etr Hlinený, Fl MU Brr -I: MA010: Trees and Forests Coding of planted trees Definition: ( ((()()())()) (()(())) ) ((000) 0 ) ^ V ( 0 (0 (000) The code of a rooted ordered tree is determined recursively from the codes of the subtrees of its root, concatenated in a prescribed order and enclosed in a pair of parentheses, c Remark: Instead of '(' and ')', one may use other pair of symbols such as '0' and 'ľ. Lemma 4.9. Two rooted ordered trees are isomorphic if and only if their codes are identical as strings. Proof: Easily by induction on the depth.. ________itr Hliněný, Fl MU Br__________________________ D -I: MA010: Trees and Forests Fact. If s is the code of a rooted ordered tree T, then T can be drawn using the following procedure: - Reading the first char '(', we put the pen on the paper (marking the root). - Reading every next '(', we draw the edge to the next child of the current vertex.d - Reading each ')', we return the pen to the parent. In the root, we lift the pen and are finished, c Example 4.10. We draw the planted tree with the code ((()(()()()()))(()()))• D etr Hlinený, Fl MU Bri -hMAOlO: Trees and Forests The core idea behind the tree isomorphism testing algorithm is to compare their codes which are rooted in the centers and ordered lexicographically among the children. Algorithm 4.11. Testing isomorphism of two trees. input < trees T and U; if (\V(T)\\=\V(U)\) return 'Not isomorphic.'; (T,r) = rooted_at_center(T) ; (U,s) = rooted_at_center(U) ; k = minimal_code(T,r); m = minimal_code(U,s); if (k==m as strings) return 'Isomorphic.'; else return 'Not isomorphic.'; c Function minimal_code(tree X, rootr) { if (|V(X)|==1) return "()"; d = number of components of X-r, i.e. subtrees of r; for (i = l,...,d) { Y[i] = i-th connected component of X-r; s [i] = i-th neighbour of r, i.e. the root of the subtree Y [i]; k[i] = minimal_code(Y[i],s[i]); } sort as k[l]<=k[2]<=. . .<=k[d] lexicographically; return "("+k[l]+...+k[d]+")"; etr Hliněný, Fl MU Brno 11 Fl: MA010: Trees and Forests A mathematical proof of Algorithm 4.11 is given as follows. Theorem 4.12. Let T, U be two trees on the same number of vertices, and let (T1, r) and (U1, s) be their rooted trees obtained in the first step of Algorithm 4.11 (r, s are the centers ofT, U). Then: a) T and U are isomorphic if, and only if, (T1, r) is isomorphic to (U1, s). b) (T1, r) is isomorphic to (U1, s) if, and only if, minimal_code(T', r) = minimal_code(U', s). Proof (sketch): Claim (a) immediately follows from the unique choice of a center. Claim (b) is proved by induction on the depth of our rooted trees T',r and U',s. (If these depths differ, then they are clearly not isomorphic.) Two rooted trees of depth 0 are always isomorphic with the code '()'. So let T', r and U', s be of depth £ > 0. If their codes in the algorithm are identical, then T',r and U',s are clearly isomorphic. On the other hand, \fT',r and U',s are isomorphic, then there exists a "matching" bijection between the subtrees of their roots, and hence the codes of matching subtrees are identical by the inductive assumption. Since the codes of the subtrees are sorted in the same way, we get minimal_code(T', r) = minimal_code(U', s). Ü etr Hliněný, Fl MU Brno 12 Fl: MAOIO: Trees and Forests 4.4 Spanning trees of graphs Definition 4.13. A spanning tree of a connected graph G is a subgraph T of G such that T is a tree and V(T) = V(G). Example 4.14. How many distinct spanning trees are contained in this graph! We argue as follows; which edges have to be removed from our graph to obtain a tree? Easily, one from each of the two cycles. Hence, by the principle of independent choices, we have got 5 • 6 = 30 spanning trees. Ü etr Hliněný, Fl MU Brno 13 Fl: MAOIO: Trees and Forests r The following claim is a real "beautiful gem" of graph theory. Theorem 4.15. (Cayley) The complete graph Kn for n > 0 contains exactly nn~2 distinct spanning trees. Even more generally, we have: Definition. The Laplace matrix Q = (