1. Home
  2. Explore
  3. Computer Science
  4. Informed Search

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Informed Search

Speaker 1Speaker 2

Informed Search

18:49
0:00-18:49
  1. Maria

    Welcome to our episode on informed search. Before we get into the algorithms, there are a few course reminders. Homework One has two required parts: an electronic component on Gradescope, which you can retry until the deadline, and a written, exam-style component. It is due Tuesday next week because of Labor Day, but homework will typically be due on Mondays.

  2. Simon

    For the written part, the recommendation is to print the two-page assignment and try it on your own first, as you would an exam. After you’ve spent some time on it, discussing the problems with other students is fine, but you still need to submit your own work. It’s graded on effort and completion, and later you’ll correct your own work; that correction is graded on whether you do it precisely.

  3. Maria

    Project One is also out, due next Friday at 4 p.m. The deadline is meant to leave even last-minute workers with Friday evening and the weekend free. More importantly, this project is representative of the projects to come, unlike Project Zero, so it’s a useful way to assess your readiness with the coding.

  4. Simon

    You can work in a team of two, and the instructors recommend pair programming or another way of working together rather than dividing the project into separate halves. That way, both people learn the material. When you submit through Gradescope, remember that one person submits and must identify the partner, or the partner won’t receive a grade. Piazza and office hours are there for questions.

  5. Maria

    Sections have started, and you can attend any section of the relevant type, not just the one you signed up for on Piazza. The times and teaching assistants differ, so sections may vary a little; the staff also aim to record a webcast of that week’s section material on Fridays. For homework, whether you write by hand or type is up to you, but follow the PDF template because it helps with grading.

  6. Simon

    All right, onto informed search. Let’s begin with the basic search-problem model. We describe the world using states, which capture the details relevant to an agent’s decisions, and actions available in each state. A successor function says what state an action leads to and what that action costs.

  7. Maria

    We also specify a start state and a goal test, which tells us whether a state meets the goal condition. A search algorithm builds up a search tree, usually only part of it. Each tree node represents a path to a state, and that path’s cost is the sum of its action costs. The algorithm keeps candidate paths on a fringe and chooses which one to expand next.

  8. Simon

    That distinction between a state and a search-tree node matters: a node represents a path, not just a world configuration. An optimal search algorithm is one that finds a least-cost plan to a goal. And search problems don’t have to be about traveling through a map; pancake flipping gives us a different kind of example.

  9. Maria

    Imagine four differently sized pancakes stacked in the wrong order. The goal is largest at the bottom, then the next largest, and so on, with the smallest on top. You can place a spatula between pancakes—or beneath the stack—and flip everything above it. For a stack of four, there are three useful flip positions; flipping just the top pancake doesn’t change its position in a meaningful way.

  10. Simon

    There are 24 possible states, because there are four choices for the bottom pancake, then three for the next position, two for the next, and one for the top: four factorial. If a flip’s cost is the number of pancakes it moves, the robot can prefer a plan with a lower total flipping cost. The problem is about arranging a state, not finding a route across a map.

  11. Maria

    The general tree-search loop is straightforward. Put the start state on the fringe; while the fringe isn’t empty, choose a candidate, check whether it satisfies the goal, and if it doesn’t, expand it and add its successors. Different algorithms use different rules for choosing from the fringe. Depth-first search can use a stack, breadth-first search a queue, and a unified implementation can use a priority queue whose priorities depend on the strategy.

  12. Simon

    Uniform Cost Search is the uninformed method we’ll build on. It chooses the fringe path with the lowest cost so far. It is complete—if a solution exists, it will find one—and optimal, meaning it finds a least-cost solution. Its drawback is that it doesn’t use information about where the goal is, so it explores outward in all directions according to path cost.

  13. Maria

    On a grid, that can mean expanding many states that aren’t heading toward the goal. In a maze, Uniform Cost Search may expand nearly every reachable square that lies within the cost of the best route before it finally reaches the goal. It knows whether a state is a goal when it checks, but it has no estimate of which states are promising.

  14. Simon

    That’s where a heuristic comes in. A heuristic function, usually written h, estimates how much cost remains from a state to a goal. It’s designed for the particular search problem. The estimate can guide the search toward the goal, but it should also be quick enough to compute to be useful.

  15. Maria

    For Pac-Man moving north, east, south, and west on a grid, Manhattan distance to the goal is a natural heuristic. It fits the movement pattern, though it ignores walls, so it may underestimate the route that’s actually possible. Euclidean, or straight-line, distance is another estimate, but it’s less closely matched to movement when diagonal moves aren’t allowed.

  16. Simon

    For a route-finding problem such as traveling between cities in Romania, straight-line distance to Bucharest is a convenient heuristic if we have the cities’ coordinates. It estimates how far remains without requiring us to calculate the actual road route. For pancake flipping, one proposal is the number of pancakes out of place: each misplaced pancake must be involved in some flip, so that count gives information about the remaining work.

  17. Maria

    Another pancake idea is to look for pancakes that are already in a correctly ordered group. But remember, a heuristic normally measures estimated remaining cost, so it should be zero at a goal. A count of correctly ordered pancakes would need to be converted—for example, by subtracting it from the total—before it has that interpretation.

  18. Simon

    A different pancake heuristic is the label of the largest pancake that is still out of place. If that pancake is number three, the heuristic is three: some operation must reach at least that deep into the stack to put it in place. On the example in the lecture, this estimate was three while the number-of-misplaced-pancakes estimate was two. Since an action could take the stack directly to the goal at cost three, the first heuristic gave the more useful estimate in that case.

  19. Maria

    Now we can define Greedy Search. It chooses the fringe node with the smallest heuristic value—the one that appears closest to a goal—and ignores the cost already accumulated on the path. On the Romania map, it follows whichever city has the shortest straight-line distance to Bucharest. That can be fast, and it may find a solution quickly, but it doesn’t guarantee the solution is cheapest.

  20. Simon

    The danger is that a step can look promising because it gets closer to the goal while costing a lot. Greedy Search doesn’t account for that cost, so it may keep pursuing a tempting route and overlook a cheaper alternative. In the Romania example, it found a route through Fagaras, even though the route through Ramnicu Valcea was cheaper.

  21. Maria

    In the Pac-Man maze, Greedy Search expanded far fewer squares than Uniform Cost Search, but found a suboptimal path. Some squares that seemed less promising according to Manhattan distance stayed on the fringe and never got expanded, because the search reached the goal first. So less search work doesn’t automatically mean a better answer.

  22. Simon

    The next algorithm, A Star Search, combines what Uniform Cost Search and Greedy Search each contribute. Uniform Cost Search is careful about the cost so far but doesn’t aim toward the goal; Greedy Search aims toward the goal but ignores cost so far. A Star uses both.

  23. Maria

    We write g(n) for the cost accumulated to reach node n, and h(n) for the estimated cost remaining. A Star ranks each fringe node using f(n) = g(n) + h(n). In the small example, it expanded fewer nodes than Uniform Cost Search and still found the lower-cost route, because it considered both the cost already paid and the estimated cost still to come.

  24. Simon

    There’s a crucial implementation detail: don’t declare success when you first put a goal onto the fringe, or enqueue it. Wait until the goal is removed from the fringe for expansion—dequeued, or popped. A goal path may be discovered early but have a higher cost than another path that hasn’t been fully considered yet.

  25. Maria

    For example, the search might first enqueue a goal with path cost five while another fringe node has f-value four and can still lead to a goal of cost four. If you stop as soon as the cost-five goal is enqueued, you return the wrong answer. Waiting until a goal is popped lets the lower-priority-cost candidate be considered first.

  26. Simon

    But A Star isn’t automatically optimal for every possible heuristic. If h overestimates the remaining cost, a good route can be held back on the fringe while a more expensive goal is popped first. In the counterexample from the lecture, the heuristic claimed a route had six units remaining when it actually had only three, and that misleading estimate caused a suboptimal solution.

  27. Maria

    The condition that gives us the optimality guarantee for A Star Tree Search is admissibility. Let h-star(n) mean the true cheapest cost from n to a goal. A heuristic is admissible if, for every node, h(n) is no greater than h-star(n). In plain language, it is optimistic: it never overestimates the cheapest remaining cost.

  28. Simon

    Manhattan distance in a grid with walls is admissible because ignoring walls can only make the estimated route shorter or equal to the actual cheapest route. The largest-misplaced-pancake heuristic is admissible for the reason we discussed: at least that much of the stack still has to be reached by a flip. You can justify admissibility by reasoning about what every solution must do, without first knowing the exact optimal cost.

  29. Maria

    Here’s the intuition behind the optimality proof. Take an optimal goal node A in the search tree and a suboptimal goal node B. If B is on the fringe, then either A has already been expanded, or some ancestor n of A must still be on the fringe. Because the heuristic is admissible, f(n) cannot exceed the cost of the optimal solution represented by A.

  30. Simon

    And because A is optimal while B is suboptimal, the cost of A is lower than the cost of B. At goal nodes the heuristic is zero, so their f-values are just their path costs. Thus f(n) is no greater than f(A), and f(A) is less than f(B). The fringe ordering makes n come before B; following A’s ancestors in turn gets A expanded before B. So A Star cannot pop a suboptimal goal first.

  31. Maria

    Notice that admissible doesn’t mean accurate. A heuristic can be a very loose underestimate and still preserve optimality, though it may not guide the search much. If h is zero everywhere, it is admissible, but f is just g, so A Star reduces to Uniform Cost Search. A more informative admissible heuristic can reduce the number of expansions.

  32. Simon

    That effect showed up in the maze examples. Uniform Cost Search spread out broadly, while A Star, using a heuristic related to distance to the goal, favored the direction of the goal and expanded fewer states. In one small Pac-Man maze, Uniform Cost Search expanded more than 9,000 nodes, whereas A Star with its heuristic expanded 175 and still found the optimal solution.

  33. Maria

    The water-maze comparison helps distinguish the strategies visually. Breadth-first search spreads out by number of steps. Greedy search heads toward what looks closest to the goal but can choose a costly route. Uniform Cost Search accounts for the cost of traversed squares but explores broadly. Depth-first search follows a path deeply, and A Star uses both accumulated cost and an estimate of progress toward the goal.

  34. Simon

    Heuristic design is the practical art here. Consider the eight-puzzle: a state is a configuration of the eight numbered tiles and the empty space, and an action moves a tile into the empty spot. There are nine factorial possible arrangements. If each tile move costs one, a simple heuristic is the number of tiles in the wrong position.

  35. Maria

    That heuristic is admissible because every misplaced tile has to be moved at least once. It can make a huge difference: in the lecture’s examples, Uniform Cost Search expanded 3.6 million nodes on a problem twelve steps from the goal, while A Star with a heuristic did far less work. But we can improve on the misplaced-tile count.

  36. Simon

    Total Manhattan distance adds up, for each tile, how many grid steps it is from its destination. It’s admissible because it imagines a relaxed problem in which tiles can move toward their destinations without constraining one another. That relaxed problem can’t require more moves than the real one, so its optimal cost is a lower bound. In the example, it reduced the expansions from 227 to 73.

  37. Maria

    Relaxed problems are a general way to invent admissible heuristics. Add actions or remove constraints to make a simpler version of the original problem, then use the optimal cost in that relaxed problem as the heuristic. For Pac-Man, one relaxation is to ignore walls. For the eight-puzzle, it’s to let tiles move without getting in each other’s way.

  38. Simon

    There’s a trade-off: a heuristic closer to the true remaining cost tends to reduce the number of nodes A Star expands, but may take more work to compute. The perfect heuristic would be the true optimal cost, but if you already knew that value, you’d have essentially solved the problem. In practice, we want a useful estimate that is inexpensive to obtain.

  39. Maria

    We can compare heuristics more formally. One heuristic dominates another if its value is at least as high at every node, while both remain admissible. A higher admissible estimate is generally more informative because it is closer to the true cost without exceeding it. Some heuristics can’t be ordered this way, since one is higher at some nodes and lower at others.

  40. Simon

    And if two heuristics are admissible, taking their maximum is also admissible. The maximum is at least as informative as either one by itself, because it keeps the stronger estimate at each node without exceeding the true cost. That gives us a simple way to combine useful lower bounds.

  41. Maria

    So far we’ve been talking about Tree Search, where the same state can appear multiple times through different paths. That can make the search tree grow enormously even when the underlying state graph is much smaller. Graph Search tries to avoid repeating work by keeping track of states it has already expanded.

  42. Simon

    It keeps an expanded-state collection called the closed set. Before calling the successor function on a state, check whether that state is already in the closed set. If so, skip it; if not, expand it and add it to the set. Store this as a set, not a list, so checking membership is efficient.

  43. Maria

    For breadth-first search, suppose we’ve already reached a state in one step and later encounter another path to it that takes two steps. Expanding it again through the longer route can’t reveal anything useful: the possible continuations are the same, but this path is already worse. Avoiding that repeated expansion saves work. Graph Search remains complete in the account we’re using because the part it skips is represented by an expansion it has already done.

  44. Simon

    Optimality is the subtle part. In A Star Graph Search, a state might first be expanded along a more expensive path. If a better route to that state is found later, the closed set can prevent the state from being expanded again, so the better route’s continuations may never be explored. Admissibility alone doesn’t prevent that problem.

  45. Maria

    The stronger condition used for A Star Graph Search is consistency. For every transition from a to c with step cost, the heuristic at a must be no greater than that step cost plus the heuristic at c. It’s a local condition: the estimate before taking one step can’t be greater than the cost of that step plus the estimate afterward.

  46. Simon

    In the counterexample, one state had a heuristic of four, its successor had a heuristic of one, and the connecting step cost one. That violates consistency, because four is greater than one plus one. Changing the first estimate to two makes the inequality hold. Consistency is what prevents the search from being misled in the way that caused the closed-set problem.

  47. Maria

    Consistency also explains the key property of A Star’s f-values. If h(a) is no greater than the step cost plus h(c), adding the cost already paid to both sides shows that f(a) is no greater than f(c). So along a path, f never decreases as we move forward.

  48. Simon

    That means, with a consistent heuristic, when A Star Graph Search pops a goal, no later candidate can offer a cheaper solution. The f-values move upward along paths, and anything with a lower f-value would have been popped earlier. Consistency is stronger than admissibility, and it gives Graph Search the optimality guarantee that Tree Search gets from admissibility.

  49. Maria

    Let’s close with the main picture. A Star combines cost so far, g, with estimated cost remaining, h, and chooses by f equals g plus h. For Tree Search, admissible heuristics guarantee an optimal solution; for Graph Search, consistent heuristics provide the needed guarantee. Good heuristic design—often using relaxed problems—is what makes A Star efficient in practice.

  50. Simon

    And the practical lessons are just as important: don’t stop when a goal is merely enqueued; wait until it is popped. Keep the closed set as a set in Graph Search. And when designing a heuristic, ask both whether it is a valid lower bound and whether computing it is worth the search work it saves.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent