Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 21 7.3 How to count trees A: Counting edges How many edges does a tree have, if it has n vertices? Answer: n − 1. Can you prove it? Proof: by induction on n. The induction starts at n = 1, with the empty graph on one vertex. If n > 1, then there is at least one edge. Removing one edge disconnects the tree into two smaller trees. If these have i and n − i vertices, then by induction they have i − 1 and n − i − 1 edges, making a total of n − 2. Adding in the edge we removed, we get a total of n − 1 edges in the original tree. B: Examples Let tn be the number of trees on n vertices, 1, 2, 3 . . . , n. It is easy to see that t1 = t2 = 1. If n = 3, then the tree is a path, and there are three choices for the label of the middle vertex, so t3 = 3. (Alternatively, say there are 3! labellings, but there is no distinction between the two ends of the path, so we have to divide by 2.) Example 1 (n = 4) There are two possible shapes for a tree on 4 vertices. One is a path, which has 4!/2 = 12 distinct labellings. The other is a ‘star’ (one vertex joined to all the others), which has just four labellings, as only the labelling of the centre vertex is significant. Hence t4 = 12 + 4 = 16. At this point you might start to see the pattern, but one more example will make it more obvious: Example 2 (n = 5) Again we have a path, with 5!/2 = 60 labellings, and a star, with 5 labellings. There is one more shape of tree on 5 vertices: u u u u @ @@u In this case the number is again 5!/2, because the two right-hand leaves (a leaf is a vertex of degree 1) are indistinguishable, but apart from this ambiguity all labellings are distinct. Hence t5 = 60 + 5 + 60 = 125. 1 Now we see the pattern clearly: Conjecture 1 The number of labelled trees on n vertices is tn = nn−2 . C: A bijection To prove this conjecture, we need to construct a bijection f between the set T of trees on the vertex set V = {1, 2, . . . , n}, and the set S of sequences a1 , a2 , . . . , an−2 over {1, 2, . . . , n}. Essentially, the sequence tells us where to join the branches onto the tree. We assume that n ≥ 3, so that T has at least two edges. Algorithm 1 Remove the leaf with the smallest label, and write down the label of the vertex it is joined to. Repeat until there are exactly two vertices left. Example 3 1u @ @ @ 2u @ @u4 3u @ @ @ 6u 5u 7u Step 1: Remove 4, write down 1 Step 2: Remove 6, write down 2 Step 3: Remove 2, write down 1 Step 4: Remove 1, write down 2 Step 5: Remove 7, write down 5 Step 6: Remove 5, write down 3 The output sequence is 1, 2, 1, 3, 5, 3. 2 @ @u8 To complete the proof, you need to convince yourself that, given the sequence, you can reconstruct the labelled tree. Then you will have proved that the function f is a bijection, and hence that the number of labelled trees on n vertices is nn−2 . The following algorithm defines the inverse function to f , hence showing it is a bijection. All that is missing now is the proof that this really is the inverse function! Algorithm 2 Given a sequence of n−2 numbers chosen from {1, 2, 3, . . . , n}, the numbers not chosen are the labels on the leaves of the tree. More generally, the degree of each vertex is 1 more than the number of times it appears in the sequence. Keep track of the degrees of the vertices as we go along. Then for each number a in the sequence, in turn, join it to the smallest numbered vertex that needs only one more edge incident to it. This creates n − 2 edges altogether, and the last edge joins the only two vertices whose degrees are not yet what they should be. 3