1. 搜索策略概述 (Search Strategy Overview)
搜索策略的核心思想是:
- 选择一个选项进行探索。
- 如果第一个选择不正确,则保留其他选项以供后续检查。
- 所有的搜索方法都使用这种基本搜索策略。
问题:应该先选择哪个选项?
English Translation:
The core idea of a search strategy is:
- Choose an option to explore.
- If the first choice is incorrect, keep other options for later inspection.
- All search methods use this basic search strategy.
Problem: Which option should be chosen first?
Exam Notes:
- A search strategy determines how to explore possible solutions.
- Selecting the correct option early can reduce computational effort.
2. 不同的搜索策略 (Different Search Strategies)
- 选择扩展的节点不同。
- 选择更接近目标的节点 → 更少工作量 → 更快找到答案。
- 在深入讨论搜索策略之前,需要先定义一些术语。
English Translation:
- Different choices for node expansion.
- A better choice (closer to the goal) → Less work → Faster solution.
- Before discussing search strategies in detail, some key terms need to be defined.
Exam Notes:
- Different strategies prioritize different nodes.
- A heuristic approach can speed up the process.
3. 节点的五个组成部分 (Five Components of a Well-Defined Node)
- 状态 (State): 该节点在状态空间中的状态。
- 父节点 (Parent): 该节点由哪个节点生成。
- 动作 (Action): 由哪个动作从父节点生成。
- 路径成本 (Path-Cost): 从初始状态到当前节点的总成本 g(n)。
- 深度 (Depth): 从初始状态到该节点的步数。
English Translation:
- State: The state of the node in the state space.
- Parent: The node from which this node was generated.
- Action: The action that led to the generation of this node.
- Path-Cost: The total cost from the initial state to this node, denoted as g(n).
- Depth: The number of steps from the initial state to this node.
Exam Notes:
- Understanding node structure is crucial for search algorithms.
- Path cost influences search efficiency.
4. 队列与搜索前沿 (Queue and Search Frontier)
搜索算法需要存储“前沿”(frontier),即当前探索的节点集合。数据结构应支持快速选择下一个扩展节点,通常使用队列。
English Translation:
The search algorithm must store the "frontier," which is the set of currently explored nodes. The data structure should support fast selection of the next node to expand, usually using a queue.
Exam Notes:
- The frontier maintains the nodes waiting for expansion.
- Choosing an appropriate data structure impacts efficiency.
5. 队列的三种类型 (Three Types of Queues)
- FIFO 队列 (First-In, First-Out):
- 弹出队列中最早插入的元素。
- LIFO 队列 (Last-In, First-Out,栈):
- 弹出队列中最近插入的元素。
- 优先级队列 (Priority Queue):
- 根据某种排序函数,弹出优先级最高的元素。
English Translation:
- FIFO Queue (First-In, First-Out):
- Pops the oldest element in the queue.
- LIFO Queue (Last-In, First-Out, Stack):
- Pops the most recently inserted element.
- Priority Queue:
- Pops the element with the highest priority according to a defined ordering function.
Exam Notes:
- FIFO is used in Breadth-First Search (BFS).
- LIFO is used in Depth-First Search (DFS).
- Priority queues are used in informed search strategies.
📌 中文博客:搜索算法性能衡量与不同搜索策略
在人工智能和计算机科学中,搜索算法的性能决定了解决问题的效率。我们可以通过完备性(Completeness)、最优性(Optimality)、时间复杂度(Time Complexity)和空间复杂度(Space Complexity)等指标来衡量搜索策略的优劣。此外,搜索策略可以分为无信息搜索(Uninformed Search)和有信息搜索(Informed Search),不同策略适用于不同的应用场景。本文将详细介绍这些概念。
1. 搜索算法的性能衡量标准
1.1 完备性(Completeness)
完备性指的是算法是否一定能找到解。如果一个搜索算法总是能够找到可达解,则它是完备的。
此外,还需要考虑:
- 算法是否会陷入无限循环(如深度优先搜索可能会陷入死循环)。
- 是否能够避免重复状态,提高搜索效率。
1.2 最优性(Optimality)
最优性指的是找到的解是否是最优解,即是否找到代价最小的路径。例如:
- 广度优先搜索(BFS) 在代价一致时能保证最优解。
- A 搜索算法* 在满足某些条件下可以找到最优解。
1.3 时间复杂度(Time Complexity)
时间复杂度表示搜索算法找到解所需的时间,通常用节点扩展的数量衡量。
时间复杂度的计算涉及以下参数:
- b(分支因子):每个节点的平均子节点数。
- d(目标节点的最浅深度):找到解时的最小步数。
- m(状态空间的最大深度):问题的最大可能路径。
通常,时间复杂度表示为 O(b^d) 或 O(b^m)。
1.4 空间复杂度(Space Complexity)
空间复杂度表示搜索过程中需要存储的最大节点数,也可以用O(b^d) 或 O(b^m) 表示。
2. 时间与空间复杂度的计算
时间与空间复杂度通常由以下因素决定:
- b(分支因子):每个节点可扩展的最大子节点数。
- d(目标节点的深度):最优解所在的最小深度。
- m(状态空间的最大深度):搜索树的最大可能路径。
计算时通常使用 大 O 记号(Big-O notation) 来衡量复杂度。例如:
- 广度优先搜索(BFS) 的时间复杂度为 O(b^d),空间复杂度也为 O(b^d)。
- 深度优先搜索(DFS) 的时间复杂度为 O(b^m),空间复杂度为 O(bm)(只存储路径上的节点)。
3. 搜索策略分类
3.1 无信息搜索(Uninformed Search)
无信息搜索(又称盲目搜索)不使用额外的启发信息,仅依赖基本的搜索规则遍历搜索空间。主要包括:
- 广度优先搜索(BFS)
- 深度优先搜索(DFS)
- 一致代价搜索(Uniform Cost Search)
- 深度限制搜索(Depth-limited Search)
- 迭代加深搜索(Iterative Deepening Search)
3.2 有信息搜索(Informed Search / Heuristic Search)
有信息搜索使用启发式函数(Heuristic Function) 来指导搜索,使得算法更高效。
常见的启发式搜索算法:
- A 搜索*
- 贪心最佳优先搜索(Greedy Best-First Search)
4. 具体搜索算法解析
4.1 广度优先搜索(BFS)
广度优先搜索使用FIFO 队列(先进先出),按照层次展开搜索,确保最优解。
BFS 伪代码
BREADTH-FIRST-SEARCH(problem)
1. 初始化:
- 创建一个 FIFO 队列 frontier,存入初始节点
- 创建 explored 集合,存储已访问节点
2. 进入循环:
- 若 frontier 为空,返回失败
- 取出队列头部节点 node
- 若 node 是目标状态,返回成功
- 遍历 node 的所有子节点 child:
- 若 child 不在 frontier 和 explored 中,加入 frontier
BFS 的时间复杂度和空间复杂度:O(b^d)。
4.2 深度优先搜索(DFS)
深度优先搜索使用LIFO 栈(后进先出),优先沿着路径向下探索,适合解决空间复杂度较大的问题。
DFS 伪代码
DEPTH-FIRST-SEARCH(problem)
1. 初始化:
- 创建一个 LIFO 栈 frontier,存入初始节点
- 创建 explored 集合,存储已访问节点
2. 进入循环:
- 若 frontier 为空,返回失败
- 取出栈顶节点 node
- 若 node 是目标状态,返回成功
- 遍历 node 的所有子节点 child:
- 若 child 不在 frontier 和 explored 中,加入 frontier
DFS 的时间复杂度:O(b^m),空间复杂度 O(bm)。
5. 总结
-
衡量搜索算法的性能主要考虑:
- 完备性(能否找到解)
- 最优性(找到的解是否最优)
- 时间复杂度(搜索所需时间)
- 空间复杂度(存储需求)
-
时间复杂度计算时,主要参数包括:
- b(分支因子):每个节点的最大子节点数
- d(最小目标深度)
- m(最大路径长度)
-
搜索策略分类:
- 无信息搜索(Uninformed Search):不依赖启发信息(如 BFS, DFS)。
- 有信息搜索(Informed Search):使用启发式指导搜索(如 A*)。
-
广度优先搜索(BFS)适用于:需要最优解的情况,但占用较大内存。
-
深度优先搜索(DFS)适用于:内存受限的问题,但不保证最优解。
📌 英文翻译:Searching Algorithm Performance & Strategies
1. Performance Metrics
- Completeness: Can a solution always be found?
- Optimality: Is the solution the best among possible solutions?
- Time Complexity: How long does it take to find a solution?
- Space Complexity: How much memory is required?
2. Time & Space Complexity Calculation
- b: Branching factor (number of successors per node)
- d: Depth of the shallowest goal node
- m: Maximum path length
Complexity measured using Big-O notation:
- BFS: O(b^d)
- DFS: O(b^m)
3. Search Strategies
- Uninformed Search: No heuristic, includes BFS, DFS.
- Informed Search: Uses heuristics, includes A*.
4. BFS vs DFS
- BFS: Guarantees optimal solution, but uses more memory.
- DFS: Uses less memory, but may not find the best solution.
📚 考试笔记(Exam Notes)
- BFS: FIFO Queue, O(b^d).
- DFS: LIFO Stack, O(b^m).
- Time Complexity: O(b^d) vs O(b^m).
- Uninformed vs Informed Search: Blind vs Guided.
希望这份博客+英文翻译+考试笔记对你的复习有帮助!📖💡
1.2 Optimality Conditions of A Search Algorithm*
English Explanation
-
Admissible Heuristic
The heuristic function h(n)h(n) must never overestimate the true cost h∗(n)h^*(n) from node nn to the goal.
Example: In grid-based maps, Manhattan distance or Euclidean distance is commonly used as a heuristic estimate. -
Consistent Heuristic
h(n)≤c(n,m)+h(m)h(n) \leq c(n, m) + h(m)
For every pair of adjacent nodes nn and mm, the heuristic must satisfy:where c(n,m)c(n, m) is the movement cost between nodes nn and mm. This ensures path expansion consistency and prevents unnecessary backtracking.
-
Finite Problem Domain
The graph must contain a finite number of nodes; otherwise, the algorithm may enter an infinite loop. -
Non-Negative Path Costs
The actual cost g(n)g(n) of all paths must be non-negative, ensuring the validity of physical quantities such as time and distance.
BFS 和 DFS 评价及一致代价搜索(UCS)
在搜索算法中,广度优先搜索(BFS) 和 深度优先搜索(DFS) 是最基础的无信息搜索方法,它们分别适用于不同的搜索场景。此外,一致代价搜索(Uniform Cost Search, UCS)改进了 BFS,使其可以找到最小代价路径。本文将探讨这些算法的特点、优缺点和适用场景。
1. 广度优先搜索(BFS)的评价
1.1 BFS 的特点
- 完备性(Completeness) ✅
BFS 总能找到解(如果有解),因为它按照层次进行搜索。 - 最优性(Optimality) ✅
当每一步的代价相同时,BFS 保证找到最短路径(最优解)。 - 时间复杂度(Time Complexity) ⏳
设 b 为分支因子,d 为目标节点的最小深度,时间复杂度为: O(bd)O(b^d) - 空间复杂度(Space Complexity) 💾
需要存储所有已访问的节点,空间复杂度为: O(bd)O(b^d)
1.2 BFS 的缺点
- 当分支因子 bb 很大时,BFS 消耗大量内存。
- 计算资源需求高,在某些情况下不可行(如棋类游戏)。
2. 一致代价搜索(UCS)
2.1 UCS 的特点
一致代价搜索(UCS)是一种基于路径代价(path cost g(n)g(n))的搜索方法,它是BFS 的改进版:
- 始终扩展当前代价最小的节点,而不是按层次扩展。
- 适用于不同步长的路径(即每条边的代价不同)。
2.2 UCS vs BFS
特性 | BFS | UCS |
---|---|---|
数据结构 | FIFO 队列(先进先出) | 优先队列(Priority Queue) |
目标检测 | 在生成节点时检查是否是目标 | 在扩展节点时检查是否是目标 |
路径代价 | 忽略路径代价,只按层扩展 | 始终扩展当前路径代价最小的节点 |
2.3 UCS 的性质
- 完备性(Completeness) ✅
只要每条边的代价大于 0,UCS 就能找到解。 - 最优性(Optimality) ✅
UCS 总能找到总代价最小的路径,适用于路径代价不均匀的情况。 - 时间 & 空间复杂度 ⏳
与 BFS 类似,UCS 可能会扩展大量节点,时间复杂度为: O(bd)O(b^d)
3. 深度优先搜索(DFS)的评价
3.1 DFS 的特点
- 总是优先扩展最深的节点,使用LIFO 栈(后进先出) 进行管理。
- 适用于内存受限的问题,因为只存储当前路径上的节点。
3.2 DFS 的优缺点
特性 | DFS |
---|---|
完备性(Completeness) ❌ | 可能会陷入无限循环(除非搜索空间有限) |
最优性(Optimality) ❌ | 可能找到次优解或最差解 |
时间复杂度 ⏳ | O(bm)O(b^m) (m 为搜索树最大深度) |
空间复杂度 💾 | O(bm)O(bm) (线性存储路径节点) |
适用场景:
- 当搜索树很深且解接近叶子节点时,DFS 可能更快找到解。
- 适用于受限内存环境,但不保证最优解。
4. BFS vs DFS:哪种更好?
算法 | 优点 | 缺点 | 适用场景 |
---|---|---|---|
BFS | 找到最短路径,适用于代价一致的情况 | 占用大量内存 | 适用于小搜索空间或最短路径问题 |
UCS | 适用于路径代价不同的情况,保证最优解 | 计算量大 | 适用于代价不同的路径问题 |
DFS | 节省内存,适用于大搜索空间 | 可能陷入死循环,不保证最优解 | 适用于搜索树很深的情况 |
BFS 适用于:
✅ 目标在浅层(小 dd 值)
✅ 需要最优解(路径代价相同)
❌ 不适用于搜索空间很大时(内存占用过高)
DFS 适用于:
✅ 搜索空间很深,但解在较深层
✅ 需要节省内存
❌ 不适用于路径代价不同的情况(可能找到非最优解)
📌 英文翻译:Evaluation of BFS, DFS & UCS
1. Evaluation of BFS
- Complete ✅: Always finds a solution if one exists.
- Optimal ✅: Finds the shortest path if all step costs are equal.
- Time Complexity ⏳: O(bd)O(b^d)
- Space Complexity 💾: O(bd)O(b^d)
2. Uniform Cost Search (UCS)
- Expands the lowest-cost node first (instead of expanding by levels like BFS).
- Uses a priority queue instead of a FIFO queue.
- Guarantees the optimal solution if costs are positive.
Comparison: BFS vs UCS
Feature | BFS | UCS |
---|---|---|
Queue Type | FIFO | Priority Queue |
Goal Test | When generating a node | When expanding a node |
Path Cost | Ignores | Expands the lowest-cost path |
3. Evaluation of DFS
- Complete? ❌ Not always (may enter infinite loops).
- Optimal? ❌ Not guaranteed.
- Time Complexity: O(bm)O(b^m) (where mm is the max depth).
- Space Complexity: O(bm)O(bm) (stores only the current path).
4. BFS vs DFS: Which is better?
Algorithm | Pros | Cons | Best Use Cases |
---|---|---|---|
BFS | Guarantees shortest path | High memory usage | Small search space, shortest path problems |
UCS | Guarantees optimal solution | Large computation | Different path costs |
DFS | Uses less memory | May not find the optimal solution | Large search space, deep solutions |
📌 考试笔记(Exam Notes)
BFS
- FIFO queue, expands level by level.
- Complexity: O(b^d) (time & space).
- Best for shortest path problems.
UCS
- Uses priority queue, always expands the lowest-cost node.
- Guarantees optimal path.
- Time complexity: O(b^d).
DFS
- LIFO stack, expands deepest nodes first.
- Complexity: O(b^m) time, O(bm) space.
- Best for deep search spaces.
(关于 Depth-Limited Search、Iterative Deepening Search、Evaluation of IDS、Iterative Lengthening Search)的要点,分别以英文概述和中文解释的形式进行整理说明。
1. Depth-Limited Search (DLS)
English Explanation
-
Definition
Depth-Limited Search is a variant of Depth-First Search (DFS) that imposes a fixed maximum depth dd. It explores the search tree down to depth dd but does not proceed further. -
Key Points
- If dd is too small, the algorithm may fail to find a solution if the solution lies beyond depth d.
- If d is too large, the search may suffer the same issues as standard DFS (e.g., deep or infinite paths before finding a solution).
- DLS is complete only if the state space (tree) is known to be finite and the solution depth does not exceed d.
- It is not optimal because it does not necessarily find the shallowest (or least-cost) goal when multiple solutions exist.
中文解释
-
定义
深度限制搜索(DLS)是在深度优先搜索(DFS)中人为地设置一个最大搜索深度 d,当搜索达到深度 dd 后就不再向下扩展。 -
要点
- 如果 dd 设得太小,可能会找不到真实深度大于 d 的解。
- 如果 dd 设得过大,就又回到了传统 DFS 的问题,比如可能会在非常深的路径上浪费大量时间。
- DLS 在有限的搜索空间内(并且解的深度不超过 d)才算完全(complete),即一定能找到解。
- 由于可能遗漏更浅层的解,或者并不考虑成本最优,DLS 并不具备最优性。
2. Iterative Deepening Search (IDS)
English Explanation
-
Definition
Iterative Deepening Search incrementally increases the depth limit. First, it tries depth d=0, then d=1, d=2, and so on, effectively combining the space efficiency of DFS with the completeness and optimality of BFS (in terms of the shallowest goal). -
Key Points
- By repeatedly deepening the search limit, IDS avoids the need to guess a single fixed depth.
- Complete and optimal (when all step costs are the same) because it guarantees finding the shallowest goal.
- The time complexity is often described as O(b^d) (similar to BFS), and space complexity is relatively low (similar to DFS), because at any given time it only stores a single DFS path plus some bookkeeping.
中文解释
-
定义
迭代加深搜索(IDS)并不预先设定一个固定深度,而是从深度 0 开始,一点一点加大搜索深度:先搜索深度 0,再搜索深度 1,接着深度 2…… 这样它既能像 BFS 那样保证最先找到最浅层的解,又保持了 DFS 的低内存占用。 -
要点
- 通过不断增加深度限制,IDS 避免了一次性猜测一个不合适的最大深度。
- 在每个深度上做 DFS,但因为会从浅到深逐层加大深度,所以对于等代价边的情况,它既是完全的又是最优的(能保证先找到最浅层的解)。
- 时间复杂度与 BFS 相同,大约为 O(b^d);但 空间复杂度更接近于 DFS,仅需要记录当前搜索路径和少量辅助信息。
3. Evaluation of IDS
English Explanation
- Optimal: Finds the shortest solution (in terms of the number of steps or uniform step cost).
- Complete: Will eventually find the solution if it exists (given a finite branching factor).
- Time/Space Complexity:
- Time is similar to BFS, O(bd)O(b^d).
- Space is much less than BFS, more akin to DFS.
- When to Use:
Suitable when the search space is large, and you do not know the depth of the solution.
中文解释
- 最优性:如果所有边或操作的代价相同,IDS 能够找到最浅层的解,即最优解。
- 完备性:只要搜索空间是有限且分支因子有限,IDS 会逐步加深,总能找到存在的解。
- 时间/空间复杂度:
- 时间复杂度与 BFS 相同,约为 O(bd)O(b^d)。
- 空间占用却接近 DFS,远小于 BFS。
- 适用场景:
尤其适合搜索空间庞大、且不知道解大概会在第几层的情况。
4. Iterative Lengthening Search (ILS)
English Explanation
-
Definition
Iterative Lengthening Search uses path cost pp as the limit, instead of depth dd. It is an iterative version of Uniform Cost Search that repeatedly increases the allowable path cost threshold. -
Key Points
- It retains the benefits of Uniform Cost Search (finding least-cost path) while using less memory (similar to iterative methods).
- It restarts the search each time with an increased cost limit, which can incur significant overhead compared to a single run of uniform cost search.
- Suitable if we have unknown or wide-ranging path costs, and we want an approach that incrementally explores paths within certain cost bounds.
中文解释
-
定义
迭代加长搜索(ILS)是以路径代价 p(而非深度)作为限制,每次设定一个代价上限,搜索所有路径代价不超过这个上限的解。如果找不到或还未到达目标,就提高上限重新搜索,类似于对 一致代价搜索(Uniform Cost Search) 做“迭代加深”的思路。 -
要点
- 结合了一致代价搜索能找到最小代价路径的优点,且采用迭代搜索方式,通常不需要一次性占用太多内存。
- 每次提升代价上限都要重新搜索,因此比单纯的一致代价搜索多了额外开销。
- 适用于路径代价不确定或范围较大的问题,能一步步试探合适的代价区间。
补充说明:与“启发函数”无关的情况
在以上几种搜索方法(DLS、IDS、ILS)中,并 不使用 启发函数(它们都属于“无信息搜索”或“盲目搜索”范畴)。因此,这些方法不涉及“启发函数不能太乐观”的问题。那是 A* 等启发式搜索才会关注的条件。
- “启发函数不能太乐观”(Admissible)是 A* 搜索保持最优性的必要条件,指的是估计代价不能超过真实代价。
- DLS / IDS / ILS 则主要依靠深度限制或代价限制的迭代机制来确保它们的完备性、最优性(或兼顾一定的性能优势)。
小结
- Depth-Limited Search (DLS):给 DFS 加一把“锁”,限制搜索深度;简单易行,但深度不好预估,且不保证最优。
- Iterative Deepening Search (IDS):在深度上逐层加深,兼有 BFS 的最优与 DFS 的低内存优点。
- Iterative Lengthening Search (ILS):在路径代价上逐层提高阈值,是一致代价搜索的迭代版本,能找到最小代价解,但会有重启带来的额外花销。
如果问题需要利用启发式信息(heuristic)并且希望保证最优解,则需要 A* 搜索 或其他启发式搜索算法,此时就要注意启发函数不能太乐观(Admissible)和保持一致性(Consistent)等条件。