p221 Wirth

Embed Size (px)

Citation preview

  • 8/8/2019 p221 Wirth

    1/7

    P. WegnerEducation Editor ProgramDevelopm ent byStepwiseRefinementNi k l a u s W i r t hEidgeniSss i sche Technische HochschuleZiirich, Switzerland

    Th e creat i ve ac t i v i t y o f pro gra m m i ng-- t o be d is ti ngu ishedf r o m c o d i n g - - i s u s u a l ly t a u g h t b y e x a m p l e s s er v i n g t oexhibi t certain techniques . I t i s here considered as aseq uence o f des i gn dec i s i ons concerning t hedeco mp os i t i on o f tasks i n t o subt asks and o f dat a i n t odata s tructures . T he proc ess of success ive refinement ofspeci f icat ions i s i l lustrated by a short but nontriviale x a m p l e , f r o m w h i c h a n u m b e r o f c o n c l u s i o n s a r e d ra w nregard i ng t he ar t and t he ins truc t ion o f programm i ng .

    K ey W ords and P hrases : educati on i n programm i ng ,programming techniques , s tepwise program construct ion

    CR Categories: 1 .50, 4 .0

    1. IntroductionProgramming is usually taught by examples. Experi-

    ence shows that the success of a programming coursecritically depends on the choice of these examples. Un-fortunately, they are too often selected with the primeintent to demonstrat e what a computer can do. Instead,a main criterion for selection should be their suitabilityto exhibit certain widely applicable techniques. Further-more, examples of programs are com monl y presented asfinished "products" followed by explanations of theirpurpose and their linguistic details. But active program-ming consists of the design o f n e w programs, rather thancontemplation of old programs. As a consequence ofthese teaching methods, the student obtains the impres-sion that programming consists mainly of mastering alanguage (with all the peculiarities and intricacies soabund ant in moder n PL's) and relying on one's intuitionto somehow transform ideas into finished programs.Clearly, programming courses should teach methods ofdesign and construction, and the selected examplesshould be such that a gradual d e v e l o p m e n t can be nicelydemonstrated.

    This paper deals with a single example chosen w i t h

    these two purposes in mind. Some well-known tech-niques are briefly demonstrat ed and motivated (strategyof preselection, stepwise construction of trial solutions,introduction of auxiliary data, recursion), and the pro-gram is gradually developed in a sequence of re f in emen ts teps .

    In each step, one o r several instructions of the givenprogram are decomposed into more detailed instruc-tions. This successive decomposition or refinement ofspecifications terminates when all instructions are ex-pressed in terms of an underlying computer or program-ming language, and must therefore be guided by thefacilities available on that computer or language. The re-sult of the execution of a program is expressed in termsof data, and it may be necessary to introduce furtherdata for communication between the obtained subtasksor instructions. As tasks are refined, so the data mayhave to be refined, decomposed, or structured, and it isnatural to ref ine progra m a nd data specif ica tions inparallel.

    Every refinement step implies some design decisions.It is important tha t these decision be made explicit, andthat the programmer be aware of the underlying criteriaand of the existence of alternative solutions. The pos-sible solutions to a given problem emerge as the leaves ofa tree, each node representing a point of deliberationand decision. Subtrees may be considered as f a m i l i e s o fsolutions with certain commo n characteristics and struc-tures. The notion of such a tree may be particularlyhelpful in the situation of changing purpose and environ-ment to which a program may sometime have to beadapted.

    A guideline in the process of stepwise refinementshould be the principle to decompose decisions as muchas possible, to untangle aspects which are only seeminglyinterdependen t, and to def?r those decisions which con-cern details of representation as long as possible. T h i s

    221 Communicat ions Apri l 1971o f Volume 14the ACM Nu mber 4

  • 8/8/2019 p221 Wirth

    2/7

    w i l l r e s u l t i n p r o g r a m s w h i c h a r e e a s i e r t o a d a p t t o d i f -f e r e n t e n v i r o n m e n t s ( l a n g u a ge s a n d c o m p u t e r s ) , w h e r ed i f fe r e n t r e p r e s e n t a t i o n s m a y b e r e q u i r e d .

    T h e c h o s e n s a m p l e p r o b l e m i s f o r m u l a t e d a t t h e b e -g i n n i n g o f s e c t i o n 3. T h e r e a d e r i s s t r o n g l y u r g e d t o t r yt o f i n d a s o l u ti o n b y h i m s e l f b e f o r e e m b a r k i n g o n t h ep a p e r w h i c h - - o f c o u r s e - - p r e s e n t s o n l y o n e o f m a n yp o s s i b l e s o l u t i o n s .

    2 . N o t a t i o nF o r t h e d e s c r i p t io n o f p r o g r a m s , a s l i gh t ly a u g m e n -

    t e d Algol 60 n o t a t i o n w i l l b e u s e d . I n o r d e r t o e x p r e s sr e p e t i t io n o f s t a t e m e n t s i n a m o r e l u c i d w a y t h a n b y u s eo f la b e l s a n d j u m p s , a s t a t e m e n t o f th e f o r mrepeat (statement sequence}until (Boo lean expression}i s i n t r o d u c e d , m e a n i n g t h a t t h e s t a t e m e n t s e q u e n c e i s t ob e r e p e a t e d u n t i l t h e B o o l e a n e x p r e s s i o n h a s o b t a i n e dt h e v a l u e t r u e .

    3 . T h e 8 - Q u e e n s P r o b l e m a n d an A p p r o ac h t o I tsS o l u t i o n 1

    Given are an 8 X 8 chessboard and 8 quee ns which are hostileto each other. Find a position for eac h queen (a configuration) suchthat no queen may be taken by any other queen (i .e. such that everyrow, column, and diagonal contains at most one queen).T h i s p r o b l e m i s c h a r a c t e r is t i c f o r t h e ra t h e r f r e q u e n t

    s i t u a t io n w h e r e a n a n a l y t i c a l s o l u t io n i s n o t k n o w n ,a n d w h e r e o n e h a s t o r e s o r t t o th e m e t h o d o f t r ia l a n de r r o r . T y p i c a l l y , t h e r e e x i s ts a s e t A o f c a n d i d a t e s f o rs o l u ti o n s , a m o n g w h i c h o n e i s to b e s e le c t e d w h i c hs a t is f ie s a c e r t a i n c o n d i t i o n p . T h u s a s o l u t i o n i s c h a r -a c t e r i z e d a s a n x s u c h t h a t ( x E A ) ^ p( x ) .

    A s t r a i g h t f o r w a r d p r o g r a m t o f i n d a s o l u t i o n i s :repeat Generate the next eleme nt of A and c all it xuntil p(x) V (no more elements in A);i fp (x ) then x = solutionT h e d i f f i c u lt y w i t h t h i s s o r t o f p r o b l e m u s u a l l y i s t h es h e e r s i ze o f A , w h i c h f o r b i d s a n e x h a u s t i v e g e n e r a t i o no f c a n d i d a t e s o n t h e g r o u n d s o f e f f ic i e nc y c o n s i d e r a -t i o n s . I n t h e p r e s e n t e x a m p l e , A c o n s i s t s o f6 4 ! / (5 6 ! 8 ! ) -+- 232 e l e me n t s (b o ar d co n f ig u ra -t i o n s ). U n d e r t h e a s s u m p t i o n t h a t g e n e r a t i o n a n dt e s t o f e a c h c o n f i g u r a t i o n c o n s u m e s 1 0 0 t zs , i tw o u l d r o u g h l y t a k e 7 h o u r s t o f i n d a s o l u t io n . I t i so b v i o u s l y n e c e s s a r y t o i n v e n t a " s h o r t c u t , " a m e t h o dw h i c h e l im i n a t e s a l a r g e n u m b e r o f " o b v i o u s l y " d is -q u a l i f i e d c o n t e n d e r s . T h i s s trategy o f preselect ion isc h a r a c t e r i z e d a s f o ll o w s : F i n d a r e p r e s e n t a t i o no f p i n t h e f o r m p = q A r . T h e n l e t B , ={ x { ( x C A ) A r ( x ) } . O b v i o u s l y B r c A . I n s t e a d o fg e n e r a t i n g e le m e n t s o f A , o n l y e l e m e n t s o f B a r e p r o -d u c e d a n d t e s t e d o n c o n d i t i o n q i n s t e a d o f p . S u i t a b l e1 This problem was investigated by C. F . Gauss in 1850.22 2

    c a n d i d a t e s f o r a c o n d i t i o n r a r e t h o s e w h i c h s a t i s f yt h e f o l l o w i n g r e q u i r e m e n t s :1 . B r i s m u c h s m a l l e r t h a n A .2 . E l e m e n t s o f B~ a r e e a s i l y g e n e r a t e d .3 . C o n d i t i o n q is e a s ie r t o t e s t t h a n c o n d i t i o n p .T h e c o r r e s p o n d i n g p r o g r a m t h e n is :repeat Generate the nex t element of B and call it xuntil q(x) V (no m ore elements in B);i f q(x) then x = solutionA s u i t a b le c o n d i t i o n r i n t h e 8 - q u e e n s p r o b l e m i s t h er u l e t h a t i n e v e r y c o l u m n o f th e b o a r d t h e r e m u s t b ee x a c t l y o n e q u e e n . C o n d i t i o n q t h e n m e r e l y s p e ci fi e st h a t t h e r e b e a t m o s t o n e q u e e n i n e v e r y r o w a n d i ne v e r y d i a g o n a l, w h i c h i s e v i d e n tl y s o m e w h a t e a s i e rt o t e s t t h a n p . T h e s e t B r ( c o n f i g u r a t i o n s w i t h o n e q u e e ni n e v e r y c o l u m n ) c o n t a i n s " o n l y " 88 = 224 e l e m e n t s .T h e y a r e g e n e r a t e d b y r e s t r i c t i n g t h e m o v e m e n t o fq u e e n s t o c o l u m n s . T h u s a l l o f t h e a b o v e c o n d i t i o n sa re sa t i s f i ed .

    A s s u m i n g a g a i n a t i m e o f 1 0 0 u s f o r t h e g e n e r a t i o na n d t e s t o f a p o t e n t i a l s o l u t i o n , f i n d i n g a s o l u t i o nw o u l d n o w c o n s u m e o n l y 1 00 s e c o n d s. H a v i n g a p o w e r -f u l c o m p u t e r a t o n e ' s d i s p o s a l, o n e m i g h t e a s i l y b ec o n t e n t w i t h t h i s g a i n i n p e r f o r m a n c e . I f o n e i s l e s sf o r t u n a t e a n d i s f o r c e d t o , s a y, s o l v e t h e p r o b l e m b yh a n d , i t w o u l d t a k e 2 8 0 h o u r s o f g e n e r a t i n g a n d t e s t in gc o n f i g u r a t i o n s a t t h e r a t e o f o n e p e r s e c o n d . I n t h isc a s e i t m i g h t p a y t o s p e n d s o m e t i m e f i n d i n g f u r t h e rs h o r t c u t s . I n s t e a d o f a p p ly i n g t h e s a m e m e t h o d a sb e f o r e , a n o t h e r o n e i s a d v o c a t e d h e r e w h i c h i s c h a r -a c t e r i z e d as f o l lo w s : F i n d a r e p r e s e n t a t i o n o f tr i a ls o l u t i o n s x o f t h e f o r m [ x t , x ~ , . . . , x , ] , s u c h t h a te v e r y t r ia l s o l u t i o n c a n b e g e n e r a t e d i n s t e p s w h i c hp r o d u c e [ x l] , [ x l , x 2 ], . . . , [ x l , x 2 , . - - , x , ] r e s p e c -t iv e l y. T h e d e c o m p o s i t i o n m u s t b e s u c h t h a t :1 . E v e r y s t e p ( g e n e r a t i n g x i ) m u s t b e c o n s i d e r a b l y

    s i m p l e r to c o m p u t e t h a n t h e e n t ir e c a n d i d a t e x .2 . q ( x) D q ( x l . . , x j) f o r a l l j _< n .T h u s a f u l l so l u t i o n c a n n e v e r b e o b t a i n e d b y e x t e n d -i n g a p a r t i a l t r i a l s o l u t i o n w h i c h d o e s n o t s a t i s f yt h e p r e d i c a t e q . O n t h e o t h e r h a n d , h o w e v e r , a p a r t i a lt r i a l s o l u t i o n s a t i s f y i n g q m a y n o t b e e x t e n s i b l e i n t o ac o m p l e t e s o lu t i o n . T h i s m e t h o d o f s tepwise construe-t ion of tr ial solut ions t h e r e f o r e r e q u i r e s t h a t t r i a l s o l u-t i o n s f a il i n g a t s t e p j m a y h a v e t o b e " s h o r t e n e d "a g a i n i n o r d e r t o t r y d i f f e r e n t e x t e n s i o n s . T h i s t e c h n i q u ei s ca l l ed backtracking a n d m a y g e n e r a l l y b e c h a r a c t e r -i z e d b y t h e p r o g r a m :j : = l ;relmat trystep j;i f succ essful then advance else regressuntil U < 1) V U > n)

    I n t h e 8 - q u e e n s e x a m p l e , a s o l u t i o n c a n b e c o n -s t r u c t e d b y p o s i t i o n i n g q u e e n s i n s u c c e s s iv e c o l u m n ss t a r t in g w i t h c o l u m n 1 a n d a d d i n g a q u e e n i n t h e n e x tc o l u m n i n e a c h s t e p . O b v i o u s l y , a p a r t i a l c o n f i g u r a t i o nn o t s a t i sf y i n g t h e m u t u a l n o n a g g r e s s i o n c o n d i t i o n m a y

  • 8/8/2019 p221 Wirth

    3/7

    n e v e r b e e x t e n d e d b y t h i s m e t h o d i n t o a f u ll s o l u t io n .A l s o , s i nc e d u r i n g t h e j t h s t e p o n l y j q u e e n s h a v e t o b ec o n s i d e r e d a n d t e s t e d f o r m u t u a l n o n a g g r e s s i o n , f in d i nga p a r t i a l s o l u t i o n a t s t e p j r e q u i r e s l e ss e f f o r t o f i n sp e c -t i o n t h a n f i n d in g a c o m p l e t e s o l u t i o n u n d e r t h e c o n d i -t i o n t h a t a l l 8 q u e e n s a r e o n t h e b o a r d a l l t h e t i m e . B o t hs t a t e d c r i t e r i a a r e t h e r e f o r e s a t i s f ie d b y th e d e c o m p o s i -t i o n i n w h i c h s t e p j c o n s i s t s o f f i n d in g a s a fe p o s i t i o n f o rt h e q u e e n in t h e j t h c o l u m n .

    T h e p r o g r a m s u b s e q u e n t l y t o b e d e v e l o p e d i s b a s e do n t h i s m e t h o d ; i t g e n e r a t e s a n d t e s t s 8 7 6 p a r t i a l c o n -f i g u r a t io n s b e f o r e f in d i n g a c o m p l e t e s o l u t io n . A s s u m i n ga g a i n t h a t e a c h g e n e r a t i o n a n d t e s t ( w h i c h i s n o w m o r ee a s i ly a c c o m p l i s h e d t h a n b e f o r e ) c o n s u m e s o n e s e c o n d ,t h e s o l u t i o n i s f o u n d i n 1 5 m i n u t e s , a n d w i t h t h e c o m -p u t e r t a k i n g 1 0 0 ~ s p e r s t e p , i n 0. 0 9 s e c o n d s .

    4 . D e v e l o p m e n t o f t h e P r o g r a mW e n o w f o r m u l a t e t h e s t ep w i s e g e n e r a t i o n o f p a r t ia l

    s o l u t i o n s t o t h e 8 - q u e e n s p r o b l e m b y t h e f o l l o w i n g f i rs tv e r s i o n o f a p r o g r a m :variable board, pointer, saJe;considerfirstcolumn;repeat tryeolumn;if safe thenbegin setqueen; eonsidernexteohlmn

    en d else regressuntil lasteoldone V regre ssoutoffirsteolT h i s p r o g r a m i s c o m p o s e d o f a s e t o f m o r e p r i m i t i v e i n -s t r u c t i o n s ( o r p r o c e d u r e s ) w h o s e a c t i o n s m a y b e d e -s c r i b e d a s f o l l o w s :

    eonsiderfirs teolumn. The problem essentially consists of inspect-ing the safety of squares. A pointer variable designates the currentlyinspected square. The colum n in wh ich this square lies is called thecurrently inspected column. This procedure initializes the pointer todenote the first column.tryeolumn. Starting at the current square of inspection in thecurrently considered column, m ove down the column either until asafe square is found, in which case the Boolean variable safe is setto true, o r until the las t square is reached and is also unsafe, inwhich cas e the variable safe is set to false.setqueen. A queen is positioned onto the last inspected square.eonsidernextcolumn. Adva nce o the next column and init ializeits pointer of inspection.regress . Regress to a column w here it is possible to move thepositioned queen further down, and remove the queens positionedin the columns over w hich regression takes place. (No te that wemay have to regress over at most two columns. Why?)T h e n e x t st e p o f p r o g r a m d e v e l o p m e n t w a s c h o s e n t or e f in e t h e d e s c r i p t io n s o f t h e i n s t r u c t i o n s t r y c o l u m n a n dr e g r e s s a s f o l l o w s :procedure tryeolumn;repeat advancepointer; tes tsquareun t i l saJb V lastsquareprocedure regress;

    b eg in reconsiderpriorcolumni f - r e g r e s s o u t o f f i r s t c o l t henb eg in removequeen;if lastsquare t hen

    2 2 3

    b eg in reconsiderpriorcolumn;if ~ regressoutoffirstcol t henremovequeenen den den d

    T h e p r o g r a m i s e x p r e s s e d in t e r m s o f t h e i n s tr u c t i o n s :considerfirstcolumnconsidernextcolumnreconsiderpriorcolumnadvancepointertestsquare (sets he variable safe)setqueenremovequeena n d o f th e p r e d i c a t e s :lastsquarelasteoldoneregressoutoffirsteolI n o r d e r t o r e f in e th e s e i n s t r u c t i o n s a n d p r e d i c a t e s f u r-t h e r i n t h e d i r e c t i o n o f i n s t r u c t i o n s a n d p r e d i c a t e s a v a i l -a b l e in c o m m o n p r o g r a m m i n g l a n gu a g e s, i t be c o m e sn e c e s s a r y t o e x p re s s t h e m i n t e r m s o f d a t a r e p r e s e n t a b l ei n t h o s e l a n g u a g e s. A d e c i s i on o n h o w t o r e p r e s e n t t h er e l e v a n t f a ct s i n t e r m s o f d a t a c a n t h e r e f o r e n o l o n g e r b ep o s t p o n e d . F i r s t p r i o r i t y i n d e c i si o n m a k i n g i s g i ve n t ot h e p r o b l e m o f h o w t o r e p r e s e n t t h e p o s i t i o n s o f t h eq u e e n s a n d o f t h e s q u a r e b e i n g c u r r e n t l y i n s p e c te d .

    T h e m o s t s t r a i g h t f o r w a r d s o l u t i o n ( i. e . t h e o n e m o s tc l o s e ly r e fl e c ti n g a w o o d e n c h e s s b o a r d o c c u p i e d b y m a r -b l e p i ec e s ) i s t o i n t r o d u c e a B o o l e a n s q u a r e m a t r i x w i t hB [i , j] = t r u e d e n o t i n g t h a t s q u a r e ( i, j ) i s o c c u p i e d . T h es u c c es s o f a n a l g o r i t h m , h o w e v e r , d e p e n d s a l m o s t a l-w a y s o n a s u i ta b l e c h o i c e o f i ts d a t a r e p r e s e n t a t i o n i nt h e l i g h t o f t h e e a s e i n w h i c h t h i s r e p r e s e n t a t i o n a l l o w st h e n e c e s s a r y o p e r a t i o n s t o b e e x p r e s se d . A p a r t f r o mt h i s , c o n s i d e r a t i o n r e g a r d i n g s t o r a g e r e q u i r e m e n t s m a yb e o f p r im e i m p o r t a n c e ( a l t h o u g h h a r d l y i n th i s c a se ) .A c o m m o n d i ff i cu l ty i n p r o g r a m d e s i g n li es in t h e u n f o r -t u n a t e f a c t t h a t a t t h e s t a g e w h e r e d e c i si o n s a b o u t d a t ar e p r e s e n t a t i o n s h a v e t o b e m a d e , i t o f t e n is s ti ll d if f i cu l tt o f o r e s e e th e d e t a i l s o f t h e n e c e s s a r y i n s t r u c t i o n s o p e r -a t i n g o n t h e d a t a , a n d o f t e n q u i t e im p o s s i b l e t o e s t i m a t et h e a d v a n t a g e s o f o n e p o s s i b le r e p r e s e n t a t i o n o v e ra n o t h e r . I n g e n e r a l , i t i s t h e r e f o r e a d v i s a b l e t o d e l a y d e -c i s io n s a b o u t d a t a r e p r e s e n t a t i o n a s lo n g a s p o s s i b le( b u t n o t u n t i l it b e c o m e s o b v i o u s t h a t n o r e a l iz a b l e s o-l u t i o n w i ll s u i t t h e c h o s e n a l g o r i t h m ) .

    I n th e p r o b l e m p r e s e n t e d h e r e , i t i s f a i r l y e v i d e n te v e n a t t h i s s t a g e t h a t t h e f o l l o w i n g c h o i c e i s m o r es u i t a b le t h a n a B o o l e a n m a t r i x i n t e r m s o f s i m p l ic i tyo f l a t e r i n s t r u c t io n s a s w e ll a s o f s t o r a g e e c o n o m y .

    j i s t h e i n d e x o f t h e c u r r e n t l y i n s p e c t e d c o l u m n ;( x j , j ) i s t h e c o o r d i n a t e o f th e l a s t i n sp e c t e d s q u a r e ;a n d t h e p o s i t i o n o f t h e q u e e n i n c o l u m n k < j i s gi v e nb y t h e c o o r d i n a t e p a i r ( X k , k ) o f t h e b o a r d . N o w t h ev a r i a b l e d e c l a r a t i o n s f o r p o i n t e r a n d b o a r d a r e r e f i n e di n t o :i n t e g e r j (0 < j _< 9 )integer array x[1:8] (0 < x~ < 8)

    Com mun icat ions Apri l 1971o f Vo lu me 14. . . . . ~ v _ _ - - L - - - A

  • 8/8/2019 p221 Wirth

    4/7

    a n d t h e f u r t h e r r e f in e m e n t s o f s o m e o f t h e a b o v e i n s t r u c -t i o n s a n d p r e d i c a t e s a r e e x p r e s s e d a s :procedure c o n s i d e r f i r s t c o l u m n ;begin j : = 1 ; x [ 1 ] : = 0 e n dprocedure c o n s i d e r n e x t c o l u m n ;

    b e g i n - - j : = j - I - t ; x[j] : = 0 e n dprocedure r e c o n s i d e t T r i o r c o l u m n ; j : = j - 1procedure a d v a n c e p o i n t e r ;

    x [ j ] : = x [ j ] + 1Boolean procedure l a s t square;

    l a s ts q u a r e : - x [ j ] - 8Boolean procedure t a s t co ldo tw;

    l a s t c o Mo n e : = j > 8Boolean procedure regres sou to f f i r s t eo l ;

    r e g r e s s o u t o ff i r s tc o l : - j < 1A t t h is s t a g e, t h e p r o g r a m i s e x p r e s s e d in t e r m s o f t h ei n s t r u c t i o n s :t es t squares e t q u e e nr e m o v e q u e e nA s a m a t t e r o f f ac t , th e i n s t r u c t i o n s setqueen a n dremo veq u een m a y b e r e g a r d e d a s v a c u o u s , i f w e d e c i d et h a t t h e p r o c e d u r e tes tsquare i s t o d e t e r m i n e t h ev a l u e o f t h e v a r i a b l e sa fe s o le l y o n t h e g r o u n d s o f t h ev a l u e s x l . - . x ~ _ 1 w h i c h c o m p l e t e l y r e p r e s e n t t h e p o -s i t i o n s o f t h e j - 1 q u e e n s s o f a r o n t h e b o a r d . B u tu n f o r t u n a t e l y t h e i n s t r u c t i o n tes tsquare i s t h e o n em o s t f r e q u e n t l y e x e c u t e d , a n d i t is th e r e f o r e t h e o n ei n s t r u c t i o n w h e r e c o n s i d e r a t i o n s o f e f fi c ie n c y a r e n o to n l y j u s t i f i e d b u t e s s e n t i a l f o r a g o o d s o l u t i o n o f t h ep r o b l e m . E v i d e n t l y a v e r s io n o f tes tsquare e x p r e s s e do n l y i n t e r m s o f x l . . . x j _ l i s in e f f i c ie n t a t b e s t . I ts h o u l d b e o b v i o u s t h a t tes tsquare i s e x e c u t e d f a r m o r eo f t e n t h a n setqueen a n d remo veq u een . T h e l a t t e r p r o -c e d u r e s a r e e x e c u t e d w h e n e v e r t h e c o l u m n ( j) i s c h a n g e d( s ay m t im e s ) , t h e f o r m e r w h e n e v e r a m o v e t o t h e n e x ts q u a r e i s u n d e r t a k e n ( i. e . x ~ i s c h a n g e d , s a y n ti m e s ) .I t o w e v e r , setqueen a n d remo veq u een a r e t h e o n l yp r o c e d u r e s w h i c h a f f e c t t h e c h e s s b o a r d . E f f ic i e n c ym a y t h e r e fo r e b e g a in e d b y th e m e t h o d o f in troducinga u x i l i a r y v a r i a b l e s V ( x l . . . x j ) s u c h t h a t :1 . W h e t h e r a s q u a r e i s s a fe c a n b e c o m p u t e d m o r e

    e a s i l y f r o m V(x) t h a n f r o m x d i r e c t l y ( s a y i n uu n i ts o f c o m p u t a t i o n i n s t e a d o f k u u n i ts o f c o m -p u t a t i o n ) .

    2 . T h e c o m p u t a t i o n o f V(x) f r o m x ( w h e n e v e r xc h a n g e s ) i s n o t t o o c o m p l i c a t e d ( s a y o f v u n i ts o fc o m p u t a t i o n ) .

    T h e i n t r o d u c t i o n o f V i s a d v a n t a g e o u s ( a p a r t f r o mc o n s i d e r a t i o n s o f s t o r a g e e c o n o m y ) , i f

    n vn ( k - 1 ) u > m u o r - - ( k - - 1 ) > - ,m ui .e . if t h e g a i n i s g r e a t e r t h a n t h e l o s s in c o m p u t a t i o nu n i t s .

    A m o s t s t r a i g h t f o rw a r d s o l u t i o n t o o b t a i n a s i m p l ev e r s i o n o f tes tsquare i s t o i n t r o d u c e a B o o l e a n m a t r i xB s u c h t h a t B[i, j ] = t ru e s i g n if i e s t h a t sq u ar e ( i , j )22 4

    i s n o t t a k e n b y a n o t h e r q u e e n . B u t u n f o r t u n a t e l y , i t sr e c o m p u t a t i o n w h e n e v e r a n e w q u e e n i s r e m o v e d (v )i s p r o h i b i t i v e ( w h y ? ) a n d w i ll m o r e t h a n o u t w e i g ht h e g a i n .

    T h e r e a l i z a t i o n t h a t t h e r e l e v a n t c o n d i t i o n f o r s a f e t yo f a s q u a r e i s t h a t t h e s q u a r e m u s t l i e n e i t h e r i n ar o w n o r i n a d i a g o n a l a l r e a d y o c c u p i e d b y a n o t h e rq u e e n , l e a d s t o a m u c h m o r e e c o n o m i c c h o i c e o f V .W e i n t r o d u c e B o o l e a n a r r a y s a , b , c w i t h t h e m e a n i n g s :a k = t r u e : n o q u e e n i s p o s i t i o n e d i n r o w kbk = t r ue : n o q u e e n i s p o s i t i o n e d i n t h e / - d i a g o n a l kck = t r u e : n o q u e e n i s p o s i t i o n e d i n t h e \ - d i a g o n a l kT h e c h o i c e o f th e i n d e x r a n g e s o f th e s e a r r a y s i s m a d ei n v ie w o f t h e f a c t t h a t s q u a r e s w i t h e q u a l s u m o ft h e i r c o o r d i n a t e s l i e o n t h e s a m e / - d i a g o n a l , a n dt h o s e w i t h e q u a l d i f fe r e n c e l ie o n t h e s a m e \ - d i a g o n a l .W i t h r o w a n d c o l u m n i n d ic e s f r o m 1 t o 8 , w e o b t a i n :Boolean array a l l : 8 ] , b [ 2 :1 6 1 , c [ - - 7 : 7 1

    U p o n e v e r y i n t r o d u c t i o n o f a u x il i a r y d a t a , c a r e h a st o b e t a k e n o f th e i r correct initialization. S i n c e o u ra l g o r i t h m s t a r ts w i t h a n e m p t y c h e s s b o a r d , t h is f a c tm u s t b e r e p r e s e n t e d b y i n i t i a l l y a s s i g n i n g t h e v a l u et r u e t o a ll c o m p o n e n t s o f t h e a r r a y s a , b, a n d c . W ec a n n o w w r i t e :procedure t es t square;

    s a f e : = a [ x[ j ]] / ~ b [ j + x [ j ] ] / ~ c [ j - x [ j ] ]procedure s e t q u e e n ;a[x[j]] : = b [ ] + x [ j ] ] : = x [ j - x [ j ] ] : = fa lseprocedure r e m o v e q u e e n ;a[x[j]] : = b l j + x [ j ] ] : = c [ j - x [ ] ] ] : = true

    T h e c o r r e c t n e s s o f th e l a t te r p r o c e d u r e i s b a s e d o n t h ef a c t t h a t e a c h q u e e n c u r r e n t l y o n t h e b o a r d h a d b e e np o s i t i o n e d o n a s a f e s q u a r e , a n d t h a t a l l q u e e n s p o s i -t i o ne d a f t e r t h e o n e t o b e r e m o v e d n o w h a d a l r e a d yb e e n r e m o v e d . T h u s t h e s q u a r e t o b e v a c a t e d b e -c o m e s s a f e a g a in .

    A c r i ti c a l e x a m i n a t i o n o f t h e p r o g r a m o b t a i n e d s of a r r e v e a l s t h a t t h e v a r i a b l e x[j] o c c u r s v e r y o f t e n , a n d

  • 8/8/2019 p221 Wirth

    5/7

    i n p a r t i c u l a r a t t h o s e p l a c e s o f t h e p r o g r a m w h i c h a r ea l s o e x e c u t e d m o s t o f t e n . M o r e o v e r , e x a m i n a t i o n o fx [ j ] o c c u r s m u c h m o r e f r e q u e n t ly th a n r e a s s ig n m e n t o fv a l u e s t o j . A s a c o n s e q u e n c e , t h e p r i n c i p le o f i n t r o d u c -t i o n o f a u x i li a r y d a t a c a n a g a i n b e a p p l i e d t o i n c r e a s ee f f i c ie n c y : a n e w v a r i a b l einteger ii s u se d t o r e p r e s e n t t h e v a l u e s o f a r d e n o t e d b y x [ j ] .C o n s e q u e n t l y x [ j ] : = i m u s t a l w a y s b e e x e c u t e d b e f o r ej i s i n c reas ed , an d i : = x [ j ] a f t e r j i s d e c r e a s e d . T h i sf i na l s te p o f p r o g r a m d e v e l o p m e n t l e a d s t o t h e r e-f o r m u l a t i o n o f s o m e o f th e a b o v e p r o c e d u r e s a s fo l lo w s :procedure testsquare ;

    safie := a[i] A b[ i +j ] A c[i--]]procedure setqueen;a[i] := b[i+j] := e l i - - j ] : = falseprocedure rem o veq u een ;a[i] := b[ i j ] := c [ i - j ] := t r u eprocedure considerflrstcoh~mn ;begin := 1 ; i : = 0end

    p r o c e d u r e advancepointer; i : = i + lprocedure co n s i d ern ex tco l u m t Gb e gi nx [ j] : = i ; j : = j + l ; i : = 0 e n dBoolean procedure astsquare;tastsquare := i = 8

    T h e f i n a l p r o g r a m , u s i ng th e p r o c e d u r e stestsquarese /q u eenreg ressrem o veq u eena n d w i t h t he o t h e r p r o c e d u r e s d i r e c t l y s u b s t it u t e d , n o wh a s t h e f o r mj := 1; i : = 0 ;repeat

    repea t i : = i + 1 testsquareu n t i l sale V (i = 8) ;if saJb t h e nb e g i n setqueen; x[j] := i; j := j-q-l; i := 0en d else reg ress

    u n t i l ( j > 8 ) V ( J < 1 ) ;i f i > 8 then PRINT(x) else FAILU RE

    2 2 5

    I t i s n o t e w o r t h y t h a t t h i s p r o g r a m s t i l l d i s p l a y s t h es t r u c t u r e o f t h e v e r s i o n d e s i g n e d i n th e f i r s t s t e p . N a t u -r a l l y o t h e r , e q u a l l y v a l i d s o l u t i o n s c a n b e s u g g e s t e d a n db e d e v e l o p e d b y t h e s a m e m e t h o d o f s t e pw i s e p r o g r a mr e f i n e m e n t . I t i s p a r t i c u l a r l y e s s e n t ia l t o d e m o n s t r a t e t h i sf a c t t o s t u d e n t s . O n e a l t e r n a t i v e s o l u t i o n w a s s u g g e s t e dt o t h e a u t h o r b y E . W . D i j k s t r a . I t i s b a s e d o n t h e v i e wt h a t t h e p r o b l e m c o n s i st s o f a s t e p w i s e e x te n s i o n o f th eb o a r d b y o n e c o l u m n c o n t a i n i n g a s a f e ly p o s i t i o n e dq u e e n , s t a r t i n g w i t h a n u l l - b o a r d a n d t e r m i n a t i n g w i t h8 c o lu m n s . T h e p r o c e s s o f e x t e n d i n g t h e b o a r d i s f o r m u -l a t e d a s a p r o c e d u r e , a n d t h e n a t u r a l m e t h o d t o o b t a i na c o m p l e t e b o a r d i s by recu rs i o n n o f t h is p r o c e d u r e . I t c a ne a s i ly b e c o m p o s e d o f t h e s a m e s e t o f m o r e p r i m i t iv ei n s t r u c t i o n s w h i c h w e r e u s e d i n t h e f i r s t s o l u t i o n .p r o c e d u r e T ryco l u m n ( j ) ;

    beg in integer i ; i : = 0;r e p e a t i : = i + 1 ; t e s t s q u a r e ;

    i f s a g t h e nbegin setqueen; x[j] := i ;

    i f j < 8 t h e n T r y c o lu m n ( j + 1 ) ;i f -7 safe t h e n rem o veq u een

    en dunti l s a f e M ( i = 8 )

    en dT h e p r o g r a m u s i n g t h i s p r o c e d u r e t h e n i sT ryco l u m n (1 ) ;i f s a f e t h e n PRINT(x) else FAILU RE( N o t e t h a t d u e t o t h e i n t r o d u c t i o n o f th e v a r i a b l e i l o c a lt o t h e r e c u r s i v e p r o c e d u r e , e v e r y c o l u m n h a s i ts o w np o i n t e r o f i n s p e c t i o n i. A s a c o n s e q u e n c e , t h e p r o c e -d u r e stestsquarese t q u eenrem o veq u eenm u s t b e d e c l a r e d l o c a l l y w i th i n T r y c o l u m n t o o , b e c a u s et h e y r e f e r t o t h e i d e s i g n a t in g t h e s c a n n e d s q u a r e i n th ec u r r e n t c o l u m n . )

    5 . T h e G e n e r a l i z e d 8 - Q u e e n s P r o b l e m

    I n t h e p r a c t i c a l w o r l d o f c o m p u t i n g , i t i s r a t h e r u n -c o m m o n t h a t a p r o g r a m , o n c e i t p e rf o r m s c o r re c t l y a n ds a t is f a c t or i ly , r e m a i n s u n c h a n g e d f o r e v e r . U s u a l l y i t su s e r s d i s c o v e r s o o n e r o r l a t e r t h a t t h e i r p r o g r a m d o e sn o t d e l i v e r a l l t h e d e s i r e d r e s u l t s , o r w o r s e , t h a t t h e r e -s u lt s r e q u e s t e d w e r e n o t t h e o n e s r e a l l y n e e d e d . T h e ne i t h e r a n e x t e n s i o n o r a c h a n g e o f t h e p r o g r a m i s c a l le df o r , a n d i t i s i n t h i s c a s e w h e r e t h e m e t h o d o f s t e p w i s ep r o g r a m d e s i gn a n d s y s t e m a t i c s t r u c tu r i n g is m o s t v a l u -a b l e a n d a d v a n t a g e o u s . I f t h e s t r u c tu r e a n d t h e p r o g r a mc o m p o n e n t s w e r e w e ll c ho s e n , t h e n o f t e n m a n y o f th ec o n s t i t u e n t i n s t r u c t i o n s c a n b e a d o p t e d u n c h a n g e d .T h e r e b y t h e e f f o r t o f r e de s i g n a n d r e v e r i fi c a t io n m a y b ed r a s t i c a l ly r e d u c e d . A s a m a t t e r o f f a c t , t h e a d a p t a b i l i t yo f a p r o g r a m t o c h a n g e s i n i t s o b j e c t i v e s ( o f t e n c a l l e dm a i n t a i n a b i l i t y ) a n d t o c h a n g e s i n i ts e n v i r o n m e n tCom munica t ions Apri l 1971o f Vo lu me t4t h e A C M N u m b e r 4

  • 8/8/2019 p221 Wirth

    6/7

    (nowadays called portability) can be measured primarilyin terms of the degree to which it is neatly structured.

    It is the purpose of the subsequent section to demon-strate this advantage in view of a generalization of theoriginal 8-queens problem and its solution through anextension of the program components introduced be-fore.

    The generalized problem is formulated as follows:Finda ll possible configurationsof 8 hostile queens on an 8 N 8chessboard, such that no queen may be taken by any other queen.

    The new problem essentially consists of two parts:1. Finding a method to generate further solutions.2. Determining whether all solutions were generated

    or not.It is evidently necessary to generate and test candi-

    dates for solutions in some s y s t e m a t i c m a n n e r . Acommon technique is to find an o r d e r i n g o f c a n d i d a t e sand a condition to identify the last candidate. If anordering is found, the solutions can be mapped ontothe integers. A condition limiting the numeric valuesassociated with the solutions then yields a criterion fortermination of the algorithm, if the chosen methodgenerates solutions strictly in increasing order.

    It is easy to find orderings of solutions for the presentproblem. We choose for convenience the mapping

    8M ( x ) = ~ xj l0 j-1j = lAn upper bound for possible solutions is thenM ( x ~ ) = 88888888and the "convenience" lies in the circumstance that ourearlier program generating one solution generates theminimu m solution which can be regarded as the startingpoint fro m which to proceed to the next solution. This isdue to the chosen method of testing squares strictly pro-ceeding in increasing order of M ( x ) starting with00000000. The method for generating further solutionsmust now be chosen such that starting with the configu-ration of a given solution, scanning proceeds in the sameorder of increasing M, until either the next higher solu-tion is found or the limit is reached.

    6 . T h e E x t e n d e d P r o g r a m

    The technique of extending the two given programsfinding a solution to the simple 8-queens problem isbased on the idea o f modification of the global structureonly, and of using the same building blocks. The globalstructure mus t be changed such that upon finding a solu-tion the algorithm will produce an appropriate indica-tio n-e .g, by printing the solution--and then proceed tofind the next solution until it is found or the limit isreached. A simple condition for reaching the limit is theevent when the first queen is moved beyond row 8, inwhich case regression out of the first column will take

    place. These deliberations lead to the fol lowing modifiedversion o f the nonrecursive program:c o n s i d e r f i r s t c o l u m n ;repeat trycolumn;if sqfe thenbegin setqueef~; eonsMernextcolurrm;if lastcoMone thenbegin PRINT(x); regress

    en den d else regress

    until regressoutoffirstcolIndication of a solution being found by printing it nowoccurs directly at the level of detection, i.e. before leav-ing the repetition clause. Then the algorithm proceedsto find a next solution whereby a shortcut is used bydirectly regressing to the prior column; since a solutionplaces one queen in each row, there is no point in fur thermoving the last queen within the eighth column.

    The recursive program is extended with even greaterease following the same considerations:procedure T r y c o l u m n ( j ) ;begin integer i;(declarations of procedures estsquare, advaneequeen,setqueen, rem ovequeen, lastsquare)i :=0;repeat advaneequeen; testsquare;

    if safe thenbegin setqueen; xlj] := i;if -n lasteoldone then Tryeolumn(]+l) else PRINT(x);removequeenen duntil astsquare

    en dThe main program starting the algorithm then consists(apart from initialization of a, b, and e) of the singlestatement T r y c o l u m n ( 1 ) .

    In concluding, it should be noted that both programsrepresent the same algorithm. Both determine 92 solu-tions in the s a m e order by testing squares 15720 times.This yields an average of 171 tests per solution; the maxi-mum is 876 tests for finding a next solution (the firstone), and the minimum is 8. (Both programs coded inthe language Pascal were executed by a CDC 6400 com-puter in less than one second.)

    7 . C o n c l u s i o n sThe lessons which the described example was sup-

    posed to illustrate can be summarized by the followingpoints.

    1. Program construction consists of a sequence ofr e f i n e m e n t s t e p s . In each step a given task is broken upinto a number of subtasks. Each refinement in the de-scription of a task may be accompanied by a refinementof the description of the data which constitute the meansof communication between the subtasks. Refinement ofthe description of program and data structures shouldproceed in parallel.

    2. The degree o f m o d u l a r i ty obtained in this way will22 6

  • 8/8/2019 p221 Wirth

    7/7

    d e t e r m i n e t h e e a s e o r d i f fi c u lt y w i t h w h i c h a p r o g r a mc a n b e a d a p t e d t o c h a n g e s o r e x t e n s i o n s o f t h e p u r p o s eo r c h a n g e s i n t h e e n v i r o n m e n t ( l a n g u a g e , c o m p u t e r ) i nw h i c h i t i s e x e c u t e d .

    3 . D u r i n g t h e p r o c e s s o f s t e p w i s e r e f i n e m e n t , a no-tatiot~ w h i c h i s n a t u r a l t o t h e p r o b l e m i n h a n d s h o u l d b eu s e d a s l o n g a s p o s s i b l e . T h e d i r e c t i o n i n w h i c h t h e n o t a -t i o n d e v e l o p s d u r i n g t h e p r o c e s s o f r e f i n e m e n t is d e te r -m i n e d b y t he l a n g u a g e i n w h i c h t h e p r o g r a m m u s t u l ti -m a t e l y b e s p e c i f i e d , i . e . w i t h w h i c h t h e n o t a t i o n u l t i -m a t e l y b e c o m e s i d e n t i c a l . T h i s l a n g u a g e s h o u l d t h e r e -f o r e a l l o w u s t o e x p r e s s a s n a t u r a l l y a n d c l e a r l y a s p o s -s i n e t h e s t ru c t u re s o f p r o g r a m a n d d a t a w h i ch e m e r g ed u r i n g t h e d e s i g n p r o c e s s . A t t h e s a m e t im e , i t m u s t g i v eg u i d a n c e i n t h e r e f i n e m e n t p r o c e s s b y e x h i b i t i n g t h o s eb a s i c f e a t u r e s a n d s t r u c t u r i n g p r i n c i p l e s w h i c h a r c n a t u -r a l t o th e m a c h i n e b y w h i c h p r o g r a m s a r e s u p p o s e d t ob e e x e c u t e d . I t i s r e m a r k a b l e t h a t i t w o u l d b e d if f i cu l t t of i nd a l a n g u a g e t h a t w o u l d m e e t t h e s e im p o r t a n t r e q u i re -m e n t s t o a l e s s e r d e g r e e t h a n t h e o n e l a n g u a g e s t il l u s e dm o s t w i d e ly in t e a c h i n g p r o g r a m m i n g : F o r t r a n .

    4 . E a c h r e f i n e m e n t im p l i e s a n u m b e r o f design deci-sions b a s e d u p o n a s e t o f d e s ig n c r it e r ia . A m o n g t h e s ec r i t e r i a a r e e f f i c i e n c y , s t o r a g e e c o n o m y , c l a r i ty , a n d r e g -u l a r i t y o f s tr u c t u r e . S t u d e n t s m u s t b e t a u g h t t o b e c o n -s c i o u s o f t h e i n v o l v e d d e c i s i o n s a n d t o c r i t i c a l l y e x a m i n ea n d t o r e j e c t s o l u t i o ns , s o m e t i m e s e v e n i f t h e y a r e c o r -r e c t a s fa r a s t h e r e s u l t i s c o n c e r n e d ; t h e y m u s t l e a r nt o w e i g h t h e v a r i o u s a s p e c t s o f d e s i g n a l t e r n a t i v e s i n t h el i g h t o f t h e s e c r i t e r i a . I n p a r t i c u l a r , t h e y m u s t b e t a u g h tt o r e v o k e e a r l i e r d e c i s i o n s , a n d t o b a c k u p , i f n e c e s s a r ye v e n t o t h e t o p . R e l a t i v e l y s h o r t s a m p l e p r o b l e m s w i llo f t e n s u f f i c e t o i l l u s t r a t e t h i s i m p o r t a n t p o i n t ; i t is n o tn e c e s s a r y t o c o n s t r u c t a n o p e r a t i n g s y s t e m f o r th i sp u r p o s e .

    5 . T h e d e t a i l e d e l a b o r a t i o n s o n t h e d e v e l o p m e n t o fe v e n a s h o r t p r o g r a m f o r m a l o n g s t o r y , i n d i c a t i n g t h a tc a r e f u l p r o g r a m m i n g i s n o t a t r iv i a l s u bj e c t. I f t h is p a p e rh a s h e l p e d t o d i s p el t h e w i d e s p r e a d b e l i e f t h a t p r o g r a m -m i n g i s e a s y a s l o n g a s t h e p r o g r a m m i n g l a n g u a g e i sp o w e r f u l e n o u g h a n d t h e a v a i la b l e c o m p u t e r i s f a s te n o u g h , t h e n i t h a s a c h i e v e d o n e o f i ts p u r p o s e s .

    Acknowledgments. T h e a u t h o r g r a t e f u l ly a c k n o w l -e d g e s t h e h e l p f u l a n d s t i m u l a t i n g i n f lu e n c e o f m a n y d is -c u s s i o n s w i th C . A . R . H o a r e a n d E . W . D i j k s t r a .

    Announcement:G raphics and Im agePr oce s s i ng - -A N ew Departm ent inC om m u ni ca t i ons

    W i th th i s i s su e o f C o m m u n i c a t i o n s w e a r ep l e a s e d t o a n n o u n c e a n e w d e p a r tm e n t :G r a p h i c s a n d I m a g e P r o c e s s in g . T h e e d it o r o ft h i s d e p ar t m e n t w i l l b e W i l li a m N e w m a n o f t h eU n i v e r s i t y o f C a l i f o r n i a a t I r v i n e .T h i s d e p a r t m e n t w i l l c o v e r f i v e p r in c i p a lareas:QH a r d w a r e a n d s o ft w a r e t e c h n i q u e s f orp r o d u c i n g p i c t o r i a l o u t p u t ;oM e t h o d o l o g y f o r t h e d i g i ti z a ti o n a n d i n p u t o fp i c t o r i a l i n f o r m a t i o n ;oM e t h o d o l o g y f o r t h e d i g it a l p r o c es s in g a n ds t o r a g e o f s u c h i n f o r m a t i o n ;L a n g u a g e s a n d t e c h n i q u e s f o r i n t e r a c t in g w i t hgraphica l sys tems ;C o m p u t e r a p p l i c a t io n s in w h i c h a n y o f t h eabove p lay a s ign i f ican t ro le.

    R e f e r e n c e sTh e following articles are listed for further reference on the subjectof programming.1. Dijkstra, E. W. A constructive approach to the problem ofprogram correctness. BIT 8 (1968), 174-186.2. Dijkstra,~E. W. Notes on structured programm ing. EW D 249,Technical U. Eindhoven, The Netherlands, 1969.3. Naur, P. Programming by action clusters. BIT9 (1969) 250-258.4. Wirth, N. Programming and programming languages. Proc.lnternat. C omput. S yrup., Bonn, Germany, M ay 1970.2 2 7

    D r . N e w m a n i s p r o f e s so r o f i n f o r m a t i o n a n dc o m p u t e r s c i e n c e a t th e U n i v e r s i t y o f C a l i f o r n i aa t Ir v in e . P r e v i o u s l y , h e w a s a t th e U n i v e r s i t yo f U t a h i n t h e C o m p u t e r S c i e n c e D e p a r tm e n t .H e h o l d s a P h . D . i n C o m p u t e r S c i e n c e fr o mL o n d o n U n i v e r si ty , w a s a R e s e a r c h F e l l o wa t H a r v a r d , a n d h a s b e e n a m e m b e r o fACM s ince 19 67 .Com munica t ions Apri l 1971o f Vo lu me 1 4th e ACM Nu m b er 4