On Some Entertanng Applcatons of the Concept of Set n Computer Scence Course Krasmr Yordzhev *, Hrstna Kostadnova ** * Assocate Professor Krasmr Yordzhev, Ph.D., Faculty of Mathematcs and Natural Scences, South-West Unversty, Blagoevgrad, Bulgara, Е-mal: yordzhev@swu.bg ** Ph.D. student Hrstna Kostadnova, Faculty of Mathematcs and Natural Scences, South- West Unversty, Blagoevgrad, Bulgara, Е-mal: kostadnova@swu.bg Abstract: Some aspects of programmng educaton are examned n ths work. It s emphassed, based on the entertanment value, the most approprate examples are chosen to demonstrate the dfferent language constructons and data structures. Such an example s the demonstrated algorthm for solvng the wdespread nowadays Sudoku puzzle. Ths s made, because of the connecton wth the term set and puttng t nto practce n the programmng. Usng the so bult program there are solved some combnatoral problems, connected to the Sudoku matrces. Key words: Educaton n programmng, programmng languages, data structures, set, Sudoku matrx, Sudoku puzzle. INTRODUCTION The present work s contemplated to help the lecturer n programmng n hs aspraton for gvng an approprate, nterestng and entertanng example of the advantage to use the term set n programmng. For ths purpose the students have to be famlar wth the basc defntons of the set theory and to be skllful at the basc operatons wth sets. It follows the well-known fact that to be a good programmer t s necessary (but not effcent) to be a good mathematcan. A classcal example for the use of sets n programmng has become the programmng realzaton of the problem for fndng prme numbers usng the method Seve of Eratosthenes [,5,8]. How to construct a faster algorthm, solvng the problem for recevng all n n bnary matrces, whch contan exactly k ones n each row and each column, wth the help of the set theory and the operatons over sets, s shown n [10]. We wll examne the entertanng and actual problem, whch s very nterestng for the students algorthms for solvng Sudoku. It s wdespread puzzle nowadays, whch presents n the entertanng pages n most of the newspapers and magaznes and n entertanng web stes. Sudoku, or Su Doku, s a Japanese word (or phrase) meanng somethng lke Number Place. On the other sde, Sudoku matrces fnd an nterestng combnatoral applcaton, for example n the desgn theory [7]. The connecton between the set of all m m permutaton matrces (.e. bnary matrces whch contan ust one 1 n every row and every column) and the set of all m m Sudoku matrces, s shown n [3].
As far as the authors of ths study know there s not a unversal formula for the number of Sudoku matrces n wth every natural number n. We consder t as an open problem n mathematcs. When n 3 n [4] s shown that there are exactly = 6 670 903 75 01 07 936 960 = n number Sudoku matrces. 3 7 = 9! 7 7 704 67 971 = 0 8 = 3 57 7 704 67 971 6.67110 PROBLEM FORMULATION AND ALGORITHM DESCRIPTION Let n s a postve nteger and let n. Let S s s a square m m matrx, whose elements n ths table are ntegers, belongng to the closed nterval1,m. The matrx S s dvded to n, n n square submatrces, whch are not ntersected and wll be called blocks, wth the help of n 1 horzontal and n 1 vertcal lnes (the matrx S, when n 3, s shown on fg. 1). 1 m s 11 s 1 s 13 s 14 s 15 s 16 s 17 s 18 s 19 s 1 s s 3 s 4 s 5 s 6 s 7 s 8 s 9 s 31 s 3 s 33 s 34 s 35 s 36 s 37 s 38 s 39 s 41 s 4 s 43 s 44 s 45 s 46 s 47 s 48 s 49 s 51 s 5 s 53 s 54 s 55 s 56 s 57 s 58 s 59 s 61 s 6 s 63 s 64 s 65 s 66 s 67 s 68 s 69 s 71 s 7 s 73 s 74 s 75 s 76 s 77 s 78 s 79 s 81 s 8 s 83 s 84 s 85 s 86 s 87 s 88 s 89 s 91 s 9 s 93 s 94 s 95 s 96 s 97 s 98 s 99 Fg. 1 Let denote by A, k, l n the blocks n the above descrbed matrx S s. Then by defnton f s A, then ( k 1) n kn and ( l 1) ( l 1). Let s belong to the block Ak l and let and are known. Then t s easy to guess that k and l can be calculated wth the help of the formulas 1 k 1 n and 1 l 1, n where, as usual, we denote by x the functon: whole part of the real number x. set We say that s 1,,, m n 1 S, 1, m n s a Sudoku matrx f there s ust one number of the Z m n every row, every column and every block. The puzzle named Sudoku s wdespread nowadays. It s gven a Sudoku matrx, n whch some of the elements are erased. The mssng elements wll be equal to 0, f we need ths. The task n the puzzle s to restore the mssng elements of the Sudoku matrx. It s supposed that the authors
of the concrete puzzle have chosen the mssng elements, so the problem has only one soluton. Ths condton we wll mss and wll not reckon wth t. In ths work we wll buld our programmng product to show the number of every possble soluton. If the task has no soluton ths number has to be zero. The most popular puzzles Sudoku are when n 3,.e. m 9. We are gong to descrbe an algorthm for creatng a computer program, whch fnds all solutons (f there are some) of random Sudoku. For ths am we wll use the knowledge of the set theory. We examne the sets, and, where 1, m n, 1 k, l n. For every R C B 1,,,m, the set R conssts of all mssng numbers n the -th row of the matrx. Analogously we defne the sets C, 1,,, m correspondngly for the mssng numbers n the -th column and B, k, l 1,,, n correspondngly for the mssng numbers n the blocks A of S. When the algorthm starts workng t traverses many tmes all of the elements S, such that s 0,.e. these are the elements, whch real values we have to fnd. Let s 0 and let s A. We assume Then the followng three cases are possble: P R C B. ) P (empty set). The task has no soluton n ths case; ) P d, d Z m 1,,, m,.e. P : the number of the elements of P s equal to 1 ( P s a set contanng one element). Then the only one possblty for s s s d,.e. we have found the unknown value of s n ths case. After ths we remove the common element d from the sets R, C and B, and then we contnue to the next zero element of the matrx S (f there s such an element); ) P. Then we can not say anythng about the unknown value of s and we move on the next mssng (zero) element of the matrx S. We traverse all zero elements of the matrx S untl one the followng events occur: е1) For some, {1,,, m} s true s 0, but P R C B ; е) All elements n S become postve; е3) All zero elements of S are traversed, but t does not occur nether event е1, nor event е. In other words for all the remanng zero elements n S, the above descrbed case s always true. In case that t occurs one of the events е1 or е, then the procedure stops ts work and vsualzes the obtaned result. In case that event e3 occurs, then the algorthm has to contnue workng by usng other methods, for example t has to apply the tral and error method. In the concrete case ths method conssts of the followng thngs: 1 1 We choose random s S, such that s 0 and let k 1, l 1 n n. Let P R C B d, d, d }. Then for every d r P, r 1,,, t we assume s d. Such { 1, t an assumng we call a random tral. We count the number of all random trals, untl the soluton s found, n the programmng realzaton of the algorthm. After ths we solve the problem for fndng the unknown elements of the Sudoku matrx, whch contan one element less than the prevous s r
matrx. It s comfortable to use a recurson here. The base of the recurson,.e. we go out of the procedure f there occurs event e1 or e. It s absolutely sure that t wll happen (.e. there wll not be an nfnte cycle ), because when we do the random trals we reduce the number of the zero elements by 1. The above descrbed algorthm s realzed n the Pascal programmng language, because of the two man reasons: 1) Pascal s chosen, of the most educatonal nsttutons n the system of the secondary educaton, as a frst programmng language. Ths means that the basc am of ths work s realzed to help the teacher n hs preparaton n the process of fndng nterestng applcaton of the teachng materal. That s the way the presented materal becomes more nterestng for the pupls. ) There are bult n nstruments used to work wth sets n the Pascal programmng language. The above descrbed deas can be realzed n other random programmng language, for example C++. But n ths case we have to look for addtonal nstruments to work wth sets for example the assocatve contaners set and multset realzed n Standard Template Lbrary (STL) [1,5]. It can be used the template class set of the system of computer algebra Symbolc C++, programmng code s gven n detals n [9]. Of course can be bult another class set, and specfc methods of ths class can be descrbed, as a tranng. Ths s a good exercse, havng n mnd the fact that the cardnal number of the basc ( unversal ) set s not very bg. For example the standard puzzle Sudoku has basc set the set of the ntegers from 1 to 9 plus the empty set. SOME COMBINATORIAL APPLICATION Obvously, f for solvng gven Sudoku, there s no need to use the tral and error method,.e. the problem can be solved by usng set theoretcal operatons (0 random trals), then ths Sudoku has a unque soluton. Such a Sudoku s shown on fgure. The opposte proposton s not true, as t s n the next example, shown on fgure 3, where there s a unque soluton and program has made 18 random trals only to receve ths soluton, as the number of the random trals (after the fnal soluton s receved, the program contnues workng f t s necessary) s 33. 3 5 3 8 4 1 6 4 5 7 7 8 5 9 1 6 4 6 3 3 1 7 6 1 4 7 8 3 4 8 6 7 1 5 7 3 4 9 1 1 4 9 7 4 5 3 8 1 7 6 3 7 5 8 4 9 Fg. Fg. 3 Sometmes the Sudoku authors do not examne f the soluton s unque. For example the Sudoku, shown on fgure 4 (s publshed n a newspaper s rubrc Easy Sudoku, the name of the newspaper we wll not menton) has 4 dfferent solutons, the program does 16 random trals untl the fnal soluton s obtaned, and the common number of the random trals s 18. The Sudoku puzzle shown on fgure 5 has no soluton, although on a frst sght there s not contradcton n the condton. The program has to make 1 random trals to receve the result that ths puzzle has no soluton.
4 9 3 8 8 9 3 3 8 4 1 7 9 4 1 4 7 8 3 7 5 8 7 3 5 7 9 3 8 5 5 9 4 7 9 8 4 8 7 3 1 8 9 5 8 9 3 7 5 3 1 3 4 5 7 4 8 7 8 3 Fg. 4 Fg. 5 There are even more combnatoral results wth dfferent Sudoku matrces, receved by the help of the above descrbed algorthm and some experments. There are 83 576 Sudoku matrces of the knd shown on fgure 6; There are 6 80 Sudoku matrces of the knd shown on fgure 7; There are 680 Sudoku matrces of the knd shown on fgure 8; There are 1 78 Sudoku matrces of the knd shown on fgure 9; There are Sudoku matrces of the knd shown on fgure 10; There are 8 Sudoku matrces of the knd shown on fgure 11; 1 3 * * * * * * 4 5 6 * * * * * * 7 8 9 * * * * * * * * * 1 3 * * * * * * 4 5 6 * * * * * * 7 8 9 * * * * * * * * * 1 3 * * * * * * 4 5 6 * * * * * * 7 8 9 1 3 4 5 6 7 8 9 4 5 6 7 8 9 1 3 7 8 9 1 3 4 5 6 3 1 * * * * * * 5 6 4 * * * * * * 8 9 7 * * * * * * 3 1 * * * * * * 6 4 5 * * * * * * 9 7 8 * * * * * * Fg. 6 Fg. 7 1 3 4 * 6 7 8 9 4 5 6 * * * 1 3 7 8 * * * * * 5 6 * * * * * * * 1 8 * * * * * * * 7 3 4 * * * * * 1 6 7 8 * * * 3 4 5 9 1 3 * 5 6 7 8 1 3 4 5 6 7 8 9 4 5 6 7 8 9 1 3 7 8 9 1 3 4 5 6 3 4 5 6 7 8 9 1 5 6 7 8 9 1 3 4 8 9 1 3 4 5 6 7 Fg. 8 Fg. 9
1 3 4 5 6 7 8 9 4 5 6 7 8 9 1 3 7 8 9 1 3 4 5 6 3 4 5 6 7 * * * 5 6 7 8 9 1 * * * 8 9 1 3 4 * * * 3 4 5 * * * * * * 6 7 8 * * * * * * 9 1 * * * * * * 1 * * 7 * * 4 * * * * * 8 * * 5 * * * 3 * * 9 * * 6 7 * * 4 * * 1 * * * 8 * * 5 * * * * * 9 * * 6 * * 3 4 * * 1 * * 7 * * * 5 * * * * 8 * * * 6 * * 3 * * 9 Fg. 10 Fg. 11 REFERENCES [1] Collns W. J., Data structures and the standard template lbrary. McGraw-Hll, 00. [] Dahl D. J., Dkstra E. W., Hoare C. A. R., Structured Programmng. Academc Press Inc., 197. [3] Dahl G., Permutaton matrces related to Sudoku. Lnear Algebra and ts Applcatons, 009, 430, 457 463. [4] Felgenhauer B., Jarvs F. Enumeratng Possble Sudoku Grds. Preprnt avalable at http://www.afarvs.staff.shef.ac.uk/sudoku/sudoku.pdf [5] Jensen, K., Wrth N., Pascal User Manual and Report. 3 rd ed., Sprnger-Verlag, 1985. [6] Lschener R., STL Pocket Reference. O Relly Meda, 004. [7] Mo Hu-Dong, Xu Ru-Gen, Sudoku Square a New Desgn n Feld Experment. Acta Agronomca Snca, 008, 34(9), 1489 1493. [8] Prce D., UCSD Pascal A Consderate Approach. Prentce-Hall, 1983. [9] Sh T. K., Steeb W.-H., Hardy Y., Symbolc C++: An Introducton to Computer Algebra usng Obect-Orented Programmng, Sprnger, 000. [10] Yordzhev, K., Stefanov S., On Some Applcatons of the Consept of Set n Computer Scence Course. Mathematcs and Educatons n Mathematcs, 003, v.3, 49-5.