# 邻接表

邻接表是一种用于表示图的数据结构，其中每个顶点存储一个与其相邻的顶点列表（在有向图中，则是通过一条出边可达的顶点列表）。这种表示法不使用一张集中的邻接信息表，而是把跟踪自身邻域的责任交给每个顶点，经典的可视化方式是把顶点串成链表，每个顶点再挂一条属于自己的邻居链表。<sup>[1](https://www.cs.cornell.edu/courses/cs2110/2025fa/lectures/lec21/)</sup>

| 关键事实 | 详情 |
|---|---|
| 空间 | 对于有 \( V \) 个顶点、\( E \) 条边的图，为 \( O(V + E) \)；邻接矩阵则为 \( O(V^2) \)<sup>[2](https://mexicodev.org/en/dsa-course/graph-representations)</sup> |
| 边检测 | 用普通列表检查 \( (u, v) \) 是否为一条边，耗时 \( O(\deg(u)) \)<sup>[3](https://www.cis.temple.edu/~wu/teaching/Spring2022/Chapter3.pdf)</sup> |
| 邻居遍历 | 获取一个节点的邻居列表只需 \( O(1) \)，扫描该列表的耗时与度数成正比<sup>[4](https://www.cs.cmu.edu/~15122-archive/n18/lec/23-graphs.pdf)</sup> |
| 无向图的重复 | 在无向图中，每条边 \( uv \) 出现在两个列表中：\( \mathrm{Adj}[u] \) 中有节点 \( v \)，\( \mathrm{Adj}[v] \) 中有节点 \( u \)；在有向图中每条边只出现在一个列表里<sup>[5](https://cs.uwaterloo.ca/~dstinson/Stinson-CS341Slides.pdf)</sup> |
| 最佳适用 | 稀疏图，即 \( E \ll V^2 \) 的情形；只有当常数时间的边存在性检测主导工作负载时，矩阵才划算<sup>[2](https://mexicodev.org/en/dsa-course/graph-representations)</sup> |
| 库中的形式 | NetworkX 将其图实现为字典套字典再套字典的邻接结构；Boost 的 `adjacency_list` 把每顶点容器和顶点集合容器参数化<sup>[6](https://networkx.org/documentation/stable/reference/introduction.html)</sup><sup> • </sup><sup>[7](https://www.boost.org/doc/libs/latest/libs/graph/doc/html/graph/graph_classes/overview.html)</sup> |

## 定义与结构

形式上，对于有 \( |V| = n \) 个顶点的图 \( G = (V, E) \)，邻接表表示由 \( n \) 个列表组成。对每个 \( u \in V \)，都有一个列表 \( \mathrm{Adj}[u] \)；对每个满足 \( uv \in E \) 的 \( v \)，\( \mathrm{Adj}[u] \) 中都有一个节点 \( v \)。同一定义对有向图和无向图都适用。<sup>[5](https://cs.uwaterloo.ca/~dstinson/Stinson-CS341Slides.pdf)</sup> 在常见的基于数组的形式中，图是一个由列表构成的数组 \( \mathrm{adj} \)，其中 \( \mathrm{adj}[i] \) 包含所有满足 \( (i, j) \in E \) 的下标 \( j \)。<sup>[8](https://opendatastructures.org/versions/edition-0.1e/ods-cpp.pdf)</sup>

重复规则直接来自对称性。无向边 \( uv \) 必须能从两个端点中的任一个被发现，因此它同时向 \( \mathrm{Adj}[u] \) 和 \( \mathrm{Adj}[v] \) 各贡献一个条目；有向边只贡献给一个列表。<sup>[5](https://cs.uwaterloo.ca/~dstinson/Stinson-CS341Slides.pdf)</sup> 因此无向图中所有列表长度之和为 \( 2|E| \)，这正是总内存为 \( O(V + E) \) 的原因。<sup>[9](https://www.cs.cornell.edu/courses/cs312/2006fa/recitations/rec17.html)</sup>

## 成本概况

邻接表和邻接矩阵的成本概况恰好互为镜像。邻接表使用 \( O(V + E) \) 空间，边检测为 \( O(\deg(u)) \)，邻居遍历是直接的；邻接矩阵使用 \( O(V^2) \) 空间，边检测为 \( O(1) \)，邻居遍历为 \( O(V) \)，因为要找一个顶点的邻居必须扫描矩阵的整整一行。<sup>[2](https://mexicodev.org/en/dsa-course/graph-representations)</sup><sup> • </sup><sup>[7](https://www.boost.org/doc/libs/latest/libs/graph/doc/html/graph/graph_classes/overview.html)</sup> 卡内基梅隆大学（CMU）的一份讲义以略为不同的界陈述了同一权衡：空间 \( O(\max(v, e)) \)，边查找 \( O(\min(v, e)) \)——因为 \( \min(v-1, e) \) 是任一单列表的最大长度，边插入为常数时间，邻居获取为 \( O(1) \)。<sup>[4](https://www.cs.cmu.edu/~15122-archive/n18/lec/23-graphs.pdf)</sup>

**简单实现中的操作成本。** 在数组套列表的设计中，`addEdge(i, j)` 以常数时间把 \( j \) 追加到 \( \mathrm{adj}[i] \)；`removeEdge(i, j)` 和 `hasEdge(i, j)` 需要搜索列表，耗时 \( O(\deg(i)) \)；`outEdges(i)` 耗时 \( O(\deg(i)) \)；而 `inEdges(i)` 必须扫描每个顶点的列表，耗时 \( O(n + m) \)。<sup>[8](https://opendatastructures.org/versions/edition-0.1e/ods-cpp.pdf)</sup> 若把每个顶点的邻居存进哈希集合或平衡搜索树而非普通列表，边检测成本可以降低，代价是额外的空间和更慢的邻居枚举。<sup>[4](https://www.cs.cmu.edu/~15122-archive/n18/lec/23-graphs.pdf)</sup>

**稀疏性为何起决定作用。** 现实中的图是稀疏的，这正是邻接表成为默认表示、而矩阵只在稠密的边角情形中胜出的原因。<sup>[2](https://mexicodev.org/en/dsa-course/graph-representations)</sup> 一个具体算例让差距变得直观：一块 \( 91 \times 91 \) 的区域网格，90 行中每行有 91 条边，91 列中每列有 90 条边，共 16,380 条；由于每个区域至多 4 个邻居，无向邻接表容纳 32,760 个条目，而矩阵则要预留 \( 91^2 \) 个单元。<sup>[10](https://www.bugbotlab.com/learn/a4-2-adjacency-matrix-and-list/)</sup> 一份 OpenDSA 教材给出的字节级统计是：对一个含 12 条边的小型无向例子，邻接表占用 \( 4|V| + 8|E| = 116 \) 字节。<sup>[11](https://opendsa.cs.vt.edu/ODSA/Books/jmu/cs327/spring-2019/S03/html/GraphIntro.html)</sup>

遍历算法建立在这一表示之上。[广度优先搜索](https://www.edgepedia.org/zh/breadth-first-search)在邻接表上以 \( O(m + n) \) 时间运行，因为每个邻接表至多被扫描一次，所有列表上的总工作量为 \( \sum_{u \in V} \deg(u) = 2m \)。<sup>[3](https://www.cis.temple.edu/~wu/teaching/Spring2022/Chapter3.pdf)</sup> 20 世纪 70 年代初，[约翰·霍普克罗夫特](https://www.edgepedia.org/zh/john-hopcroft)（John Hopcroft）和罗伯特·塔扬（Robert Tarjan）正是基于这一洞见提出了他们的线性时间图算法，邻接表也因此成为算法课程中讲授的标准表示。<sup>[12](https://learngraphtheory.org/articles/graph-representation.html)</sup>

## 实现选择

链表的经典图景只是帮助可视化的辅助手段，而非规定做法。据康奈尔大学（Cornell）的 CS 2110 课程所述，链表很适合可视化，但在实践中并非好选择：检查某个邻居是否存在——许多图算法依赖的一步——意味着按顺序走一遍列表，而把两层列表都换成哈希映射则能实现快速的成员查询。<sup>[1](https://www.cs.cornell.edu/courses/cs2110/2025fa/lectures/lec21/)</sup> 《开放数据结构》（Open Data Structures）一书明确列出了待决问题：每个列表采用何种集合类型（基于数组的列表、链表还是哈希表），以及是否保留一份入边结构以加速反向查找。<sup>[8](https://opendatastructures.org/versions/edition-0.1e/ods-cpp.pdf)</sup>

**邻接数组。** 一种常见的紧凑实现是邻接数组：把所有边记录存进一个数组，与每个顶点关联的记录占据一段连续区间，另用一个数组保存每个顶点第一条关联边的下标。<sup>[13](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE.pdf)</sup> 这种布局就是高性能图框架使用的压缩稀疏行（CSR）格式，其价值在于存储效率和局部性；把边列表转换为 CSR 是 Gunrock、Hornet 等系统中的标准加载步骤。<sup>[14](https://arxiv.org/abs/2311.14650)</sup>

**库中的参数化。** Boost 图库的 `adjacency_list` 类把容器选择暴露为模板选择器：`vecS`（向量）、`listS`（链表）和 `setS`（集合）控制每顶点的出边列表和顶点集合。选择器会改变操作成本；例如 `setS` 以 \( O(\log(E/V)) \) 的插入成本保证不存在平行边，而基于序列的列表以摊还常数时间插入，`vecS` 则使顶点删除的代价变为 \( O(V + E) \)，因为所有顶点描述符都必须重新编号。<sup>[15](https://www.boost.org/doc/libs/1_88_0/libs/graph/doc/using_adjacency_list.html)</sup> Boost 的概览表把 `adjacency_list` 评为适用于大多数处理稀疏动态图的程序的选择，其内存为 \( O(V + E) \)，边查找为 \( O(\text{degree}) \)，邻居遍历为 \( O(\text{out-degree}) \)。<sup>[7](https://www.boost.org/doc/libs/latest/libs/graph/doc/html/graph/graph_classes/overview.html)</sup>

**Python 字典。** NetworkX 的内部数据结构基于用 Python 字典实现的邻接表表示：字典套字典再套字典，外层字典以节点为键，中层以相邻节点为键，最内层保存边的属性。文档指出，用字典套列表本来也可行，但那样无法快速检测边，也不便于存储边数据。<sup>[6](https://networkx.org/documentation/stable/reference/introduction.html)</sup> NetworkX 还定义了同样形状的文本交换格式：每行以一个源节点标签开头，后接其目标标签，因此边为 a-b、a-c 和 d-e 的图写成两行：`a b c` 和 `d e`。<sup>[16](https://networkx.org/documentation/stable/reference/readwrite/adjlist.html)</sup>

## 图数据库中的邻接表

[图数据库](https://www.edgepedia.org/zh/graph-database)管理系统把顶点的邻域索引为邻接表，以实现快速遍历，也就是把顶点与其邻居做连接。关于 A+ 索引子系统的研究指出，现有系统使用各自专用且固定的邻接表结构，这使每个系统只在固定的工作负载集合上高效，并转而提出了可调、空间高效的邻接表索引。<sup>[17](https://arxiv.org/abs/2004.00130)</sup>

扫描局部性是大规模下决定性能的关键性质。LiveGraph 存储系统的设计使邻接表扫描完全是顺序的，即便存在并发事务也从不要求随机访问——它把每个列表的边存入一种称为事务边日志（Transactional Edge Log）的日志结构存储的连续块中；其评估报告在 TAO 工作负载上峰值吞吐为每秒 877 万次请求，而基于 LMDB 的基线为 324 万次。<sup>[18](https://arxiv.org/abs/1910.05773)</sup> 其动机在于：邻接表包含图的关键拓扑信息，对它们的全部或部分扫描是图工作负载中的基本操作，从简单查询到全图分析皆然。<sup>[18](https://arxiv.org/abs/1910.05773)</sup>

## 变体与相关表示

有若干结构占据了普通列表与矩阵之间的位置。面向边的邻接表由 L.K. Chen、B.S. Ting 和 A. Sangiovanni-Vincentelli 在 1976 年的一份伯克利（Berkeley）技术报告中提出，它围绕边而非顶点来组织无向图的存储。<sup>[19](https://www2.eecs.berkeley.edu/Pubs/TechRpts/1976/28852.html)</sup> 混合设计结合了两种经典表示的长处：一篇 arXiv 论文提出一种混合链表与哈希表的结构，像矩阵一样支持快速的边添加与搜索，具有线性内存，并能直接获取顶点的邻接表。<sup>[20](https://arxiv.org/abs/0908.3089)</sup> 杰夫·埃里克森（Jeff Erickson）的算法教材还在“红鲱鱼原则”这一标题下指出，“邻接表”根本不必是列表，因为任何能枚举一个顶点邻居的结构都算数。<sup>[13](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE.pdf)</sup>

## References

1. [21. Graphs, CS 2110, Cornell University](https://www.cs.cornell.edu/courses/cs2110/2025fa/lectures/lec21/)
2. [Graph representations, Data Structures & Algorithms, From Scratch](https://mexicodev.org/en/dsa-course/graph-representations)
3. [Graph Representation: Adjacency List, Temple University CIS (Sedgewick-style lecture notes)](https://www.cis.temple.edu/~wu/teaching/Spring2022/Chapter3.pdf)
4. [Lecture 23: Representing Graphs, Carnegie Mellon University 15-122](https://www.cs.cmu.edu/~15122-archive/n18/lec/23-graphs.pdf)
5. [Data Structures for Graphs: Adjacency Lists, D.R. Stinson, CS 341, University of Waterloo](https://cs.uwaterloo.ca/~dstinson/Stinson-CS341Slides.pdf)
6. [Introduction, NetworkX 3.7 documentation](https://networkx.org/documentation/stable/reference/introduction.html)
7. [Graph Data Structures, Boost.Graph Documentation](https://www.boost.org/doc/libs/latest/libs/graph/doc/html/graph/graph_classes/overview.html)
8. [12. Graphs: AdjacencyLists, Open Data Structures (C++ edition)](https://opendatastructures.org/versions/edition-0.1e/ods-cpp.pdf)
9. [Recitation 17: Graph algorithms, Cornell CS 312](https://www.cs.cornell.edu/courses/cs312/2006fa/recitations/rec17.html)
10. [Adjacency matrix and adjacency list, OCR H446 / AQA, BugBotLab](https://www.bugbotlab.com/learn/a4-2-adjacency-matrix-and-list/)
11. [Graphs Chapter Introduction, JMU CS327 Discrete Structures II, OpenDSA](https://opendsa.cs.vt.edu/ODSA/Books/jmu/cs327/spring-2019/S03/html/GraphIntro.html)
12. [Graph Representation Explained, Learn Graph Theory](https://learngraphtheory.org/articles/graph-representation.html)
13. [Basic Graph Algorithms, Jeff Erickson, Algorithms](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE.pdf)
14. [GVEL: Fast Graph Loading in Edgelist and Compressed Sparse Row (CSR) formats, Subhajit Sahu, arXiv:2311.14650](https://arxiv.org/abs/2311.14650)
15. [Using the Boost Graph Library: Using adjacency_list, Boost 1.88.0](https://www.boost.org/doc/libs/1_88_0/libs/graph/doc/using_adjacency_list.html)
16. [Adjacency List, NetworkX 3.7 documentation](https://networkx.org/documentation/stable/reference/readwrite/adjlist.html)
17. [A+ Indexes: Tunable and Space-Efficient Adjacency Lists in Graph Database Management Systems, Mhedhbi et al., arXiv:2004.00130](https://arxiv.org/abs/2004.00130)
18. [LiveGraph: A Transactional Graph Storage System with Purely Sequential Adjacency List Scans, Zhu et al., arXiv:1910.05773](https://arxiv.org/abs/1910.05773)
19. [An Edge-Oriented Adjacency List for Undirected Graphs, Chen, Ting, and Sangiovanni-Vincentelli, UC Berkeley EECS Technical Report UCB/ERL M589, 1976](https://www2.eecs.berkeley.edu/Pubs/TechRpts/1976/28852.html)
20. [Data structure for representing a graph: combination of linked list and hash table, arXiv:0908.3089](https://arxiv.org/abs/0908.3089)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/adjacency-list

English original: https://www.edgepedia.org/adjacency-list
