# 图搜索

图搜索是一类算法，系统地遍历或探索图的顶点和边，以寻找目标节点、路径或某种结构（如最短路径树）。在标准表述中，一个解是从起始节点到满足目标谓词的节点的路径，而最优解是总代价最低的从起点到目标的路径，其中代价是沿路径各弧代价之和。<sup>[1](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch3.S3.html)</sup> 根据算法和问题的不同，输出可能是单个目标节点、一条路径、从源点可达的顶点集合，或一种遍历顺序。<sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15210-f14/www/lectures/graph-searches.pdf)</sup>

| 关键事实 | 细节 |
|---|---|
| 通用机制 | 维护一个已访问集合和一个前沿；前沿的选择规则（队列、栈或优先队列）决定算法<sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15210-f14/www/lectures/graph-searches.pdf)</sup> |
| BFS 复杂度 | 邻接表上为 \( O(\|V\| + \|E\|) \) 时间；仅当所有边代价相等时最优<sup>[3](https://people.eecs.berkeley.edu/~vazirani/algorithms/chap4.pdf)</sup><sup> • </sup><sup>[4](https://inst.eecs.berkeley.edu/~cs188/sp26/assets/lectures/cs188-sp26-lec03.pdf)</sup> |
| Dijkstra 复杂度 | 二叉堆为 \( O((\|V\| + \|E\|) \log \|V\|) \)；数组队列为 \( O(\|V\|^{2}) \)；负边长度下失效<sup>[3](https://people.eecs.berkeley.edu/~vazirani/algorithms/chap4.pdf)</sup> |
| A* 评估 | \( f(n) = g(n) + h(n) \)；树搜索配合可采纳启发式最优，图搜索配合一致启发式最优<sup>[5](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)</sup><sup> • </sup><sup>[4](https://inst.eecs.berkeley.edu/~cs188/sp26/assets/lectures/cs188-sp26-lec03.pdf)</sup> |
| UCS 代价界 | \( O(b^{C^{*}/\epsilon}) \) 时间，其中 \( C^{*} \) 为最优解代价，\( \epsilon \) 为最小步代价<sup>[5](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)</sup> |
| 内存权衡变体 | IDA* 使用 \( O(d) \) 空间；Fringe Search 在网格寻路上比优化后的 A* 快 10–40%<sup>[6](https://doi.org/10.7916/d8hq46x1)</sup><sup> • </sup><sup>[7](https://webdocs.cs.ualberta.ca/~holte/Publications/fringe.pdf)</sup> |
| 近期方向 | LLM 生成的途经点在多达 2,000 个节点的图上将 A* 节点扩展减少约 50%<sup>[8](https://arxiv.org/html/2606.23136)</sup> |

## 工作原理

每一次图搜索都实例化同一个循环。算法维护一个已到达顶点的已访问集合，以及一个与已访问顶点相连的未访问顶点前沿，当前沿为空时终止，返回从源点可达的顶点集合。施加于前沿的选择规则定义了策略：选择前沿全部顶点得到广度优先搜索，选择最近加入的顶点得到深度优先搜索，按某种度量选择优先级最高的顶点得到优先级优先（最佳优先）搜索。<sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15210-f14/www/lectures/graph-searches.pdf)</sup>

两个区分组织了这个家族。第一，树搜索不检查节点是否已被访问过，使用较少内存但可能重复访问节点，而图搜索维护一个已探索集合，每个节点至多访问一次，使用较多内存。<sup>[9](http://chalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf)</sup> 第二，图可以显式存储或隐式生成。在[状态空间搜索](https://www.edgepedia.org/zh/state-space-search)中，节点表示状态，弧表示动作，搜索图完全不需要存储：算法只需要一个生成节点邻居的过程和一个判断节点是否为目标的测试。<sup>[1](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch3.S3.html)</sup>

## 具体做法

**广度优先搜索**将前沿视为队列，逐层探索，维持如下不变量：在第 i 层开始时，已访问集合恰好包含距源点距离小于 i 的顶点，前沿恰好包含距离等于 i 的顶点；它至多经过 \( \|V\| \) 轮终止。<sup>[9](http://chalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf)</sup><sup> • </sup><sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15210-f14/www/lectures/graph-searches.pdf)</sup> 当顶点 v 首次从 u 被发现时，其距离设为 \( d(s,u) + 1 \)，因此在无权图中 BFS 树是一棵最短路径树。<sup>[10](https://courses.cs.duke.edu/spring19/compsci230/Notes/lecture18.pdf)</sup>

**深度优先搜索**将前沿视为栈，沿一条路径探索直到受阻，然后回溯；实现时用全局计数器的 \( \mathrm{pre}(u) \) 和 \( \mathrm{post}(u) \) 时间戳标记每个顶点，满足 \( \mathrm{pre}(u) < \mathrm{post}(u) \)，且两者均在 1 到 \( 2n \) 之间。<sup>[9](http://chalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf)</sup><sup> • </sup><sup>[10](https://courses.cs.duke.edu/spring19/compsci230/Notes/lecture18.pdf)</sup>

**一致代价搜索（Dijkstra）**将前沿视为按路径代价 \( g(n) \) 排序的优先队列，其核心更新规则为 \( \mathrm{dist}(v) = \min\{\mathrm{dist}(v), \mathrm{dist}(u) + l(u,v)\} \)，保留的 dist 值为高估或恰好正确。<sup>[9](http://chalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf)</sup><sup> • </sup><sup>[3](https://people.eecs.berkeley.edu/~vazirani/algorithms/chap4.pdf)</sup>

**A\*** 按 \( f(n) = g(n) + h(n) \) 排序前沿，将后向代价 \( g(n) \) 与前向估计 \( h(n) \) 结合；一致代价搜索是 \( h = 0 \) 的特例。<sup>[5](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)</sup>

BFS 以线性时间 \( O(\|V\| + \|E\|) \) 运行：每个顶点恰好入队一次，每条边被检查一次（有向）或两次（无向）。<sup>[3](https://people.eecs.berkeley.edu/~vazirani/algorithms/chap4.pdf)</sup> 对 Dijkstra，二叉堆给出 \( O((\|V\| + \|E\|) \log \|V\|) \)，数组优先队列给出 \( O(\|V\|^{2}) \)，对稠密图更可取。<sup>[3](https://people.eecs.berkeley.edu/~vazirani/algorithms/chap4.pdf)</sup> BFS 仅当所有边代价相等时最优；一致代价搜索对非负代价最优，且假设最优解代价有限、最小步代价为正时是完备的。<sup>[5](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)</sup><sup> • </sup><sup>[4](https://inst.eecs.berkeley.edu/~cs188/sp26/assets/lectures/cs188-sp26-lec03.pdf)</sup>

启发式函数 \( h(n) \) 返回一个非负实数，估计从节点 n 到目标的最小代价路径的代价。若它从不高估该代价，即形式上 \( 0 \le h(n) \le h^{*}(n) \)，其中 \( h^{*}(n) \) 是到最近目标的真实代价，则称其为可采纳的。<sup>[11](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch3.S6.html)</sup><sup> • </sup><sup>[5](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)</sup> 若它在每条弧上满足三角不等式 \( h(A) - h(C) \le \mathrm{cost}(A \text{ to } C) \)，则称其是一致的。<sup>[4](https://inst.eecs.berkeley.edu/~cs188/sp26/assets/lectures/cs188-sp26-lec03.pdf)</sup> 保证结构是不对称的：A* 树搜索配合可采纳启发式最优，而 A* 图搜索要求一致启发式。<sup>[5](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)</sup><sup> • </sup><sup>[4](https://inst.eecs.berkeley.edu/~cs188/sp26/assets/lectures/cs188-sp26-lec03.pdf)</sup> 两个可采纳启发式可通过逐点最大值组合，\( h(n) = \max(h_{a}(n), h_{b}(n)) \)，该组合可采纳且支配两者。<sup>[11](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch3.S6.html)</sup><sup> • </sup><sup>[5](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)</sup>

## 起源

Dijkstra 的最短路径方法于 1959 年以一篇三页论文发表，题为《关于图论中两个问题的注记》（"A Note on Two Problems in Connexion with Graphs"），载于《Numerische Mathematik》第 1 卷，第 269–271 页；其关键洞见是：若 R 是从 P 到 Q 的最小路径上的节点，则知道到 Q 的最小路径意味着知道到 R 的最小路径。<sup>[12](https://doi.org/10.1007/bf01386390)</sup>

A* 由彼得·哈特（Peter Hart）、尼尔斯·尼尔森（Nils Nilsson）和伯特伦·拉斐尔（Bertram Raphael）于 1968 年在《启发式确定最小代价路径的形式基础》（"A Formal Basis for the Heuristic Determination of Minimum Cost Paths"，IEEE Transactions on Systems Science and [Cybernetics](https://www.edgepedia.org/zh/cybernetics)）中提出，该文提出了一种排序节点扩展的方式，并证明了所得算法的可采纳性，即保证对任意图都能找到从起点到首选目标节点的最优路径。<sup>[13](https://doi.org/10.1109/tssc.1968.300136)</sup>

理查德·科尔夫（Richard Korf）1985 年的论文《深度优先迭代加深：一种最优可采纳树搜索》（"Depth-First Iterative Deepening: An Optimal Admissible Tree Search"）引入了 DFID，它对深度 1、2、3 等执行重复的深度优先搜索，使用 \( O(d) \) 空间同时保证最短长度解，并证明了该方法在指数树搜索的解代价、运行时间和空间上渐近最优。<sup>[6](https://doi.org/10.7916/d8hq46x1)</sup>

## 变体

A* 将整个已探索区域保存在内存中，因此内存受限的变体以最优性保证或额外重计算换取减少的空间。<sup>[4](https://inst.eecs.berkeley.edu/~cs188/sp26/assets/lectures/cs188-sp26-lec03.pdf)</sup>

**IDA\*** 将迭代加深思想应用于 A*：每次迭代执行深度优先搜索，当某分支的总代价 \( f = g + h \) 超过阈值时剪枝，阈值从初始状态的代价估计开始，每次迭代增加到超过上一阈值的最小代价。<sup>[14](https://www.cse.sc.edu/~mgv/csce580f09/gradPres/korf_IDAStar_1985.pdf)</sup>

Fringe Search 通过维护两个列表（当前和稍后）跨越 A* 与 IDA* 之间的时空权衡，使第 i 次迭代的前沿被复用为第 \( i+1 \) 次迭代的基础，消除了 IDA* 的重复访问；在网格寻路实验中，它比高度优化的 A* 快约 10–40%。<sup>[7](https://webdocs.cs.ualberta.ca/~holte/Publications/fringe.pdf)</sup>

第二个家族处理变化的边代价。终身规划 A*（Lifelong Planning A*，LPA*，科尼希（Koenig）和利哈乔夫（Likhachev），2002）结合增量搜索与启发式搜索，在边代价随时间变化的有限图上反复寻找最短路径，而 D* Lite（科尼希（Koenig）和利哈乔夫（Likhachev），2002）将 LPA* 与 D* 的思想结合用于移动机器人；D* 和 D* Lite 实现相同的导航策略，速度大致相当，但 D* Lite 在算法上更简单。<sup>[15](https://idm-lab.org/bib/abstracts/papers/aimag04a.pdf)</sup>

## 应用

图搜索支撑可达性与连通性测试、无权与加权最短路径、二分性测试、[图划分](https://www.edgepedia.org/zh/graph-partitioning)、最大流计算中的子程序，以及人工智能中的状态空间问题求解。<sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15210-f14/www/lectures/graph-searches.pdf)</sup> LLM 推理正日益被视为搜索：[思维树](https://www.edgepedia.org/zh/tree-of-thoughts)（Tree of Thoughts，姚（Yao）及同事，2023）体现了直接状态评估，其中 LLM 提出候选下一步，由基于 LLM 的评估器打分以引导束搜索或剪枝 DFS。<sup>[16](https://doi.org/10.48550/arxiv.2305.10601)</sup><sup> • </sup><sup>[17](https://arxiv.org/html/2608.30395)</sup> 在经典图上，生成中间途经点的 LLM 辅助 A* 在多达 2,000 个节点的图上将扩展节点减少约 50%，而路径代价仅比最优增加 0.34–0.66。<sup>[8](https://arxiv.org/html/2606.23136)</sup>

## 局限与替代方案

**指数爆炸**是主要的失败模式。未能检测重复状态可能导致指数级更多的工作。<sup>[5](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)</sup><sup> • </sup><sup>[18](http://www.ai.mit.edu/courses/6.034b/searchcomplex.pdf)</sup> DFS 也不一定找到最短路径，可能在存在仅含一条边的路径时发现一条含两条边的路径。<sup>[10](https://courses.cs.duke.edu/spring19/compsci230/Notes/lecture18.pdf)</sup>

**代价假设**是严格的。一致代价搜索要求非负动作代价；给所有边代价加一个正常数行不通，因为它对较长路径惩罚更多，解决的是不同的问题，而负代价需要 Bellman-Ford。[Dijkstra 算法](https://www.edgepedia.org/zh/dijkstras-algorithm)同样不适用于负边长度。<sup>[19](https://stanford-cs221.github.io/spring2023-extra/modules/search/search2.pdf)</sup><sup> • </sup><sup>[3](https://people.eecs.berkeley.edu/~vazirani/algorithms/chap4.pdf)</sup> **不可采纳启发式**丧失最优性保证：一种 LLM 辅助 A* 方法在基于地标（ALT）的启发式中加入途经点项，使组合估计可能高估真实代价，因此增强后的搜索不再严格可采纳，以形式保证换取减少的探索。<sup>[8](https://arxiv.org/html/2606.23136)</sup>

**替代方案**与搜索大量重叠。[动态规划](https://www.edgepedia.org/zh/dynamic-programming)可以处理有环图；Bellman–Ford 是一种支持负边权的动态规划最短路径算法。若从源点可达一个负环且该负环可到达终点，则不存在有限的从源到目的地的最短路径。一致代价搜索处理非负代价的有环图，并探索比任何终止状态更便宜的状态。<sup>[19](https://stanford-cs221.github.io/spring2023-extra/modules/search/search2.pdf)</sup><sup> • </sup><sup>[23](https://www.cs.cmu.edu/afs/cs/academic/class/15451-f19/www/LectureNotes/lec03-dp2.pdf)</sup> 一项同行评审的比较研究得出结论，A* 式启发式搜索与分支定界“本质相同”，仅在解释层面不同，尽管启发式搜索可以探索任何类型的图，而分支定界生成的图具有限制性继承性质；诺（Nau）、库马尔（Kumar）和卡纳尔（Kanal）（1984）以及德克特（Dechter）和珀尔（Pearl）（1988）的早期工作建立了相关的形式联系。<sup>[20](https://link.springer.com/article/10.1023/A:1022573412940)</sup><sup> • </sup><sup>[21](https://doi.org/10.1016/0004-3702%2884%2990004-3)</sup><sup> • </sup><sup>[22](https://doi.org/10.1007/978-1-4613-8788-6_5)</sup>

## References

1. [Artificial Intelligence: Foundations of Computational Agents, 3rd ed., §3.3 Graph Searching](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch3.S3.html)
2. [CMU 15-210 Chapter 9: Graph Search (parallel algorithms course notes)](https://www.cs.cmu.edu/afs/cs/academic/class/15210-f14/www/lectures/graph-searches.pdf)
3. [Algorithms (Dasgupta, Papadimitriou, Vazirani), Chapter 4: Paths in graphs](https://people.eecs.berkeley.edu/~vazirani/algorithms/chap4.pdf)
4. [UC Berkeley CS188 Lecture 3: Informed Search](https://inst.eecs.berkeley.edu/~cs188/sp26/assets/lectures/cs188-sp26-lec03.pdf)
5. [CMU 15-281 Lecture 3: Heuristics, Greedy Search, A* Search, Optimality](https://www.cs.cmu.edu/~15281-s24/lectures/Lecture3_S24.pdf)
6. [Korf, Richard E. (1985). Depth-First Iterative Deepening: An Optimal Admissible Tree Search. .](https://doi.org/10.7916/d8hq46x1)
7. [Fringe Search: Beating A* in Pathfinding (Björnsson, Enzenberger, Holte, Schaeffer, Sparling; IJCAI 2003)](https://webdocs.cs.ualberta.ca/~holte/Publications/fringe.pdf)
8. [LLM-Aided A* Search in Non-Geometric Network Graphs](https://arxiv.org/html/2606.23136)
9. [Chalmers AI Course Lecture 2: Classical Search Algorithms](http://chalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf)
10. [Duke CompSci 230 Lecture 18: Depth-First Search and Breadth-First Search](https://courses.cs.duke.edu/spring19/compsci230/Notes/lecture18.pdf)
11. [Artificial Intelligence: Foundations of Computational Agents, 3rd ed., §3.6 Informed (Heuristic) Search](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch3.S6.html)
12. [E. W. Dijkstra (1959). A note on two problems in connexion with graphs. Numerische Mathematik.](https://doi.org/10.1007/bf01386390)
13. [Peter Hart, Nils Nilsson, Bertram Raphael (1968). A Formal Basis for the Heuristic Determination of Minimum Cost Paths. IEEE Transactions on Systems Science and Cybernetics.](https://doi.org/10.1109/tssc.1968.300136)
14. [Depth-First Iterative-Deepening: An Optimal Admissible Tree Search (Korf, 1985)](https://www.cse.sc.edu/~mgv/csce580f09/gradPres/korf_IDAStar_1985.pdf)
15. [Incremental Heuristic Search in Artificial Intelligence (Koenig, Likhachev, Liu, Guntly, AI Magazine 2004)](https://idm-lab.org/bib/abstracts/papers/aimag04a.pdf)
16. [Yao, Shunyu and colleagues (2023). Tree of Thoughts: Deliberate Problem Solving with Large Language Models. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2305.10601)
17. [When LLM Meets Tree Search: A Systematic View of Inference as Search in Large Language Models](https://arxiv.org/html/2608.30395)
18. [Notes on the Complexity of Search (MIT 6.034 handout)](http://www.ai.mit.edu/courses/6.034b/searchcomplex.pdf)
19. [Stanford CS221 Lecture 6: Search II (UCS and A*)](https://stanford-cs221.github.io/spring2023-extra/modules/search/search2.pdf)
20. [Are Branch and Bound and A* Algorithms Identical?](https://link.springer.com/article/10.1023/A:1022573412940)
21. [General Branch and Bound, and its relation to A∗ and AO∗ (Artificial Intelligence, 1984)](https://doi.org/10.1016/0004-3702%2884%2990004-3)
22. [Rina Dechter, Judea Pearl (1988). The Optimality of A*. .](https://doi.org/10.1007/978-1-4613-8788-6_5)
23. [cs.cmu.edu](https://www.cs.cmu.edu/afs/cs/academic/class/15451-f19/www/LectureNotes/lec03-dp2.pdf)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Graph and network algorithms › Graph traversal and search

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/graph-search

English original: https://www.edgepedia.org/graph-search
