報告者:李慧娟 教授:徐熊健 Contents The Breadth-First Search Depth-First Search Hill Climbing Best-First Search Strategy The Breadth-First Search The Breadth-First Search The Breadth-First Search Step 1 • Form a one-element queue consisting of the root node. Step 2 • Test to see if the first element in the queue is a goal node. If it is, stop. Otherwise, go to step 3. Step 3 • Remove the first element from the queue. Add the first element’s descendants, if any, to the end of the queue. Step 4 • If the queue is empty, then failure. Otherwise, go to Step 2. The Breadth-First Search The Breadth-First Search 寬度優先搜尋(BFS)演算法 Breadth-first search (BFS) is a general technique for traversing a graph BFS on a graph with n vertices and m edges takesO(n + m ) time 依序走訪同一層的所有節點,走訪完畢後,才繼續走 訪下一層的節點 BFS 會使用到Queue (佇列--先進先出) 來紀錄過程中 展開的節點 The Breadth-First Search The Breadth-First Search a Queue a b c b c d e d e f g f g h h Output Depth-First Search The Breadth-First Search Depth-First Search Step 1 • Form a one-element stack consisting of the root node. Step 2 • Test to see if the top element in the stack is a goal node. If it is, stop; otherwise, go to step 3. Step 3 • Remove the top element from the stack and add the first element’s descendants, if any, to the top of the stack. Step 4 • If the stack is empty, then failure. Otherwise, go to Step 2. The Breadth-First Search Depth-First Search 深度優先搜尋(DFS)演算法 Depth-first search (DFS) is a general techniquefor traversing a graph DFS on a graph with n vertices and m edges takes O(n + m ) time 先走訪越深的子節點, 直到該點沒有子節點後,回溯到最 近尚有未走訪子節點的節點,再繼續下訪其他節點 DFS 會使用到Stack (堆疊—後進先出) 來紀錄過程中展 開的節點 The Breadth-First Search Depth-First Search a b Output c d f ebh d e h f g cg a -----Stack 日常生活中的例子:深度搜尋法(Depth-first search) Hill Climbing Hill Climbing Step 1 • Form a one-element stack consisting of the root node. Step 2 • Test to see if the top element in the stack is a goal node. If it is, stop; otherwise, go to step 3. Step 3 • Remove the top element from the stack and expand the element. Add the descendants of the element into the stack ordered by the evaluation function. Step 4 • If the list is empty, then failure. Otherwise, go to Step 2. Hill Climbing 基本的Hill Climbing 演算法 1. 從搜尋空間中亂數取一點a作為出發點 2. 考慮a點周圍可用的狀態點 3. 取a點周圍最好品質(錯位少)的一點b,並移往b點 4. 重複2~4,直到找不到更好的點 5. 則最後的狀態點就是用Hill Climbing找到的最佳解 6. 若有兩點以上是最好解,則亂數擇一 Hill Climbing並不能保證得到最佳化 solution,但卻可 以有近似 solution Hill Climbing 8-puzzle problem Given a initial arrangement and the goal state, the problem is to determine whether there exists a sequence of movements from initial state to goal state, where each item can be moved only horizontally or vertically to the empty spot. Example: The initial arrangement and the goal state of the 8-puzzle problem. Hill Climbing Hill Climbing strategy for 8-puzzle problem Evaluation function f(n) = w(n), where w(n) is # of misplaced tiles in node n. The hill climbing strategy is to select the least f(n) to expand the present node Ex: (3) 1, 2, 8 are misplace, f(n)=3 Hill Climbing An 8-puzzle problem solved by a hill climbing method. 1 2 8 7 3 4 6 goal state 5 Best-First Search Strategy Best-First Search Strategy Step 1 • Construct a heap by using the evaluation function. First, form a one-element heap consisting of the node. Step 2 • Test to see if the root element in the heap is a goal node. If it is, stop; otherwise, go to step 3. Step 3 • Remove the heap element from the heap and expand the element. Add the descendants of the element into the heap. Step 4 • If the heap is empty, then failure. Otherwise, go to Step 2. Best-First Search Strategy Combine depth-first search and breadth-first search. 這個搜尋法只是根據最佳化的評估函數來選擇下 一個搜尋的節點 當評估函數的準確度愈高,則愈可能找到最佳的節點 反之,評估函數可能無作用,甚至可能導至錯誤的搜 尋。 Best-First Search Strategy An 8-puzzle problem solved by a hill climbing method. 1 2 8 7 3 4 6 5 goal state Goal Node