COMPSCI 120:
Mathematics for Computer Science

Trees

Exercise 6.1. A graph GG on 2n2n vertices is said to have “doubled degrees” if it has exactly 2 vertices with degree kk , for every k{1,2,n}k \in \{1,2,\ldots n\} .

How many trees exist with “doubled degrees?”

Exercise 6.2. You’re a curator of a large modern art museum! Because your museum is particularly “edgy,” the room in which you’re displaying your artwork is a very strange-looking polygon.

You want to install 360360^\circ cameras in the corners of your gallery, in such a way that your cameras see the entire room. What is the smallest number of cameras you need to install?

In general, what’s the largest number of cameras you could need for an art gallery that is a nn -sided polygon?

Trees

In our last section, we saw that the language of graph theory could be used to describe tons of real-life objects: the internet, transportation, social networks, tasks, and many other things! Even your computer (a particularly relevant thing to consider in a computer science class) can be described as a graph:

If you draw this out, you’d get something similar to the drawing below:

This graph represents the file system for your computer, and is extremely useful for organizing files: imagine trying to find a document if literally every file on your computer had to live on your desktop, for instance!

This graph has a particularly useful structure: starting from C:\fbox{C:} , there’s always exactly one way to get to any other file or folder if you don’t allow backtracking. That is: there are no files you can’t get to by starting from your root and working your way down, and also there are no files that you can get to in multiple different ways! This is a very nice property for a file system to have: you want to be able to navigate to every file in some way, and it’s very nice to know that files in different places are different (imagine deleting a file from your desktop and having all copies of it disappear in other places!)

We call graphs with the structural property described above trees. Trees come up all the time in real life:

In short: they’re useful!

To define what a tree is, we first need to introduce a useful concept from graph theory that we didn’t have time to discuss last chapter:

Definition 6.1.Link copied A graph GG has another graph HH as a subgraph if HH is “contained within” GG . In other words, if you can take GG and remove vertices and/or edges from it until you get the graph HH , then HH is a subgraph of GG .

For example, the graph below has C5C_5 as a subgraph, because we can delete the “inside” vertices and edges to have just a C5C_5 left over.

Note that any graph GG is “trivially” a subgraph of itself, as we can just delete “nothing” from GG and have GG left over.

With this stated, we can define a tree as follows:

Definition 6.2.Link copied A tree is a graph TT that is connected and has no cycle graph CnC_n as a subgraph.

For example, the three graphs in the margins are not trees, as each of them has a cycle graph of some length as a subgraph.

However, the three graphs below are all trees:

We call vertices of degree 1 in a tree the leaves of the tree. For example, the leaves of the trees above are colored green.

To get a bit of practice with these ideas, let’s prove a straightforward claim about trees:

Theorem 6.1. If TT is a tree containing at least one edge, then TT has at least two leaves.

Proof. Consider the following process for generating a path in TT :

Algorithm 6.11.

Notice that this process must eventually stop: on a tree with nn vertices, we can only put nn vertices in our path because the “no cycle” property stops us from repeating vertices. When it stops, the endpoints of the path generated are both leaves because this is the only way we stop this process. Therefore, this process eventually finds two leaves in any tree!

\square

As a bit of extra practice, let’s try to use our tree language to sketch a solution to our second exercise:

Answer to Exercise 6.2. It turns out that you can guard any nn -sided polygon (without any holes, and where all sides are straight) with at most n3\left \lfloor \frac{n}{3} \right \rfloor cameras! To do so, use the following process:

Useful Results on Trees

One particularly useful thing about trees is that they can be defined in many different ways! In the section above, we define a tree as a connected graph with no cycles. However, we have two other properties that also characterize when a graph is a tree:

Theorem 6.2.

T is a tree\boxed{\begin{array}{c} \textit{T is a tree} \\ \end{array}} is equivalent tothere is exactly onepath between any twovertices in T\boxed{\begin{array}{c} \textit{there is exactly one} \\ \textit{path between any two} \\ \textit{vertices in T} \end{array}}

Theorem 6.3.

T is a tree\boxed{\begin{array}{c} \textit{T is a tree} \\ \end{array}} is equivalent toT is connectedand has n-1 edges\boxed{\begin{array}{c} \textit{T is connected} \\ \textit{and has n-1 edges} \\ \end{array}}

Recall from Claim that that two statements are equivalent if they hold in precisely the same situations: i.e. whenever one is true, the other is true, and vice-versa.

We prove the first of these theorems here:

Proof of Theorem 6.2. Because we’re proving that these two statements are equivalent, we need to show that if either of them is true, then the other statement follows. That is, if you wanted to show that “attending office hours” and “getting an A+A+ in Compsci 120” were equivalent things, you wouldn’t be satisfied if I said “everyone who got an A+A+ in Compsci 120 attended office hours:” you’d also want to know whether “everyone who attended office hours got an A+A+ “!

As such, this proof needs to go in 2 steps:

We do each of these one-by-one:

\square

This result should help us understand how our “trees don’t have cycle” property connects to the “there’s exactly one path from C:\fbox{C:} to any other file” property that made our filesystem example so useful!

The second of these results requires induction, and so we’ll delay its proof until Section 7.9. It’s quite useful, though, and worth knowing even if we can’t prove it yet! For example, it lets us solve one of our exercises:

Answer to Exercise 6.1. Let TT be a tree on 2n2n vertices with the “doubled degrees” property. Notice that if a graph TT on 2n2n vertices has the “doubled degree” property, then the sum of the degrees in TT is 1+1++n+n=2(1+2++n)=2n2+n2=n2+n1+1+\ldots + n + n = 2(1+2+\ldots + n) = 2\dfrac{n^2+n}{2} = n^2+n .

As well, the degree-sum formula from our graph theory chapter tells us that the sum of the degrees in TT is twice the number of edges. Therefore, we have that n2+n=2En^2+n = 2E .

Finally, because TT is a tree, we know that it has one less edge than it has vertices (i.e. it has 2n12n-1 edges, because GG is on 2n2n vertices.)

Combining this all together tells us that n2+n=2(2n1)=4n2n^2+n = 2(2n-1) = 4n-2 ; i.e. n23n+2=0n^2-3n + 2 = 0 ; i.e. (n1)(n2)=0(n-1)(n-2) = 0 , i.e. n=1n=1 or n=2n=2 . In other words, if TT is a tree with the doubled degrees property, then TT is either a tree on 22 or 44 vertices.

For n=1n=1 , the “doubled degree” property would tell us that TT should be a two-vertex graph with two vertices of degree 1. There is exactly one tree of this form, namely  

For n=2n=2 , the “doubled degree” property would tell us that TT should be a four-vertex graph with two vertices of degree 1 and two of degree 2. There is also exactly one tree of this form, namely  

Rooted Trees

In many of the examples we looked at earlier (file systems, genealogy) it is natural to think of one vertex in our tree as the start, or root of our tree, and of all of our other vertices as “descending” from that root vertex. We formalize this idea with the concept of a rooted tree, defined here:

Definition 6.3.Link copied We say that a rooted tree is any tree in which we designate one vertex rr to be the “root” of the tree.

Given a rooted tree, we can draw it as follows:

Algorithm 6.12.

Keep doing this until you run out of vertices to draw!

Example 6.1. Three trees are given below.

We draw each of them with vertex AA chosen as the root:

We do this again,but now with vertex BB chosen as the root:

The following terms are useful when discussing rooted trees:

Definition 6.4.Link copied We say that the children of a vertex vv are all of the neighbors of vv at the level directly below vv , and the parent of vv is the neighbor of vv at the level directly above vv . The height of a rooted tree is the largest level index created when drawing the graph as above.

A particularly useful tree in computer science is a binary tree:

Definition 6.5.Link copied A binary tree is a rooted tree in which every vertex has either no children, one child, or 22 children. A binary tree is called full if every vertex only ever has no children or two children.

You can generalize this to mm -ary trees for any mm , by changing the restriction here to ask that every vertex has at most mm children.

Example 6.2. Three binary trees are drawn below.

To finish out our chapter and practice working with this concept, let’s study a quick result about binary trees:

Exercise 6.3. Suppose that TT is a full binary (i.e. 2-ary) tree with 100 leaves. How many vertices does TT have in total?

Answer to Exercise 6.3. There are three kinds of vertices in TT :

Suppose that there are pp parent vertices in our graph; then the sum of degrees in this graph is 2+3p+1002 + 3p + 100 . As shown in class, the sum of degrees in any graph is twice the number of edges. Because this is a tree on 1+p+1001 + p + 100 vertices (100 leaves, one root, and pp parents), this is therefore equal to 2p+2002p + 200 , as any tree has one less edge than it has vertices.

So: 2+3p+100=2p+2002 + 3p + 100 = 2p + 200 implies that p=98p=98 , and therefore that we have 199\fbox{199} vertices in total!

Practice Problems