12

I found a puzzle in http://www.puzzles.ca/wordsearch/transportation.html where one has to find word in a grid and (s)he can read words from 8 directions. The following question raised to my mind:

We have been given a set of words. Find an algorithm which puts those words in n x m grid where n and m are given. Does anyone have suggestions for an algorithm to create suitable grid as the problem seems difficult if size of the grid is only just enough to fit alphabets to the grid and words overlaps each others?

puzzleenthusiast
  • 143
  • 1
  • 1
  • 5
  • Suggestions about what, exactly? – user541686 Jun 13 '11 at 15:44
  • Yeah, very interesting. An important thing to note is it's not even always possible just based on the math of n and m. For example, if I have 8 5-letter words with very few shared characters, it might not be possible to fit them in a 6x6 grid. However, 9 5-letter words with a bunch of shared characters would fit. So my first step would be to determine how many 'seed points' I would need, i.e. how many unique places a word would have to 'start' from. – NickHalden Jun 13 '11 at 15:46
  • 2
    When the set of words is arranged in the grid, should you fill in the gaps with random characters (will there be gaps?)? Do you have to make sure you do not accidentally create new words doing that? – Anders Lindahl Jun 13 '11 at 15:48
  • 1
    similar to [Algorithm to generate a crossword](http://stackoverflow.com/questions/943113/) – Nick Dandoulakis Jun 13 '11 at 15:49
  • @Mehrdad, suggestion to the algorithm, as fast as possible. @Anders Lindahl: In my case the rest alphabets can be arbitrary. – puzzleenthusiast Jun 13 '11 at 15:59
  • Since it's not always possible,I think you should work the other way arround. Try putting the sets of words in the smallest rectangle they can fit in. You an order the world by letter in common, then try all the possibilities with a tree. The complexity should be something like O(P^N) where N is the number of words and P to be defined depending on the number of position. – Ricky Bobby Jun 13 '11 at 16:16
  • I think it requires too much memory to put the whole tree into the memory in the case that grid is big and there are many words. I believe that some kind of recursion will do the job with less memory usage. I was thinking whether one can use some kind of heuristic to search the solution (best-first, IDA*, simulated annealing). But I was unable to evaluate time complexities of those algorithms or I have no experience whether those will work at all. And what kind of heuristic would be good for this problem? – puzzleenthusiast Jun 13 '11 at 18:42
  • I'm thinking that this might be NP-hard by a reduction from some sort of packing problem, but I'm not sure if this is the case. – templatetypedef Jun 13 '11 at 18:54
  • @templatetypedef: I heard once that this is a subcase of \Pi_3^p completeness problem thus NP-complete. However, I'm not familiar with that problem so I can't say anything. – puzzleenthusiast Jun 15 '11 at 16:07

1 Answers1

6

An algorithm is described in this SO question also

https://stackoverflow.com/a/23435654/3591273

Hope this helps

UPDATE: Summary of an algorithm (as given in previous link)

  1. Randomly select the first empty wordslot to be filled from the grid and fill it with a suitable word

  2. Find all empty wordslots that have intersections to already filled wordslots

  3. Sort them by a constraint ratio (eg number of available solutions for each one)

  4. Loop through the empty slots of previous step and try a number of candidate words (from the available words)

  5. Select the wordslot and the word to fill that retains grid consistency (ie grid has a solution after this word slot is filled with this word) and also the number of solutions in next step is maximum (this minimises bactracks in next steps) and go to step 2

  6. If no word found in previous step, try to backtrack to a previous word and use an alternative candidate (unless available candidates are exhausted)

  7. Optionally reset any word slots that might need reset after the backtrack (ie mark them as empty again) and go to step 2

  8. If no backtrack found then this configuration has no solution

  9. If all empty slots are filled you have one solution

Community
  • 1
  • 1
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
  • 3
    Could you put more info that just a sentence fragment and a link, please. That would be great, thanks! – ElectronicGeek May 02 '14 at 20:25
  • This is for crosswords, not wordsearch, so this fails to verify incidentally created matches with words that you think you still need to place, possibly even in multiples, thus allowing for multpiple (including wrong) solutions. – marcolz Dec 18 '18 at 14:30
  • @marcolz, given that I have implemented professional crossword makers with this algorithm, I can assure you that this does not happen. if an empty wordpslot is indeed filled by intersecting filled wordslots then this is taken into account. There are checks for that, not possible to give whole implementation which is long, only the general idea behind the algorithm. Indeed these cases are checked and taken care of, in the actual implementation – Nikos M. Apr 08 '20 at 13:40