500 likes | 508 Views
Learn about the Word Ladder problem and how Depth-First Search algorithm can be applied to find a solution. Explore the concept of tree depth and branching factor.
E N D
Before we consider Heuristic Search, let us review a little first We will begin by considering a new problem space, for Word Ladders…. Search to Solve Word Ladders
Example of a word ladder Change DOG to CAT. Change ONLY one letter at a time and form a new legal word at each step.
What is the depth of the solution? Since all three letters are different in the two words, and we have to change only one letter at a time, it is clear that we have to take at least three steps.
What is the diameter of the Word Ladder problem in general? In other words, what two English words could you give me, that would require the deepest tree? The two words are “charge” and “comedo”, and the tree is of depth 49. (Note: this is using a standard English dictionary, no plurals or verb conjugations) ? {“charge”, “change”, “chance”, “chancy”, “chanty”, “shanty”, “shanny”, “shinny”, “whinny”, “whiney”, “whiner”, “shiner”, “shiver”, “shaver”, “sharer”, “scarer”, “scaler”, “sealer”, “healer”, “header”, “reader”, “render”, “renter”, “ranter”, “ranker”, “hanker”, “hacker”, “hackee”, “hackle”, “heckle”, “deckle”, “decile”, “defile”, “define”, “refine”, “repine”, “rapine”, “ravine”, “raving”, “roving”, “roping”, “coping”, “coming”, “homing”, “hominy”, “homily”, “homely”, “comely”, “comedy”, “comedo”}
Invented by Lewis Carroll Review point: A problem may have multiple solutions. Different solutions may have different costs. We generally want the cheapest solution, the optimal solution. 1832 –1898 He suggested: APE ARE ERE ERR EAR MAR MAN But we can do: APE APT OPT OAT MAT MAN which takes one less move…
Some problems may have no solutions This problem has no solution
This time we are given the tree depth, here it is six. The branching factor is the number of letters in the word (4), times 25. 100 is a huge branching factor, but the fairly limited tree depth means it might be tenable. We can use depth-limited search, L = 6
But how do we know the legal paths? In other words, how do we know what are legal words? It would be best if we had a dictionary of legal words, but if needed…
:::: AROG has 1,060,00 hits FROM has 25,270,000,000 hits Actually 25,270,000,000 is the max Google allows
The “Google hits” strategy suggests a way to do greedy search, or hill-climbing search. We can expand the nodes with the highest count first That way we are very unlikely to waste time exploring the: FROG CROG … subtree etc
M K A D H B C E L F I Assume we have this tree. We have two goal states (highlighted). To understand all our algorithms, we can ask: “in what order do the nodes get REMOVE-FRONT (dequeued) for a given algorithm. G J
M K A D H B C E L F I I am going to do Depth First Search(Enqueue nodes in LIFO (last-in, first-out) order) You should do all algorithms (except perhaps bi-directional search) and make up new trees etc. G J
M K A D H B C E L F I Depth First Search Before entering the loop, the initial state A is enqueued Nodes A We now enter the loop for the first time (next slide), and do a test for an empty nodes data structure G J
M K H A D C B E L F I Depth First Search The front of Nodes is dequeued, it was A Nodes We ask A, are you the goal? Since the answer is no, we expand all A’s children (do every operator) and enqueue them in Nodes. Nodes C B G We now jump back to the top of the loop… J
M K H D A C B E L F I Depth First Search The front of Nodes is dequeued, it was B Nodes C We ask B, are you the goal? Since the answer is no, we expand all B’s children (do every operator) and enqueue them in Nodes. Nodes C E D G We now jump back to the top of the loop… J
M K H D A C B E L F I Depth First Search The front of Nodes is dequeued, it was D Nodes C E We ask D, are you the goal? Since the answer is no, we expand all D’s children (do every operator) and enqueue them in Nodes. Nodes C E I H G We now jump back to the top of the loop… J
M K H D A C B E L F I Depth First Search The front of Nodes is dequeued, it was H Nodes C E I We ask H, are you the goal? Since the answer is no, we expand all H’s children. As it happens, there are none Nodes C E I G We now jump back to the top of the loop… J
M K H D A C B E L F I Depth First Search The front of Nodes is dequeued, it was I Nodes C E We ask I, are you the goal? Since the answer is no, we expand all I’s children. As it happens, there are none Nodes C E G We now jump back to the top of the loop… J
M K H D A C B E L F I Depth First Search The front of Nodes is dequeued, it was E Nodes C We ask E, are you the goal? Since the answer is no, we expand all E’s children (do every operator) and enqueue them in Nodes. Nodes C J G We now jump back to the top of the loop… J
M K A D H B C E L F I Depth First Search The front of Nodes is dequeued, it was J Nodes C We ask J, are you the goal? Since the answer is yes, we report success! G J
Heuristic Search The search techniques we have seen so far... • Breadth first search • Uniform cost search • Depth first search • Depth limited search • Iterative Deepening • Bi-directional Search ...are all too slow for most real world problems uninformed search blind search
7 8 4 3 5 1 6 2 1 2 3 4 5 6 7 8 D FWD FW C C Sometimes we can tell that some states appear better that others...
...we can use this knowledge of the relative merit of states to guide search Heuristic Search (informed search) A Heuristic is a function that, when applied to a state, returns a number that is an estimate of the merit of the state, with respect to the goal. In other words, the heuristic tells us approximately how far the state is from the goal state*. Note we said “approximately”. Heuristics might underestimate or overestimate the merit of a state. But for reasons which we will see, heuristics that only underestimate are very desirable, and are called admissible. *I.e Smaller numbers are better
1 1 2 2 3 3 1 2 3 4 4 5 5 6 6 4 5 6 7 7 8 8 7 8 1 2 3 4 5 6 7 8 N N N N N N N Y Heuristics for 8-puzzle I Current State • The number of misplaced tiles (not including the blank) Goal State In this case, only “8” is misplaced, so the heuristic function evaluates to 1. In other words, the heuristic is telling us, that it thinks a solution might be available in just 1 more move. Notation: h(n) h(current state) = 1
3 1 2 2 8 3 4 4 5 5 6 6 7 7 1 8 Heuristics for 8-puzzle II 3 3 Current State 2 spaces • The Manhattan Distance (not including the blank) 8 3 spaces Goal State 8 1 In this case, only the “3”, “8” and “1” tiles are misplaced, by 2, 3, and 3 squares respectively, so the heuristic function evaluates to 8. In other words, the heuristic is telling us, that it thinks a solution is available in just 8 more moves. 3 spaces 1 Total 8 Notation: h(n) h(current state) = 8
5 6 4 3 4 2 1 3 3 0 2 h(n) We can use heuristics to guide “hill climbing” search. In this example, the Manhattan Distance heuristic helps us quickly find a solution to the 8-puzzle. But “hill climbing has a problem...”
6 1 2 3 4 5 8 6 7 1 2 3 1 2 3 5 7 4 5 8 4 5 6 7 6 7 8 1 2 1 2 3 6 6 4 5 3 4 5 6 7 8 6 7 8 h(n) In this example, hill climbing does not work! All the nodes on the fringe are taking a step “backwards” (local minima) Note that this puzzle is solvable in just 12 more steps.
We have seen two interesting algorithms. Uniform Cost • Measures the cost to each node. • Is optimal and complete! • Can be very slow. Hill Climbing • Estimates how far away the goal is. • Is neither optimal nor complete. • Can be very fast. Can we combine them to create an optimal and complete algorithm that is also very fast?
Uniform Cost SearchEnqueue nodes in order of cost 19 17 19 17 14 16 14 16 13 15 5 2 5 2 5 2 7 1 7 1 5 4 Intuition: Expand the cheapest node. Where the cost is the path cost g(n) Hill Climbing SearchEnqueue nodes in order of estimated distance to goal 17 19 Intuition: Expand the node you think is nearest to goal. Where the estimate of distance to goal is h(n)
The A* Algorithm (“A-Star”)Enqueue nodes in order of estimate cost to goal, f(n) g(n) is the cost to get to a node. h(n) is the estimated distance to the goal. f(n) = g(n) + h(n) We can think of f(n) as the estimated cost of the cheapest solution that goes through node n Note that we can use the general search algorithm we used before. All that we have changed is the queuing strategy. If the heuristic is optimistic, that is to say, it never overestimates the distance to the goal, then… A* is optimal and complete!
Informal proof outline of A* completeness • Assume that every operator has some minimum positive cost, epsilon. • Assume that a goal state exists, therefore some finite set of operators lead to it. • Expanding nodes produces paths whose actual costs increase by at least epsilon each time. Since the algorithm will not terminate until it finds a goal state, it must expand a goal state in finite time. Informal proof outline of A* optimality • When A* terminates, it has found a goal state • All remaining nodes have an estimated cost to goal (f(n)) greater than or equal to that of goal we have found. • Since the heuristic function was optimistic, the actual cost to goal for these other paths can be no better than the cost of the one we have already found.
How fast is A*? A* is the fastest search algorithm. That is, for any given heuristic, no algorithm can expand fewer nodes than A*. How fast is it? Depends of the quality of the heuristic. • If the heuristic is useless (ie h(n) is hardcoded to equal 0 ), the algorithm degenerates to uniform cost. • If the heuristic is perfect, there is no real search, we just march down the tree to the goal. Generally we are somewhere in between the two situations above. The time taken depends on the quality of the heuristic.
What is A*’s space complexity? A* has worst case O(bd) space complexity, but an iterative deepening version is possible ( IDA* )
A B C D E 1 2 3 4 5 www.youtube.com/watch?v=iogFXDWqDak
A Worked Example: Maze Traversal Problem: To get from square A3 to square E2, one step at a time, avoiding obstacles (black squares). Operators: (in order) • go_left(n) • go_down(n) • go_right(n) each operator costs 1. Heuristic: Manhattan distance A B C D E 1 2 3 4 5
A3 A A2 A4 B3 B g(A2) = 1 h(A2) = 4 g(B3) = 1 h(B3) = 4 g(A4) = 1 h(A4) = 6 A2 B3 A4 C D E 1 2 3 4 5 Operators: (in order) • go_left(n) • go_down(n) • go_right(n) each operator costs 1.
A3 A A4 A1 A2 B3 B g(A2) = 1 h(A2) = 4 g(B3) = 1 h(B3) = 4 g(A4) = 1 h(A4) = 6 A2 B3 A4 C D g(A1) = 2 h(A1) = 5 A1 E 1 2 3 4 5 Operators: (in order) • go_left(n) • go_down(n) • go_right(n) each operator costs 1.
A3 A A1 A2 A4 B3 B4 B g(A2) = 1 h(A2) = 4 g(B3) = 1 h(B3) = 4 g(A4) = 1 h(A4) = 6 A2 B3 A4 C3 C D g(A1) = 2 h(A1) = 5 A1 E g(C3) = 2 h(C3) = 3 g(B4) = 2 h(B4) = 5 1 2 3 4 5 C3 B4 Operators: (in order) • go_left(n) • go_down(n) • go_right(n) each operator costs 1.
A3 A A1 A2 A4 B3 B4 B1 B g(A2) = 1 h(A2) = 4 g(B3) = 1 h(B3) = 4 g(A4) = 1 h(A4) = 6 A2 B3 A4 C3 C D g(A1) = 2 h(A1) = 5 A1 E g(C3) = 2 h(C3) = 3 g(B4) = 2 h(B4) = 5 1 2 3 4 5 C3 B4 g(B1) = 3 h(B1) = 4 B1 Operators: (in order) • go_left(n) • go_down(n) • go_right(n) each operator costs 1.
A3 A A1 A2 A4 B3 B4 B5 B1 B g(A2) = 1 h(A2) = 4 g(B3) = 1 h(B3) = 4 g(A4) = 1 h(A4) = 6 A2 B3 A4 C3 C D g(A1) = 2 h(A1) = 5 A1 E g(C3) = 2 h(C3) = 3 g(B4) = 2 h(B4) = 5 1 2 3 4 5 C3 B4 g(B1) = 3 h(B1) = 4 B1 Operators: (in order) • go_left(n) • go_down(n) • go_right(n) each operator costs 1. g(B5) = 3 h(B5) = 6 B5
Here is a larger version of the previous example The black square is the initial state The white square is the goal state. The blue squares are the barriers In this version, diagonal moves are allowed.
Success! Please watch the video A* Pathfinding Algorithm Visualization https://www.youtube.com/watch?v=19h1g22hby8