• Aucun résultat trouvé

T

N/A
N/A
Protected

Academic year: 2022

Partager "T"

Copied!
65
0
0

Texte intégral

(1)

COMPUTER PROGRAMMING

PRE-FASCICLE 3A

A DRAFT OF SECTION 7.2.1.3:

GENERATING ALL COMBINATIONS

DONALD E.KNUTH StanfordUniversity

ADDISON{WESLEY

6

77

(2)

urrent informationaboutthisbookandrelatedbooks.

Seealsohttp://www-s-faulty. sta nf or d. ed u/~ kn ut h/ sg b.h tm lforinformation

aboutTheStanfordGraphBase,inludingdownloadablesoftwarefordealingwiththe

graphsusedinmanyoftheexamplesinChapter7.

Seealsohttp://www-s-faulty. sta nf or d. ed u/~ kn ut h/ mm ixw ar e. ht ml fordown-

loadablesoftwaretosimulatetheMMIXomputer.

Copyright

2002byAddison{Wesley

Allrightsreserved.Nopartofthispubliationmaybereprodued,storedinaretrieval

system,ortransmitted,inanyform, orby anymeans,eletroni, mehanial,photo-

opying, reording, or otherwise, without the prior onsent of the publisher, exept

that the oÆial eletroni le may be used to print single opies for personal (not

ommerial)use.

Zerothprinting(revision11),31Marh2005

(3)

[TheArt of Combinations℄ has a relation

toalmost everyspeies of useful knowledge

that themind ofman an beemployed upon.

| JAMES BERNOULLI, Ars Conjetandi (1713)

This booklet ontains draft material that I'm irulating to experts in the

eld, in hopes that they an help remove its most egregious errors before too

many other people see it. I am also, however, posting it on the Internet for

ourageous and/or random readers who don't mind the risk of reading a few

pagesthathavenotyetreahedaverymaturestate. Beware: Thismaterialhas

notyetbeenproofreadasthoroughlyasthemanusriptsofVolumes1,2,and3

were at the time of theirrst printings. And those arefully-hekedvolumes,

alas,were subsequentlyfoundtoontainthousandsofmistakes.

Giventhisaveat, Ihopethatmyerrorsthistime willnotbesonumerous

and/orobtrusivethatyouwillbedisouragedfromreadingthematerialarefully.

Ididtrytomakeitbothinterestingandauthoritative,asfarasitgoes. Butthe

eldissovast,Iannothopetohavesurroundeditenoughtoorralitompletely.

ThereforeIbegyoutoletmeknowaboutanydeieniesyoudisover.

Toputthematerialinontext,thisisSetion7.2.1.3ofalong,longhapter

onombinatorialalgorithms. Chapter7willeventuallyllthreevolumes(namely

Volumes 4A, 4B, and 4C), assuming that I'm able to remain healthy. It will

begin with a short review of graph theory, with emphasis on some highlights

ofsigniantgraphsinTheStanfordGraphBase, from whihI willbedrawing

manyexamples. Then omesSetion 7.1, whih deals withthetopi of bitwise

manipulations. (Idraftedabout60pagesaboutthat subjetin1977,but those

pages need extensive revision; meanwhile I've deided to work for awhile on

the material that follows it, so that I an get a better feel for how muh to

ut.) Setion7.2isaboutgeneratingallpossibilities,anditbegins withSetion

7.2.1: Generating Basi Combinatorial Patterns|whih, in turn, begins with

Setion 7.2.1.1, \Generating all n-tuples,"and Setion 7.2.1.2, \Generatingall

permutations." (Readersof the present booklet should havealready looked at

those setions, drafts of whih are availableas Pre-Fasiles2A and 2B.) The

stage isnow setfor themain ontentsof thisbooklet, Setion7.2.1.3: \Gener-

atingallombinations." Thenwillome Setion7.2.1.4 (aboutpartitions), et.

Setion 7.2.2willdealwith baktrakingingeneral. And soit willgo on,ifall

goeswell;an outlineoftheentireChapter7 as urrently envisagedappearson

thetaopwebpagethatisitedonpageii.

(4)

Eventheapparentlylowlytopi ofombinationgeneration turnsouttobe

surprisingly rih, with ties to Setions 1.2.1, 1.2.4, 1.2.6, 2.3.2, 2.3.4.2, 3.4.2,

4.3.2, 4.6.1, 4.6.2, 5.1.2, 5.4.1, 5.4.2, 6.1, and 6.3 of the rst three volumes.

Istronglybelieveinbuildinguparmfoundation,soIhavedisussedthistopi

muh more thoroughlythanI will beableto dowith materialthat isneweror

lessbasi. To mysurprise,Iame upwith110 exerises,eventhough|believe

itornot|Ihadtoeliminatequiteabitoftheinterestingmaterialthatappears

inmyles.

Someofthethingspresentedarenew,tothebestofmyknowledge,although

Iwillnotbeatallsurprisedto learnthatmyownlittle\disoveries"havebeen

disoveredbefore. Pleaselook,forexample,attheexerisesthat I'velassedas

researhproblems(ratedwithdiÆultylevel46orhigher),namelyexerises53,

56, 67, and 83; I've also impliitly posed additional unsolved questions in the

answers to exerises 59, 63, 101,105, and 109. Are those problemsstill open?

Pleaseletmeknowifyouknowofasolutiontoanyoftheseintriguingquestions.

Andofourseifnosolutionisknowntodaybutyoudomakeprogressonanyof

theminthefuture,I hopeyou'll letmeknow.

I urgently need your help also with respet to some exerises that I made

upasIwaspreparingthismaterial. Iertainlydon'tliketogetreditforthings

thathavealreadybeenpublishedbyothers, andmostoftheseresultsarequite

natural \fruits" that were just waiting to be \pluked." Therefore please tell

meifyouknow whoI should haveredited,with respetto the ideas foundin

exerises9, 18, 19,20, 26, 27, 28, 30,31, 32, 33, 34, 35,36, 37, 41, 42, 43, 44,

45,48,51,59,62,63,64,65,66,69,79,82(b{f),85,86,87,93,and/or110.

Ishallhappilypayander'sfeeof$2.56foreaherrorinthisdraftwhenitis

rstreportedtome,whetherthaterrorbetypographial,tehnial,orhistorial.

ThesamerewardholdsforitemsthatIforgottoputintheindex. Andvaluable

suggestions forimprovementsto the text areworth 32/eah. (Furthermore, if

younda bettersolutiontoanexerise,I'llatuallyrewardyouwithimmortal

gloryinsteadofmeremoney,bypublishingyournameintheeventualbook: )

Cross referenes to yet-unwritten material sometimes appear as `00'; this

impossiblevalueisaplaeholderfortheatualnumberstobesuppliedlater.

Happyreading!

Stanford,California D.E.K.

13June 2002

(5)

7.2.1.3. Generatingallombinations. Combinatorialmathematisisoften

desribed as \the study of permutations, ombinations, et.," so we turn our

attentionnow to ombinations. A ombination of n things, takent ata time,

oftenalledsimplyat-ombinationofnthings,isawaytoseletasubsetofsizet

fromagivensetofsizen. WeknowfromEq.1.2.6{(2)thatthereareexatly n

t

ways to dothis; andwelearned inSetion 3.4.2 howto hoose t-ombinations

atrandom.

Seleting t of n objets is equivalent to hoosing the n t elements not

seleted. Wewillemphasizethissymmetrybyletting

n = s+ t (1)

throughoutourdisussion,andwewilloftenrefertoat-ombinationofnthings

as an \(s;t)-ombination." Thus, an (s;t)-ombination is a way to subdivide

s+t objetsintotwoolletionsof sizessandt.

If Iaskhowmanyombinations of 21anbetaken outof 25,

Ido ineetaskhowmanyombinations of 4maybetaken.

Fortherearejustasmanyways oftaking21asthere areofleaving4.

|AUGUSTUS DEMORGAN, AnEssayonProbabilities (1838)

There are two main waysto represent(s;t)-ombinations: We an listthe

elements

t :::

2

1

that havebeenseleted,orweanworkwithbinary strings

a

n 1 :::a

1 a

0

forwhih

a

n 1

++a

1 +a

0

= t: (2)

Thelatterrepresentationhass0sand t1s, orrespondingto elementsthat are

unseletedor seleted. Thelistrepresentation

t :::

2

1

tendsto workoutbest

ifwelettheelementsbemembersofthesetf0;1;:::;n 1gandifwelistthem

indereasing order:

n>

t

>>

2

>

1

0: (3)

Binarynotationonnetsthese two representationsniely,beausetheitemlist

t :::

2

1

orrespondsto thesum

2 t

++2 2

+2 1

= n 1

X

k=0 a

k 2

k

= (a

n 1 :::a

1 a

0 )

2

: (4)

(6)

Of ourse we ould also list the positions b

s :::b

2 b

1

of the 0s ina

n 1 :::a

1 a

0 ,

where

n>b

s

>>b

2

>b

1

0: (5)

Combinations are important not only beause subsets are omnipresent in

mathematisbutalsobeausetheyareequivalenttomanyotherongurations.

For example, every (s;t)-ombination orresponds to a ombination of s+1

thingstakentatatimewithrepetitionspermitted,alsoalledamultiombination,

namelya sequeneofintegersd

t :::d

2 d

1 with

sd

t

d

2 d

1

0: (6)

Onereasonisthatd

t :::d

2 d

1

solves(6)ifandonlyif

t :::

2

1

solves(3),where

t

=d

t

+t 1; :::;

2

=d

2

+1;

1

=d

1

(7)

(seeexerise 1.2.6{60). Andthereis anotherusefulwayto relateombinations

withrepetitiontoordinaryombinations,suggestedbySolomonGolomb[AMM

75(1968),530{531℄,namelytodene

e

j

=

j

; if

j s;

e

j s

; if

j

>s.

(8)

In this form the numbers e

t :::e

1

don't neessarily appear in desending or-

der, but the multiset fe

1

;e

2

;:::;e

t

g is equal to f

1

;

2

;:::;

t

g if and only if

fe

1

;e

2

;:::;e

t

g isaset. (See Table1andexerise 1.)

An(s;t)-ombinationisalsoequivalenttoaomposition ofn+1intot+1

parts,namelyanorderedsum

n+1 = p

t

++p

1 +p

0

; wherep

t

;:::;p

1

;p

0

1. (9)

Theonnetionwith(3)isnow

p

t

=n

t

; p

t 1

=

t

t 1

; :::; p

1

=

2

1

; p

0

=

1

+1: (10)

Equivalently,ifq

j

=p

j

1,wehave

s = q

t

++q

1 +q

0

; whereq

t

;:::;q

1

;q

0

0, (11)

aomposition ofsintot+1nonnegative parts,relatedto(6)bysetting

q

t

=s d

t

; q

t 1

=d

t d

t 1

; :::; q

1

=d

2 d

1

; q

0

=d

1

: (12)

Furthermore it is easy to see that an (s;t)-ombination is equivalent to a

path of length s+t from orner to orner of an st grid, beause suh a

path ontainss vertial steps and t horizontal steps. Thus, ombinations an

be studied in at least eight dierent guises. Table 1 illustrates all 6

3

= 20

possibilitiesintheases=t=3.

Theseousinsofombinationsmightseemratherbewilderingatrstglane,

but most of them an be understood diretly from the binary representation

a

n 1 :::a

1 a

0

. Consider,forexample,the\random"bitstring

a

23 :::a

1 a

0

= 011001001000011111101101; (13)

(7)

Table1

THE (3;3)-COMBINATIONS AND THEIR EQUIVALENTS

a5a4a3a2a1a0 b3b2b1 321 d3d2d1 e3e2e1 p3p2p1p0 q3q2q1q0 path

000111 543 210 000 210 4111 3000

001011 542 310 100 310 3211 2100

001101 541 320 110 320 3121 2010

001110 540 321 111 321 3112 2001

010011 532 410 200 010 2311 1200

010101 531 420 210 020 2221 1110

010110 530 421 211 121 2212 1101

011001 521 430 220 030 2131 1020

011010 520 431 221 131 2122 1011

011100 510 432 222 232 2113 1002

100011 432 510 300 110 1411 0300

100101 431 520 310 220 1321 0210

100110 430 521 311 221 1312 0201

101001 421 530 320 330 1231 0120

101010 420 531 321 331 1222 0111

101100 410 532 322 332 1213 0102

110001 321 540 330 000 1141 0030

110010 320 541 331 111 1132 0021

110100 310 542 332 222 1123 0012

111000 210 543 333 333 1114 0003

whih has s=11zeros and t=13 ones, henen=24. Thedual ombination

b

s :::b

1

liststhepositionsofthezeros,namely

2320191716141312114 1;

beause the leftmost position is n 1 and the rightmost is 0. The primal

ombination

t :::

1

liststhepositionsoftheones, namely

222118151098 76 53 20:

Theorrespondingmultiombinationd

t :::d

1

liststhenumberof0stotheright

ofeah 1:

10108 62 22 222 11 0:

Theompositionp

t :::p

0

liststhedistanesbetweenonseutive1s,ifweimagine

additional1s attheleftandtheright:

2 13 351 11 11 21 21:

Andthenonnegativeompositionq

t :::q

0

ountshowmany0sappearbetween

\feneposts"representedby1s:

10 22 400 00 01 01 0;

thuswehave

a

n 1 :::a

1 a

0

=0 qt

10 qt

1

1:::10 q1

10 q0

: (14)

ThepathsinTable1alsohaveasimpleinterpretation(seeexerise2).

(8)

Lexiographi generation. Table 1 shows ombinations a

n 1 :::a

1 a

0 and

t :::

1

inlexiographiorder, whih isalsothelexiographi orderof d

t :::d

1 .

Notiethatthedual ombinationsb

s :::b

1

andtheorrespondingompositions

p

t :::p

0 ,q

t :::q

0

thenappearinreverse lexiographiorder.

Lexiographiorder usually suggests the most onvenientway to generate

ombinatorial ongurations. Indeed, Algorithm 7.2.1.2L already solves the

problem for ombinations in the form a

n 1 :::a

1 a

0

, sine (s;t)-ombinations

inbitstringformarethesameaspermutationsofthemultisetfs0;t1g. That

general-purposealgorithmanbestreamlinedinobviouswayswhenitisapplied

to this speial ase. (See also exerise 7.1{00, whih presents a remarkable

sequeneofsevenbitwiseoperationsthatwill onvert anygivenbinarynumber

(a

n 1 :::a

1 a

0 )

2

to the lexiographially next t-ombination, assuming that n

doesnotexeedtheomputer'swordlength.)

Let'sfous,however,ongeneratingombinationsintheotherprinipalform

t :::

2

1

,whihismorediretlyrelevanttothewaysinwhihombinationsare

oftenneeded, and whih is more ompat than the bit strings when t is small

omparedton. Intherstplaeweshouldkeepinmindthatasimplesequene

ofnestedloopswill dothe job nielywhent is very small. For example,when

t=3thefollowinginstrutionssuÆe:

For

3

=2,3,:::,n 1(in thisorder)dothefollowing:

For

2

=1,2,:::,

3

1(inthisorder)dothefollowing:

For

1

=0,1,:::,

2

1 (inthisorder)dothefollowing:

Visit theombination

3

2

1 .

(15)

(Seetheanalogoussituationin7.2.1.1{(3).)

On the other hand when t is variable or not so small, we an generate

ombinations lexiographially by following the general reipe disussed after

Algorithm7.2.1.2L,namelytondtherightmostelement

j

thatanbeinreased

and then to set the subsequent elements

j 1 :::

1

to their smallest possible

values:

Algorithm L (Lexiographi ombinations). This algorithm generates all t-

ombinations

t :::

2

1

of the n numbers f0;1;:::;n 1g, given n t 0.

Additionalvariables

t+1 and

t+2

areused assentinels.

L1.[Initialize.℄ Set

j

j 1 for1jt;alsoset

t+1

nand

t+2 0.

L2.[Visit.℄ Visittheombination

t :::

2

1 .

L3.[Findj.℄ Setj 1. Then,while

j +1=

j+1 ,set

j

j 1andj j+1;

repeatuntil

j

+16=

j+1 .

L4.[Done?℄ Terminatethealgorithmifj>t.

L5.[Inrease

j .℄ Set

j

j

+1and returnto L2.

The running time of this algorithm is not diÆult to analyze. Step L3 sets

j

j 1 just after visiting a ombination forwhih

j+1

=

1

+j, and the

numberofsuh ombinationsisthenumberofsolutionsto theinequalities

n>

t

>>

j+1

j; (16)

(9)

but this formula is equivalent to a (t j)-ombination of the n j objets

fn 1;:::;jg,sotheassignment

j

j 1oursexatly n j

t j

times. Summing

for1jttellsusthat theloopinstepL3isperformed

n 1

t 1

+

n 2

t 2

++

n t

0

=

n 1

s

+

n 2

s

++

s

s

=

n

s+1

(17)

timesaltogether,oranaverageof

n

s+1 .

n

t

=

n!

(s+1)!(t 1)!

.

n!

s!t!

= t

s+1

(18)

times pervisit. This ratio is less than 1 when t s, so Algorithm L is quite

eÆientinsuhases.

But the quantity t=(s+1) an be embarrassingly large when t is near n

ands issmall. Indeed,AlgorithmL oasionally sets

j

j 1 needlessly, at

timeswhen

j

alreadyequals j 1. Furthersrutinyreveals that we neednot

alwayssearhfortheindexjthatisneededinstepsL4andL5,sinetheorret

value of j an often be predited from the ations just taken. For example,

afterwehaveinreased

4

andreset

3

2

1

totheirstartingvalues210,thenext

ombination willinevitably inrease

3

. These observations leadto a tuned-up

version ofthealgorithm:

AlgorithmT(Lexiographiombinations). ThisalgorithmislikeAlgorithmL,

butfaster. It alsoassumes,foronveniene,thatt<n.

T1.[Initialize.℄ Set

j

j 1 for1j t;thenset

t+1 n,

t+2

0,and

j t.

T2.[Visit.℄ (Atthispointj isthesmallestindex suhthat

j+1

>j.) Visitthe

ombination

t :::

2

1

. Then,ifj>0,set x j andgoto stepT6.

T3.[Easyase?℄ If

1

+1<

2 ,set

1

1

+1andreturntoT2. Otherwiseset

j 2.

T4.[Findj.℄ Set

j 1

j 2 andx

j

+1. Ifx=

j+1

, setj j+1 and

repeatthisstepuntilx6=

j+1 .

T5.[Done?℄ Terminatethealgorithmifj>t.

T6.[Inrease

j .℄ Set

j

x,j j 1,andreturntoT2.

Now j =0 instep T2 ifand onlyif

1

>0, so theassignments instepT4 are

neverredundant. Exerise6 arriesoutaompleteanalysis ofAlgorithmT.

Notie that the parameter n appears only in the initialization steps L1

and T1, notinthe prinipalparts ofAlgorithms L and T. Thus wean think

oftheproessas generatingtherst n

t

ombinationsofaninnite list,whih

dependsonlyon t. This simpliationarises beausethe listof t-ombinations

forn+1thingsbeginswiththelistfornthings,underouronventions;wehave

beenusinglexiographiorderonthedereasingsequenes

t :::

1

forthisvery

reason,insteadofworkingwiththeinreasingsequenes

1 :::

t .

DerrikLehmer notied another pleasant property ofAlgorithms L and T

[AppliedCombinatorialMathematis,editedbyE.F.Bekenbah(1964),27{30℄:

(10)

TheoremL. Theombination

t :::

2

1

isvisitedafter exatly

t

t

++

2

2

+

1

1

(19)

otherombinationshavebeenvisited.

Proof. There are k

k

ombinations 0

t :::

0

2

0

1 with

0

j

=

j

fort j >k and

0

k

<

k

,namely

t :::

k+1

followedbythek-ombinationsoff0;:::;

k 1g.

Whent=3,forexample,thenumbers

2

3

+ 1

2

+ 0

1

; 3

3

+ 1

2

+ 0

1

; 3

3

+ 2

2

+ 0

1

; :::; 5

3

+ 4

2

+ 3

1

that orrespondto the ombinations

3

2

1

inTable1 simply run through the

sequene 0, 1, 2, :::, 19. Theorem L gives us a nie way to understand the

ombinatorial number system of degree t, whih represents every nonnegative

integerN uniquelyintheform

N =

n

t

t

++

n

2

2

+

n

1

1

; n

t

>>n

2

>n

1

0: (20)

[SeeErnestoPasal,GiornalediMatematihe 25(1887),45{49.℄

Binomialtrees. ThefamilyoftreesT

n

dened by

T

0

= ; T

n

=

T

0

T

1

T

n 1 :::

0 1 n 1

forn>0; (21)

arises in several important ontexts and sheds further light on ombination

generation. Forexample,T

4 is

0

0 0

0 0

0 0

0 1

1 1

1 2

2 3

; (22)

and T

5

, rendered more artistially, appears as the frontispiee to Volume 1 of

thisseriesofbooks.

NotiethatT

n

islikeT

n 1

,exeptforanadditionalopyofT

n 1

;therefore

T

n has2

n

nodesaltogether. Furthermore, thenumberofnodesonlevelt isthe

binomialoeÆient n

t

;thisfataountsforthename\binomialtree." Indeed,

thesequene of labels enountered onthe path from theroot to eah node on

leveltdenesaombination

t :::

1

,andallombinationsourinlexiographi

orderfromlefttoright. Thus,AlgorithmsLandTanberegardedasproedures

totraversethenodesonleveltofthebinomialtreeT

n .

(11)

TheinnitebinomialtreeT

1

isobtainedbylettingn!1in(21). Theroot

ofthis treehas innitelymanybranhes, but every nodeexept fortheoverall

rootatlevel0istherootofanitebinomialsubtree. Allpossiblet-ombinations

appearinlexiographiorderonleveltofT

1 .

Let'sgetmorefamiliarwithbinomialtreesbyonsideringallpossibleways

to pak a ruksak. More preisely, suppose we have n items that take up

respetivelyw

n 1 ,:::,w

1 ,w

0

unitsofapaity,where

w

n 1

w

1 w

0

; (23)

wewantto generateallbinaryvetorsa

n 1 :::a

1 a

0

suh that

aw=a

n 1 w

n 1

++a

1 w

1 +a

0 w

0

N; (24)

where N is thetotal apaity of a ruksak. Equivalently, we want to nd all

subsetsC off0;1;:::;n 1gsuhthatw(C)= P

2C w

N;suhsubsetswill

bealledfeasible. We willwrite a feasible subsetas

1 :::

t

, where

1

>>

t

0, numbering the subsripts dierently from the onvention of (3) above

beauset isvariableinthisproblem.

Every feasible subsetorresponds to a node ofT

n

, and ourgoalis to visit

eahfeasiblenode. Clearlytheparentofeveryfeasiblenodeisfeasible,andsois

theleftsibling,ifany;thereforeasimpletreeexplorationproedureworkswell:

Algorithm F (Fillinga ruksak). Thisalgorithm generatesallfeasibleways

1 :::

t

tollaruksak,givenw

n 1 ,:::,w

1 ,w

0

,andN. WeletÆ

j

=w

j w

j 1

for1j<n.

F1.[Initialize.℄ Sett 0,

0

n,andr N.

F2.[Visit.℄ Visittheombination

1 :::

t

,whihusesN runitsofapaity.

F3.[Tryto add w

0 .℄ If

t

>0 and rw

0

, set t t+1,

t

0,r r w

0 ,

andreturntoF2.

F4.[Try to inrease

t

.℄ Terminate ift = 0. Otherwise, if

t 1

>

t

+1 and

t +1

,set

t

t

+1,r r Æ

t

,andreturntoF2.

F5.[Remove

t

.℄ Setr r+w

t

,t t 1,andreturntoF4.

NotiethatthealgorithmimpliitlyvisitsnodesofT

n

inpreorder,skippingover

unfeasiblesubtrees. Anelement >0 is plaedinthe ruksak, ifit ts, just

aftertheproedurehasexploredallpossibilitiesusingelement 1initsplae.

Therunningtimeisproportionaltothenumberoffeasibleombinationsvisited

(seeexerise20).

Inidentally,thelassial\knapsakproblem"ofoperationsresearh isdif-

ferent: ItasksforafeasiblesubsetC suhthat v(C)= P

2C

v()ismaximum,

whereeahitemhasbeenassignedavaluev(). AlgorithmFisnotapartiu-

larlygoodwaytosolvethatproblem,beauseitoftenonsidersasesthatould

beruledout. Forexample,ifCandC 0

aresubsetsoff1;:::;n 1gwithw(C)

w(C 0

)N w

0

andv(C)v(C 0

),AlgorithmFwillexaminebothC[f0gand

C 0

[f0g,butthelattersubsetwillneverimprovethemaximum. Wewillonsider

methodsforthelassialknapsakproblem later;AlgorithmFisintendedonly

forsituationswhenall ofthefeasiblepossibilitiesarepotentiallyrelevant.

(12)

Grayodesforombinations. Insteadofmerelygeneratingallombinations,

weoftenprefertovisittheminsuh awaythateahoneisobtainedbymaking

onlya smallhangeto itspredeessor.

For example, we an ask for what Nijenhuis and

Wilfhavealleda\revolvingdooralgorithm": Imagine

tworoomsthatontainrespetivelysandtpeople,with

a revolving door between them. Whenever a person

goesintotheoppositeroom,somebodyelseomesout. Canwedeviseasequene

ofmovessothat eah(s;t)-ombinationoursexatlyone?

The answeris yes, and infat a huge number of suh patterns exist. For

example, it turns out that if we examineall n-bit strings a

n 1 :::a

1 a

0 in the

well-known order of Gray binary ode (Setion 7.2.1.1), but selet only those

thathaveexatlys0sandt1s,theresultingstringsformarevolving-doorode.

Here'stheproof: Graybinaryodeisdenedbythereurrene

n

=0

n 1 ,

1 R

n 1

of7.2.1.1{(5),soits(s;t) subsequenesatisesthereurrene

st

= 0

(s 1)t

; 1 R

s(t 1)

(25)

when st > 0. We also have

s0

= 0 s

and

0t

= 1 t

. Therefore it is lear by

indutionthat

st

begins with 0 s

1 t

and ends with 10 s

1 t 1

when st > 0. The

transition at the omma in (25) is from the last element of 0

(s 1)t to the

lastelementof 1

s(t 1)

,namelyfrom 010 s 1

1 t 1

=010 s 1

11 t 2

to 110 s

1 t 2

=

110 s 1

01 t 2

when t 2, and this satises the revolving-door onstraint. The

aset=1 alsoheksout. Forexample,

33

isgivenbytheolumnsof

000111 011010 110001 101010

001101 011100 110010 101100

001110 010101 110100 100101

001011 010110 111000 100110

011001 010011 101001 100011

(26)

and

23

an be found in the rst two olumns of this array. One more turn

ofthe door takesthe last elementinto the rst. [These properties of

st were

disovered by D. T. Tang and C. N. Liu, IEEE Trans. C-22 (1973), 176{180;

alooplessimplementationwaspresentedbyJ.R.Bitner,G.Ehrlih, andE.M.

Reingold,CACM 19(1976),517{521.℄

Whenweonvertthebitstringsa

5 a

4 a

3 a

2 a

1 a

0

in(26)totheorresponding

index-listforms

3

2

1

,astrikingpatternbeomesevident:

210 431 540 531

320 432 541 532

321 420 542 520

310 421 543 521

430 410 530 510

(27)

Therstomponents

3

ourininreasingorder;butforeahxedvalueof

3 ,

thevalues of

2

our indereasing order. Andfor xed

3

2

, thevalues of

1

are againinreasing. The same is true ingeneral: Allombinations

t :::

2

1

(13)

appearinlexiographiorderof

(

t

;

t 1

;

t 2

; :::; ( 1) t 1

1

) (28)

intherevolving-doorGrayode

st

. Thispropertyfollowsbyindution,beause

(25)beomes

st

=

(s 1)t

; (s+t 1) R

s(t 1)

(29)

forst>0 whenweuseindex-list notationinsteadofbitstringnotation. Conse-

quentlythesequeneanbegeneratedeÆientlybythefollowingalgorithmdue

toW.H.Payne[seeACMTrans.Math. Software 5(1979),163{172℄:

Algorithm R (Revolving-door ombinations). This algorithm generatesall t-

ombinations

t :::

2

1

of f0;1;:::;n 1g in lexiographi order of the alter-

nating sequene (28), assuming that n > t > 1. Step R3 has two variants,

dependingonwhether tisevenor odd.

R1.[Initialize.℄ Set

j

j 1 fortj1,and

t+1 n.

R2.[Visit.℄ Visittheombination

t :::

2

1 .

R3.[Easyase?℄ Ift is odd: If

1

+1<

2

, inrease

1

by1 and returntoR2,

otherwiseset j 2and goto R4. If tis even: If

1

>0,derease

1 by 1

and returnto R2,otherwisesetj 2 andgotoR5.

R4.[Trytoderease

j

.℄ (Atthispoint

j

=

j 1

+1.) If

j

j,set

j

j 1 ,

j 1

j 2,andreturnto R2. Otherwiseinreasej by1.

R5.[Try to inrease

j

.℄ (At this point

j 1

= j 2.) If

j

+1 <

j+1 , set

j 1

j ,

j

j

+1,andreturn toR2. Otherwiseinreasej by 1,and

goto R4ifjt.

Exerises21{25 explorefurther propertiesof this interesting sequene. Oneof

themisanieompaniontoTheoremL:Theombination

t

t 1 :::

2

1

isvisited

byAlgorithmR afterexatly

N =

t +1

t

t 1 +1

t 1

++( 1) t

2 +1

2

( 1) t

1 +1

1

[todd℄ (30)

otherombinationshavebeenvisited. WemayallthistherepresentationofN

inthe\alternatingombinatorialnumbersystem"ofdegreet;oneonsequene,

for example, is that every positive integer has a unique representation of the

formN = a

3

b

2

+

1

witha>b>>0. AlgorithmRtellsushowtoadd1

toN inthissystem.

Although the strings of (26) and (27)are notin lexiographi order, they

are examplesof a more generalonept alledgenlex order, a name oinedby

TimothyWalsh. Asequeneofstrings

1 ,:::,

N

issaidto beingenlexorder

when all strings with a ommon prex our onseutively. For example, all

3-ombinationsthat beginwith53appeartogetherin(27).

Genlex ordermeansthatthestringsan bearranged ina triestruture, as

inFig.31ofSetion6.3,but withthehildrenofeahnodeorderedarbitrarily.

Whenatrieistraversedinanyordersuhthateahnodeisvisitedjustbeforeor

justafteritsdesendants,allnodeswithaommonprex|thatis,allnodesof

(14)

asubtrie|appearonseutively. Thispriniplemakesgenlexorderonvenient,

beauseitorrespondstoreursivegenerationshemes. Manyofthealgorithms

wehaveseenforgeneratingn-tupleshavethereforeproduedtheirresultsinsome

version of genlex order; similarly, the method of \plain hanges" (Algorithm

7.2.1.2P) visits permutations ina genlex order of the orresponding inversion

tables.

Therevolving-doormethodofAlgorithmRisagenlexroutinethathanges

onlyoneelementoftheombinationateahstep. Butitisn'ttotallysatisfatory,

beauseitfrequentlymusthangetwo oftheindies

j

simultaneously,inorder

topreservetheondition

t

>>

2

>

1

. Forexample,AlgorithmRhanges

210into320,and(27)inludes ninesuh\rossing"moves.

Thesoure ofthis defetan betraedto ourproof that (25)satises the

revolving-door property: We observed that the string010 s 1

11 t 2

is followed

by 110 s 1

01 t 2

when t 2. Hene the reursive onstrution

st

involves

transitionsof theform 110 a

0 $010 a

1, whena substring like11000 is hanged

to01001orvie versa;thetwo1s rosseahother.

AGraypathforombinationsis saidtobehomogeneous ifithanges only

one of the indies

j

at eah step. A homogeneous sheme is haraterized

in bitstring form by having only transitions of the forms 10 a

$ 0 a

1 within

strings,for a 1, when wepass from one string

tothenext. Withahomogeneousshemewean,

for example, play all t-note hords on an n-note

keyboardbymovingonlyone ngerata time.

A slight modiation of (25) yields a genlex

sheme for (s;t)-ombinations that is pleasantly

homogeneous. The basi idea is to onstrut a

sequenethat begins with0 s

1 t

and endswith 1 t

0 s

, andthefollowingreursion

suggestsitselfalmostimmediately: LetK

s0

=0 s

,K

0t

=1 t

,K

s( 1)

=;,and

K

st

= 0K

(s 1)t

; 10K R

(s 1)(t 1)

; 11K

s(t 2)

forst>0: (31)

At the ommas of this sequene we have 01 t

0 s 1

followed by 101 t 1

0 s 1

, and

10 s

1 t 1

followed by 110 s

1 t 2

; both of these transitions are homogeneous, al-

thoughtheseondonerequiresthe1tojumparosss0s. TheombinationsK

33

fors=t=3are

000111 010101 101100 100011

001011 010011 101001 110001

001101 011001 101010 110010

001110 011010 100110 110100

010110 011100 100101 111000

(32)

inbitstringform,andtheorresponding\ngerpatterns"are

210 420 532 510

310 410 530 540

320 430 531 541

321 431 521 542

421 432 520 543.

(33)

(15)

Whenahomogeneousshemeforordinaryombinations

t :::

1

isonverted

to the orresponding sheme (6) for ombinations with repetitions d

t :::d

1 , it

retains thepropertythat only one oftheindies d

j

hangesat eah step. And

whenit isonvertedto theorresponding shemes(9)or (11)forompositions

p

t :::p

0 or q

t :::q

0

,onlytwo(adjaent)partshange when

j

hanges.

Near-perfet shemes. But wean do even better! All(s;t)-ombinations

an be generated by a sequene of strongly homogeneous transitions that are

either01$10or001$100. Inotherwords,weaninsistthateahstepauses

a single index

j

to hange by at most 2. Let's all suh generation shemes

near-perfet.

Imposing suh strong onditions atually makes it fairly easy to disover

near-perfet shemes, beause omparativelyfew hoies areavailable. Indeed,

ifwerestritourselvestogenlexmethodsthat arenear-perfetonn-bitstrings,

T.A. Jenkynsand D.MCarthy observed that allsuh methods an be easily

haraterized[ArsCombinatoria40(1995),153{159℄:

Theorem N. Ifst>0,thereareexatly2s near-perfetwaystolistall(s;t)-

ombinations ina genlex order. Infat, when 1 a s, there is exatly one

suh listing, N

sta

, that begins with 1 t

0 s

and ends with 0 a

1 t

0 s a

; the other s

possibilitiesarethereverselists,N R

sta .

Proof. Theresultertainlyholdswhens=t=1;otherwiseweuseindutionon

s+t. ThelistingN

sta

,ifitexists,musthavetheform1X

s(t 1) ,0Y

(s 1)t

forsome

near-perfet genlex listings X

s(t 1) and Y

(s 1)t

. Ift =1, X

s(t 1)

is thesingle

string0 s

;heneY

(s 1)t

mustbeN

(s 1)1(a 1)

ifa>1,andit mustbeN R

(s 1)11

ifa=1. Ontheotherhandift>1,thenear-perfetonditionimpliesthatthe

laststringofX

s(t 1)

annotbeginwith 1;heneX

s(t 1)

=N

s(t 1)b

forsomeb.

Ifa>1, Y

(s 1)t

mustbeN

(s 1)t(a 1)

,henebmustbe1;similarly,bmustbe1

ifs =1. Otherwise we have a =1 <s, and this fores Y

(s 1)t

=N R

(s 1)t for

some. Thetransitionfrom10 b

1 t 1

0 s b

to0 +1

1 t

0 s 1

isnear-perfetonlyif

=1 andb=2.

TheproofofTheoremNyieldsthefollowingreursiveformulaswhenst>0:

N

sta

= 8

>

<

>

: 1N

s(t 1)1

; 0N

(s 1)t(a 1)

; if1<as;

1N

s(t 1)2

; 0N R

(s 1)t1

; if1=a<s;

1N

1(t 1)1

; 01 t

; if1=a=s.

(34)

Also,ofourse,N

s0a

=0 s

.

LetussetA

st

=N

st1 andB

st

=N

st2

. Thesenear-perfetlistings,disovered

byPhillipJ.Chasein1976,havetheneteetofshiftinga leftmostblokof1s

totherightbyoneor twopositions,respetively,andtheysatisfythefollowing

mutualreursions:

A

st

=1B

s(t 1)

; 0A R

(s 1)t

; B

st

=1A

s(t 1)

; 0A

(s 1)t

: (35)

\To takeonestepforward,taketwo steps forward, thenone stepbakward; to

taketwo steps forward, takeone stepforward,thenanother." These equations

(16)

Table2

CHASE'S SEQUENCES FOR (3;3)-COMBINATIONS

A

33

= b

C R

33

543 531 321 420

541 530 320 421

540 510 310 431

542 520 210 430

532 521 410 432

B

33

=C

33

543 520 432 410

542 510 430 210

540 530 431 310

541 531 421 320

521 532 420 321

holdforallinteger valuesofsandt,ifwedeneA

st andB

st

to be;whensor

tis negative,exeptthat A

00

=B

00

= (theemptystring). ThusA

st

atually

takes min(s;1) forward steps, and B

st

atually takes min(s;2). For example,

Table2 showstherelevantlistings fors=t =3,using anequivalentindex-list

form

3

2

1

insteadofthebitstringsa

5 a

4 a

3 a

2 a

1 a

0 .

Chasenotiedthat aomputerimplementationofthesesequenesbeomes

simplerifwedene

C

st

=

A

st

; ifs+tisodd;

B

st

; ifs+tiseven;

b

C

st

=

A R

st

; ifs+tiseven;

B R

st

; ifs+tisodd.

(36)

[SeeCongressus Numerantium69(1989),215{242.℄ Thenwehave

C

st

= (

1C

s(t 1)

; 0 b

C

(s 1)t

; ifs+tisodd;

1C

s(t 1)

; 0C

(s 1)t

; ifs+tiseven;

(37)

b

C

st

= (

0C

(s 1)t

; 1 b

C

s(t 1)

; ifs+tiseven;

0 b

C

(s 1)t

; 1 b

C

s(t 1)

; ifs+tisodd.

(38)

Whenbit a

j

is ready to hange, wean tell where we are in the reursion by

testingwhetherj isevenorodd.

Indeed, the sequene C

st

an be generated by a surprisingly simple algo-

rithm,basedongeneralideasthat applytoany genlexsheme. Letussaythat

bita

j

isativeinagenlexalgorithmifitissupposedtohangebeforeanythingto

itsleftisaltered. (Thenodeforanativebitintheorrespondingtrieisnotthe

rightmosthildofits parent.) Supposewehaveanauxiliarytablew

n :::w

1 w

0 ,

where w

j

= 1 if and only if either a

j

is ative or j < r, where r is the least

subsriptsuhthata

r 6=a

0

;wealsoletw

n

=1. Thenthefollowingmethodwill

ndthesuessorofa

n 1 :::a

1 a

0 :

Set j r. Ifw

j

=0,set w

j

1,j j+1, andrepeat until

w

j

=1. Terminateifj =n; otherwiseset w

j

0. Changea

j

to1 a

j

,and makeanyotherhangestoa

j 1 :::a

0

andrthat

applyto thepartiulargenlexshemebeingused.

(39)

Thebeautyofthisapproah omesfromthefatthattheloopisguaranteedto

beeÆient: Wean prove that theoperationj j+1 will beperformedless

thanonepergenerationstep,ontheaverage (seeexerise36).

(17)

Byanalyzingthetransitionsthat ourwhen bitshange in(37)and(38),

weanreadilyeshouttheremainingdetails:

Algorithm C (Chase'ssequene). Thisalgorithmvisitsall(s;t)-ombinations

a

n 1 :::a

1 a

0

,wheren=s+t,inthenear-perfetorderofChase'ssequeneC

st .

C1.[Initialize.℄ Seta

j

0 for0 j <s, a

j

1 forsj <n, andw

j 1

for0jn. Ifs>0,set r s;otherwisesetr t.

C2.[Visit.℄ Visit theombinationa

n 1 :::a

1 a

0 .

C3.[Find j and branh.℄ Set j r. If w

j

= 0, set w

j

1, j j+1, and

repeatuntil w

j

=1. Terminateifj =n;otherwisesetw

j

0 andmakea

four-way branh: Go to C4 if j is odd anda

j

6=0, to C5 ifj is even and

a

j

6=0,toC6ifj isevenanda

j

=0,toC7ifj isoddanda

j

=0.

C4.[Move right one.℄ Set a

j 1

1, a

j

0. If r = j > 1, set r j 1;

otherwiseifr=j 1setr j. Returnto C2.

C5.[Moverighttwo.℄ If a

j 2

6=0,goto C4. Otherwiseset a

j 2 1,a

j 0.

Ifr=j,setr max(j 2;1);otherwiseifr=j 2,setr j 1. Return

toC2.

C6.[Moveleftone.℄ Seta

j 1,a

j 1

0. Ifr=j>1,setr j 1;otherwise

ifr=j 1 setr j. ReturntoC2.

C7.[Movelefttwo.℄ Ifa

j 1

6=0,gotoC6. Otherwiseseta

j 1,a

j 2

0. If

r=j 2,setr j;otherwiseifr=j 1,setr j 2. ReturntoC2.

*Analysis of Chase's sequene. The magialproperties of AlgorithmC ry

outforfurther exploration,and a loserlook turns outto bequite instrutive.

Givena bitstringa

n 1 :::a

1 a

0

,letusdenea

n

=1,u

n

=nmod2,and

u

j

=(1 u

j+1 )a

j+1

; v

j

=(u

j

+j)mod2; w

j

=(v

j +a

j

)mod2; (40)

forn>j0. For example,wemighthaven=26and

a

25 :::a

1 a

0

=11001001000011111101101010;

u

25 :::u

1 u

0

=10100100100001010100100101;

v

25 :::v

1 v

0

=00001110001011111110001111;

w

25 :::w

1 w

0

=11000111001000000011100101:

(41)

Withthese denitions wean prove by indutionthat v

j

=0 ifandonlyif bit

a

j

isbeing\ontrolled"byCratherthanby b

C inthereursions(37){(38) that

generatea

n 1 :::a

1 a

0

,exeptwhena

j

ispart ofthenalrun of0sor1s atthe

rightend. Thereforew

j

agreeswiththevalueomputedbyAlgorithmCatthe

momentwhena

n 1 :::a

1 a

0

isvisited,forrj<n. Theseformulasanbeused

todetermineexatlywhereagivenombinationappearsinChase'ssequene(see

exerise39).

If we want to work with the index-list form

t :::

2

1

instead of the bit

strings a

n 1 :::a

1 a

0

, it is onvenient to hange the notation slightly, writing

(18)

C

t

(n)forC

st and

b

C

t (n)for

b

C

st

whens+t=n. Then C

0 (n)=

b

C

0

(n)=,and

thereursionsfort0 taketheform

C

t+1

(n+1)= (

nC

t (n);

b

C

t+1

(n); ifniseven;

nC

t (n); C

t+1

(n); ifnisodd;

(42)

b

C

t+1

(n+1)= (

C

t+1 (n); n

b

C

t

(n); ifnisodd;

b

C

t+1 (n); n

b

C

t

(n); ifniseven.

(43)

Thesenewequationsan beexpandedtotellus,forexample,that

C

t+1

(9)=8C

t (8); 6C

t (6); 4C

t

(4); :::; 3 b

C

t (3); 5

b

C

t (5); 7

b

C

t (7);

C

t+1

(8)=7C

t (7); 6C

t (6); 4C

t

(4); :::; 3 b

C

t (3); 5

b

C

t (5);

b

C

t+1

(9)= 6C

t (6); 4C

t

(4); :::; 3 b

C

t (3); 5

b

C

t (5); 7

b

C

t (7); 8

b

C

t (8);

b

C

t+1

(8)= 6C

t (6); 4C

t

(4); :::; 3 b

C

t (3); 5

b

C

t (5); 7

b

C

t (7);

(44)

notiethatthesamepatternpredominatesinallfoursequenes. Themeaningof

\:::"inthemiddledependsonthevalueoft: WesimplyomitalltermsnC

t (n)

andn b

C

t

(n)wheren<t.

Exeptfor edgeeets at the very beginningor end, allof theexpansions

in(44)arebasedontheinniteprogression

:::; 10; 8; 6; 4; 2; 0; 1; 3; 5; 7; 9; :::; (45)

whihisanaturalwaytoarrangethenonnegativeintegersintoadoublyinnite

sequene. If we omit all terms of (45) that are < t, given any integer t 0,

theremainingtermsretain thepropertythat adjaentelementsdierbyeither

1 or 2. Rihard Stanleyhas suggested the nameendo-order for this sequene,

beause we an rememberit by thinking\even numbers dereasing, odd:::".

(NotiethatifweretainonlythetermslessthanN andomplementwithrespet

toN, endo-orderbeomesorgan-pipeorder;see exerise6.1{18.)

Weouldprogramthereursionsof(42)and(43)diretly,butitisinterest-

ingto unwindthem using(44),thusobtaininganiterativealgorithmanalogous

toAlgorithmC.TheresultneedsonlyO(t)memoryloations,anditisespeially

eÆientwhentisrelativelysmallomparedton. Exerise45ontainsthedetails.

*Near-perfet multiset permutations. Chase's sequeneslead ina natural

way to an algorithm that will generate permutations of any desired multiset

fs

0 0;s

1

1;:::;s

d

dgina near-perfetmanner,meaningthat

i) everytransition iseithera

j+1 a

j

$a

j a

j+1 or a

j+1 a

j a

j 1

$a

j 1 a

j a

j+1

;

ii) transitionsoftheseondkindhavea

j

=min(a

j 1

;a

j+1 ).

AlgorithmCtellsushowto dothiswhend=1,andweanextendittolarger

valuesofdbythefollowingreursiveonstrution[CACM 13(1970),368{369,

376℄: Suppose

0

;

1

; :::;

N 1

(19)

isanynear-perfetlistingofthepermutationsoffs

1

1;:::;s

d

dg. ThenAlgo-

rithmC,withs=s

0

andt=s

1

++s

d

,tellsushowtogeneratea listing

j

=

j 0

s

; :::; 0 a

j 0

s a

(46)

inwhihalltransitionsare0x$x0or00x$x00;thenalentryhasa=1or2

leadingzeros,dependingonsandt. Thereforealltransitionsofthesequene

0

; R

1

;

2

; :::; (

N 1 or

R

N 1

) (47)

arenear-perfet;andthislistlearlyontainsallthepermutations.

Forexample,thepermutationsoff0;0;0;1;1;2ggeneratedinthis wayare

211000,210100,210001,210010,200110,200101,200011,201001,201010,201100,

021100,021001,021010,020110,020101,020011,000211,002011,002101,002110,

001120,001102,001012,000112,010012,010102,010120,011020,011002,011200,

101200,101020,101002,100012,100102,100120,110020,110002,110200,112000,

121000,120100,120001,120010,100210,100201,100021,102001,102010,102100,

012100,012001,012010,010210,010201,010021,000121,001021,001201,001210.

*Perfet shemes. Why shouldwesettlefora near-perfetgeneratorlikeC

st ,

insteadofinsistingthatalltransitionshavethesimplestpossibleform01$10?

One reason is that perfet shemes don't always exist. For example, we

observed in7.2.1.2{(2)that thereis noway togenerate allsixpermutations of

f1;1;2;2gwithadjaentinterhanges;thusthereisnoperfetshemefor(2;2)-

ombinations. Infat,ourhanesofahievingperfetionareonlyabout1in4:

Theorem P. Thegenerationofall(s;t)-ombinationsa

s+t 1 :::a

1 a

0

byadja-

entinterhanges 01$10ispossibleifandonlyifs1or t1 orstisodd.

Proof. Consider all permutations of the multiset fs0;t1g. We learned in

exerise5.1.2{16 thatthenumberm

k

ofsuhpermutations havingkinversions

istheoeÆientofz k

inthez-nomialoeÆient

s+t

t

z

= s+t

Y

k=s+1

(1+z++z k 1

) .

t

Y

k=1

(1+z++z k 1

): (48)

Everyadjaentinterhangehangesthenumberofinversionsby1,soaperfet

generationshemeispossibleonlyifapproximatelyhalfofallthepermutations

have an odd number of inversions. More preisely, the value of s+t

t

1

=

m

0 m

1 +m

2

mustbe0 or1. Butexerise49showsthat

s+t

t

1

=

b(s+t)=2

bt=2

[stiseven℄; (49)

andthisquantityexeeds1unlesss1 ort1 orstisodd.

Conversely, perfet shemes are easy with s 1 or t 1, and they turn

out to be possible also whenever st is odd. The rst nontrivial ase ours

for s = t = 3, when there are four essentially dierent solutions; the most

symmetrialofthese is

210 310 410 510 520 521 531 532 432 431

421 321 320 420 430 530 540 541 542 543 (50)

(20)

(see exerise 51). Several authors have onstruted Hamiltonian paths in the

relevant graph for arbitrary odd numbers s and t; for example, the method

of Eades, Hikey, and Read [JACM 31 (1984), 19{29℄ makes an interesting

exeriseinprogrammingwithreursiveoroutines. Unfortunately,however,none

ofthe known onstrutionsare suÆientlysimple to desribeina short spae,

ortoimplementwithreasonableeÆieny. Perfetombinationgeneratorshave

thereforenotyetprovedto beofpratialimportane.

Insummary,then, wehaveseenthat thestudyof(s;t)-ombinationsleads

to many fasinating patterns, some of whih are of great pratialimportane

andsomeofwhiharemerelyelegantand/orbeautiful. Figure26illustratesthe

prinipaloptionsthatareavailableintheases=t=5,when 10

5

=252ombi-

nationsarise. Lexiographiorder(AlgorithmL),therevolving-doorGrayode

(AlgorithmR),thehomogeneous shemeK

55

of(31), andChase'snear-perfet

sheme(AlgorithmC)areshowninparts(a),(b),(),and(d)oftheillustration.

Part(e) showsthenear-perfetshemethatisas losetoperfetionaspossible

whilestillbeingingenlexorderofthearray(seeexerise34),whilepart(f)is

theperfet shemeof Eades, Hikey, and Read. Finally, Figs. 26(g) and 26(h)

arelistingsthatproeedbyrotatinga

j a

j 1 :::a

0 a

j 1 :::a

0 a

j

orbyswapping

a

j

$a

0

,akinto Algorithms7.2.1.2C and7.2.1.2E(seeexerises55and56).

*Combinations of a multiset. Ifmultisets an havepermutations, theyan

haveombinationstoo.Forexample,onsiderthemultisetfb;b;b;b;g;g;g;r;r;r;

w;wg,representingasakthatontainsfourblueballsandthreethataregreen,

threered, two white. There are 37waysto hooseveballs from this sak; in

lexiographiorder(butdesendingineah ombination)theyare

gbbbb;ggbbb;gggbb;rbbbb;rgbbb;rggbb;rgggb;rrbbb;rrgbb;rrggb;

rrggg;rrrbb;rrrgb;rrrgg;wbbbb;wgbbb;wggbb;wgggb;wrbbb;wrgbb;

wrggb;wrggg;wrrbb;wrrgb;wrrgg;wrrrb;wrrrg;wwbbb;wwgbb;wwggb;

wwggg;wwrbb;wwrgb;wwrgg;wwrrb;wwrrg;wwrrr: (51)

This fatmight seem frivolous and/or esoteri, yetwewill see inTheorem W

belowthatthelexiographigenerationofmultisetombinationsyieldsoptimal

solutionstosigniantombinatorialproblems.

James Bernoulliobserved inhis ArsConjetandi (1713),119{123, that we

an enumerate suh ombinations by looking at the oeÆient of z 5

in the

produt(1+z+z 2

)(1+z+z 2

+z 3

) 2

(1+z+z 2

+z 3

+z 4

). Indeed,hisobservation

iseasytounderstand,beausewegetallpossibleseletionsfromthesakifwe

multiplyoutthepolynomials

(1+w+ww)(1+r+rr+rrr)(1+g+gg+ggg)(1+b+bb+bbb+bbbb):

Multisetombinationsarealsoequivalenttoboundedompositions, namely

toompositionsinwhihtheindividualpartsarebounded. Forexample,the37

multiombinationslistedin(51)orrespondto37solutionsof

5=r

3 +r

2 +r

1 +r

0

; 0r

3

2; 0r

2

;r

1

3; 0r

0 4;

namely5=0+0+1+4=0+0+2+3=0+0+3+2=0+1+0+4= =2+3+0+0.

(21)

Fig.26. Examples

of(5;5)-ombinations:

a)lexiographi;

b)revolving-door;

)homogeneous;

d)near-perfet;

e)nearer-perfet;

f)perfet;

g)suÆx-rotated;

h)right-swapped.

(a) (b) () (d) (e) (f) (g) (h)

(22)

Boundedompositions,inturn,arespeialasesofontingenytables,whih

areof greatimportane instatistis. Andallof these ombinatorial ongura-

tionsan be generated with Gray-like odes as well as in lexiographi order.

Exerises60{63 exploresomeofthebasiideas involved.

*Shadows.Setsofombinationsappearfrequentlyinmathematis. Forexample,

asetof2-ombinations(namelyasetofpairs)isessentiallyagraph,andasetof

t-ombinationsforgeneralt isalleda uniform hypergraph. If thevertiesofa

onvexpolyhedronareperturbedslightly,sothatnothreeareollinear,nofour

liein a plane, and ingeneralno t+1 liein a (t 1)-dimensional hyperplane,

theresulting(t 1)-dimensionalfaesare\simplexes"whosevertieshavegreat

signianein omputerappliations. Researhers havelearned that suh sets

ofombinationshaveimportantpropertiesrelatedtolexiographigeneration.

If is any t-ombination

t :::

2

1

, its shadow is the set of all its

(t 1)-element subsets

t 1 :::

2

1

, :::,

t :::

3

1 ,

t :::

3

2

. For example,

5310 = f310;510;530;531g. We an also represent a t-ombination as a bit

stringa

n 1 :::a

1 a

0

,inwhihaseisthesetofallstringsobtainedbyhang-

inga 1 to a 0: 101011=f001011;100011;101001;101010g. If A is any set of

t-ombinations,wedeneitsshadow

A =

S

fj2Ag (52)

to be the set of all (t 1)-ombinations in the shadows of its members. For

example,5310=f10;30;31;50;51;53g.

These denitions apply also to ombinations with repetitions, namely to

multiombinations: 5330= f330;530;533g and 5330 =f30;33;50;53g. In

general, when A is a set of t-element multisets, A is a set of (t 1)-element

multisets. Notie,however,thatAneverhasrepeatedelementsitself.

Theupper shadow

withrespettoa universeU isdenedsimilarly,but

itgoesfrom t-ombinationsto(t+1)-ombinations:

= fU j2g; for2U; (53)

A = S

f

j2Ag; forAU: (54)

If,for example,U =f0;1;2;3;4;5;6g, wehave

5310=f53210;54310;65310g;

ontheotherhand,ifU =f10;11;:::;16g,wehave

5310=f53100;53110;

53210;53310;54310;55310;65310g.

Thefollowingfundamentaltheorems,whihhavemanyappliationsinvar-

ious branhes of mathematis and omputer siene, tell us how small a set's

shadowsan be:

TheoremK. IfAisasetofNt-ombinationsontainedinU =f0;1;:::;n 1g,

then

jAjjP

Nt

j and j

Ajj

Q

Nnt

j; (55)

whereP

Nt

denotestherstN ombinationsgenerated byAlgorithmL,namely

the N lexiographially smallest ombinations

t :::

2

1

that satisfy (3), and

Q

Nnt

denotestheN lexiographiallylargest.

(23)

Theorem M. IfAisasetofN t-multiombinationsontainedinthemultiset

U =f10;11;:::;1sg,then

jAjj b

P

Nt

j and j

Ajj

b

Q

Nst

j; (56)

where b

P

Nt

denotestheNlexiographiallysmallestmultiombinationsd

t :::d

2 d

1

thatsatisfy(6),and b

Q

Nst

denotes theN lexiographiallylargest.

Both ofthese theorems areonsequenes of a strongerresult thatweshall

provelater. TheoremKisgenerallyalledtheKruskal{Katonatheorem,beause

it was disovered by J. B.Kruskal[Math. OptimizationTehniques,edited by

R.Bellman(1963),251{278℄ andredisoveredbyG.Katona[TheoryofGraphs,

Tihany 1966, edited by Erd}os and Katona (Aademi Press, 1968), 187{207℄;

M.P.Shutzenberger hadpreviouslystateditinaless-well-knownpubliation,

with inomplete proof [RLE Quarterly Progress Report 55 (1959), 117{118℄.

Theorem M goes bak to F. S. Maaulay, many years earlier [Pro. London

Math.So.(2)26 (1927),531{555℄.

Beforeproving(55)and(56),let'stakealoserlookatwhatthoseformulas

mean. Weknow from TheoremL that therstN of allt-ombinationsvisited

byAlgorithmL arethosethatpreeden

t :::n

2 n

1 ,where

N =

n

t

t

++

n

2

2

+

n

1

1

; n

t

>>n

2

>n

1 0

isthedegree-tombinatorialrepresentationofN. Sometimesthisrepresentation

hasfewerthantnonzeroterms,beausen

j

anbeequaltoj 1;let'ssuppress

thezeros,andwrite

N =

n

t

t

+

n

t 1

t 1

++

n

v

v

; n

t

>n

t 1

>>n

v

v1: (57)

Nowtherst n

t

t

ombinations

t :::

1

arethet-ombinationsoff0;:::;n

t 1g;

thenext nt

1

t 1

arethoseinwhih

t

=n

t and

t 1 :::

1

isa(t 1)-ombination

off0;:::;n

t 1

1g;andsoon. Forexample,ift=5andN = 9

5

+ 7

4

+ 4

3

,the

rstN ombinationsare

P

N5

=f43210;:::;87654g[f93210;:::;96543g[f97210;:::;97321g: (58)

Theshadowofthis setP

N5

is,fortunately,easytounderstand: Itis

P

N5

=f3210;:::;8765g[f9210;:::;9654g[f9710;:::;9732g; (59)

namelytherst 9

4

+ 7

3

+ 4

2

ombinationsinlexiographiorderwhent=4.

Inotherwords,ifwedeneKruskal'sfuntion

t

bytheformula

t N =

n

t

t 1

+

n

t 1

t 2

++

n

v

v 1

(60)

whenN hastheuniquerepresentation(57),wehave

P

Nt

= P

(tN)(t 1)

: (61)

(24)

Theorem K tells us, for example, that a graph with a million edges an

ontainat most

1414

3

+

1009

2

= 470;700; 300

triangles,that is, at most 470,700,300sets ofvertiesfu;v;wg with u v

w u.Thereasonisthat1000000= 1414

2

+ 1009

1

byexerise17,andtheedges

P

(1000000)2

dosupport 1414

3

+ 1009

2

triangles;butifthereweremore,thegraph

would neessarilyhave atleast

3

470700301= 1414

2

+ 1009

1

+ 1

0

=1000001

edgesintheirshadow.

Kruskaldened theompanionfuntion

t N =

n

t

t+1

+

n

t 1

t

++

n

v

v+1

(62)

to deal with questions suh as this. The and funtions are related by an

interestinglawprovedinexerise 72:

M +N =

s+t

t

implies

s M+

t N =

s+t

t+1

; ifst>0. (63)

Turning toTheoremM,thesizesof b

P

Nt and

b

Q

Nst

turn outtobe

j b

P

Nt j =

t

N and j

b

Q

Nst

j = N+

s

N (64)

(seeexerise 81),wherethefuntion

t

satises

t N =

n

t 1

t 1

+

n

t 1 1

t 2

++

n

v 1

v 1

(65)

whenN hastheombinatorialrepresentation (57).

Table 3 shows how these funtions

t N,

t

N, and

t

N behave for small

values of t and N. When t and N are large, they an be well approximated

intermsof a remarkablefuntion (x) introdued by TeijiTakagi in1903; see

Fig.27andexerises82{85.

TheoremsKandMareorollariesofamuhmoregeneraltheoremofdisrete

geometry,disoveredbyDa-LunWangandPingWang[SIAMJ.AppliedMath.

33 (1977), 55{59℄, whih we shall now proeed to investigate. Consider the

disreten-dimensional torus T(m

1

;:::;m

n

)whoseelementsareinteger vetors

x=(x

1

;:::;x

n

)with0 x

1

<m

1

,:::, 0x

n

<m

n

. Wedene thesumand

diereneoftwo suhvetorsxandy asinEqs.4.3.2{(2) and4.3.2{(3):

x+y = (x

1 +y

1

)modm

1

;:::;(x

n +y

n

)modm

n

; (66)

x y = (x

1 y

1

)modm

1

;:::;(x

n y

n

)modm

n

: (67)

Wealsodene theso-alledrossorder onsuh vetors bysayingthat xy if

andonlyif

x<y or (x=y andxylexiographially); (68)

here,as usual,(x

1

;:::;x

n )=x

1

++x

n

. For example,whenm

1

=m

2

=2

andm

3

=3,the12vetorsx

1 x

2 x

3

inrossorder are

000; 100; 010; 001; 110; 101; 011; 002; 111; 102; 012; 112; (69)

(25)

Table3

EXAMPLESOF THE KRUSKAL{MACAULAYFUNCTIONS, , AND

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

1

N= 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2N= 0 2 3 3 4 4 4 5 5 5 5 6 6 6 6 6 7 7 7 7 7

3

N= 0 3 5 6 6 8 9 9 10 10 10 12 13 13 14 14 14 15 15 15 15

4N= 0 4 7 9 10 10 13 15 16 16 18 19 19 20 20 20 23 25 26 26 28

5

N= 0 5 9 12 14 15 15 19 22 24 25 25 28 30 31 31 33 34 34 35 35

1N= 0 0 1 3 6 10 15 21 28 36 45 55 66 78 91 105120136153171190

2

N= 0 0 0 1 1 2 4 4 5 7 10 10 11 13 16 20 20 21 23 26 30

3

N= 0 0 0 0 1 1 1 2 2 3 5 5 5 6 6 7 9 9 10 12 15

4N= 0 0 0 0 0 1 1 1 1 2 2 2 3 3 4 6 6 6 6 7 7

5

N= 0 0 0 0 0 0 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5

1N= 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2N= 0 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6

3

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

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

5

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

0 22

14

7

0 7

5

8

5 5N N

8

5

+ 7

4

9

5

0 2/3

1/2

1/4

0

1/4 1/2 (x )

3/4 1

Fig. 27. ApproximatingaKruskal funtionwith the Takagifuntion. (The

smoothurveintheleft-handgraphisthelowerbound

5

N N ofexerise80.)

omittingparentheses andommasforonveniene. Theomplement ofavetor

inT(m

1

;:::;m

n )is

x = (m

1

1 x

1

;:::;m

n

1 x

n

): (70)

Notiethatxyholds ifandonlyif xy. Thereforewehave

rank(x)+rank(x) = T 1; whereT =m

1 :::m

n

, (71)

ifrank(x)denotesthenumberofvetorsthat preedexinrossorder.

Wewillnditonvenienttoallthevetors\points"andtonamethepoints

e

0 ,e

1 ,:::, e

T 1

ininreasingrossorder. Thus wehavee

7

=002 in(69),and

e

r

=e

T 1 r

ingeneral. Notiethat

e

1

=100:::00; e

2

=010:::00; :::; e

n

=000:::01; (72)

(26)

thesearetheso-alledunitvetors. Theset

S

N

= fe

0

;e

1

;:::;e

N 1

g (73)

onsisting of thesmallest N points is alleda standard set, and inthe speial

aseN =n+1wewrite

E =fe

0

;e

1

;:::;e

n

g=f000:::00;100:::00;010:::00;:::;000:::01g: (74)

Anyset of pointsX hasa spread X +

, a ore X Æ

, and a dual X

, dened

bytherules

X +

= fx2S

T

jx2X orx e

1

2X or orx e

n

2Xg; (75)

X Æ

= fx2S

T

jx2X andx+e

1

2X and andx+e

n

2Xg; (76)

X

= fx2S

T

jx2=Xg: (77)

Wean alsodenethespreadofX algebraially,writing

X +

= X+E; (78)

whereX+Y denotesfx+yjx2X andy2Y g. Clearly

X +

Y ifandonlyif X Y Æ

: (79)

Thesenotionsanbeillustratedinthetwo-dimensionalasem

1

=4,m

2

=6,by

themore-or-lessrandomtoroidalarrangementX =f00;12;13;14;15;21;22;25g

forwhihwehave,pitorially,

Æ

+ +

+ +

+ + +

+

Æ

Æ Æ

Æ Æ

Æ Æ Æ

+ + + + + +

+

; (80)

X X

Æ

andX +

X

X Æ

andX +

hereX inthersttwodiagramsonsistsofpointsmarked orÆ,X Æ

omprises

justthe Æs,and X +

onsistsof +spluss plusÆs. Notiethat ifwerotate the

diagramforX Æ

andX +

by180 Æ

,weobtainthediagramforX Æ

andX +

,but

with(;Æ;+; )respetivelyhanged to(+; ;;Æ); andinfattheidentities

X Æ

= X +

; X

+

= X Æ

(81)

holdingeneral(seeexerise86).

NowwearereadytostatethetheoremofWangandWang:

TheoremW. LetXbeanysetofN pointsinthedisretetorusT(m

1

;:::;m

n ),

wherem

1

m

n

. ThenjX +

jjS +

N

jand jX Æ

jjS Æ

N j.

Inotherwords,thestandardsetsS

N

havethesmallestspread andlargestore,

amongallN-pointsets. Wewillprovethisresultbyfollowingageneralapproah

rstusedbyF.W.J.Whipple toproveTheoremM[Pro.London Math. So.

(2)28(1928),431{437℄. Therststepisto provethat thespread andtheore

ofstandardsetsarestandard:

(27)

Lemma S. Therearefuntionsand suh thatS

N

=S

N andS

Æ

N

=S

N .

Proof. WemayassumethatN >0. Letrbemaximumwithe

r 2S

+

N

,andlet

N = r+1; we must provethat e

q 2 S

+

N

for 0 q < r. Suppose e

q

= x=

(x

1

;:::;x

n )and e

r

=y =(y

1

;:::;y

n

), and letk bethe largest subsript with

x

k

>0. Siney 2S +

N

,thereisasubsriptj suhthaty e

j 2S

N

. ItsuÆesto

provethat x e

k

y e

j

,andexerise 88doesthis.

The seond part follows from (81), with N = T (T N), beause

S

N

=S

T N .

Theorem Wis obviouslytruewhenn=1,so weassumebyindutionthat

ithasbeenprovedinn 1dimensions. Thenextstepisto ompress thegiven

setX inthekthoordinateposition,bypartitioningitintodisjointsets

X

k

(a) = fx2X jx

k

=ag (82)

for0a<m

k

and replaingeahX

k (a)by

X 0

k

(a) = f(s

1

;:::;s

k 1

;a;s

k

;:::;s

n 1 )j(s

1

;:::;s

n 1 )2S

jX

k (a)j

g; (83)

asetwiththesamenumberofelements. ThesetsSusedin(83)arestandardin

the(n 1)-dimensionaltorus T(m

1

;:::;m

k 1

;m

k+1

;:::;m

n

). Notie that we

have (x

1

;:::;x

k 1

;a;x

k+1

;:::;x

n ) (y

1

;:::;y

k 1

;a;y

k+1

;:::;y

n

) if and only

if(x

1

;:::;x

k 1

;x

k+1

;:::;x

n ) (y

1

;:::;y

k 1

;y

k+1

;:::;y

n

); therefore X 0

k (a)=

X

k

(a)ifandonlyifthe(n 1)-dimensionalpoints(x

1

;:::;x

k 1

;x

k+1

;:::;x

n )

with(x

1

;:::;x

k 1

;a;x

k+1

;:::;x

n

)2X areassmallaspossiblewhenprojeted

onto the(n 1)-dimensionaltorus. Welet

C

k

X = X 0

k (0)[X

0

k

(1)[[X 0

k (m

k

1) (84)

bethe ompression of X inposition k. Exerise 90 proves the basi fat that

ompressiondoesnotinreasethesizeofthespread:

jX +

j j(C

k X)

+

j; for1kn: (85)

Furthermore,ifompressionhangesX,itreplaessomeoftheelementsbyother

elementsoflowerrank. ThereforeweneedtoproveTheoremWonlyforsetsX

thataretotallyompressed,havingX =C

k

X forallk.

Consider, for example, the ase n = 2. A totally ompressed set in two

dimensionshasallpointsmovedtotheleftoftheirrowsandthebottomoftheir

olumns,as intheeleven-pointsets

+ + + + + +

or

+ + + + +

or

+ + + + +

or

+ + + + +

or

+ + + +

;

the rightmost of these is standard, and has the smallest spread. Exerise 91

ompletestheproofofTheoremWintwo dimensions.

Références

Documents relatifs

Moreover, Algorithm H produes partitions in lexiographi order of

rst suh algorithms to be published were not ited in Setion 7.2.1.6 beause. they have been superseded by better tehniques; but it is appropriate

[r]

[r]

Die Coefficienten der bestandig convergirenden P0tenzrcihe, in die F(x) entwickelt werden kann, wOrden sicht leieht ergeben, wGnn diese Frage bejaht werden

Yau, Incompressible minimal sur- faces, three dimensional manifolds with nonnegative scalar curvature, and the positive mass conjecture in general relativity,

From this observation and the fact t h a t subsequent arguments referring to convexity will be based only on the existence of midpoints, it follows t h a t the

The theory of O-fiat modules is obtained by a dualization of the theory of O-injective modules (see.. The proofs of the following two results are dual to the proofs