Discrete MathematicsTrees1 of 5
Trees · 1

Trees: Connected Graphs with No Cycles

We want to connect a network of dots into a single piece using the fewest lines possible, with no redundant loops.

1

What Is a Tree?

A tree is a connected graph that contains no closed loops, which we call cycles.

cycle\htmlClass{lc-orange}{\text{cycle}}
What Is a Tree?

Because a tree is connected without a single spare line in a loop, every tree has three properties that all come from the same fact:

tree⏟connected, no cycles  ⟹  {1 unique path between any two dotserasing any line splits it into 2 piecesadding any line closes 1 loop\displaystyle \underbrace{\text{tree}}_{\text{connected, no cycles}} \implies \begin{cases} \route{\text{1 unique path}} \text{ between any two dots} \\ \text{erasing any line splits it into } \counted{2} \text{ pieces} \\ \text{adding any line closes } \spare{1 \text{ loop}} \end{cases}

Because only one path connects any two dots in a tree, every line along that path is the only bridge holding the two sides together, and any new line between those dots creates a second route that closes one loop.

Every pair of dots in a tree has just one path between them, so adding a line between them closes one loop and cutting any line on that path splits the tree in two.
2

Leaves and Why a Tree Has n−1n - 1 Lines

To count how many lines a tree has without drawing it, we look at its outer tips. A leaf is a dot of degree 11 (a dot with only one line attached), and every tree with at least 22 dots has at least two leaves, which forces every tree on nn dots to have n−1\counted{n - 1} lines.

Leaves and Why a Tree Has n − 1 Lines

When you hang a tree from any top dot, every other dot hangs from the single line right above it, pairing every line with the dot at its bottom end:

lines in a tree⏟n−1\displaystyle \underbrace{\counted{\text{lines in a tree}}}_{\counted{n - 1}} =total dots⏟n−1⏟top dot with no line above it\displaystyle = \underbrace{\text{total dots}}_{n} - \underbrace{\topdot{1}}_{\topdot{\text{top dot with no line above it}}}

No matter which dot sits at the top of the tree, every other dot hangs from the single line right above it, leaving only the top dot without a line.

Interactive c2
3

Forests and the Two-out-of-Three Rule

A forest is a graph with no cycles, made up of one or more separate trees sitting side by side. We can use the hanging-tree picture to count how many separate pieces a forest has from its dots and lines alone, and then see how many properties we need to check to be sure a graph is a tree.

Forests and the Two-out-of-Three Rule
Text solution
Pair each line with the dot hanging below itHanging each separate tree in a cycle-free graph from its own top dot pairs every line with one hanging dot, leaving one unattached top dot for each separate tree:
pieces in a forest\displaystyle \counted{\text{pieces in a forest}} =dots−lines\displaystyle = \text{dots} - \text{lines}
Subtract the lines from the dotsWith 2020 dots and 1717 lines, 1717 dots hang from lines and the remaining dots sit at the top of their own separate trees:
pieces\displaystyle \counted{\text{pieces}} =20−17\displaystyle = 20 - 17 =3 trees\displaystyle = \counted{3} \text{ trees}

For any graph on nn dots, a tree has three defining features:

1. Connected2. No cycles3. n−1 lines\displaystyle \begin{aligned} &\text{1. Connected} \\ &\text{2. No cycles} \\ &\text{3. } \counted{n - 1} \text{ lines} \end{aligned}

Checking only that a graph has n−1\counted{n - 1} lines isn't enough by itself, because a line used in a loop means one fewer line is left to connect a separate piece.

3 lines\htmlClass{lc-green}{3} \text{ lines}
3 lines\htmlClass{lc-green}{3} \text{ lines}

With n−1\counted{n - 1} lines on nn dots, every line used to close a loop leaves another piece of the graph disconnected, so the graph connects into one piece if and only if those n−1\counted{n - 1} lines form no loops.

With one fewer line than dots, using a line to close a loop always leaves another piece cut off, so the whole graph connects into one piece only when it has no loops.
4

Counting Leaves from Degrees

Often we are given the degrees of the branching dots inside a tree and want to find how many leaves the tree must have. We can count the leaves either by watching how each branching dot splits paths as we build the tree, or by using the Handshake Lemma, which says that because every line has 22 ends, adding up the degrees of all dots counts every line twice:

sum of degrees\displaystyle \text{sum of degrees} =2×lines\displaystyle = 2 \times \text{lines}
Counting Leaves from Degrees
4 dots: degree 32 dots: degree 4rest: leaves, degree 1\displaystyle \begin{aligned} 4\text{ dots} &\text{: degree } 3 \\ 2\text{ dots} &\text{: degree } 4 \\ \text{rest} &\text{: } \counted{\text{leaves}}\text{, degree } 1 \end{aligned}
leaves\displaystyle \counted{\text{leaves}} =  ?\displaystyle = \;?
Text solution
Count the new leaves added by each branching dotStarting from a single line with 2\counted{2} leaves, each dot of degree dd uses 1\incoming{1} line to attach to the tree and splits that path into d−1\added{d - 1} outgoing branches, adding d−2\added{d - 2} leaves to the total:
leaves=2⏟start+4(3−2)⏟four degree-3 dots+2(4−2)⏟two degree-4 dots=2+4+4=10 leaves\displaystyle \begin{aligned} \text{leaves} &= \underbrace{\counted{2}}_{\text{start}} + \underbrace{\added{4(3 - 2)}}_{\text{four degree-}3 \text{ dots}} \\ &\quad + \underbrace{\added{2(4 - 2)}}_{\text{two degree-}4 \text{ dots}} \\ &= \counted{2} + \added{4} + \added{4} \\ &= \counted{10} \text{ leaves} \end{aligned}
Or solve with the Handshake Lemma using L\counted{L} leavesA tree with 44 dots of degree 33, 22 dots of degree 44, and L\counted{L} leaves of degree 11 has 6+L6 + \counted{L} dots and (6+L)−1=5+L(6 + \counted{L}) - 1 = 5 + \counted{L} lines, so setting the sum of degrees equal to twice the lines gives L\counted{L}:
4(3)+2(4)+L(1)=2(5+L)20+L=10+2LL=10 leaves\displaystyle \begin{aligned} 4(3) + 2(4) + \counted{L}(1) &= 2(5 + \counted{L}) \\ 20 + \counted{L} &= 10 + 2\counted{L} \\ \counted{L} &= \counted{10} \text{ leaves} \end{aligned}

Notice that a dot of degree 22 adds 2−2=0\added{2 - 2} = 0 leaves because a path simply goes in one side and continues straight out the other without splitting, so degree-22 dots never change the leaf count.

5

Counting Different Trees on nn Dots

Now suppose we are given nn dots and want to find how many genuinely different tree structures we can draw. Remember from graph isomorphism that two drawings count as the same graph if they are connected the exact same way, so one can be slid around on the board to match the other without cutting any lines.

Counting Different Trees on n Dots
Text solution
Longest path of 6\route{6} dotsLaying all 66 dots in a single straight chain uses every dot, giving 1\counted{1} tree with no leftover dots to attach:
longest path 6  ⟹  1 tree\displaystyle \route{\text{longest path } 6} \implies \counted{1} \text{ tree}
Longest path of 5\route{5} dots (11 leftover dot)The 11 leftover dot cannot attach to either end of the 55-dot chain without extending the longest path to 6\toolong{6}, so it must attach either to the 2nd2\text{nd} dot (same as the 4th4\text{th} by flipping the chain) or to the middle 3rd3\text{rd} dot:
longest path 5  ⟹  2 trees\displaystyle \route{\text{longest path } 5} \implies \counted{2} \text{ trees}
Longest path of 4\route{4} dots (22 leftover dots)Neither leftover dot can attach to the ends or attach to the other leftover dot without creating a path of 5\toolong{5}, so both must attach directly to the two middle dots: either both on one middle dot, or one on each middle dot:
longest path 4  ⟹  2 trees\displaystyle \route{\text{longest path } 4} \implies \counted{2} \text{ trees}
Longest path of 3\route{3} dots (33 leftover dots) and totalAll 33 leftover dots must attach directly to the single middle dot to form a star, and adding the counts across all path lengths gives the total number of non-isomorphic trees on 66 dots:
total trees on 6 dots\displaystyle \text{total trees on } 6 \text{ dots} =1+2+2+1\displaystyle = 1 + 2 + 2 + 1 =6 trees\displaystyle = \counted{6} \text{ trees}
longest path of 6 dots1 tree\htmlClass{lc-mute}{\text{longest path of 6 dots}}\quad \htmlClass{lc-green}{1}\text{ tree}
longest path of 5 dots2 trees\htmlClass{lc-mute}{\text{longest path of 5 dots}}\quad \htmlClass{lc-green}{2}\text{ trees}
longest path of 4 dots2 trees\htmlClass{lc-mute}{\text{longest path of 4 dots}}\quad \htmlClass{lc-green}{2}\text{ trees}
longest path of 3 dots1 tree\htmlClass{lc-mute}{\text{longest path of 3 dots}}\quad \htmlClass{lc-green}{1}\text{ tree}
6

Spanning Trees

When a connected network has extra lines that create loops, a spanning tree is a subgraph that keeps all nn dots of the original graph connected while removing redundant lines until no cycles remain.

Spanning Trees
10 dots, 15 lines10\text{ dots, }15\text{ lines}
lines to erase=  ?\htmlClass{lc-orange}{\text{lines to erase}} = \;?
Text solution
Find the number of lines left in the spanning treeBecause the spanning tree connects all 1010 dots with no cycles, it must end up with one fewer line than dots:
spanning tree lines\displaystyle \text{spanning tree lines} =10−1\displaystyle = 10 - 1 =9 lines\displaystyle = \counted{9} \text{ lines}
Subtract from the original lines to find how many loop lines to eraseErasing one line from a cycle at a time keeps the graph connected until 9\counted{9} lines remain and the last cycle breaks:
lines to erase\displaystyle \text{lines to erase} =15−9\displaystyle = 15 - \counted{9} =6 lines\displaystyle = \spare{6} \text{ lines}

Key concepts

A tree is a graph on nn dots that is connected with no cycles. Hanging a tree from any top dot pairs every line with the dot hanging from its bottom end, leaving only the top dot without a line above it. That single picture gives us every count we need:

Lines in a tree and pieces in a forest

A tree on nn dots always has n−1\counted{n - 1} lines. In a cycle-free forest, every missing line creates one more separate tree with its own top dot, so pieces=dots−lines\counted{\text{pieces}} = \text{dots} - \text{lines} (for instance, 2020 dots and 1717 lines with no cycles form 20−17=320 - 17 = \counted{3} separate trees). If instead you start with a connected graph that has too many lines (mm lines on nn dots) and want to reduce it to a spanning tree, you erase one line from a cycle at a time until n−1\counted{n - 1} lines remain, removing m−(n−1)\spare{m - (\counted{n - 1})} lines in total.

Recognizing a tree (Two-out-of-Three rule)

On nn dots, confirming any two of connected, no cycles, and n−1\counted{n - 1} lines guarantees the graph is a tree (for example, a connected graph on 88 dots with 7\counted{7} lines cannot have a loop and must be a tree). When not to use the line count alone: never conclude a graph is a tree just because it has n−1\counted{n - 1} lines, or even n−1\counted{n - 1} lines and two leaves, without checking connectivity or cycles, since a 3\spare{3}-dot triangle alongside an isolated 4th4\text{th} dot still has 4−1=34 - 1 = \counted{3} lines.

Counting leaves from degrees

Start with a 11-line tree (2\counted{2} leaves) and add d−2\added{d - 2} leaves for each branching dot of degree dd (for example, a single dot of degree 44 and the rest leaves gives 2+(4−2)=4\counted{2} + \added{(4 - 2)} = \counted{4} leaves, while degree-22 dots add 2−2=0\added{2 - 2} = 0 leaves), or set sum of degrees=2(n−1)\text{sum of degrees} = 2(\counted{n - 1}) using the Handshake Lemma.

Listing different trees on nn dots

Organize by the length of the longest path from nn down to 33, attaching leftover dots only where they cannot extend the longest path, and check whether flipping the path turns one placement into another.

7

Practice

1

A tree has 1414 dots. (a) How many lines does it have? (b) If you erase one line, how many separate connected pieces does the tree break into? (c) If instead you keep the original tree and draw one new line between two dots whose unique path in the tree is 44 lines long, how many cycles does the graph now have, and how many lines are in that cycle?

Hang the tree from one top dot for (a), think about how many paths connect the two endpoints of the erased line for (b), and trace the closed route formed by the tree path together with the new shortcut line for (c).

(a) A tree on nn dots has n−1\counted{n - 1} lines (watch out for answering 1414 by forgetting that the top dot has no line above it):
lines\displaystyle \text{lines} =14−1\displaystyle = 14 - \topdot{1} =13 lines\displaystyle = \counted{13} \text{ lines}
(b) Because the erased line was the unique path between its two endpoints, cutting that bridge splits the tree into 2\counted{2} pieces.
(c) The 1\spare{1} new line joins the ends of the unique 4\route{4}-line path in the tree to close 1\spare{1} cycle, and that cycle contains both the old path and the new line (don't forget to include the new line itself, which would give 44 instead of 55):
lines in the cycle\displaystyle \text{lines in the cycle} =4+1\displaystyle = \route{4} + \spare{1} =5 lines\displaystyle = \counted{5} \text{ lines}
2

A forest on 2828 dots has no cycles and consists of 66 separate trees. How many lines does the forest have, and how many new lines must you draw between trees to connect the entire forest into a single tree?

Hang each of the 66 trees from its own top dot to see how many dots have no line above them, then compare the forest's line count to a single tree on 2828 dots.

In any forest, each separate tree has 11 top dot with no line above it, so pieces=dots−lines\counted{\text{pieces}} = \text{dots} - \text{lines}:
6\displaystyle 6 =28−lines  ⟹  lines\displaystyle = 28 - \text{lines} \implies \text{lines} =28−6\displaystyle = 28 - 6 =22 lines\displaystyle = \counted{22} \text{ lines}
A single tree on all 2828 dots has 28−1=2728 - 1 = \counted{27} lines. Beware of answering 66 new lines to join 66 trees: joining 66 trees into 11 tree takes 6−1=56 - 1 = \counted{5} lines:
new lines needed\displaystyle \text{new lines needed} =27−22\displaystyle = 27 - 22 =5 new lines\displaystyle = \counted{5} \text{ new lines}
3

Three graphs each have 88 dots and 77 lines. For each one, state whether it must be a tree, cannot be a tree, or might or might not be a tree, and explain why: (a) Graph A is connected. (b) Graph B contains a cycle of 44 lines. (c) Graph C has at least two leaves, with no other information given.

Check which graphs have two of the three tree properties (connected, no cycles, n−1=7n - 1 = \counted{7} lines), and for Graph C see whether you can build a loop on a few dots and still get two leaves on the remaining dots.

(a) Must be a tree: Graph A is connected and has n−1=8−1=7n - 1 = 8 - 1 = \counted{7} lines, and having two of the three tree properties forces it to have no cycles.
(b) Cannot be a tree: A tree cannot contain any cycles (and using a line to close the 4\spare{4}-cycle also forces the 88-dot, 77-line graph to be disconnected).
(c) Might or might not be a tree: Do not mistake "has at least two leaves" for one of the Two-out-of-Three properties (which would wrongly say it must be a tree). Graph C could be an 88-dot tree, or it could be disconnected with a cycle, such as a 3\spare{3}-dot triangle (3\spare{3} lines) on one side and a separate 55-dot chain (44 lines, with 2\counted{2} leaves at its ends) on the other, which uses 3+4=7\spare{3} + 4 = \counted{7} lines on 88 dots and has 2\counted{2} leaves.
4

A tree has three dots of degree 55, four dots of degree 33, seven dots of degree 22, and all of its remaining dots are leaves of degree 11. How many leaves does the tree have, and how many dots does it have in total?

Start with 2\counted{2} leaves and add d−2\added{d - 2} leaves for each non-leaf dot of degree dd, then add the leaves to the non-leaf dots to find the total number of dots.

Starting from 2\counted{2} leaves, each dot of degree 55 adds 5−2=3\added{5 - 2} = 3 leaves, each dot of degree 33 adds 3−2=1\added{3 - 2} = 1 leaf, and each dot of degree 22 adds 2−2=0\added{2 - 2} = 0 leaves (be careful not to forget the starting 2\counted{2} leaves, which would give 1313, or to add 11 leaf per degree-22 dot, which would give 2222):
leaves=2+3(5−2)+4(3−2)+7(2−2)=2+9+4+0=15 leaves\displaystyle \begin{aligned} \text{leaves} &= \counted{2} + \added{3(5 - 2)} + \added{4(3 - 2)} + \added{7(2 - 2)} \\ &= \counted{2} + \added{9} + \added{4} + \added{0} \\ &= \counted{15} \text{ leaves} \end{aligned}
(Equivalently, by the Handshake Lemma with L\counted{L} leaves, there are 3+4+7+L=14+L3 + 4 + 7 + \counted{L} = 14 + \counted{L} dots and 13+L13 + \counted{L} lines, so 3(5)+4(3)+7(2)+L=2(13+L)3(5) + 4(3) + 7(2) + \counted{L} = 2(13 + \counted{L}), giving 41+L=26+2L41 + \counted{L} = 26 + 2\counted{L} and L=15\counted{L} = \counted{15}.) Adding the 15\counted{15} leaves to the 1414 non-leaf dots gives the total number of dots:
total dots\displaystyle \text{total dots} =3+4+7+15\displaystyle = 3 + 4 + 7 + \counted{15} =29 dots\displaystyle = \counted{29} \text{ dots}
5

In a tree, every dot is either a branching dot of degree 44 or a leaf of degree 11. If the tree has 2020 leaves, how many dots of degree 44 and how many lines does it have?

Write an equation for the 2020 leaves using kk dots of degree 44, then add both kinds of dots together before finding the number of lines.

Each dot of degree 44 adds 4−2=2\added{4 - 2} = 2 leaves to the starting 2\counted{2} leaves. With kk dots of degree 44 (don't divide 2020 by 22 without subtracting the starting 2\counted{2} leaves first, which would give 1010 instead of 99):
2+2k\displaystyle \counted{2} + \added{2k} =20  ⟹  2k\displaystyle = 20 \implies \added{2k} =18  ⟹  k\displaystyle = 18 \implies k =9 dots of degree 4\displaystyle = \counted{9} \text{ dots of degree } 4
Combining the 99 dots of degree 44 with the 2020 leaves gives 9+20=299 + 20 = \counted{29} total dots (don't compute 20−1=1920 - 1 = 19 lines from the leaves alone):
lines\displaystyle \text{lines} =29−1\displaystyle = 29 - 1 =28 lines\displaystyle = \counted{28} \text{ lines}
6

Using the longest-path method, find (a) how many genuinely different (non-isomorphic) trees on 77 dots have a longest path of 66 dots, and (b) how many have a longest path of 44 dots. Describe each tree.

Draw a chain of 66 dots for (a) and 44 dots for (b), then place the leftover dots on interior dots where they cannot lengthen the chain, checking which placements match by flipping the chain.

(a) Start with a chain of 6\route{6} dots and 11 leftover dot, which cannot attach to the 1st1\text{st} or 6th6\text{th} dot at the ends. Attaching it to the 2nd2\text{nd} dot is the same as the 5th5\text{th} dot by flipping the chain, and attaching it to the 3rd3\text{rd} dot is the same as the 4th4\text{th} dot by flipping (watch out for counting all 44 interior dots separately without flipping, which would give 44 instead of 22). This gives 2\counted{2} trees: one with the extra leaf on the 2nd2\text{nd} dot, and one with the extra leaf on the 3rd3\text{rd} dot.
(b) Start with a chain of 4\route{4} dots and 33 leftover dots. None can attach to the two ends, and two leftover dots cannot attach one after another into a 22-dot branch off a middle dot because that branch plus the longer side of the 44-dot path would create a path of 5\toolong{5} dots. Thus all 33 leftover dots must attach directly as leaves to the two middle dots, giving 2\counted{2} trees: one with all 33 leftover dots on the same middle dot (degrees 5,2,1,1,1,1,15, 2, 1, 1, 1, 1, 1), and one with 22 on one middle dot and 11 on the other (degrees 4,3,1,1,1,1,14, 3, 1, 1, 1, 1, 1).
7

A connected computer network has 1212 computers, and every computer is wired directly to 33 others. How many wires must you remove to reduce the network to a spanning tree?

Use the Handshake Lemma to find how many wires are in the network right now, then subtract the number of wires that remain in a spanning tree on 1212 computers.

By the Handshake Lemma, the sum of the degrees is 12×3=3612 \times 3 = 36, and dividing by 22 because each wire touches 22 computers gives the current number of wires (don't forget to divide by 22, which would give 3636 wires and 36−11=2536 - 11 = 25 removed):
current wires\displaystyle \text{current wires} =12×32\displaystyle = \frac{12 \times 3}{2} =18 wires\displaystyle = 18 \text{ wires}
Any spanning tree on 1212 computers keeps 12−1=1112 - 1 = \counted{11} wires, so the number of wires to remove is:
wires to remove\displaystyle \text{wires to remove} =18−11\displaystyle = 18 - \counted{11} =7 wires\displaystyle = \spare{7} \text{ wires}
8

Prove that if a tree contains a dot of degree dd (with d≥2d \ge 2), then the tree must have at least dd leaves.

Start at the dot of degree dd and take a walk out along each of its dd lines without ever turning around on the same line, then explain why every walk must stop at a leaf and why no two walks can stop at the same leaf.

Start at the dot of degree dd and take dd separate walks out along each of its dd attached lines, never turning back along the line just used (do not assume the dd immediate neighbors of the starting dot are already leaves, because those neighbors might branch further). Because the tree has a finite number of dots and no cycles, none of the dd walks can revisit a dot and loop, so each walk must eventually stop at a leaf of degree 11. Furthermore, no two of these dd walks can ever end at the same leaf (or meet at any dot along the way), because their two routes from the starting dot to the first dot where they met would form a closed cycle. Therefore, the dd walks must end at d\counted{d} different leaves, so the tree has at least d\counted{d} leaves. (Equivalently, starting from 2\counted{2} leaves, the dot of degree dd adds d−2\added{d - 2} leaves and every other non-leaf dot of degree k≥2k \ge 2 adds k−2≥0\added{k - 2} \ge 0 leaves, giving at least 2+(d−2)=d\counted{2} + \added{(d - 2)} = \counted{d} leaves.)