## Lecture 31: TreesIn this lecture, we will study trees: Basic Definition Properties of Trees with some proofs. Pruefer Sequences: Representing and Counting Labelled Trees using Clayley's Theorem. If time permits, counting rooted/unlabelled trees using Catalan Numbers.
## TreesYou have most probably studied rooted trees and binary search trees as a data-structure for organizing lists of numbers and efficiently performing insertions and deletions. However a tree is a useful structure in many other parts of CS: Game trees in AI (how can we make computers play games like chess, go,…). Decision trees in ML. Spanning trees in network routing. Parse trees for compilers. The list goes on and on
## Tree (definition)Let us start with the simplest kind of trees: unrooted and undirected trees. Definition
A is fully connected (the entire graph is a maximally connected component), is acyclic (there are no cycles in ).
A rooted tree is a fully connected, acyclic graph with a special
node that is called the root of the tree. You may have
studied rooted trees in your data structures class. With a root, it is
possible to define a ## ExamplesHere are some examples of unrooted trees: ## Non-ExamplesThe following graph is not a tree. It has a cycle: Here is another example that has more than one maximal connected components and is not a tree: ## Properties of Trees## Leaves of a TreeA Leaves are . Leaves are . Leaves are .
Proof
If the tree just has one node, then it is trivially a leaf. If the tree has two nodes connected by an edge, both nodes are leaves. Let us focus on trees with three or more nodes. Let us assume that a tree that does not have a leaf exists. Therefore for every node in this tree . We are going to show that has a cycle, yielding a contradiction with the assumption that it is a tree. Start from any node in the tree and do a walk as follows: Take any edge out for to reach . For any node , take an edge other than the that we took to enter . Since , such a vertex is always available.
Note that the walk above can be continued for arbitrarily many steps. However, since number of vertices is finite, the walk repeats a vertex. Let for some , . We can now conclude the existence of a cycle with in it. Therefore is not a tree, yielding a contradiction. Thus, we have concluded that every tree has a leaf. ## Number of Edges
Proof
Proof is by weak induction on the number of nodes .
Take some tree with nodes. It must have a leaf . Removing the leaf gives us a tree with nodes that must have edges in it. The leaf itself was connected to the rest of the tree by one edge. Therefore has edges. ## Number of Leaves
We will prove this in class by the following argument. We already know that any tree has at least one leaf. Proof
Let us assume that there is a tree with nodes and exactly one leaf . Therefore all nodes other than have degree . Sum of degrees of all nodes . However, we know that sum of degrees of all nodes = 2 * number of edges. number of edges . However a tree has precisely edges. This leads to a contradiction.
## Paths in Trees
We will argue that having two distinct paths will necessarily imply that the tree has a cycle which will lead to a contradiction. ## Counting Labeled Trees and Pruefer SequencesWe will now present a very elegant method of representing labeled trees and count them using Pruefer sequences. ## Labelled vs. Unlabelled TreesWe distinguish between instances of trees where the names of the vertices matter and instances where all that matters are the connection between vertices. The former class of trees are called labelled trees and the latter are called unlabelled trees. Take the following three trees: They have the same number of nodes and are ‘‘isomorphic+ to each other. The tree on the left is obtained by substituting with everywhere. Similarly the tree on the right is obtained by substituting with for the tree in the middle.
We can view a tree in two ways: Labelled Tree: The labels on the nodes actually matter and two isomorphic trees with different labels are actually different trees. In particular, the above example represents different labelled trees. Unlabelled Trees: The labels do not matter. Isomorphic trees count as the same. In particular, all trees in the above example are the same.
All the examples above represent the following unlabelled tree: ## Pruffer SequencesGiven a labelled tree , we can represent it by a sequence of numbers called its Basic idea behind a Pruefer Sequence is to keep removing the leaves one at a time and write down a number corresponding to each leaf that we remove: Take the lowest numbered leaf in the tree and remove it. Add the number of the node the removed leaf is connected to the Pruefer sequence. Repeat until just two vertices remain.
## ExampleLet us write down the Pruffer sequence for this tree: To start with, the smallest numbered leaf is . We remove from the tree and add to the Pruefer sequence. We get the tree: The smallest number leaf is now , which is connected to . We add to the Pruefer sequence, to get the tree: The smallest numbered leaf is a , which is connected to . Therefore, we add to the Pruefer sequence. To complete out the process, we get the Pruefer sequence: 2,1,2 ## Example-2Let us try other trees and write down their Pruefer sequences. The Pruefer sequence is . ## Example-3The Pruefer sequence is . ## Reconstructing Trees From Pruefer CodeLet us now figure out what a valid Pruefer Sequence is and how to reconstruct a tree uniquely from its Pruefer Sequence. ## Pruefer SequencesFor a tree with nodes assumed to be labelled , its Pruefer sequence has numbers in the sequence. Each number can be from . In fact, any sequence of numbers from is possible.
## Reconstruction Algorithm: ExampleLet us try and reconstruct a tree from its given Pruefer sequence:
The algorithm looks at the smallest number that we have not yet seen so far in the Pruefer sequence. The smallest number that does not appear so far in the sequence is a . Therefore we conclude that must be the first leaf removed and that it is connected to a .
The remaining sequence is . We remember that has been resolved: . The smallest number that does not appear is . Therefore, we conclude that must have been a leaf at this stage and is connected to .
The remaining sequence is . We remember that have been resolved: . Smallest number is a and it must be connected to a .
The remaining sequence is . We remember that have been resolved . Smallest number is a and it must be connected to a .
The remaining sequence is . resolved and Here is the complete run of the algorithm
The last step is special since we have two nodes unseen, the Pruefer sequence is exhausted and we conclude that they must form an edge. ## Clayley's FormulaThere are exactly distinct labelled trees on nodes. |