Not every problem can be solved in the same way. Studying the characteristics of a problem helps in choosing the method.
Characteristics of Problems
We ask a few questions about a problem. Can it be decomposed into smaller independent sub-problems, as in symbolic integration? Can steps be ignored or undone, as in theorem proving, or are they irreversible, as in chess? Is the universe predictable, so that we can plan, or uncertain, as in card games? Is a good solution absolute, since any path is fine, or relative, since we need the best path? Is the solution a state or a path? Does the problem need much knowledge, or only a little? And is interaction with a person needed during the solution? The answers guide the choice of method.
Brute-Force (Uninformed) Search
A brute-force or blind search uses only the problem definition and has no knowledge of how close a state is to the goal. It is general, but it can be slow. We judge a search by four properties. Completeness asks if it always finds a solution when one exists. Optimality asks if the solution it finds is the cheapest. Time complexity and space complexity count the nodes that it makes and stores. With branching factor b and depth d, the number of nodes grows like b to the power d.
Breadth-First and Depth-First Search
Breadth-first search (BFS) expands the shallowest node first, using a queue. It is complete, and optimal when each step costs the same, but it needs a lot of memory, of order b^d. Depth-first search (DFS) expands the deepest node first, using a stack. It needs little memory, of order b × d, but it can follow an infinite path and miss the goal, so it is not complete, and the solution found may not be the shortest. Depth-limited search stops DFS at a fixed depth, and iterative deepening repeats it with a growing limit, giving the completeness of BFS with the memory of DFS.
Definition: Brute-force search. A search method that uses only the problem definition, exploring states systematically with no knowledge of how close a state is to the goal.
Table 1.3: Brute-force search methods
| Method | Data structure | Complete | Optimal |
|---|---|---|---|
| Breadth-first | Queue | Yes | Yes (equal costs) |
| Depth-first | Stack | No | No |
| Iterative deepening | Stack, repeated | Yes | Yes (equal costs) |
Example 1.3: Breadth-first search on a graph
Problem: Find the shortest path from A to F in the graph A-B, A-C, B-D, C-D, D-F, C-E, E-F using BFS.
Solution:
- Start with queue [A].
- Expand A: add B and C.
- Expand B: add D. Expand C: add E (D is already added).
- Expand D: add F. The goal F is found, reached through A, B, D.
Result: The shortest path has 3 edges, A to B to D to F.
from collections import deque
g = {"A":["B","C"],"B":["D"],"C":["D","E"],"D":["F"],"E":["F"],"F":[]}
q = deque([["A"]])
seen = {"A"}
while q:
p = q.popleft()
if p[-1] == "F":
print("-".join(p))
break
for n in g[p[-1]]:
if n not in seen:
seen.add(n)
q.append(p + [n])
Output
A-B-D-F
Common mistakes
- Using DFS when the shortest path is needed.
- Forgetting a visited list, so that the search loops on graphs with cycles.
- Ignoring memory: BFS can run out of memory before it runs out of time.
Key takeaways
- Blind search has no idea of the distance to the goal.
- BFS uses a queue and is complete and optimal for equal costs.
- DFS uses a stack, needs little memory but is not complete.
- Iterative deepening combines the strengths of both.
Practice questions
- What is blind search?
- Compare BFS and DFS.
- What is the branching factor?
Answers to the practice questions
- Search that uses only the problem definition, with no knowledge of how close a state is to the goal.
- BFS uses a queue, is complete and finds the shallowest goal but needs much memory; DFS uses a stack and little memory but may miss the goal.
- The number of successors of a node.
Sources
- Official BCA syllabus Artificial Intelligence and Expert Systems, course code 25BCA405DS02, Unit I
