0

What would be the time complexity? I just want to avoid this being O(n!). Would using depth first search be time complexity O(n^2), as for each letter it may have to go through all other letters worst case?

I guess I'm not sure if I'm thinking about this the right way.

When I say use depth first search, I mean starting depth first search from first the letter, and then starting from the second letter, etc.

Is that necessary?

Note:
The original problem is to find all possible words in a crossword/boggle board. I'm thinking of using the trie data structure to find if a word is in the dictionary, but am thinking about ways of generating the words themselves.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
snow
  • 191
  • 1
  • 2
  • 8
  • Do you mean you want to run all possible combinations in a crossword puzzle in order to find the solution? If this is the case, and there are `n` blank places, and you are running a brute force on **all** options, then there are `n!` possible combinations to check. – shapiro yaacov Jun 11 '15 at 06:02
  • But you can only go four directions or so from each letter, so wouldn't that lessen the complexity? Sorry for my confusion. The original problem is to find all possible words in the crossword puzzle. I was thinking of using the trie data structure to find if a word is in the dictionary, but was thinking about ways of generating the words themselves. Thank you for your response! – snow Jun 11 '15 at 06:11
  • Let's say there is a total of 20 words to place in the crossword. Hold an array of 20 nodes which each are the start of a list of all the words of the proper length. Filling this might be an issue, so I'd start with either very long or very short words (one letter words are "A", "I", and "establishmentarianism" will have few friends in that list). Then, since you now have some letter constraints, you could start narrowing down the options for the rest – shapiro yaacov Jun 11 '15 at 06:40
  • Is your question actually: "How should I go about solving a crossword puzzle with brute force (no definitions given)" ? – shapiro yaacov Jun 11 '15 at 06:44
  • The question is : how do I generate possible words from a cross-word puzzle. Let's assume that there is a data-structure that contains all the valid words (like a trie). – snow Jun 11 '15 at 07:03
  • So using the my idea from the third comment, hold a trieX (just invented it - it's a trie with words of length X), and eliminate words depending on intersections between them. Again, I would recommend starting from either long or VERY short words. Another issue you will need to deal with is your termination property. – shapiro yaacov Jun 11 '15 at 07:14

1 Answers1

0

Following the discussion above, here is my answer:

Definition: a trieX is a sub trie, with words of length X only.

Since we have a trie with all words in the desired language, we can also get the appropriate trieX.

We say that the crossword puzzle has w words, so we create an array w long where each entry is the root of a trieX where X is the length of the relevantword. This gives us the list of possible words in each blank word.

Then the iterate over intersections between words and eliminate words that can not be placed. When there are no more changes done - we stop.

Two remarks:
1. In order to improve performance, we start by adding either long words, or VERY short ones. What is short or long? have a look at this and this.
2. Elimination of words from the trieX's can also be done by checking dependencies between words (if THIS words is here, then THAT words can't be there, etc.). This is more complicated, so if anyone wants to add some ideas on how to do this easily - please do.

shapiro yaacov
  • 2,308
  • 2
  • 26
  • 39