Source: https://fortierq.github.io/assets/teaching/MP/td_automate.pdf