21

In the game Hangman, is it the case that a greedy letter-frequency algorithm is equivalent to a best-chance-of-winning algorithm?

Is there ever a case where it's worth sacrificing preservation of your remaining lives, for the sake of a better chance of guessing the correct answer?

Further clarification of the problem:

  • The selected word to be guessed has been taken from a known dictionary.
  • You are given N lives, and thus have to maximise the probability of guessing all the letters in the word without making N mistakes (i.e. you may have an unlimited number of correct guesses).
  • Each word in the dictionary has equal probability, for the sake of this exercise (i.e. the word is chosen randomly)
    • a harder exercise would be to come up with a strategy against a malicious, omniscient word-chooser (I am not asking that here)

Motivation: This question is inspired by the interesting discussion at http://www.datagenetics.com/blog/april12012/index.html

They suggest an algorithm for solving the word game "Hangman" optimally.

Their strategy can be summarised thus (edited for clarification):

  • We can assume the word is drawn from a particular dictionary
  • We know the number of letters in the word
  • Eliminate all words in the dictionary that do not have the correct number of letters.
  • Choose the not-yet-guessed letter which occurs in the largest number of words in the remaining subset of the dictionary.
  • If this letter occurs, we know its location.
  • If this letter does not occur, we know it does not occur in the word.
  • Eliminate all words in the dictionary subset that do not fit exactly this correct pattern, and repeat.
  • If there are 2 (or more) letters appearing equally often, the algorithm may perform a deeper analysis of the positions to determine which one is preferred (if that is reasonable?)

At each stage, we are guessing the letter (not previously guessed) which occurs in the largest number of remaining possible words.

There is some motivation to like this algorithm - we are always minimally likely to lose a life.

But, it strikes me that this isn't necessarily the best solution: if we're trying to guess the word (within a certain number of lives), it's not necessarily always the case that the most frequently occurring letter is the most useful letter to distinguish between the remaining available words.

I'm not sure, though, as it does seem opportune to avoid losing a life wherever possible. Will it ever be the case that optimal strategy permits us to sacrifice a life for a better opportunity to win?

Question: is it the case that this greedy algorithm is equivalent to a best-chance-of-winning algorithm, or not? And can you prove it?

An example dictionary+game would be ideal to show a disproof.

Ronald
  • 325
  • 1
  • 2
  • 9
  • 1
    What do you refer to as `best-chance-of-winning algorithm`? – Iulius Curt Mar 30 '12 at 12:42
  • @iuliux By the 'standard' rules of Hangman - you win a game if you complete the word without losing N lives (N incorrect letter guesses). The best-chance-of-winning algorithm is, I suppose, a theoretical algorithm that gives you the chance of winning on the maximum possible percentage of words in the dictionary. – Ronald Mar 30 '12 at 12:51
  • for the sake of the question, you may assume the word selection is random rather than by an ominiscient opponent – Ronald Mar 30 '12 at 12:56

9 Answers9

11

Assume the following dictionary: ABC ABD AEF EGH. (I'll capitalize unguessed letters.)
Assume you have only 1 life (makes the proof so much easier...).

The algorithm proposed above:

Starting with A, you lose (1/4) or are left with aBC aBD aEF (3/4).
Now guess B and lose (1/3) or are left with abC abD (2/3).
Now guess C or D and you lose (1/2) or win (1/2).
Probability to win: 3/4 * 2/3 * 1/2 = 1/4.

Now try something else:

Starting with E, you lose (1/2) or are left with AeF eGH (1/2).
Now you know what to guess and win.
Probability to win: 1/2.

Clearly the proposed algorithm is not optimal.

Reinstate Monica
  • 4,568
  • 1
  • 24
  • 35
  • It is just an example for a special dictionary with only one life. – knivil Apr 03 '12 at 14:37
  • 1
    @knivil; Yes, exactly what was asked.: An example showing that the proposed strategy is sometimes not optimal. – Reinstate Monica Apr 03 '12 at 15:21
  • Nothing is optimal for every example. If a list is sorted then bubble sort is better than other sorting strategies. Your answer is correct and valid but you gain no knowledge. – knivil Apr 03 '12 at 15:52
  • 1
    @knivil: Well, I did gain the knowledge that the propose algorithm is not optimal in all cases. But it is clear that an optimal algorithm (i.e. one that finds the optimal move) does exist. So I don't understand your comments. (All sorting algorithms sort the list, so all sorting algorithms are optimal in that sense.) – Reinstate Monica Apr 03 '12 at 21:38
  • Yes; if it's not optimal for this toy example, that places the burden of proof _solidly_ in the hands of anyone continuing to hypothesize it might be optimal for English. Maybe it is, maybe it isn't; but "maybe" won't cut it in the wake of the toy proof this answer comprises. – JamesTheAwesomeDude Apr 06 '21 at 04:42
8

There are some critical assumptions you have to make as to what a game of "Hangman" is.

  • Do you just have one word you need to guess, or do you need to guess a phrase?
  • Are some words more likely than others?

One thing to remember is that if you pick a correct letter, you do not lose a guess.

I will provide a solution for the one-word-&-equally-likely case. The two-word case can be generalized by creating a new dictionary equal to the cartesian product of your current dictionary, etc. The more-likely-than-others case can be generalized with a bit of probability.

Before we define our algorithm, we define the concept of a reduction. If we were to guess letters L1,L2,L3,...LN all at ONCE, then we would reduce the dictionary to a smaller dictionary: some words would be eliminated, and additionally some letters may also be eliminated. For example if we had the dictionary {dog, cat, rat} and we guessed a, we would eliminate {d,g} if the guess was true, or also eliminate {c,r,t} if it was false.

The optimal algorithm is as follows:

  • Consider the game tree
  • Look at all nodes where [#guesses left == 1]
  • If there is no node, the game is impossible; if there is a node, that is your answer

Of course that is how you solve any game, and for the most part it is intractable due to the exponential size requirements. You cannot get optimal unless you perfectly replicate this, and I seriously doubt that a strategy which doesn't "look" two or more moves ahead can hope to replicate this. You can however attempt to approximate the optimal strategy as follows.

Repeat the following at each step:

  • Consider each letter: choose the letter which will maximize the expected-dictionary-reduction per expected-penalty: that is, pick the letter L which will maximize the (frac words with L#words without L + frac words without L#words with L)/(# words without L/# words total)... note that this may be infinite if all the words have a certain letter, in which case go ahead and guess it since there is no penalty.
  • Make your guess, get an updated board state
  • Eliminate all words invalidated by the new board

Of course if your dictionary has more than 2^[max number of guesses] entries, the "Hangman" game is mostly impossible in the equal-probability world (unless the dictionary is highly constrained), so you have to work in the unequal-probability world. In this world, rather than maximizing the amount of elimination you do, you maximize the "expected surprisal" (also called entropy). Each word you associate a prior probability (e.g. let's say there is a 0.00001 chance of the word being 'putrescent' and a 0.002 chance of the word being 'hangman'). The surprisal is equal to the chance, measured in bits (the negative log of the chance). An answer to a guess will either yield no letters, a single letter, or more than one letter (many possibilities). Thus:

  • For each possible guess, consider the effect that guess would have
  • For each possible outcome of the guess, consider the probability of that outcome. For example if you guessed 'A' for a 3-letter word, you'd have to consider each possible outcome in the set {A__, _A_, __A, AA_, A_A, _AA, AAA}. For each outcome, calculate the probability using Bayes's rule, and also the new possible dictionaries (e.g. in one case, you'd have a dictionary of _A_:{cAt, bAt, rAt, ...} and A__:{Art, Ark, Arm, ...} etc.). Each of these new dictionaries also has a likelihood ratio, of the form size(postOutcomeDictionary dictionary)/size(preOutcomeDictionary); the negative log of this ratio is the amount of information (in bits) which the choice conveys to you.
  • Thus you want to maximize the ratio of the expected amount of information (in bits) you gain, per the expected cost (the cost penalty is 1 if you fail and 0 if you don't). For each guess, and for each outcome of the guess, the bits-of-information-gained is bitsGainedFromOutcome[i] = -log(size(postOutcomeDictionary)/size(preOutcomeDictionary)). We take the weighted sum of these: sum{i}( prob(outcome[i])*bitsGainedFromOutcome[i] ), then divide by the probability we are wrong: prob(outcome=='___').
  • We pick the letter with the minimum sum{i}( prob(outcome[i])*bitsGainedFromOutcome[i] )/prob(outcome=='___'); in case this is infinity, there is nothing to lose, and we automatically pick it.

So to answer your question:

>In the game Hangman, is it the case that a greedy letter-frequency algorithm is equivalent to a best-chance-of-winning algorithm?

Clearly not: if the dictionary was

cATs
bATs
rATs
vATs
sATe
mole
vole
role

your algorithm would guess a or t, which have a 5/8 chance of reducing your dictionary to 5/8 size for free, and a 3/8 chance of reducing your dictionary to 3/8 size for a cost of 1. You want to choose letters which reveal the most information. In this case, you should guess S, because it has a 4/8 chance of reducing your dictionary to 4/8 size for free, a 1/8 chance of reducing your dictionary to 1/8 size for free, and a 3/8 chance of reducing your dictionary to 3/8 size for a cost of 1. This is strictly better.

edit: I wanted to use an English dictionary example (above) to demonstrate how this is not artificial, and assumed that people could extrapolate from the example without being hung up on the non-strict equality. However, here is an unambiguously clear counterexample: You have 2000 words. 1000 words contain the letter A in the first place. The other 1000 words contain a unique combination of Bs embedded elsewhere. For example, ?B???, ??B??, ???B?, ????B, ?BB??, ?B?B?, etc. The ?s represent randomly-chosen characters. There are no As in the first ?, except for one word (whose ? is an 'A'), so that the frequency of As is strictly greater than the frequency of Bs. The proposed algorithm would guess A which would result in {50%: choices_halved, 50%: choices_halved & lose_one_life}, whereas this algorithm would dictate the choice B which results in {50%: YOU_WIN, 50%: choices_halved & lose_one_life}. Percentages have been rounded very slightly. (And no, a word with double letters does not contribute twice to the 'frequency', but even if it did under an insane definition, you could trivially modify this example by making the words begin with AAA...A.)

(regarding comments: It is unreasonable to complain about strict equality in this example, e.g. "999/2000", since you can make the probabilities arbitrarily close to each other.)

(Which points out an amusing side-note: If the dictionary is large enough to make hangman impossible sometimes, a strategy ought to throw away guesses that it does not expect to be able to guess. For example if it only has 2 moves left, it ought to make the highest-probability assumption it can which eliminates subtrees with more than 2-moves worth of bits of surprise.)

ninjagecko
  • 88,546
  • 24
  • 137
  • 145
  • `cats, bats, rats, rate` - choosing `a` and `t` is perfectly fine as you don't lose any lives. – Karoly Horvath Mar 30 '12 at 14:43
  • Now you have equal frequency of 'A', 'T' and 's'. Maybe you should replace sATe with gATe in your sample dictionary below the line? – Attila Mar 30 '12 at 15:11
  • 1
    I don't see how this is better: 1) say the word is "cATs" (one of the ones with endig 's'), and 1a) you guess 's', then you are right and the next two guesses can be AT, reducing your choices to {c,b,r,v}. This is the same as 1b) if you have guessed A or T, then clearly s has highest frequency and you are left with the choice of {c,b,r,v} (in neither of these cases did you lose life) 2) the word is "role", 2a) you guess 's' then you lose a life, you should guess 'o' or 'l', you are correct and left with {m,v,r} as opposed to [cont] – Attila Mar 30 '12 at 15:20
  • 1
    [cont] 2b) guessing A or T, you lose a life and guess 'o', 'l', or 'e' and you are back to {m,v,r}. 3) the word is gATe (see my previous comment) 3a) you guess 's', lose a life, guess o or l, lose => word found or 2b) guess A or T, don't lose, guess 's', lose -> word found. In 3a) you lost 2 lives, in 3b) you lost 1 before word was found – Attila Mar 30 '12 at 15:22
  • you currently have equal frequency of 'A', 'T' and 'S', so the greedy algorithm is not excluded from picking 'S'. – Ronald Mar 30 '12 at 16:44
  • I know, but it's trivial to construct an example where this is not the case, so I did not. Perhaps I should given that this has been pointed out twice... – ninjagecko Mar 30 '12 at 20:05
  • @ninjagecko If it's trivial, then please do it so I can mark your answer as being correct. There has now been one "it's trivial" on both sides of the argument. Clearly, it's not trivial... – Ronald Mar 30 '12 at 20:34
  • 1
    @ninjagecko Your A/B counterexample is wrong. If A is right, then I can then choose B for "free" (I know I won't lose a life). If I choose A and it's wrong - then I eliminate all but 999 options. If I choose B and it's right, fair enough (same position as if I chose A then B). If I choose B and it's wrong - then I eliminate all but 1000 options (worse than if I chose A) – Ronald Mar 30 '12 at 21:38
  • Your A/B/C/X/Y counterexample is also wrong. If I choose A, and it's right, then I won't choose B or C because X now appears more frequently. So, I'll choose A then X (if A is right), or A then Y (if A is wrong). – Ronald Mar 30 '12 at 21:39
  • @RonaldStewart: Technically true, easily modifiable to work. Edited. If you care about a 0.0000000..1% probability difference though, I would not call that a reasonable complaint. The second point about the last example is a reasonable complaint however, so I removed it. – ninjagecko Mar 31 '12 at 00:12
  • no idea if the op is still here but i don't get what '(frac words with L#words without L + frac words without L#words with L)/(# words without L/# words total)" means, could you use a different form to represent it? – KTibow Feb 08 '23 at 01:36
4

I have written a script that solves hangman optimally [github].

My basic strategy is this:

  • For a pattern such as ..e.. with tried letters: e,s,t
  • Check against words of only n digits (in this case 5)
  • Create a list of possible words
    • Create a regex from the supplied info
    • In this case it would be [^est][^est]e[^est][^est]
    • Parse your word list for words that match this regex
  • Cycle through each word, counting up the number of times every letter appears
  • Your optimal next guess is the most likely letter
fredley
  • 32,953
  • 42
  • 145
  • 236
  • wow, love that! I was thinking of suggesting a Hangman Programming Competition :) but it may be that Hangman is too easily solvable. – Ronald Mar 31 '12 at 20:01
  • My only inkling is that you could do better by, knowing how many moves you have left, choose letters which are not the most frequent, but minimize the maximum number of future guesses you will ever have to make, thereby ensuring the lowest probability of being hanged. That is for pyngman 2.0 though... – fredley Mar 31 '12 at 20:03
  • I also wonder if, your opponent knows your algorithm, they can pick a word to make you lose. This would necessitate some random mixed strategy! Fun! – Ronald Mar 31 '12 at 20:56
  • 1
    @Ronald I would accuse you of overthinking this, but clearly that point was passed some time ago. I'm planning to do some analyses of various methods later, it may turn out there are no such words! – fredley Apr 01 '12 at 07:11
  • @RonaldStewart Initial testing: In a dictionary of 84k words, my script 'lost' on just over 500 games. Not bad! – fredley Apr 02 '12 at 16:17
  • 1
    @RonaldStewart If you're still interested: http://www.tommedley.com/372/hangman-overkill/ – fredley Apr 03 '12 at 14:06
  • 2
    For your rating function of "most likely letter" I would prioritize the number of words in which each letter appears, followed by the number of times it appears in each word (as a secondary sort index). That way, if two letters appear in the same number of words, but one appears more times if repetitions within words are counted, you would prefer the one that has more repetitions. The main sort index minimizes the likelihood of a bad guess, and the secondary index maximizes the number of letters likely to be revealed. – Carl Dec 08 '14 at 10:03
  • **NOTE** The **tommedly.com** link is no longer available. – Prune Nov 27 '18 at 17:35
3

About your idea to try and guess the word instead of trying to guess letters, there sure may be some isolated cases where you guess the word from the first try or something like that, but this doesn't make that algorithm better on the average case. It's about expected probability.

Some improvement that could be done to that algorithm (in the version posted by Lajos) is some more informed pick of letter to be tried.
This could be achieved by adding one more weight: consider the usage of each word the vocabulary of the language.

For example, technical, medical, juridical etc. words would have much lower chances.

Take this dictionary (with some example usage weights):

frost    6
guilt    5
genes    1
fever    2
meter    1

Here e is the most frequent letter, so it would get chosen. This would mean leaving only the medical terms, which are very unlikely. But if the decision is taken by:

weight(letter) = w * frequency +
              (1 - w) * sum( usage_weight(word) where letter in word )

then, most probably t would be chosen.


Because (let's say w = 0.2):

weight(e) = 0.2 * 3   +   0.8 * (1 + 2 + 1)
          = 3
weight(r) = 0.2 * 3   +   0.8 * (1 + 2 + 6)
          = 7.8
weight(t) = 0.2 * 3   +   0.8 * (1 + 5 + 6)
          = 10.2

Note: We should, of course use normalized weights, like frequency / num_words to get accurate weight measuring.

Note: This algorithm can and should be adapted to the opponent:

  • when playing against human, more usual words get higher weight
  • when playing against AI, it depends on the difficulty level:
    • on easy level aim for usual words
    • on hard level aim for unusual words
Iulius Curt
  • 4,984
  • 4
  • 31
  • 55
  • Your dictionary is helpful. I think R is a better guess than E - it appears in the same number of words (3), and, if you are right, you get extra information about the location of the R. (If R appears second, you already know the answer is FROST) – Ronald Mar 30 '12 at 13:29
  • Ups, I overlooked that R :) Anyway, the basic idea was that you can use some external information (language usage) to improve the choice. – Iulius Curt Mar 30 '12 at 13:34
0

The answer clearly shows why the greedy algorithm is not the best, but doesn't answer how much better we can get if we stray from the greedy path.

If we assume all words are equally likely in case you are playing against a computer opponent. The case of 4 letters, 6 lives case if you have option to look simply the second most popular letter your probability of winning increases from 55.2% to 58.2%, if you are willing to check one more letter then it increases to 59.1%.

Code: https://gist.github.com/anitasv/c9b7cedba324ec852e470c3011187dfc

A full analysis using ENABLE1 (scrabble dictionary which has 172820 words) with 2 to 6 letters, and with 0 to 10 lives, and 1-greedy to 4-greedy gives the following results. Of course at 25 lives every strategy is equivalent with 100% win rate, so not going beyond 10 lives. Going more than 4-greedy was improving probability only slightly.

letters, lives, 1-greedy, 2-greedy, 3-greedy, 4-greedy


2 letters 0 lives   3.1%    3.1%    3.1%    3.1%
2 letters 1 lives   7.2%    7.2%    7.2%    8.3%
2 letters 2 lives   13.5%   13.5%   13.5%   14.5%
2 letters 3 lives   21.8%   21.8%   22.9%   22.9%
2 letters 4 lives   32.2%   33.3%   33.3%   33.3%
2 letters 5 lives   45.8%   45.8%   45.8%   45.8%
2 letters 6 lives   57.2%   57.2%   57.2%   57.2%
2 letters 7 lives   67.7%   67.7%   67.7%   67.7%
2 letters 8 lives   76%     76%     76%     76%
2 letters 9 lives   84.3%   84.3%   84.3%   84.3%
2 letters 10 lives  90.6%   91.6%   91.6%   91.6%


3 letters 0 lives   0.9%    1.1%    1.1%    1.1%
3 letters 1 lives   3.4%    3.8%    3.9%    3.9%
3 letters 2 lives   7.6%    8.4%    8.6%    8.8%
3 letters 3 lives   13.7%   15%     15.1%   15.2%
3 letters 4 lives   21.6%   22.8%   23.3%   23.5%
3 letters 5 lives   30.3%   32.3%   32.8%   32.8%
3 letters 6 lives   40.5%   42%     42.3%   42.5%
3 letters 7 lives   50.2%   51.4%   51.8%   51.9%
3 letters 8 lives   59.6%   60.9%   61.1%   61.3%
3 letters 9 lives   68.7%   69.8%   70.4%   70.5%
3 letters 10 lives  77%     78.3%   78.9%   79.2%


4 letters 0 lives   0.8%    1%      1.1%    1.1%
4 letters 1 lives   3.7%    4.3%    4.4%    4.5%
4 letters 2 lives   9.1%    10.2%   10.6%   10.7%
4 letters 3 lives   18%     19.4%   20.1%   20.3%
4 letters 4 lives   29.6%   31.3%   32.1%   32.3%
4 letters 5 lives   42.2%   44.8%   45.6%   45.7%
4 letters 6 lives   55.2%   58.2%   59.1%   59.2%
4 letters 7 lives   68%     70.4%   71.1%   71.2%
4 letters 8 lives   78%     80.2%   81%     81.1%
4 letters 9 lives   85.9%   87.8%   88.4%   88.7%
4 letters 10 lives  92.1%   93.3%   93.8%   93.9%


5 letters 0 lives   1.5%    1.8%    1.9%    1.9%
5 letters 1 lives   6.1%    7.5%    7.9%    8%
5 letters 2 lives   15.9%   18.3%   18.9%   19.2%
5 letters 3 lives   30.1%   34.1%   34.8%   34.9%
5 letters 4 lives   47.7%   51.5%   52.3%   52.5%
5 letters 5 lives   64.3%   67.4%   68.3%   68.5%
5 letters 6 lives   77.6%   80.2%   80.6%   80.8%
5 letters 7 lives   86.9%   88.6%   89.2%   89.4%
5 letters 8 lives   92.8%   94.1%   94.4%   94.5%
5 letters 9 lives   96.4%   97.1%   97.3%   97.3%
5 letters 10 lives  98.2%   98.6%   98.8%   98.8%


6 letters 0 lives   3.2%    3.7%    3.9%    3.9%
6 letters 1 lives   12.6%   14.3%   14.9%   15%
6 letters 2 lives   29.2%   32.2%   32.8%   33%
6 letters 3 lives   50.1%   53.4%   54.2%   54.4%
6 letters 4 lives   69.2%   72.4%   73.1%   73.2%
6 letters 5 lives   83.1%   85.5%   85.9%   86.1%
6 letters 6 lives   91.5%   92.9%   93.2%   93.2%
6 letters 7 lives   95.8%   96.5%   96.7%   96.8%
6 letters 8 lives   97.9%   98.3%   98.4%   98.5%
...
Anita
  • 38
  • 6
0

The strategy of choosing the most frequent letter in the pool of possible words maximizes the probability of guessing a letter correctly on that specific turn. However, this strategy does not maximize the overall winning probability. @Reinstate Monica's answer demonstrates this fact (which applies for much more than just the hangman game).

For example, consider the following two options: play 7 games with 90% winning probability each, or play 1 game with 50% probability. At first, it seems that maximizing the winning probability is the optimal solution, but 0.9⁷<0.5 (because 0.9⁷ = 47.8%).

Nevertheless, it still is a really good algorithm in terms of speed, simplicity and winning rate. I coded it for Portuguese (my native language) and it seems to guarantee a 97% winning probability within the standard 5 mistakes or less.

ordptt
  • 130
  • 6
0

No, this greedy algorithm is not the best at all and I can prove it by giving a better solution:

In each step we know the number of letters and we know some letters. We choose all the words from our set of words which have the given letters at the given positions and their length match the length of the word in question. We select the most frequent letter from the selected subset of words and guess about the given letter. For every guesses the guessed letter will be marked as guessed and they won't be guessed again in the future. This way you have much better chances of survival than in the greedy algorithm described in your question.

EDIT:

After the question was clarified and further specifications were made, I've come to the conclusion that the algorithm is optimal.

If we have n words with the given length, containing all the right guesses ("good letters") and not containing any wrong guesses ("bad letters"), x lives, we can look at the frequency of letters in the still possible words and select the letter with the biggest frequency, let's suppose that y words contained the letter.

In this case, the confidence rate of this guess is y/n, which is bigger than the confidence rate of any other letters, which gives a higher chance of survival. So, such a step is optimal. If you make a series of steps containing only steps in this spirit, the series of steps will be optimal too, so this algorithm is optimal.

But, if we have any extra information (like a description of the word, or knowing that the probability of having the same word twice in a short period), this algorithm can be enhanced based on the extra information, all the words in the set of words will have a fitness value (probability based on the extra information) and all the letter types in the words will be weighted based on the fitness score.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Well, I guess that `given that this letter appears/does not appear in the word` included this. – Iulius Curt Mar 30 '12 at 12:50
  • No, it didn't because the algorithm in the question doesn't specify what will be done based on this knowledge. – Lajos Arpad Mar 30 '12 at 12:52
  • yes, @iuliux is correct - my intention was that this strategy was included in "taking this idea to its conclusion". But, thanks for the clarification. – Ronald Mar 30 '12 at 12:54
  • This is an improvement over the first, but the next answer below improves it further. – DRVic Mar 30 '12 at 12:58
  • @Ronald, in this case your algorithm looks to be optimal, unless you have any further information. – Lajos Arpad Mar 30 '12 at 13:13
  • In this algorithm, we always guess the letter that is most likely to be correct. However, our goal is subtly different - to guess the word within a fixed number of lives (incorrect letters). Are these equivalent goals? – Ronald Mar 30 '12 at 13:18
  • Your algorithm is optimal for the set of information described by you above. If you don't have any more useful information, then this is the best in my opinion. – Lajos Arpad Mar 30 '12 at 13:19
  • It would be nicer to have some proof, but thanks for your help. – Ronald Mar 30 '12 at 13:26
  • 1
    It is difficult to prove that an algorithm is the absolute best of all algorithms to a given problem, because there can be many possible algorithms for the same problem and you have to prove that all existing algorithms are worse or equal than yours, also, you have to prove that better ideas are impossible. – Lajos Arpad Mar 30 '12 at 13:47
-1

An English example demonstrating this is not optimal; suppose the pattern is KE??, you have eliminated all possibilities except KELL, KELP, KEMB, KEMP, KEWL, and you have one life left. Then although L is the greedy choice, it could result in a loss, whereas P is a guaranteed win.

Vinay Somawat
  • 639
  • 14
  • 23
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30717965) – MD. RAKIB HASAN Jan 05 '22 at 12:04
  • In this example, L is actually the optimal choice. Why would P be guaranteed if it loses 60% of times? – ordptt May 12 '22 at 23:19
-1

Choose a letter that divides the remaining valid words in 2 sets of nearly equal size. With positional information there could be more than 2 sets. Repeat until your set has size 1. That is the best way. The proof is left as an exercise.

knivil
  • 916
  • 5
  • 10
  • This algorithm could give you an extremely poor chance of winning. If your number of dictionary words is significantly greater than 4^(nlives), you would never get sufficient depth in your search before you are hanged. – Ronald Mar 30 '12 at 13:00
  • OP clearly asked for proof in answer -- "left for excercise" is trying to get around answering the actual question – Attila Mar 30 '12 at 13:01
  • I would argue with common sense because a proof (for an answer it is not a must have) would be very ... time consuming: maximise your information about the word. And i do not know how you derive the number 4^(nlives). What is poor, and what is the chance of winning with the other strategy? – knivil Mar 30 '12 at 13:11
  • So explain how your algorithm helps maximizing information about the word. – Attila Mar 30 '12 at 13:13
  • 1
    It minimizes the number of remaining words in each step. – knivil Mar 30 '12 at 13:14
  • 1
    @knivil: imagine you have 16 words and 2 lives. Your first guess takes you to 8 words, your second guess to 4 words, your third guess to 2 words, and your 4th guess will allow you to win. However, you only had 2 lives, so you probably hanged before the 4th guess - in fact you might give yourself 0 chance to win. Binary search seems to do worse than other options, in this case? – Ronald Mar 30 '12 at 13:23
  • 4
    @knivil: No, it doesn't. Because, when you choose the most frequent letter, if it's a fit you don't lose nothing, so it doesn't matter how many words you eliminated since it's for free, and if it's not a fit, then you like the 2 sets to be unbalanced because you keep only the smaller one. – Iulius Curt Mar 30 '12 at 13:27
  • 1
    If positional information is used then my first guess brings up more than 2 sets maybe. So i would choose the letter that minimize the maximum of all remaning sets. And the example with 16 words is yust an example, what about a general stragie over n lives and m words of unknown length ... And now it is clear that a proof is not possible: the preconditions are not formulated clearly. – knivil Mar 30 '12 at 13:27
  • 3
    "The proof is left as an exercise." They always say this when something is difficult or impossible to prove. – Lajos Arpad Mar 30 '12 at 13:57
  • Upvoting because this answer is correct. It's called entropy. – ninjagecko Mar 30 '12 at 13:59
  • Lajos Arpad: Yes of course. It was a joke. ninjagecko: yes, but "choose the letter with maximum entropy" is not an algorithm. – knivil Mar 30 '12 at 14:01