Episode 26
Boxes Inside Boxes
Duck puts boxes inside a present. Robot demonstrates how each box counts itself and then how the count of the boxes inside it is summed.


















Episode transcript
A gift with more gifts inside
Inside the monitor: Duck indicates A, the outer box. Robot is outside looking at a diagram of the nested boxes.
Diagram values: A CONTAINS B,C; C CONTAINS D
Duck: I packed up 1 huge box. Why does the inventory want 4?
Robot points at the 4 labels A, B, C, D. Duck opens A. Inside A is B and C. Duck opens C. Inside C is D.
Diagram values: COUNT EVERY BOX
Robot: We count every box (that means the outer box too).
Duck opens up boxes B & D (empty) in the monitor. Robot verifies that none of the arrows in the box diagram point back to an ancestor.
Diagram values: B EMPTY; D EMPTY
Duck: Box C has D in it.
A includes B and C. C includes D. Duck is making cards for counting.
Diagram values: A → B,C; C → D
Robot: How do you account for everything in the nest without forgetting the outer box?
Count each box once
Duck puts down counting card near every empty box. B is one box. D is one box. Robot checks stopping cases.
Diagram values: COUNT(B)=1; COUNT(D)=1
Duck: An empty box still counts as one.
Robot shows: RULE COUNT = 1 + sum of direct child box counts. Duck shows one outer outline and the child outlines inside it.
Diagram values: COUNT(X)=1+SUM(CHILD COUNTS)
Robot: For a box you have it count itself and then add the counts within it.
Duck closes D within C with 2 count on C. Robot shows 1 count for D in small inset.
Diagram values: COUNT(C)=1+1=2
Duck: C counts itself and D. So C is two.
B count is 1. C count is 2. Duck opens up A around its kids.
Diagram values: B1; C2
Robot: Now A can take advantage of counts for B and C.
Robot displays A, B, C, D, with one counting mark in each box.
Diagram values: COUNT(A)=1+1+2=4
Duck: A counts A, B, everything in C. 1+1+2 = 4.
Return the small answers
Duck traces answers of empty boxes to the outer box.
Robot: The question that they asked themselves was the same in each of the smaller boxes.
Duck points out B and D, empty boxes, as stopping points. Robot points out that these boxes have no child boxes to count.
Diagram values: EMPTY BOX →1
Duck: We stop when we have a box with no child boxes.
Robot follows from D to C and then to A. Then from B to A. Duck is comparing the two paths that join at A.
Diagram values: D1 → C2 → A4; B1 → A4
Robot: The smaller answers make the bigger answer.
D is already in the count for C. So we can't count D again at A. That'd be double counting.
Diagram values: C2 INCLUDES C,D
Duck: So I cannot re-include D after I've used all of C.
One extra little box
Duck adds an empty box (E) to B. The rest of the nesting remains the same. Robot hides the new totals.
Diagram values: B → E; E EMPTY
Duck: New puzzle. Stick an empty box E inside B. What's the new total?
Robot sets the count for E to 1. Robot sets the count for B to 2 (from 1). Duck does not change the counts for C or D.
Diagram values: COUNT(E)=1; COUNT(B)=2
Robot: E is one. Now, B counts itself and E. So B is two.
Duck updates A count from four to five based on the new count of B and the same count of C. Robot puts one mark in each of five boxes.
Diagram values: COUNT(A)=1+2+2=5
Duck: C is two. A is one plus two plus two, so five boxes.
Robot points at the (shot-down) arrow that would go from D back to A. (It's not the same as the nesting diagram proper.)
Diagram values: FINITE; NO CYCLES
Robot: This assumes the nest has finite size and no cycles.
Robot has the filled out inventory card in his hand, out of the monitor. And there are five boxes in the inventory.
Duck: Five boxes. One present. I think I've gone a bit mad.
The lesson
Recursion: The same rule, but for smaller pieces. For this finite tree of boxes in boxes, we count the current box, plus the number of counts of each of the immediate child boxes. If the box is empty, then it has no child boxes. The count of an empty box is 1. That's a stopping case. B is empty, so its count is 1. D is empty, so its count is 1. C has D in it, so C's count is 2. A has B in it, and C in it. So the count of A is (1 + 1) + 2 = 4. Note that D is included in the count of C. If we added D in at A again, we would end up double-counting D. (Of course, we're assuming finite nesting, no cycles.)
Try it
Diagram values: NEW TREE: R WITH THREE EMPTY CHILD BOXES; ADD ONE EMPTY BOX INSIDE ONE CHILD
You have a new outer R box with 3 empty child boxes. Count all the boxes including R. Now add a new empty box to one of those child boxes. What are the totals before and after?
Answer
Diagram values: BEFORE: 1 + 1 + 1 + 1 = 4; AFTER: 1 + 2 + 1 + 1 = 5
First you get one for an R and one for each of the three empty children, for a total of four. Then for each empty box you add inside a child that child counts two instead of one, so that you have five total.