窮舉搜尋
From Wikipedia, the free encyclopedia
From Wikipedia, the free encyclopedia
窮舉搜尋(粵拼:kung4 geoi2 sau2 cam4;英文:brute-force search)係最原始嗰種搜尋演算法,指將手上嘅數據(事先以某啲方式排好咗)逐個逐個
例如手上有一列以隨機次序排嘅數字,用家想要由呢個數列當中最大嗰個出嚟,用窮舉搜尋嘅話,睇嗰個人就要睇嗮個數列當中嘅每個數字,再俾出最大嗰個出嚟;呢段簡單嘅演算法用粵文寫出嚟嘅話會係[1]:
上述呢個演算法用比較形式些少嘅虛擬碼寫出嚟嘅話會係噉:
// Input: A list of numbers L.
// Output: The largest number in the list L.
if L.size = 0 return null;
largest ← L[0];
for each item in L, do
if item > largest, then
largest ← item;
return largest;
// 當中 "←" 代表指定敘述-「A ← B」即係將 A 嘅數值設做 B;
// 而 "return X" 會終止個演算法,並且將 X 嘅數值攞嚟做輸出。
廿一世紀嘅研究者一般都會嫌呢個演算法唔夠好,原因係呢個演算法喺最壞情況(worst-case)嗰陣要睇嗮成個列先至搞得掂(O(n)
,當中 n
係條陣列嘅長度),所以又有人諗咗第啲演算法出嚟做呢樣工作-而呢啲新演算法好多時都曉淨係睇個數字列嘅一小部份就經已搵得出想要嗰個數字。
玩遊戲嗰陣要做決策,最直接嘅方法就係窮舉搜尋:窮舉搜尋可以輕易解決簡單嘅遊戲—例如井字過三關噉,井字過三關嘅第一步得三個可能性咁少—
而可能性少就表示,電腦唔使嘥好多時間同第啲資源,就可以睇晒所有嘅可能性。相比之下,國際象棋淨係第一步白棋行嗰刻已經有 20 個可能性咁多,而跟住落嚟可能性嘅數量會有爆發性嘅增長,喺十步之前已經會超過 100 萬個可能性[註 1],廿一世紀初嘅電腦仲未有足夠嘅運算能力,做唔到「將咁多個可能性逐個逐個攞嚟睇,喺合理咁短嘅時間內畀答案」。
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.