有限狀態機 (finite state machine)
有限狀態機 (finite state machine),所呈現的是一種有限的狀態。
我們試著從幾個原文網站的名詞解釋來研究其含意。
資料來源 http://www.nist.gov/dads
A model of computation consisting of a set of states, a start state, an
input alphabet, and a transition function that maps input symbols and current
states to a next state. Computation begins in the start state with an input
string. It changes to new states depending on the transition function. There
are many variants, for instance, machines having actions (outputs) associated
with transitions (Mealy machine) or states (Moore machine), multiple start
states, transitions conditioned on no input symbol (a null) or more than one
transition for a given symbol and state (nondeterministic finite state
machine), one or more states designated as accepting states (recognizer), etc.
資料來源 http://www.whatis.com
In general, a state machine is any device that stores the status of
something at a given time and can operate on input to change the status and/or
cause an action or output to take place for any given change. A computer is
basically a state machine and each machine instruction is input that changes
one or more states and may cause other actions to take place. Each computer's
data register stores a state. The read-only memory from which a boot program
is loaded stores a state (the boot program itself is an initial state).
The operating system is itself a state and each application that runs begins
with some initial state that may change as it begins to handle input. Thus,
at any moment in time, a computer system can be seen as a very complex set
of states and each program in it as a state machine. In practice, however,
state machines are used to develop and describe specific device or program
interactions.
- 簡單的來說,有限狀態機是由一組狀態、一個起始狀態、
輸入、將輸入與現在狀態轉換為下一個狀態的轉換函數所組成。
- 在現實中,有許多事情可以用有限個狀態來表達,如: 紅綠燈、自動販賣機...
等等。其實,在資訊領域中,很多事情都是由有限的狀態所組成,再由於不同的輸入
而衍生出近乎無限的變化。
我們可以利用資料結構所學到的圖型 (Graph) ,來畫出不同狀態的轉變關係。
下面將以兩個例子來說明FSM。
- 紅綠燈
紅綠燈運作的原理相當簡單,從一開始綠燈,經過一段時間後,將變為黃燈,
再隔一會兒,就會變成紅燈,如此不斷反覆。其FSM如下。

- 自動販賣機
假設有簡單的一自動販賣機販售兩類商品,一類售價20元,另一類售價50元。如果該
販賣機只能辨識10元及50元硬幣。一開始機器處於Hello的狀態,當投入10元時,機器
會進入餘額不足的狀態,直到投入的金額大於20元為止。如果一次投入50元,則可以
選擇所有的產品,否則就只能選擇20元的產品。完成選擇後,將會賣出商品並且找回
剩餘的零錢,隨後,機器又將返回初始的狀態。其FSM如下。
