counting trees - brown university · 2020-04-29 · counting trees attempt 2: building up ways to...
TRANSCRIPT
Counting Trees
Counting Trees
Michael L. Littman
CS 22 2020
April 27, 2020
1 / 112
Counting Trees
Overview
Puzzle
Attempt 1: Choosing edges
Attempt 2: Building up
Attempt 3
Prufer codes
2 / 112
Counting Trees
Puzzle
Format
Different format today.
We’re going to look at one problem andwe’ll see my failed attempts to solve it, along with a solution thatactually works.
3 / 112
Counting Trees
Puzzle
Format
Different format today. We’re going to look at one problem andwe’ll see my failed attempts to solve it, along with a solution thatactually works.
4 / 112
Counting Trees
Puzzle
Counting trees
How many ways can we connected n vertices together into a tree?
Trees on 2 and trees on 3:
5 / 112
Counting Trees
Puzzle
Counting trees
How many ways can we connected n vertices together into a tree?
Trees on 2 and trees on 3:
6 / 112
Counting Trees
Puzzle
Counting trees
How many ways can we connected n vertices together into a tree?
Trees on 2 and trees on 3:
7 / 112
Counting Trees
Puzzle
Trees on 4
8 / 112
Counting Trees
Puzzle
What we know so far
Trees: Connected, acyclic.
n trees
2 13 34 16
9 / 112
Counting Trees
Puzzle
What we know so far
Trees: Connected, acyclic.
n trees
2 13 34 16
10 / 112
Counting Trees
Attempt 1: Choosing edges
Choosing edges
Ok, first thought.
A tree on n vertices has n − 1 edges out of allpossible edges:
( (n2
)n − 1
).
n trees
2 13 34 20 > 16
We counted cycles that aren’t trees.
11 / 112
Counting Trees
Attempt 1: Choosing edges
Choosing edges
Ok, first thought. A tree on n vertices has n − 1 edges
out of allpossible edges:
( (n2
)n − 1
).
n trees
2 13 34 20 > 16
We counted cycles that aren’t trees.
12 / 112
Counting Trees
Attempt 1: Choosing edges
Choosing edges
Ok, first thought. A tree on n vertices has n − 1 edges out of allpossible edges:
( (n2
)n − 1
).
n trees
2 13 34 20 > 16
We counted cycles that aren’t trees.
13 / 112
Counting Trees
Attempt 1: Choosing edges
Choosing edges
Ok, first thought. A tree on n vertices has n − 1 edges out of allpossible edges:
( (n2
)n − 1
).
n trees
2 13 34 20 > 16
We counted cycles that aren’t trees.
14 / 112
Counting Trees
Attempt 1: Choosing edges
Choosing edges
Ok, first thought. A tree on n vertices has n − 1 edges out of allpossible edges:
( (n2
)n − 1
).
n trees
2 13 34 20
> 16
We counted cycles that aren’t trees.
15 / 112
Counting Trees
Attempt 1: Choosing edges
Choosing edges
Ok, first thought. A tree on n vertices has n − 1 edges out of allpossible edges:
( (n2
)n − 1
).
n trees
2 13 34 20 > 16
We counted cycles that aren’t trees.
16 / 112
Counting Trees
Attempt 1: Choosing edges
Choosing edges
Ok, first thought. A tree on n vertices has n − 1 edges out of allpossible edges:
( (n2
)n − 1
).
n trees
2 13 34 20 > 16
We counted cycles that aren’t trees.
17 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees.
Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
18 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.
Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
19 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices.
We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
20 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
21 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree.
Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
22 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore.
Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
23 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
24 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
25 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2
< 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
26 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6
< 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
27 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
28 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees.
Inparticular, we’re missing trees where the last vertex is somewherein the middle.
29 / 112
Counting Trees
Attempt 2: Building up
Ways to add a vertex
So, let’s be careful to only generate trees. Here’s the thought.Consider a tree on n vertices. We can add vertex n + 1 andconnect it into the tree n different ways.
So, 2 vertices make 1 tree. Adding the 3rd vertex creates 2 timesmore. Adding the 4th vertex creates 3 times more.
Generalizing, we get (n − 1)! trees.
n trees
2 13 2 < 34 6 < 16
Now, we’re only counting trees, but we’re missing some trees. Inparticular, we’re missing trees where the last vertex is somewherein the middle.
30 / 112
Counting Trees
Attempt 2: Building up
Generated 4 trees
31 / 112
Counting Trees
Attempt 3
More careful growing
When we add in vertex n + 1, the other n nodes might not be atree.
They might be a forest. In general, vertex n + 1 will have oneedge to each connected component in the forest. For the edge toconnected component i , it will have a choice of which of thevertices in the connected component to connect to.
I worked on this path for awhile, and could almost write down anexpression, but it was complicated and I didn’t think I couldsimplify it. The basic idea is consider all the ways of making a treewith n′ nodes for all n′ ≤ n, then all the ways n nodes can bepartitioned into clusters, then sum and multiply...
But, even the question of how many partitions there are for nitems is hairy. See: https://oeis.org/A000110 .
32 / 112
Counting Trees
Attempt 3
More careful growing
When we add in vertex n + 1, the other n nodes might not be atree. They might be a forest.
In general, vertex n + 1 will have oneedge to each connected component in the forest. For the edge toconnected component i , it will have a choice of which of thevertices in the connected component to connect to.
I worked on this path for awhile, and could almost write down anexpression, but it was complicated and I didn’t think I couldsimplify it. The basic idea is consider all the ways of making a treewith n′ nodes for all n′ ≤ n, then all the ways n nodes can bepartitioned into clusters, then sum and multiply...
But, even the question of how many partitions there are for nitems is hairy. See: https://oeis.org/A000110 .
33 / 112
Counting Trees
Attempt 3
More careful growing
When we add in vertex n + 1, the other n nodes might not be atree. They might be a forest. In general, vertex n + 1 will have oneedge to each connected component in the forest.
For the edge toconnected component i , it will have a choice of which of thevertices in the connected component to connect to.
I worked on this path for awhile, and could almost write down anexpression, but it was complicated and I didn’t think I couldsimplify it. The basic idea is consider all the ways of making a treewith n′ nodes for all n′ ≤ n, then all the ways n nodes can bepartitioned into clusters, then sum and multiply...
But, even the question of how many partitions there are for nitems is hairy. See: https://oeis.org/A000110 .
34 / 112
Counting Trees
Attempt 3
More careful growing
When we add in vertex n + 1, the other n nodes might not be atree. They might be a forest. In general, vertex n + 1 will have oneedge to each connected component in the forest. For the edge toconnected component i , it will have a choice of which of thevertices in the connected component to connect to.
I worked on this path for awhile, and could almost write down anexpression, but it was complicated and I didn’t think I couldsimplify it. The basic idea is consider all the ways of making a treewith n′ nodes for all n′ ≤ n, then all the ways n nodes can bepartitioned into clusters, then sum and multiply...
But, even the question of how many partitions there are for nitems is hairy. See: https://oeis.org/A000110 .
35 / 112
Counting Trees
Attempt 3
More careful growing
When we add in vertex n + 1, the other n nodes might not be atree. They might be a forest. In general, vertex n + 1 will have oneedge to each connected component in the forest. For the edge toconnected component i , it will have a choice of which of thevertices in the connected component to connect to.
I worked on this path for awhile, and could almost write down anexpression, but it was complicated and I didn’t think I couldsimplify it.
The basic idea is consider all the ways of making a treewith n′ nodes for all n′ ≤ n, then all the ways n nodes can bepartitioned into clusters, then sum and multiply...
But, even the question of how many partitions there are for nitems is hairy. See: https://oeis.org/A000110 .
36 / 112
Counting Trees
Attempt 3
More careful growing
When we add in vertex n + 1, the other n nodes might not be atree. They might be a forest. In general, vertex n + 1 will have oneedge to each connected component in the forest. For the edge toconnected component i , it will have a choice of which of thevertices in the connected component to connect to.
I worked on this path for awhile, and could almost write down anexpression, but it was complicated and I didn’t think I couldsimplify it. The basic idea is consider all the ways of making a treewith n′ nodes for all n′ ≤ n, then all the ways n nodes can bepartitioned into clusters, then sum and multiply...
But, even the question of how many partitions there are for nitems is hairy. See: https://oeis.org/A000110 .
37 / 112
Counting Trees
Attempt 3
More careful growing
When we add in vertex n + 1, the other n nodes might not be atree. They might be a forest. In general, vertex n + 1 will have oneedge to each connected component in the forest. For the edge toconnected component i , it will have a choice of which of thevertices in the connected component to connect to.
I worked on this path for awhile, and could almost write down anexpression, but it was complicated and I didn’t think I couldsimplify it. The basic idea is consider all the ways of making a treewith n′ nodes for all n′ ≤ n, then all the ways n nodes can bepartitioned into clusters, then sum and multiply...
But, even the question of how many partitions there are for nitems is hairy.
See: https://oeis.org/A000110 .
38 / 112
Counting Trees
Attempt 3
More careful growing
When we add in vertex n + 1, the other n nodes might not be atree. They might be a forest. In general, vertex n + 1 will have oneedge to each connected component in the forest. For the edge toconnected component i , it will have a choice of which of thevertices in the connected component to connect to.
I worked on this path for awhile, and could almost write down anexpression, but it was complicated and I didn’t think I couldsimplify it. The basic idea is consider all the ways of making a treewith n′ nodes for all n′ ≤ n, then all the ways n nodes can bepartitioned into clusters, then sum and multiply...
But, even the question of how many partitions there are for nitems is hairy. See: https://oeis.org/A000110 .
39 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it.
It’s definitely notobvious (to me!). But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees. That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees. Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
40 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it. It’s definitely notobvious (to me!).
But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees. That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees. Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
41 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it. It’s definitely notobvious (to me!). But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees. That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees. Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
42 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it. It’s definitely notobvious (to me!). But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees.
That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees. Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
43 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it. It’s definitely notobvious (to me!). But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees. That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees. Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
44 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it. It’s definitely notobvious (to me!). But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees. That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees.
Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
45 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it. It’s definitely notobvious (to me!). But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees. That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees. Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
46 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it. It’s definitely notobvious (to me!). But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees. That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees. Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
47 / 112
Counting Trees
Prufer codes
Better way to look at it
Sometimes there’s just a better way to look at it. It’s definitely notobvious (to me!). But, it’s clever and gives a nice clean answer.
1. Create a “normal form” for trees. That way, we can at leastnotice when two different trees are actually the same.
2. Find a compact encoding for these trees. Here, “compact”means no extraneous information.
3. Then, we can show we have a bijection (!) between trees andthe encoding.
4. If we’re lucky, it’ll be easier to count encodings than trees.
48 / 112
Counting Trees
Prufer codes
Normal form for trees
Choose node n to be the root.
Order children smallest (left) tobiggest (right). There are no other choices.
49 / 112
Counting Trees
Prufer codes
Normal form for trees
Choose node n to be the root. Order children smallest (left) tobiggest (right).
There are no other choices.
50 / 112
Counting Trees
Prufer codes
Normal form for trees
Choose node n to be the root. Order children smallest (left) tobiggest (right). There are no other choices.
51 / 112
Counting Trees
Prufer codes
Normal form for trees
Choose node n to be the root. Order children smallest (left) tobiggest (right). There are no other choices.
52 / 112
Counting Trees
Prufer codes
Normal form for trees
Choose node n to be the root. Order children smallest (left) tobiggest (right). There are no other choices.
53 / 112
Counting Trees
Prufer codes
Encoding a tree
Every node has a unique parent.
So, we code just give the parentfor each node: 3 7 4 5 11 3 10 2 10 4.
List n − 1 numbers, each with a choice of n − 1 numbers (can’tpick yourself!). That’s (n − 1)n−1.
Anything extraneous? Yes, can encode a loop: 3 1 2 5.
54 / 112
Counting Trees
Prufer codes
Encoding a tree
Every node has a unique parent. So, we code just give the parentfor each node: 3 7 4 5 11 3 10 2 10 4.
List n − 1 numbers, each with a choice of n − 1 numbers (can’tpick yourself!).
That’s (n − 1)n−1.
Anything extraneous? Yes, can encode a loop: 3 1 2 5.
55 / 112
Counting Trees
Prufer codes
Encoding a tree
Every node has a unique parent. So, we code just give the parentfor each node: 3 7 4 5 11 3 10 2 10 4.
List n − 1 numbers, each with a choice of n − 1 numbers (can’tpick yourself!). That’s (n − 1)n−1.
Anything extraneous? Yes, can encode a loop: 3 1 2 5.
56 / 112
Counting Trees
Prufer codes
Encoding a tree
Every node has a unique parent. So, we code just give the parentfor each node: 3 7 4 5 11 3 10 2 10 4.
List n − 1 numbers, each with a choice of n − 1 numbers (can’tpick yourself!). That’s (n − 1)n−1.
Anything extraneous?
Yes, can encode a loop: 3 1 2 5.
57 / 112
Counting Trees
Prufer codes
Encoding a tree
Every node has a unique parent. So, we code just give the parentfor each node: 3 7 4 5 11 3 10 2 10 4.
List n − 1 numbers, each with a choice of n − 1 numbers (can’tpick yourself!). That’s (n − 1)n−1.
Anything extraneous? Yes, can encode a loop:
3 1 2 5.
58 / 112
Counting Trees
Prufer codes
Encoding a tree
Every node has a unique parent. So, we code just give the parentfor each node: 3 7 4 5 11 3 10 2 10 4.
List n − 1 numbers, each with a choice of n − 1 numbers (can’tpick yourself!). That’s (n − 1)n−1.
Anything extraneous? Yes, can encode a loop: 3 1 2 5.
59 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4 > 34 27 > 16
Yeah, we’re definitely overcounting. But, we’re guaranteed not toundercount. Every tree has a representation in this scheme. But,some representations do not produce trees. It’s not a bijection.
60 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4
> 34 27 > 16
Yeah, we’re definitely overcounting. But, we’re guaranteed not toundercount. Every tree has a representation in this scheme. But,some representations do not produce trees. It’s not a bijection.
61 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4 > 34 27
> 16
Yeah, we’re definitely overcounting. But, we’re guaranteed not toundercount. Every tree has a representation in this scheme. But,some representations do not produce trees. It’s not a bijection.
62 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4 > 34 27 > 16
Yeah, we’re definitely overcounting. But, we’re guaranteed not toundercount. Every tree has a representation in this scheme. But,some representations do not produce trees. It’s not a bijection.
63 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4 > 34 27 > 16
Yeah, we’re definitely overcounting.
But, we’re guaranteed not toundercount. Every tree has a representation in this scheme. But,some representations do not produce trees. It’s not a bijection.
64 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4 > 34 27 > 16
Yeah, we’re definitely overcounting. But, we’re guaranteed not toundercount.
Every tree has a representation in this scheme. But,some representations do not produce trees. It’s not a bijection.
65 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4 > 34 27 > 16
Yeah, we’re definitely overcounting. But, we’re guaranteed not toundercount. Every tree has a representation in this scheme.
But,some representations do not produce trees. It’s not a bijection.
66 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4 > 34 27 > 16
Yeah, we’re definitely overcounting. But, we’re guaranteed not toundercount. Every tree has a representation in this scheme. But,some representations do not produce trees.
It’s not a bijection.
67 / 112
Counting Trees
Prufer codes
Sanity check
Could it be (n − 1)n−1?
n trees
2 13 4 > 34 27 > 16
Yeah, we’re definitely overcounting. But, we’re guaranteed not toundercount. Every tree has a representation in this scheme. But,some representations do not produce trees. It’s not a bijection.
68 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times.
Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2 7 10 10 4 5
69 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf.
Write down its parent. Delete the leaf.
3 3 4 2 7 10 10 4 5
70 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent.
Delete the leaf.
3 3 4 2 7 10 10 4 5
71 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2 7 10 10 4 5
72 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2 7 10 10 4 5
73 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3
3 4 2 7 10 10 4 5
74 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3
4 2 7 10 10 4 5
75 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4
2 7 10 10 4 5
76 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2
7 10 10 4 5
77 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2 7
10 10 4 5
78 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2 7 10
10 4 5
79 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2 7 10 10
4 5
80 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2 7 10 10 4
5
81 / 112
Counting Trees
Prufer codes
Prufer rule
Repeat the following procedure n − 2 times. Find the smallestvalued leaf. Write down its parent. Delete the leaf.
3 3 4 2 7 10 10 4 5
82 / 112
Counting Trees
Prufer codes
Recover a tree
We can process any tree into a list.
But, can we recover the treefrom the list? Before we prove that we can, let’s do an example:
3 3 4 2 7 10 10 4 5
83 / 112
Counting Trees
Prufer codes
Recover a tree
We can process any tree into a list. But, can we recover the treefrom the list?
Before we prove that we can, let’s do an example:
3 3 4 2 7 10 10 4 5
84 / 112
Counting Trees
Prufer codes
Recover a tree
We can process any tree into a list. But, can we recover the treefrom the list? Before we prove that we can, let’s do an example:
3 3 4 2 7 10 10 4 5
85 / 112
Counting Trees
Prufer codes
Recover a tree
We can process any tree into a list. But, can we recover the treefrom the list? Before we prove that we can, let’s do an example:
3 3 4 2 7 10 10 4 5
86 / 112
Counting Trees
Prufer codes
Recover a tree
We can process any tree into a list. But, can we recover the treefrom the list? Before we prove that we can, let’s do an example:
3 3 4 2 7 10 10 4 5
87 / 112
Counting Trees
Prufer codes
4 by 4
88 / 112
Counting Trees
Prufer codes
Thinking inductivelyHere’s a 6-vertex tree in Prufer encoding: 1 1 3 1.
In what sense is it built out of a 5-vertex encoding? Take thevertex x that is the “first” leaf. Here, x = 2. Remove it, thenrenumber the vertices, decrementing anything larger than x . Thething to note is that the resulting tree and encoding still match!
89 / 112
Counting Trees
Prufer codes
Thinking inductivelyHere’s a 6-vertex tree in Prufer encoding: 1 1 3 1.
In what sense is it built out of a 5-vertex encoding? Take thevertex x that is the “first” leaf. Here, x = 2. Remove it, thenrenumber the vertices, decrementing anything larger than x . Thething to note is that the resulting tree and encoding still match!
90 / 112
Counting Trees
Prufer codes
Thinking inductivelyHere’s a 6-vertex tree in Prufer encoding: 1 1 3 1.
In what sense is it built out of a 5-vertex encoding?
Take thevertex x that is the “first” leaf. Here, x = 2. Remove it, thenrenumber the vertices, decrementing anything larger than x . Thething to note is that the resulting tree and encoding still match!
91 / 112
Counting Trees
Prufer codes
Thinking inductivelyHere’s a 6-vertex tree in Prufer encoding: 1 1 3 1.
In what sense is it built out of a 5-vertex encoding? Take thevertex x that is the “first” leaf.
Here, x = 2. Remove it, thenrenumber the vertices, decrementing anything larger than x . Thething to note is that the resulting tree and encoding still match!
92 / 112
Counting Trees
Prufer codes
Thinking inductivelyHere’s a 6-vertex tree in Prufer encoding: 1 1 3 1.
In what sense is it built out of a 5-vertex encoding? Take thevertex x that is the “first” leaf. Here, x = 2.
Remove it, thenrenumber the vertices, decrementing anything larger than x . Thething to note is that the resulting tree and encoding still match!
93 / 112
Counting Trees
Prufer codes
Thinking inductivelyHere’s a 6-vertex tree in Prufer encoding: 1 1 3 1.
In what sense is it built out of a 5-vertex encoding? Take thevertex x that is the “first” leaf. Here, x = 2. Remove it, thenrenumber the vertices, decrementing anything larger than x .
Thething to note is that the resulting tree and encoding still match!
94 / 112
Counting Trees
Prufer codes
Thinking inductivelyHere’s a 6-vertex tree in Prufer encoding: 1 1 3 1.
In what sense is it built out of a 5-vertex encoding? Take thevertex x that is the “first” leaf. Here, x = 2. Remove it, thenrenumber the vertices, decrementing anything larger than x . Thething to note is that the resulting tree and encoding still match!
95 / 112
Counting Trees
Prufer codes
Proof
Theorem: For every string a ∈ [1, n]n−2 (n ≥ 2), there is a uniquetree T .
Proof: Build-down induction on n.
Base Case (n = 2): There is only one tree (a 1–2 segment) andonly one encoding string (the null string).
Inductive Step (n + 1): Consider a string a of length n − 1. In thetree T encoded by a, the leaf with the smallest label x must belinked to a1.
Consider the string a′ formed by removing a1 from a and thensubtracting one from every value in a that’s larger than x . By theinductive hypothesis, there is a unique tree T0 constructed from a′.We can construct a unique tree T from T0 by adding 1 to thevalues in T0 that are x or above, then adding the edge (x , a1).
96 / 112
Counting Trees
Prufer codes
Proof
Theorem: For every string a ∈ [1, n]n−2 (n ≥ 2), there is a uniquetree T .
Proof: Build-down induction on n.
Base Case (n = 2): There is only one tree (a 1–2 segment) andonly one encoding string (the null string).
Inductive Step (n + 1): Consider a string a of length n − 1. In thetree T encoded by a, the leaf with the smallest label x must belinked to a1.
Consider the string a′ formed by removing a1 from a and thensubtracting one from every value in a that’s larger than x . By theinductive hypothesis, there is a unique tree T0 constructed from a′.We can construct a unique tree T from T0 by adding 1 to thevalues in T0 that are x or above, then adding the edge (x , a1).
97 / 112
Counting Trees
Prufer codes
Proof
Theorem: For every string a ∈ [1, n]n−2 (n ≥ 2), there is a uniquetree T .
Proof: Build-down induction on n.
Base Case (n = 2): There is only one tree (a 1–2 segment) andonly one encoding string (the null string).
Inductive Step (n + 1): Consider a string a of length n − 1.
In thetree T encoded by a, the leaf with the smallest label x must belinked to a1.
Consider the string a′ formed by removing a1 from a and thensubtracting one from every value in a that’s larger than x . By theinductive hypothesis, there is a unique tree T0 constructed from a′.We can construct a unique tree T from T0 by adding 1 to thevalues in T0 that are x or above, then adding the edge (x , a1).
98 / 112
Counting Trees
Prufer codes
Proof
Theorem: For every string a ∈ [1, n]n−2 (n ≥ 2), there is a uniquetree T .
Proof: Build-down induction on n.
Base Case (n = 2): There is only one tree (a 1–2 segment) andonly one encoding string (the null string).
Inductive Step (n + 1): Consider a string a of length n − 1. In thetree T encoded by a, the leaf with the smallest label x must belinked to a1.
Consider the string a′ formed by removing a1 from a and thensubtracting one from every value in a that’s larger than x . By theinductive hypothesis, there is a unique tree T0 constructed from a′.We can construct a unique tree T from T0 by adding 1 to thevalues in T0 that are x or above, then adding the edge (x , a1).
99 / 112
Counting Trees
Prufer codes
Proof
Theorem: For every string a ∈ [1, n]n−2 (n ≥ 2), there is a uniquetree T .
Proof: Build-down induction on n.
Base Case (n = 2): There is only one tree (a 1–2 segment) andonly one encoding string (the null string).
Inductive Step (n + 1): Consider a string a of length n − 1. In thetree T encoded by a, the leaf with the smallest label x must belinked to a1.
Consider the string a′ formed by removing a1 from a and thensubtracting one from every value in a that’s larger than x .
By theinductive hypothesis, there is a unique tree T0 constructed from a′.We can construct a unique tree T from T0 by adding 1 to thevalues in T0 that are x or above, then adding the edge (x , a1).
100 / 112
Counting Trees
Prufer codes
Proof
Theorem: For every string a ∈ [1, n]n−2 (n ≥ 2), there is a uniquetree T .
Proof: Build-down induction on n.
Base Case (n = 2): There is only one tree (a 1–2 segment) andonly one encoding string (the null string).
Inductive Step (n + 1): Consider a string a of length n − 1. In thetree T encoded by a, the leaf with the smallest label x must belinked to a1.
Consider the string a′ formed by removing a1 from a and thensubtracting one from every value in a that’s larger than x . By theinductive hypothesis, there is a unique tree T0 constructed from a′.
We can construct a unique tree T from T0 by adding 1 to thevalues in T0 that are x or above, then adding the edge (x , a1).
101 / 112
Counting Trees
Prufer codes
Proof
Theorem: For every string a ∈ [1, n]n−2 (n ≥ 2), there is a uniquetree T .
Proof: Build-down induction on n.
Base Case (n = 2): There is only one tree (a 1–2 segment) andonly one encoding string (the null string).
Inductive Step (n + 1): Consider a string a of length n − 1. In thetree T encoded by a, the leaf with the smallest label x must belinked to a1.
Consider the string a′ formed by removing a1 from a and thensubtracting one from every value in a that’s larger than x . By theinductive hypothesis, there is a unique tree T0 constructed from a′.We can construct a unique tree T from T0 by adding 1 to thevalues in T0 that are x or above, then adding the edge (x , a1).
102 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists.
It always works. Wehave a scheme for turning lists into trees. It always works. Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n. So, nn−2.Does that fit?
n trees
2 13 34 16
103 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works.
Wehave a scheme for turning lists into trees. It always works. Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n. So, nn−2.Does that fit?
n trees
2 13 34 16
104 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works. Wehave a scheme for turning lists into trees.
It always works. Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n. So, nn−2.Does that fit?
n trees
2 13 34 16
105 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works. Wehave a scheme for turning lists into trees. It always works.
Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n. So, nn−2.Does that fit?
n trees
2 13 34 16
106 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works. Wehave a scheme for turning lists into trees. It always works. Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n. So, nn−2.Does that fit?
n trees
2 13 34 16
107 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works. Wehave a scheme for turning lists into trees. It always works. Wehave a bijection.
How many lists?
n − 2 choices of numbers from 1 to n. So, nn−2.Does that fit?
n trees
2 13 34 16
108 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works. Wehave a scheme for turning lists into trees. It always works. Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n.
So, nn−2.Does that fit?
n trees
2 13 34 16
109 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works. Wehave a scheme for turning lists into trees. It always works. Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n. So, nn−2.
Does that fit?
n trees
2 13 34 16
110 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works. Wehave a scheme for turning lists into trees. It always works. Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n. So, nn−2.Does that fit?
n trees
2 13 34 16
111 / 112
Counting Trees
Prufer codes
Summing up
We have a scheme for encoding trees as lists. It always works. Wehave a scheme for turning lists into trees. It always works. Wehave a bijection.
How many lists? n − 2 choices of numbers from 1 to n. So, nn−2.Does that fit?
n trees
2 13 34 16
112 / 112