Vous êtes sur la page 1sur 15

Magic-Box Problem: Algorithms

Brian Beckman, 14 August 2010

Referring to the Magic-Box Problem (http://www.scribd.com/doc/35597649/Counting-Boxes-001, and http://www.scribd.com/-


doc/35207524/Box-004), this notes presents a mechanization of algorithm 1 and compares it to algorithm 2.

Recap
Imagine beads numbered 1 to N and an unlimited supply of identical magic boxes, each with b ³ 2 cells. You may put in each cell
nothing, or one bead, or another magic box, nested.
Your task, for any N and any b, is to put all beads in one box using the smallest possible total number of boxes .

Motivation
This is a stand-in for problems involving fixed-size recursive structures such as b-way trees. Tree algorithms usually minimize depth
or maximize balance. Here, we minimize container allocation.

Algorithm 1: Minimal by Construction


Construction
Distribute beads, creating boxes only when necessary. (A) Start with one, level-1 box; put one bead in each cell until no empty cells
remain. Now pick any level-1 cell, (B) replace the bead currently in the cell with a new, level-2 box containing the bead, and continue
putting beads in the level-2 box until no empty cells remain in it. Now, move back to level-1; (C) pick another level-1 cell, and repeat
(B). Repeat (C) until no empty cells remain at either level 1 or level 2. Continue iterating B and C, just at levels 2 and 3, then 3 and
4, and so on, until you run out of beads.

Task: Mechanize this algorithm (write a program that performs it).

New and Refined Definitions


Represent boxes with lists all of a single size, base. Represent empty boxes with 0 and beads by their number between 1 and N.

Definition: Base or Box-Size


Symbolic constants to encode the contents of boxes:

In[1]:= Remove@emptyId, beadId, boxId, errorIdD


and a function that identifies the contents of a cell

In[2]:= contentsIdAbox_, cellIndex_, base_E :=


If@cellIndex > base, errorId,
With@8c = boxPcellIndexT, h = Headž boxPcellIndexT<,
If@c === 0, emptyId,
If@h === Integer && c > 0, beadId,
If@h === List && Lengthž box === base, boxId,
errorIdDDDDD
2 AlgorithmOne003.nb

Definition: Box Depth


The depth of a box is the maximum nesting depth of boxes in it, plus 1. A box with no nested boxes has depth 1; a box containing
exactly one nested box of depth 1 in at least one of its cells has depth 2, and so on.

In[3]:= depthAbox_, base_E := depthHelper@box, 1, baseD;


depthHelperAbox_, depth_, base_E :=
If@Head@boxD =!= List, 0,
Max žž HFunction@cellI,
Switch@contentsId@box, cellI, baseD,
emptyId, depth,
beadId, depth,
boxId, depthHelper@boxPcellIT, depth + 1, baseD,
errorId, IndeterminateDD ž Range@baseDLD

Definition: Perfectly Filled Box


All the cells of a perfectly filled box contain either individual beads or perfectly filled boxes of the same depth as one another (this
is a refinement of the definition from the definitions in earlier blogs).

perfectlyFilledPAbox_, base_E := Module@8


contIds = Table@contentsId@box, i, baseD, 8i, base<D<,
In[5]:=

SameQ žž HcontIds~Prepend~ beadIdL ÈÈ


HSameQ žž HcontIds~Prepend~ boxIdL &&
SameQ žž Table@depth@boxPiT, baseD, 8i, base<D &&
And žž Table@perfectlyFilledP@boxPiT, baseD, 8i, base<DLD

Exercise: Lemma
Prove that the total number of beads in a perfectly filled box must be an integer power of the base.

Mechanization of Algorithm 1
IndexOfFirstEmptyCell
indexOfFirstEmptyCell produces the slot number of the first empty cell in the current level of a box, and returns zero if there are no
empty cells at this level.

In[6]:= indexOfFirstEmptyCellAbox_, base_E :=


indexOfFirstId@box, emptyId, 1, baseD;
indexOfFirstIdAbox_, id_, index_, base_E :=
If@index > base, 0,
If@contentsId@box, index, baseD === id, index,
indexOfFirstId@box, id, index + 1, baseDDD

IndexOfFirstNonPerfectSubBox
This examines cells in left-to-right order (order of increasing index). It stops and produces zero when it encounters a non-box.
AlgorithmOne003.nb 3

In[8]:= indexOfFirstNonPerfectSubBoxAbox_, base_E :=


indexOfFirstNPSBHelper@box, 1, baseD;
indexOfFirstNPSBHelperAbox_, index_, base_E :=
If@index > base, 0,
If@contentsId@box, index, baseD =!= boxId, 0,
If@Notž perfectlyFilledP@boxPindexT, baseD, index,
indexOfFirstNPSBHelper@box, index + 1, baseDDDD

AllSubBoxesHaveSameDepth

allSubBoxesHaveSameDepthAbox_, base_E :=
SameQ žž Table@depth@boxPiT, baseD, 8i, base<D
In[10]:=

IndexOfFirstBoxOfDifferentDepth

In[11]:= indexOffirstBoxOfDifferentDepthAbox_, base_E :=


indexOfFirstBODDHelper@box, depth@boxP1T, baseD, 2, baseD;
indexOfFirstBODDHelperAbox_, dep_, index_, base_E :=
If@index > base, Indeterminate,
If@depth@boxPindexT, baseD =!= dep, index,
indexOfFirstBODDHelper@box, dep, index + 1, baseDDD

InsertBead

In[13]:= insertBeadAbox_, bead_, base_E := Module@8


e = indexOfFirstEmptyCell@box, baseD,
b = indexOffirstBoxOfDifferentDepth@box, baseD,
n = indexOfFirstNonPerfectSubBox@box, baseD<,
If@e =!= 0, ReplacePart@box, e ® beadD,
If@n =!= 0,
H* we have a non-perfect sub-box at position n *L
ReplacePart@box, n ® insertBead@boxPnT, bead, baseDD,
H* no non-perfect boxes to fill *L
If@allSubBoxesHaveSameDepth@box, baseD,
ReplacePart@box, 1 ® newRepBox@boxP1T, bead, baseDD,
ReplacePart@box, b ® newRepBox@boxPbT, bead, baseDDD
DDD;
newBoxAbase_E := ConstantArray@0, baseD;
newRepBoxAone_, two_, base_E :=
ReplacePart@newBox@baseD, 81 ® one, 2 ® two<D

Algorithm1 is boxC
4 AlgorithmOne003.nb

Algorithm1 is boxC

In[16]:= boxCAn_, b_E := Fold@insertBead@ð1, ð2, bD &, newBox@bD, Range@nDD

Exercise: Speed it Up
This is much slower than Algorithm 2. Find out why and fix it.

Visualization
Box-Drawing Primitive

In[17]:= vhBoxAverticalP_, base_, xs_, y_: ""E :=


Style@Grid@If@verticalP,
List ž PadRight@xs, base, yD,
ListžPadRight@xs, base, yDD, Frame ® AllD, TinyD

In[18]:= boxPlotAboxes_, base_E := boxPlotHelper@False, base, boxesD;


boxPlotHelperAverticalP_, base_, boxes_E :=
vhBox@verticalP, base,
Map@If@Head@ðD === List,
boxPlotHelper@Not@verticalPD, base, ðD, ðD &, boxesDD;
Remember that a bead of value 0 denotes an empty box.

In[19]:= boxPlot@boxC@29, 4D, 4D

1 5 6 7
2 8 9 10 3 4
17 20 21 22
29 11 14
Out[19]= 12 15
18 23 24 25 0
0 13 16
19 26 27 28

Algorithm 2: Top-Down Perfectly Filled Boxes

From prior blogs.

Construction
Find the biggest power of b less than N and call it clumpSize. The number of clumps of size clumpSize is less than N by construc-
tion, and each clump fills a perfectly filled box. Take away most beads by putting them in multiples of clumpSize into perfectly filled
boxes. Distribute the remaining beads, which must be fewer than clumpSize, by putting as many at top level as possible and using
at most one cell for nested boxes.
The two algorithms produce nested boxes in different orders. Algorithm 1 does not return to the prior level until it has produced a
perfectly filled box of the next greater depth. Algorithm 2 greedily produces perfectly filled boxes of maximal depth at the current
level. Do they always produce the same number of nested boxes?

Algorithm2 is boxB
AlgorithmOne003.nb 5

Algorithm2 is boxB

In[20]:= boxBAn_, base_E := boxBHelper@n, 0, baseD;


boxBHelperAn_, zRelative_, base_E :=
H* If number n of beads is less than or
equal to the number b of cells in a single box, *L
If@n £ base,
H* then just fill it as much as possible. *L
Table@If@i £ n, i + zRelative, 0D, 8i, base<D,
H* Otherwise, nest some boxes. *L
Module@8e, clumpSize, nPreClumps, preClumpsTotal, lastClump<,
H* This is the largest exponent such that be < n *L
e = Ceiling@Log@base, nD - 1D;
clumpSize = base^e;
H* The following is the number of
clumps we may put in perfectly filled boxes. *L
nPreClumps = n~Quotient~clumpSize;
H* The following is the total
number of beads in perfectly filled boxes. *L
preClumpsTotal = clumpSize * nPreClumps;
H* The following is the number of remaining beads. *L
lastClump = n - preClumpsTotal;
H* Build the perfectly filled boxes: *L
With@8pre = Table@boxBHelper@
clumpSize, zRelative + Hi - 1L * clumpSize, baseD,
8i, nPreClumps<D<,
H* If there are remaining beads *L
If@lastClump ¹ 0,
H* There are b - nPreClumps empty cells remaining at this
level. If that is enough room for lastClump beads: *L
If@lastClump £ base - nPreClumps,
H* Put all remaining beads at this level *L
6 AlgorithmOne003.nb

H* Put all remaining beads at this level *L


pre~Join~Table@If@i £ lastClump,
zRelative + preClumpsTotal + i, 0D, 8i, base<D,
H* Otherwise,
there are too many remaining beads for this level.
There will certainly be a recursive box; the number
of cells remaining at this level is one fewer than b-
nPreClumps accounting for the one cell taken up by the
recursive box. Put as many as possible at this level. *L
With@8nRemainingCells = base - nPreClumps - 1<,
H* Filling up all remaining
cells at this level with single beads and
putting the leftovers in a nested box. *L
Hpre~ Append ~ boxBHelper@lastClump - nRemainingCells,
zRelative + preClumpsTotal, baseDL~Join~
Table@If@i £ nRemainingCells,
zRelative + preClumpsTotal +
lastClump - nRemainingCells + i, 0D, 8i, base<DDD,
H* The following is the other branch of the top
If: The last clump is zero; we're done. *L
pre~Join~Table@0, 8i, nPreClumps + 1, base<DDDDD;

Experiments
boxD (for "display" is the two algorithms displayed, side-by-side, for all to see

In[21]:= boxDAn_, b_E := 8boxPlot@boxB@n, bD, bD, boxPlot@boxC@n, bD, bD<

If you have Mathematica, here is an interactive gadget:


AlgorithmOne003.nb 7

Manipulate@boxD@Flooržn, bD,
8b, 2, 10, 1<, 8e, 3, 10, 1<, 8n, 0, b^e, 1<D
In[22]:=

n
Out[22]=
70

1 2 3 4 5 26 27 28 29 30 51 52 53 54 55 1 6 7 8 9 2 10 11 12 13
3 14 15 16 17
4 5
6 7 8 9 10 31 32 33 34 35 26 30 31 32 33 46 50 51 52 53

: >
56 57 58 59 60
66 70 0 0 0 18 22
11 12 13 14 15 36 37 38 39 40 61 62 63 64 65 69 70 , 27 34 35 36 37 47 54 55 56 57
67
19 23
20 24
16 17 18 19 20 41 42 43 44 45 66 67 0 0 0 28 38 39 40 41 48 58 59 60 61 68
21 25
69
21 22 23 24 25 46 47 48 49 50 68 29 42 43 44 45 49 62 63 64 65

Some unit tests


No waste in the binary case, number of boxes one fewer than the number of beads.

In[23]:= 8boxD@ð, 2D< & ž Range@0, 16D

9 0 0 , 0 0 =
9 1 0 , 1 0 =
9 1 2 , 1 2 =

: >
1 1
2
3 , 3
2

: >
1 3 1 2
2 4
, 3 4

: >
1 2 1 3 2
5 , 4
3 4 5

: >
1 2 1 3
5 2
6
, 4
3 4 5 6

Out[23]=
8 AlgorithmOne003.nb

: , >
3 4 5 6

: >
1 2 5 6 1 3 2 4
,
3 4 7 5 6 7

: >
1 2 5 6 1 3 2 4
,
3 4 7 8 5 6 7 8

1 3

: >
2 4 1 5
2 4
9 , 3 6
5 7 7 8
9
6 8

1 3

: >
2 4 1 5
2 4
9 3 6
10
,
5 7 7 8
9 10
6 8

Out[23]=
1 3 1 5

: >
2 4 3 6 2 4
9 10
,
5 7 11 9 7 8
11
6 8 10

1 3 1 5

: >
2 4 9 10 3 6 2 4
,
5 7 11 12 9 11 7 8

6 8 10 12

1 3 1 5

: >
2 4 9 11 3 6 2 7
10 12 , 4 8
5 7 9 11
13 13
6 8 10 12

1 3 1 5

: >
2 4 9 11 3 6 2 7
10 12 4 8
,
5 7 9 11
13 14 13 14
6 8 10 12

1 3 9 11 1 5 2 7

: >
2 4 10 12 3 6 4 8
,
5 7 13 9 11 13
15 15
6 8 14 10 12 14

1 3 9 11 1 5 2 7

: >
2 4 10 12 3 6 4 8
,
5 7 13 15 9 11 13 15
6 8 14 16 10 12 14 16
AlgorithmOne003.nb 9

: , >
6 8 14 16 10 12 14 16

In[24]:= boxD@ð, 3D & ž Range žž 80, 19<

0 0 0 0 0 0

1 0 0 1 0 0

1 2 0 1 2 0

1 2 3 1 2 3

1 1
2 4 0 4 2 3
3 0

1 1
2 4 5 4 2 3
3 5

1 4 1 2
2 5 0 4 6 3
3 6 5 0

1 4 1 2
2 5 7 4 6 3
3 6 5 7

1 4 7 1 2 3
2 5 8 4 6 8
3 6 0 5 7 0

1 4 7 1 2 3
2 5 8 4 6 8
3 6 9 5 7 9

1 2 3
1 4 5 2 3
4 5 6 10 0 6 8
10
0 7 9
7 8 9

1 2 3
1 4 5 2 3
4 5 6 10 11 6 8
10
11 7 9
7 8 9

Out[24]= 1 2 3 1 4 5
10 2 3
4 5 6 11 12 10 12 0 6 8
0 7 9
7 8 9 11

1 2 3 1 4 5
10 2 3
4 5 6 11 13 10 12 13 6 8
12 7 9
7 8 9 11

1 2 3 1 4 5
10 11 12 2 3
4 5 6 14 10 12 13 6 8
13
10 AlgorithmOne003.nb

4 5 6 14 10 12 13 6 8
13
0 7 9
7 8 9 11 14 0

1 2 3 1 4 5
10 11 12 2 3
4 5 6 15 10 12 13 6 8
13
14 7 9
7 8 9 11 14 15

1 2 3 10 11 12 1 4 5
2 6 7 3
4 5 6 13 14 15 16 10 12 13 8
16
0 9
7 8 9 0 11 14 15

1 2 3 10 11 12 1 4 5
2 6 7 3
4 5 6 13 14 15 17 10 12 13 8
16
17 9
7 8 9 16 11 14 15

1 2 3 10 11 12 1 4 5 2 6 7
3
4 5 6 13 14 15 0 10 12 13 16 18 0 8
9
7 8 9 16 17 18 11 14 15 17

1 2 3 10 11 12 1 4 5 2 6 7
3
4 5 6 13 14 15 19 10 12 13 16 18 19 8
9
7 8 9 16 17 18 11 14 15 17

In[25]:= boxD@ð, 4D & ž Range@2, 49D

1 2 0 0 1 2 0 0

1 2 3 0 1 2 3 0

1 2 3 4 1 2 3 4

1 1
2 5
5 0 0 2 3 4
3 0
4 0

1 1
2 5
5 6 0 2 3 4
3 6
4 0

1 1
2 5
5 6 7 2 3 4
3 6
4 7

1 5 1 2
2 6 5 8
0 0 3 4
3 7 6 0
4 8 7 0

1 5 1 2
2 6 5 8
AlgorithmOne003.nb 11

2 6 5 8
9 0 3 4
3 7 6 9
4 8 7 0

1 5 1 2
2 6 5 8
9 10 3 4
3 7 6 9
4 8 7 10

1 5 9 1 2 3
2 6 10 5 8 11
11 4
3 7 0 6 9 0
4 8 0 7 10 0

1 5 9 1 2 3
2 6 10 5 8 11
0 4
3 7 11 6 9 12
4 8 12 7 10 0

1 5 9 1 2 3
2 6 10 5 8 11
13 4
3 7 11 6 9 12
4 8 12 7 10 13

1 5 9 13 1 2 3 4
2 6 10 14 5 8 11 14
3 7 11 0 6 9 12 0
4 8 12 0 7 10 13 0

1 5 9 13 1 2 3 4
2 6 10 14 5 8 11 14
3 7 11 15 6 9 12 15
4 8 12 0 7 10 13 0

1 5 9 13 1 2 3 4
2 6 10 14 5 8 11 14
3 7 11 15 6 9 12 15
4 8 12 16 7 10 13 16

1 2 3 4
1 5 6 7 2 3 4
5 6 7 8
17 8 11 14
17 0 0
9 10 11 12 0 9 12 15

0 10 13 16
13 14 15 16

1 2 3 4
1 5 6 7 2 3 4
5 6 7 8
17 8 11 14
17 18 0
9 10 11 12 18 9 12 15

0 10 13 16
13 14 15 16

1 2 3 4
1 5 6 7 2 3 4
5 6 7 8
17 8 11 14
17 18 19
9 10 11 12 18 9 12 15

19 10 13 16
13 14 15 16

1 2 3 4
1 5 6 7
17 2 3 4
5 6 7 8
18 8 11 14
12 AlgorithmOne003.nb

5 6 7 8
18 17 20 0 0 8 11 14
19 20
9 10 11 12 0 9 12 15
18
0 10 13 16
13 14 15 16 19

1 2 3 4
1 5 6 7
17 2 3 4
5 6 7 8
18 17 20 21 0 8 11 14
20 21
9 10 11 12 19 9 12 15
18
0 10 13 16
13 14 15 16 19

1 2 3 4
1 5 6 7
17 2 3 4
5 6 7 8
18 17 20 21 22 8 11 14
21 22
9 10 11 12 19 9 12 15
18
20 10 13 16
13 14 15 16 19

1 2 3 4 1 5 6 7
17 18 19 20 2 3 4
5 6 7 8 17 20 21 22
21 8 11 14
22 23
9 10 11 12 0 9 12 15
18 23 0 0
0 10 13 16
13 14 15 16 19

1 2 3 4 1 5 6 7
17 18 19 20 2 3 4
5 6 7 8 17 20 21 22
21 8 11 14
23 24
9 10 11 12 22 9 12 15
18 23 24 0
0 10 13 16
13 14 15 16 19

1 2 3 4 1 5 6 7
17 18 19 20 2 3 4
5 6 7 8 17 20 21 22
21 8 11 14
24 25
9 10 11 12 22 9 12 15
18 23 24 25
23 10 13 16
13 14 15 16 19

1 2 3 4 1 5 6 7
17 18 19 20
2 3 4
5 6 7 8 17 20 21 22
21 22 23 24 8 11 14
25 26
9 10 11 12 18 23 24 25 9 12 15
0
10 13 16
13 14 15 16 0 19 26 0 0

1 2 3 4 1 5 6 7
17 18 19 20
2 3 4
5 6 7 8 17 20 21 22
21 22 23 24 8 11 14
26 27
9 10 11 12 18 23 24 25 9 12 15
25
10 13 16
13 14 15 16 0 19 26 27 0

1 2 3 4 1 5 6 7
17 18 19 20
2 3 4
5 6 7 8 17 20 21 22
21 22 23 24 8 11 14
27 28
9 10 11 12 18 23 24 25 9 12 15
25
10 13 16
13 14 15 16 26 19 26 27 28
Out[25]=

1 2 3 4 17 18 19 20 1 5 6 7
2 8 9 10 3 4
AlgorithmOne003.nb 13
Out[25]=

2 8 9 10 3 4
5 6 7 8 21 22 23 24 17 20 21 22
29 11 14
28 29
9 10 11 12 18 23 24 25 0 12 15
25 26 0 0
0 13 16
13 14 15 16 27 19 26 27 28

1 2 3 4 17 18 19 20 1 5 6 7
2 8 9 10 3 4
5 6 7 8 21 22 23 24 17 20 21 22
29 11 14
29 30
9 10 11 12 18 23 24 25 30 12 15
25 26 27 28
0 13 16
13 14 15 16 0 19 26 27 28

1 2 3 4 17 18 19 20 1 5 6 7
2 8 9 10 3 4
5 6 7 8 21 22 23 24 17 20 21 22
29 11 14
30 31
9 10 11 12 18 23 24 25 30 12 15
25 26 27 28
31 13 16
13 14 15 16 29 19 26 27 28

1 2 3 4 17 18 19 20 1 5 6 7
2 8 9 10
3 4
5 6 7 8 21 22 23 24 17 20 21 22
29 32 0 0 11 14
0 0
9 10 11 12 25 26 27 28 18 23 24 25 12 15
30
13 16
13 14 15 16 29 30 31 32 19 26 27 28 31

1 2 3 4 17 18 19 20 1 5 6 7
2 8 9 10
3 4
5 6 7 8 21 22 23 24 17 20 21 22
29 32 33 0 11 14
33 0
9 10 11 12 25 26 27 28 18 23 24 25 12 15
30
13 16
13 14 15 16 29 30 31 32 19 26 27 28 31

1 2 3 4 17 18 19 20 1 5 6 7
2 8 9 10
3 4
5 6 7 8 21 22 23 24 17 20 21 22
29 32 33 34 11 14
33 34
9 10 11 12 25 26 27 28 18 23 24 25 12 15
30
13 16
13 14 15 16 29 30 31 32 19 26 27 28 31

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 3 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
34 11 14
35
9 10 11 12 25 26 27 28 0 18 23 24 25 12 15
30 35 0 0
0 13 16
13 14 15 16 29 30 31 32 19 26 27 28 31

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 3 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
34 11 14
36
9 10 11 12 25 26 27 28 35 18 23 24 25 12 15
30 35 36 0
0 13 16
13 14 15 16 29 30 31 32 19 26 27 28 31

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 3 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
34 11 14
37
9 10 11 12 25 26 27 28 35 18 23 24 25 12 15
30 35 36 37
36 13 16
13 14 15 16 29 30 31 32 19 26 27 28 31

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
14 AlgorithmOne003.nb

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 34 35 36 3 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
37 11 14
38
9 10 11 12 25 26 27 28 0 18 23 24 25 30 35 36 37 12 15

0 13 16
13 14 15 16 29 30 31 32 19 26 27 28 31 38 0 0

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 34 35 36 3 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
37 11 14
39
9 10 11 12 25 26 27 28 38 18 23 24 25 30 35 36 37 12 15

0 13 16
13 14 15 16 29 30 31 32 19 26 27 28 31 38 39 0

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 34 35 36 3 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
37 11 14
40
9 10 11 12 25 26 27 28 38 18 23 24 25 30 35 36 37 12 15

39 13 16
13 14 15 16 29 30 31 32 19 26 27 28 31 38 39 40

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 34 35 36 3 11 12 13 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
37 38 39 40 41 14
41
9 10 11 12 25 26 27 28 18 23 24 25 30 35 36 37 0 15
0
0 16
13 14 15 16 29 30 31 32 0 19 26 27 28 31 38 39 40

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 34 35 36 3 11 12 13 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
37 38 39 40 41 14
42
9 10 11 12 25 26 27 28 18 23 24 25 30 35 36 37 42 15
41
0 16
13 14 15 16 29 30 31 32 0 19 26 27 28 31 38 39 40

1 2 3 4 17 18 19 20 1 5 6 7 2 8 9 10
33 34 35 36 3 11 12 13 4
5 6 7 8 21 22 23 24 17 20 21 22 29 32 33 34
37 38 39 40 41 14
43
9 10 11 12 25 26 27 28 18 23 24 25 30 35 36 37 42 15
41
43 16
13 14 15 16 29 30 31 32 42 19 26 27 28 31 38 39 40

1 2 3 4 17 18 19 20 33 34 35 36 1 5 6 7 2 8 9 10
3 11 12 13
4
5 6 7 8 21 22 23 24 37 38 39 40 17 20 21 22 29 32 33 34
41 44 0 0 14
44
9 10 11 12 25 26 27 28 18 23 24 25 30 35 36 37 15
41 42 0 0 42
16
13 14 15 16 29 30 31 32 43 19 26 27 28 31 38 39 40 43

1 2 3 4 17 18 19 20 33 34 35 36 1 5 6 7 2 8 9 10
3 11 12 13
4
5 6 7 8 21 22 23 24 37 38 39 40 17 20 21 22 29 32 33 34
41 44 45 0 14
45
9 10 11 12 25 26 27 28 18 23 24 25 30 35 36 37 15
41 42 43 44 42
16
13 14 15 16 29 30 31 32 0 19 26 27 28 31 38 39 40 43

1 2 3 4 17 18 19 20 33 34 35 36 1 5 6 7 2 8 9 10
3 11 12 13
4
5 6 7 8 21 22 23 24 37 38 39 40 17 20 21 22 29 32 33 34
41 44 45 46 14
46
9 10 11 12 25 26 27 28 18 23 24 25 30 35 36 37 15
41 42 43 44 42
16
13 14 15 16 29 30 31 32 45 19 26 27 28 31 38 39 40 43
AlgorithmOne003.nb 15

1 2 3 4 17 18 19 20 33 34 35 36 1 5 6 7 2 8 9 10 3 11 12 13
4
5 6 7 8 21 22 23 24 37 38 39 40 17 20 21 22 29 32 33 34 41 44 45 46 14
47
9 10 11 12 25 26 27 28 41 42 43 44 18 23 24 25 30 35 36 37 15
42 47 0 0
16
13 14 15 16 29 30 31 32 45 46 0 0 19 26 27 28 31 38 39 40 43

1 2 3 4 17 18 19 20 33 34 35 36 1 5 6 7 2 8 9 10 3 11 12 13
4
5 6 7 8 21 22 23 24 37 38 39 40 17 20 21 22 29 32 33 34 41 44 45 46 14
0
9 10 11 12 25 26 27 28 41 42 43 44 18 23 24 25 30 35 36 37 15
42 47 48 0
16
13 14 15 16 29 30 31 32 45 46 47 48 19 26 27 28 31 38 39 40 43

1 2 3 4 17 18 19 20 33 34 35 36 1 5 6 7 2 8 9 10 3 11 12 13
4
5 6 7 8 21 22 23 24 37 38 39 40 17 20 21 22 29 32 33 34 41 44 45 46 14
49
9 10 11 12 25 26 27 28 41 42 43 44 18 23 24 25 30 35 36 37 15
42 47 48 49
16
13 14 15 16 29 30 31 32 45 46 47 48 19 26 27 28 31 38 39 40 43

Exercises: Proofs
Prove that both algorithms never leave more than two cells empty.

Prove that the total number of boxes used is the same for the two algorithms.

Vous aimerez peut-être aussi