Crossword Solver
Crossword Solver
Crossword Solver
Faculty of Engineering
Research and Postgraduate Division
Master of Science
in
Artificial Intelligence
Machine Learning
Assignment 1
Student: Professor:
José Armando PhD. Marco Antonio
Lara Ramos Aceves Fernández
February 2, 2020
Contents
1 Introduction 2
1.1 Crossword Puzzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Theoretical Framework 2
2.1 Divide and Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5 Development 4
6 Results 5
6.1 Test with crossword puzzle in English . . . . . . . . . . . . . . . . . . . . . . . . . . 5
6.2 Test with crossword puzzle in Spanish . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7 Discussion 8
8 Commented Code 9
9 Conclusions 9
10 References 10
Appendices 11
A Commented Code 11
1
Crossword Puzzle Solver
1 Introduction
In this assignment a crossword puzzle solver program is design and implemented. The algorithm
approach used was divide and conquer applied through recursion. The practical implementation
was made in MATLAB R2018b ®.
The crossword puzzle used to implement and test the program is the one shown in Figure 1,
and the set of words to complete it are displayed in the Code Snippet 1.
1 ant, end, big, car, bus, has, had, tar, book, bugs, boot, boom, fuse, seek,
,→ have, hold, lane, look, live, year, ginger, matrix.
2 Theoretical Framework
In this section theory about the strategy used to solve puzzle is explained.
Divide: A problem is divided into sub-problems which are easier of faster to solve.
With based on the two previously described steps, sometimes it is needed a third step which
consists of gathering the solutions and generate the solution to the bigger problem.
2.2 Recursion
Recursion is an algorithm approach which a function, method or sequence of steps calls itself within
itself. One basic example of this notion is the Fibonacci sequence where the following number in
the sequence is the sum of the two previous numbers, this is shown in Equation 1.
f (n) = f (n − 1) + f (n − 2) (1)
Every recursion implementation is formed of two parts
Base cases: An initial case or cases which can be solved without recursion.
Recursive case: For the cases that are to be solved recursively, the recursive call must always
be to a case that makes progress toward a base case.
One of the advantages of recursion implementation is that it can simplify the solution to a
problem in an intuitive and elegant manner. However, one should be cautious in its use when
efficiency is demanded, i.e., when recursion is applied each recursive case is one step further which
consumes more memory in a stack data structure. Each solution approach with recursion can
be implemented with more efficient iterative methods, nevertheless as efficiency is not our main
concern in this work, recursion is applied.
5 Development
The implementation of the algorithm was made in MATLAB®code. It consists of four functions:
One function to try to fit a word horizontally into the crossword in a certain position x, y.
One function to try to fit a word vertically into the crossword in a certain position x, y.
One function to check if the crossword is already solved and one recursive function to solve
the puzzle starting from a certain word in the set of words.
The representation of the crossword grid and the set of words that can be used are shown as
code in the Code Snippet 3 and Figure 3.
1 % crossword configuration
2 % an asterisk represents a black square
3 % a question mark represents a white square
4 crossword = ['?' '*' '?' '*' '*'; '?' '*' '?' '*' '*'; '?' '?' '?' '?' '*';
,→ '?' '*' '?' '*' '*'; '*' '*' '?' '?' '?'; '*' '*' '?' '*' '*'];
5
6 % set of words
7 dictionary = ["ant" "end" "big" "car" "bus" "has" "had" "tar" "book" "bugs"
,→ "boot" "boom" "fuse" "seek" "have" "hold" "lane" "look" "live" "year"
,→ "ginger" "matrix"];
Notice that the search method implemented in this program is very similar to that the depth-
first-search algorithm when going through binary trees, such search method is represented in
Figure 4. In other words, in this implementation, every word which is fitted into white spaces and
a recursive function called is analogous to progress a depth level in the depth-first-search.
6 Results
6.1 Test with crossword puzzle in English
The crossword puzzle solver program worked correctly and returned in console a unique found
solution. The solution is shown in Figure 5.
As the search method is similar to a depth-first-search approach, during debugging there were
visible several attempts of the program to complete the crossword although not completely reaching
the objective. After this attempts the search continued through other words (or children). In
Figure 6 are shown several unsuccessful search paths derived from placing first the word “ant” in
the crossword.
In Figure 7 some states of the crossword during search within the “end” are shown, the cross-
words on the top are result of unsuccessful sub-searches, but the crosswords on the bottom are the
path followed when the solution was found.
The set of words in Spanish which have to be used to complete the crossword are shown in
Code Snippet 4.
1 perro, cono, gato, raton, mono, tigre, leon, buho, peon, cerdo.
In order to run this test, the corresponding variables crossword and dictionary for the
crossword in Spanish in the code in Appendix A in Code Snippet 5 were uncommented, were as
the same variables for the crossword in English.
There were found 8 different correct solutions to the crossword which are shown in Figure 9.
The program successfully returned all possible solutions.
7 Discussion
In this work a program which solves a crossword puzzle given a set of words was implemented. The
approach taken was to search the solution by trying to fit each word iterating thorough the set
of given words through recursion. It was observed that although the recursive approach considers
several combinations, the program will provide all the solutions. One of the main challenges to
make such feature possible. Another challenge was to make the program find the possible solutions
despite there could be some word within the set of words which will not be used in the crossword,
i.e., to find a way to detect when a word is unuseful and then pass it. Finally, the most difficult
part was to figure out a way to keep in memory the progress made when one word has been fit
in the crossword and then the rest are left, recursion eases this task by elegantly and internally
keeping the local variables saved in a stack data structure so that they could be available when
returning to the calling context.
8 Commented Code
The complete commented code of this program is included in Appendix A in Code Snippet 5.
9 Conclusions
It is important to try to solve games or problems by using traditional data structures or algorithms.
Gaining experience by understanding and solving the problem with these tools could provide come
insights when trying to solve the problem with more advanced or suited techniques. It will be
interesting to revisit this assigment after finalizing the course in order to know which technique
could be applied to the resolution and compare aspects among approaches and solutions.
Barroso, Clidaras, and Hölzle (2013) (Data Center Handbook , 2014) (Alger, 2012)
10 References
Alger, D. (2012). The art of the data center: A look inside the world’s most innovative and
compelling computing environments (1st ed.). USA: Prentice Hall Press.
Barroso, L. A., Clidaras, J., & Hölzle, U. (2013). The datacenter as a computer: An in-
troduction to the design of warehouse-scale machines, second edition. Synthesis Lec-
tures on Computer Architecture, 8 (3), 1-154. Retrieved from https://doi.org/10.2200/
S00516ED2V01Y201306CAC024 doi: 10.2200/S00516ED2V01Y201306CAC024
Data center handbook. (2014). John Wiley & Sons, Ltd. Retrieved from https://onlinelibrary
.wiley.com/doi/abs/10.1002/9781118937563.fmatter doi: 10.1002/9781118937563
.fmatter
Appendices
A Commented Code
Code Snippet 5: Commented MATLAB code.
1 clc
2 clear
3
4 % crossword configuration
5 % an asterisk represents a black square
6 % a question mark represents a white square
7 % crossword in English
8 crossword = ['?' '*' '?' '*' '*'; '?' '*' '?' '*' '*'; '?' '?' '?' '?' '*';
,→ '?' '*' '?' '*' '*'; '*' '*' '?' '?' '?'; '*' '*' '?' '*' '*'];
9 % crossword in Spanish
10 %crossword = ['' '*' '*' '*' ''; '' '*' '*' '*' ''; '' '' '' '' ''; '' '*'
,→ '' '*' ''; '' '*' '' '*' '*'; '*' '*' '' '*' '*'; '*' '' '' '' '?'];
11
12 % set of words
13 % dictionary in English
14 dictionary = ["ant" "end" "big" "car" "bus" "has" "had" "tar" "book" "bugs"
,→ "boot" "boom" "fuse" "seek" "have" "hold" "lane" "look" "live" "year"
,→ "ginger" "matrix"];
15 % dictionary in Spanish
16 %dictionary = ["perro" "oso" "gato" "raton" "mono" "tigre" "leon" "buho"
,→ "vaca" "cerdo"];
17 print_crossword(crossword);
18 % solving crossword starting from first word
19 solve_crossword(crossword, dictionary, 1);
20
32 fprintf('\n');
33 end
34
75 return;
76 end
77 end
78 % checking if word fits perfectly
79 % checking if position before the beginning of the word is empty
80 if x ~= 1
81 if crossword(x-1, y) ~= '*'
82 crossword(1,1) = 'N';
83 return;
84 end
85 end
86 % checking if position after ending of the word is empty
87 if x+word_length <= size(crossword, 1)
88 if crossword(x+word_length, y) ~= '*'
89 crossword(1,1) = 'N';
90 return;
91 end
92 end
93 end
94