1. Home
  2. Explore
  3. Computer Science
  4. Search in AI

Study methods

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

Search in AI

Search Problems and Uninformed Search

Agents that Plan

Planning agents consider not just the immediate next action but also the future consequences. They ask "what if?" and make decisions based on the predicted outcomes of their actions. This contrasts with reflex agents, which choose actions based only on current percepts and memory, without considering future consequences.

A reflex agent can sometimes be rational in specific circumstances (e.g., a simple rule like "if a pellet is next to you, eat it" works well in a simple environment), but generally it cannot be fully rational because it cannot precompute a table for every possible state. A reflex agent fails when the environment requires reasoning about unseen future states (e.g., turning a corner to find pellets).

A planning agent requires:

  • A model of how the world evolves in response to actions

  • A goal test to determine whether a possible future meets the objective

  • The ability to consider sequences of actions, not just the next step

Planners can be characterized as optimal (finding the best solution), complete (finding a solution if one exists), and can involve planning (full precomputation) vs. replanning (periodically re-evaluating).

A search problem is the most pared-down, simplified version of how to search for solutions.


Components of a Search Problem

A search problem consists of the following:

  • State space: The set of all possible configurations the world can be in.

  • Successor function: Given a state and an action, it returns the next state and the cost of that action.

  • Start state: The state where the agent begins.

  • Goal test: A function that determines whether a given state is a goal state.

A solution to a search problem is a sequence of actions (a plan) that transforms the start state into a goal state. The cost of a solution is the sum of the costs of each action in the plan.

Search problems are abstractions of the real world. The real world has an enormous (potentially uncountable) state space, but most details are irrelevant for a specific problem. For example, when navigating from Arad to Bucharest, the state can be just the current city, ignoring everything else. The art of choosing an abstraction—what to include and what to ignore—remains a hard problem in AI.

Example: Pathfinding in Romania

  • States: cities (Arad, Sibiu, Timisoara, etc.)

  • Successor function: taking a road leads to the adjacent city; cost is the road distance

  • Start state: Arad

  • Goal test: is the current city Bucharest?

  • Solution: the least-cost sequence of roads connecting Arad to Bucharest

State Space vs. World State

  • World state: includes every detail of the environment (positions of all objects, internal states, etc.)

  • Search state: keeps only the details needed for planning (e.g., your coordinates, remaining dots)

For the Pac-Man pathing problem (just moving from one location to another), the state is just the coordinates. For the "eat all the dots" problem, the state must also track which dots remain (a boolean per dot). For "eat all dots and keep ghosts scared" the state must also track the remaining scare time and big dot locations.

The state space size grows multiplicatively. For a Pac-Man level with 120 agent positions, 30 food dots (each present or absent: 2302^{30}230 possibilities), 12 ghost positions (each could be anywhere, possibly 12212^2122 if multiple ghosts), and 4 facing directions, the total world states are enormous. Abstraction reduces this dramatically—pathing only needs 120 states.


Search Trees vs. State Space Graphs

A state space graph is a mathematical representation of the search problem:

  • Nodes are states (each unique)

  • Arcs are successor relationships (actions)

  • Start and goal nodes are marked

This graph is usually too large to store in memory, but it helps conceptualize the problem.

A search tree is built during the search process:

  • The root node is the start state.

  • Each node corresponds to a path from the start to that state.

  • Children of a node are the possible successors.

  • The tree can contain multiple copies of the same state if it can be reached via different paths.

  • Cycles in the state space graph lead to infinite search trees.

In the search tree, each node represents an entire path in the state space graph, not just a state.


General Tree Search Algorithm

The algorithm takes a problem and a strategy (the rule for choosing which node to expand) and returns a solution or failure.

function TREE-SEARCH(problem, strategy) returns solution or failure
  initialize fringe (a list of partial plans)
  insert start state into fringe
  loop:
    if fringe is empty then return failure
    choose a leaf node from fringe according to strategy
    if the node is a goal state then return the corresponding path
    else expand the node and add its children to fringe

The fringe contains all leaf nodes (partial plans) under consideration. The expansion of a node generates its children (successors) and adds them to the fringe, removing the expanded node.

The critical design choice is how to select a node from the fringe. This determines the behavior of the search.


Uninformed Search Methods

Uninformed search means the strategy has no additional information about the desirability of states beyond the problem definition. The algorithms know only the start, the successor function, and the goal test.

Depth-First Search (DFS)

Strategy: The fringe is a stack (last-in, first-out). The most recently added node is expanded next.

Procedure: Start at the root, go down one branch as far as possible. When a dead end is reached (node has no children), backtrack to the most recent unexplored sibling. Continue until the goal is found or all nodes are exhausted.

Properties:

  • Complete? No, unless the search space is finite and loop-checking is used. Infinite paths (due to cycles or infinite state space) can cause the search to never terminate.

  • Optimal? No. It finds the leftmost solution, not necessarily the shortest or cheapest.

  • Time complexity: O(bm)O(b^m)O(bm), where bbb is the branching factor (average number of children per node) and mmm is the maximum depth of the tree. In the worst case, the entire tree is visited.

  • Space complexity: O(b⋅m)O(b \cdot m)O(b⋅m). Only the current path and its unexplored siblings need to be stored—the fringe size is proportional to depth times branching factor.

When adding children to the stack, the order (left-to-right or right-to-left) is arbitrary but affects the order of exploration. In the typical implementation, children are added in some order, and the last added is expanded first.

Loop checking (avoiding revisiting states on the current path) can make DFS complete for finite state spaces but not for infinite ones (e.g., an infinite line).

Breadth-First Search (BFS)

Strategy: The fringe is a queue (first-in, first-out). The node that has been waiting the longest is expanded next.

Procedure: Expand all nodes at depth ddd before any node at depth d+1d+1d+1. This creates layers of exploration.

Properties:

  • Complete? Yes, if the shallowest solution has finite depth sss. The algorithm will examine all nodes at depth sss before moving deeper, so if a solution exists at some depth, it will be found.

  • Optimal? Yes, if all actions have equal cost (cost = 1). The shallowest solution is the path with the fewest actions. If costs vary, BFS is not optimal.

  • Time complexity: O(bs)O(b^s)O(bs), where sss is the depth of the shallowest solution.

  • Space complexity: O(bs)O(b^s)O(bs). The fringe at depth sss contains up to bsb^sbs nodes (all nodes at that depth must be stored).

BFS is generally preferred when memory is sufficient, because it guarantees finding the shallowest solution. However, memory is often the bottleneck.

When to use DFS vs. BFS:

  • DFS uses much less memory (b⋅mb \cdot mb⋅m vs. bsb^sbs).

  • BFS is complete and optimal for uniform costs.

  • If the branching factor is high and the solution is deep, BFS may run out of memory, making DFS the only feasible option (even with its risks).

  • If the state space has cycles, DFS without loop checking may fail; BFS handles cycles gracefully.

  • DFS can be lucky and find a solution quickly, but worst-case performance is much worse.

Iterative Deepening Search (IDS)

Idea: Combine the space advantage of DFS with the time and shallow-solution advantage of BFS. Run DFS repeatedly with increasing depth limits.

function ITERATIVE-DEEPENING(problem) returns solution
  for depth = 0 to infinity:
    result = DEPTH-LIMITED-SEARCH(problem, depth)
    if result != failure then return result

Properties:

  • Complete: Yes (like BFS) because it will eventually reach any finite depth.

  • Optimal: For uniform costs, yes—because it finds the shallowest solution.

  • Time complexity: O(bs)O(b^s)O(bs) — only slightly worse than BFS because most work is done at the deepest level. For b=10b=10b=10, s=5s=5s=5, BFS visits ≈111,110\approx 111,110≈111,110 nodes; IDS visits ≈123,450\approx 123,450≈123,450 nodes (about 11% more).

  • Space complexity: O(b⋅s)O(b \cdot s)O(b⋅s) — same as DFS.

The apparent redundancy of re-exploring upper levels is minor because the number of nodes at depth ddd dominates the total. IDS is often the preferred uninformed search method for large spaces with uniform costs.

Uniform Cost Search (UCS)

Strategy: The fringe is a priority queue keyed by the cumulative path cost from the start. The node with the lowest total cost so far is expanded next.

Procedure: Expand nodes in order of increasing path cost. This explores "cost contours"—all nodes with cost ≤C\leq C≤C are expanded before any node with cost >C> C>C. When a goal node is popped from the priority queue, the algorithm terminates and returns that solution.

Why not return the goal as soon as it is generated? A cheaper path to the goal might still be in the fringe (with lower cost). By waiting until the goal is popped from the priority queue, we ensure it has the smallest cost among all reachable states.

Properties:

  • Complete? Yes, if all action costs are positive and there is a positive minimum edge cost ϵ>0\epsilon > 0ϵ>0. The depth in terms of steps is bounded by C∗/ϵC^* / \epsilonC∗/ϵ, where C∗C^*C∗ is the optimal solution cost.

  • Optimal? Yes. It finds the least-cost path.

  • Time complexity: O(bC∗/ϵ)O(b^{C^* / \epsilon})O(bC∗/ϵ) — the effective depth is C∗/ϵC^*/\epsilonC∗/ϵ, which can be large if costs are very small.

  • Space complexity: O(bC∗/ϵ)O(b^{C^* / \epsilon})O(bC∗/ϵ) — similar to time, as the fringe stores all explored but unexpanded nodes.

A key difference from BFS: BFS expands all nodes at depth ddd before those at d+1d+1d+1; UCS expands all nodes with cost ≤C\leq C≤C before those with cost >C> C>C. When all costs are equal, UCS behaves exactly like BFS.

Demonstration: In a maze with varying water depth, UCS focuses search on shallower (cheaper) water, avoiding deep (costly) paths, while BFS expands uniformly in all directions and DFS follows a single branch.


Comparison of Uninformed Search Algorithms

Algorithm

Fringe Data Structure

Complete?

Optimal?

Time Complexity

Space Complexity

DFS

Stack (LIFO)

No (unless finite with loop checking)

No

O(bm)O(b^m)O(bm)

O(b⋅m)O(b \cdot m)O(b⋅m)

BFS

Queue (FIFO)

Yes

Yes (if all costs equal)

O(bs)O(b^s)O(bs)

O(bs)O(b^s)O(bs)

IDS

Stack (LIFO) per iteration

Yes

Yes (if all costs equal)

O(bs)O(b^s)O(bs)

O(b⋅s)O(b \cdot s)O(b⋅s)

UCS

Priority Queue (by path cost)

Yes (if ϵ>0\epsilon > 0ϵ>0)

Yes

O(bC∗/ϵ)O(b^{C^* / \epsilon})O(bC∗/ϵ)

O(bC∗/ϵ)O(b^{C^* / \epsilon})O(bC∗/ϵ)

Where:

  • bbb = branching factor

  • mmm = maximum depth of the tree

  • sss = depth of the shallowest solution

  • C∗C^*C∗ = cost of the optimal solution

  • ϵ\epsilonϵ = minimum action cost

Key Takeaways

  • All these algorithms differ only in their fringe selection strategy — the rest of the algorithm is identical.

  • BFS and UCS are complete and optimal under their respective assumptions but have large memory requirements.

  • DFS uses little memory but is not complete or optimal.

  • Iterative deepening is a practical middle ground, offering BFS-like completeness and optimality with DFS-like memory usage.

  • Uniform cost search generalizes BFS to handle non-uniform action costs and is the standard for cost-sensitive optimal search without heuristic guidance.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent