Let's play a game with a coin. We will flip this coin continuously, until either we get the sequence heads, heads, tails, or we get the sequence tails, heads, heads; in the former case, you win, in the latter I win. For example, if the tosses come up , then you won by toss 4 (because ), even though appeared latter by toss 7. Had the second toss been tails, I would have won instead, since only appeared by toss 8. What is the probability that you win?
This kind of problem appears relatively often (or at least I once solved two successive problems of this fashion). Although there is a specialized method of solution that works for this problem (if you get any tails in the first two tosses, you lose, otherwise you win), I will explain a more general method that can be applied to several such problems.
First, we observe the possible states of the game. There are nine states, which we can encode as . A pipe (|) indicates the beginning of the game, while an asterisk (*) indicates the time just before the current toss. Thus indicates that we got tails as the first toss, and now we're at the second toss, while indicates the last two tosses were heads and tails, respectively. After each toss, our state might change to something else; for example, after a toss of heads from , we end up in the state , since now the last two tosses are tails and heads in that order. (The fact that the third-last toss was heads no longer matters.) is a win for you, and is a win for me; once we enter either of these, we won't go anywhere else.
Now we can construct a directed graph, where the vertices are states and edges are possible transitions, where edges are weighted according to the probability of entering such state. In other words, we create a Markov chain, as given in the following diagram made lovingly by Microsoft Paint:
Red arrows indicate the state change on getting heads, while orange arrows indicate the state change on getting tails. (Except the two rightmost, finish states, where the arrow indicates the state change for whatever the toss; once we end up on one of those, we won't change any more state.) For a Markov chain, the different colors can be ignored.
Now, we put a number into each state, indicating the probability that you win. The winning states are easy: has probability and has probability .
There is an important equality that always holds: . For example, let's apply this to the state . Suppose it has probability . Then, according to the equality, we have ; the first summand is if we follow the arrow to
HHT (getting a tails), while the second is if we follow the arrow to (getting a heads). We can easily solve . This means once we end up on , you will always win no matter what.
Similarly, we can solve for . This one is a bit harder, as we obtain a system of linear equations in three variables, but we can solve that to give all zeroes. Once we get to any of these three states, you're doomed.
We can then continue to and . For the former, we can compute . The latter can also be computed in the same way to give . And finally, this leads up to , the starting state, which means at the beginning you have only a probability of winning. Yes, I'm a jerk.
This visualization allows us to solve most problems of this sort better, without getting confused of how states lead to each other, and what your equations are. This is not much simpler than making equations without visualization, but it helps making sure you won't miss anything and also helps to tell which nodes you can probably compute earlier. At the worst case, yes, you need to make a system of linear equations and solve it by hand as usual, but hey, you now know that if you need to do so then it's the best way available.
Sample problems (read: the two problems I did that made me want to write this note):