Heuristic Search Blai Bonet Breadth-first search and uniform-cost search Universidad Sim´ on Bol´ıvar, Caracas, Venezuela c 2015 Blai Bonet Goals for the lecture Lecture 5 Breadth-first search Breadth-first search explores the search tree layer by layer expanding all nodes at depth d before expanding any node at depth d + 1 • Learn breadth-first search and uniform-cost search • Tree-search and graph-search variants of the algorithms Nodes get ordered for expansion using a FIFO queue • Fundamental characteristics Algorithm can be implemented as a tree- or graph-search algorithm depending on whether duplicates are pruned or not • Perform analysis on canonical search trees Breadth-first search is a fundamental algorithms in CS c 2015 Blai Bonet Lecture 5 c 2015 Blai Bonet Lecture 5 Breadth-first search for explicit graphs [CLRS] 1 Breadth-first search: example breadth-first-search(Vertex root): 2 3 4 5 6 7 % initialization foreach Vertex u color[u] = White distance[u] = ∞ parent[u] = null 8 9 10 11 12 Queue q color[root] = Gray distance[root] = 0 q.insert(root) % FIFO queue 13 14 15 16 17 18 19 20 21 22 23 % search while !q.empty() Vertex u = q.pop() foreach Vertex v in adj[u] if color[v] == White color[v] = Gray distance[v] = distance[u] + 1 parent[v] = u q.insert(v) color[u] = Black c 2015 Blai Bonet search tree Lecture 5 c 2015 Blai Bonet Breadth-first search: example Lecture 5 Breadth-first search for implicit graphs 1 2 % breadth-first search without duplicate elimination breadth-first-search(): 3 4 5 6 7 % initialization Queue q Node root = make-root-node(init()) q.insert(root) 8 9 10 11 % search while !q.empty() Node n = q.pop() 12 % check for goal if n.state.is-goal() return n 13 14 15 % expand node foreach <s,a> in n.state.successors() q.insert(n.make-node(s,a)) 16 17 18 19 20 c 2015 Blai Bonet Lecture 5 return null c 2015 Blai Bonet % failure: there is no path from root to goal Lecture 5 Recall API and basic primitives 1 2 3 4 Differences between breadth-first searches struct AbstractState { bool is-goal() list<pair<State, Action> > successors() } 5 6 DerivedState init() 7 8 9 10 CLRS’s breadth-first search is for explicit graphs, ours is for implicit graphs and thus explores the search tree associated to a graph struct Node { Node(AbstractState s, Node p, Action a, unsigned gcost) : state(s), parent(p), action(a), g(gcost) 11 Our breadth-first search returns as soon as a node associated to a goal state is selected for expansion (why not generated?) Node make-node(AbstractState state, Action action) return new Node(state, this, action, g + c(state, action)) 12 13 14 extract-path(vector<Action> &reversed-path) Node node = this while node != 0 && node.parent != 0 reversed-path.push-back(node.action) node = node.parent 15 16 17 18 19 20 } 21 22 23 Node make-root-node(AbstractState state) return new Node(state, null, -1, 0) c 2015 Blai Bonet Lecture 5 c 2015 Blai Bonet Breadth-first search for implicit graphs 1 2 3 4 5 6 Extended API for duplicate elimination I % breadth-first search with duplicate elimination breadth-first-search(): % initialization Queue q set-color(init(),Gray) q.insert(make-root-node(init())) 1 7 8 9 10 Lecture 5 2 % search while !q.empty() Node n = q.pop() void set-color(AbstractState state, Color color) 3 4 unsigned get-color(AbstractState state) 5 11 % check for goal if n.state.is-goal() return n 12 13 Later on we’ll see how to implement these functions efficiently 14 % expand node foreach <s,a> in n.state.successors() if get-color(s) == White set-color(s,Gray) q.insert(n.make-node(s,a)) set-color(n.state,Black) 15 16 17 18 19 20 21 22 return null c 2015 Blai Bonet % failure: there is no path from root to goal Lecture 5 c 2015 Blai Bonet Lecture 5 Properties of breadth-first search Uniform-cost search Expands all nodes at depth d before expanding any at depth d + 1 Breadth-first search finds shortest path rather than minimum-cost path from initial state to goal state – Complete: if there is a path, outputs a path, else outputs null Uniform-cost search explores search tree cost-layer by cost-layer expanding all nodes at cost c before expanding any node at cost > c – Optimality: it returns a shortest path (not min cost!) – Time complexity: O(bd ) (i.e. exponential in goal depth d) Nodes get ordered for expansion using a priority queue – Space complexity: O(bd ) (i.e. exponential in goal depth d) Like breadth-first search, algorithm can be implemented as tree- or graph-search depending on whether duplicates are pruned or not Time and space complexities calculated in canonical search tree with branching factor b where shallowest goal appears at depth d c 2015 Blai Bonet Lecture 5 c 2015 Blai Bonet Uniform-cost search for implicit graphs 1 2 3 4 5 6 7 Uniform-cost search: expansion % uniform-cost search with duplicate elimination uniform-cost-search(): % initialization PriorityQueue q % ordered by node’s g set-color(init(),Gray) set-distance(init(),0) q.insert(make-root-node(init())) 21 22 23 10 11 25 26 27 % search while !q.empty() Node n = q.pop() 28 29 31 % check for goal if n.state.is-goal() return n 13 14 32 33 34 15 % expand node expansion-for-uniform-cost-search(n,q) set-color(n.state,Black) 16 17 18 35 36 37 38 19 return null c 2015 Blai Bonet % is it the first time we see the state? if get-color(s) == White set-color(s,Gray) set-distance(s,g) q.insert(n.make-node(s,a)) 30 12 20 expansion-for-uniform-cost-search(Node n, PriorityQueue q): foreach <s,a> in n.state.successors() g = n.g + c(n.state,a) 24 8 9 Lecture 5 % a shorter path was found, update open list else if g < get-distance(s) assert(get-color(s) == Gray) % why? set-distance(s,g) s.node.parent = n s.node.action = a s.node.g = g q.decrease-priority(s.node,g) % failure: there is no path from root to goal Lecture 5 c 2015 Blai Bonet Lecture 5 Extended API for duplicate elimination II Traveling in Romania (AIMA) Oradea 71 Neamt 87 Zerind 151 75 Iasi 1 2 Arad void set-color(AbstractState state, Color color) 140 92 3 4 Sibiu unsigned get-color(AbstractState state) Vaslui 80 void set-distance(AbstractState state, unsigned dist) Rimnicu Vilcea Timisoara 7 8 Fagaras 118 5 6 99 unsigned get-distance(AbstractState state) 111 9 Lugoj 142 211 Pitesti 97 70 98 Mehadia Later on we’ll see how to implement these functions efficiently 146 75 Drobeta 85 101 Hirsova Urziceni 86 138 Bucharest 120 90 Craiova Giurgiu Eforie Task: find optimal path from Rimnicu Vilcea to Vaslui c 2015 Blai Bonet Lecture 5 c 2015 Blai Bonet Uniform-cost search: example Lecture 5 Delayed duplicate elimination Rimnicu Vilcea Oradea 231 Sibiu 80 Arad 220 Fagaras 179 Craiova 146 Pitesti Drobeta 266 Bucharest Often one implements a weaker form of duplicate elimination in which duplicates are allowed to enter the open list (nodes generated that are waiting for expansion) 97 385 We call this form delayed duplicate elimination Zerind c 2015 Blai Bonet 295 Timisoara 338 Mehadia 341 Lugoj 411 Giurgiu 475 Urziceni Vaslui 470 It can be thought as that the search explicates graph edges rather than graph vertices 612 Hirsova 568 Eforie 654 Lecture 5 c 2015 Blai Bonet Lecture 5 UCS with delayed duplicate elimination 1 2 3 4 5 6 Properties of uniform-cost search % uniform-cost search with delayed duplicate elimination uniform-cost-search(): PriorityQueue q % ordered by node’s g set-color(init(),Gray) set-distance(init(),0) q.insert(make-root-node(init())) – Complete: if there is a path, outputs a path else outputs null (if cmin > 0 or duplicates pruned) – Optimality: it returns a minimum-cost path 7 8 9 10 11 % search while !q.empty() Node n = q.pop() State ns = n.state – Time complexity: O(bc – Space complexity: O(bc 12 % delayed duplicate elimination if get-color(ns) == White || n.g < get-distance(ns) set-distance(ns,n.g) if ns.is-goal() return n 13 14 15 16 19 20 21 22 23 24 % expand node foreach <s,a> in ns.successors() set-color(s,Gray) set-distance(s,n.g+c(ns,a)) q.insert(n.make-node(s,a)) set-color(ns,Black) return null % failure: there is no path from root to goal c 2015 Blai Bonet Lecture 5 Summary • Breadth-first search • Properties of breadth-first search • Uniform-cost search • Properties of uniform-cost search • Delayed duplicate detection (search explicates edges rather than nodes) c 2015 Blai Bonet ) (= O(bd ) if all costs are 1) ∗ /c min ) (= O(bd ) if all costs are 1) If duplicates aren’t pruned and minimum edge cost cmin = 0, uniform-cost search may not terminate (i.e. it’s not complete) 17 18 ∗ /c min Lecture 5 Time and space complexities calculated in canonical search tree with branching factor b and cmin > 0 where minimum-cost goal appears at depth d with cost c∗ c 2015 Blai Bonet Lecture 5
© Copyright 2024