# 图（抽象数据类型）

图作为一种抽象数据类型，是一种存储顶点（节点）集合与边集合的结构，其中每条边是一对顶点，并支持对这些顶点和边进行添加、删除、查询和遍历的操作。它是数学图 \( G = (V, E) \) 在编程层面的对应物，是网络分析、路由、依赖追踪和社交网络建模的基础。大多数实现会区分有向边与无向边，允许或禁止平行边与自环，并将数据作为属性或权重附加到顶点和边上。<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心操作 | 添加和删除顶点与边；测试成员关系（has_node、has_edge）；报告度、入度、出度和边数；遍历邻居<sup>[2](https://networkx.org/documentation/networkx-1.7/reference/classes.digraph.html)</sup> |
| 邻接矩阵代价 | 边的添加、删除和查找为常数时间；列出入边或出边需 \( O(n) \)；空间为 \( O(n^2) \)<sup>[3](https://opendatastructures.org/ods-cpp/12_1_Representing_Graph_Mat.html)</sup> |
| 邻接表代价 | 空间 \( O(n+m) \)；addEdge 为常数时间；removeEdge 和 hasEdge 为 \( O(\deg(i)) \)；outEdges 为常数时间；inEdges 为 \( O(n+m) \)<sup>[4](https://opendatastructures.org/ods-java/12_2_AdjacencyLists_Graph_a.html)</sup> |
| 遍历代价 | 广度优先搜索与深度优先搜索均以 \( O(|V| + |E|) \) 时间运行，可占用 \( O(|V|) \) 状态<sup>[5](https://courses.cs.cornell.edu/cs2110/2022fa/lectures/traversals/index.html)</sup> |
| 变体 | 有向、无向、多重边和自环支持因类而异；NetworkX 将其命名为 Graph、DiGraph、MultiGraph 和 MultiDiGraph<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup> |
| 属性与权重 | 任意数据均可附加到节点和边上；'weight' 关键字下的数值边数据会被迪杰斯特拉最短路径等算法默认读取<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup> |
| 矩阵视角 | GraphBLAS 在（通常稀疏的）邻接矩阵与关联矩阵上定义图操作，因此少量矩阵运算即可覆盖广泛的图算法<sup>[6](https://www.mit.edu/~kepner/GraphBLAS/GraphBLAS-Math-release.pdf)</sup> |

## 定义与变体

数据类型由其存储的内容和承诺的行为来定义。图持有一组顶点和一组边，边是顶点的对；在有向图中，边是有序对 \((u, v)\)，具有源和汇，而在无向图中，对没有方向。边的端点顺序是否重要是两大设计轴之一；另一轴是同一对顶点之间是否允许多条边。<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup>

**自环与平行边。** 自环是一条起点和终点为同一节点的边。<sup>[7](https://networkx.org/documentation/latest/auto_examples/drawing/plot_selfloops.html)</sup> 各库的允许范围不同：NetworkX 的 Graph 类忽略两个节点间的多重边，但允许自环，其 MultiGraph 和 MultiDiGraph 类以一定性能为代价增加了平行边支持。<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup> 在传统的无向邻接表实现中，自环 \( v\text{-}v \) 在 \( v \) 的邻接表中出现两次，并为该顶点的度贡献二。<sup>[8](https://algs4.cs.princeton.edu/code/javadoc/edu/princeton/cs/algs4/Graph.html)</sup>

**属性与权重。** 节点和边通常携带数据。NetworkX 允许任何可哈希的 Python 对象作为节点，任何对象作为边属性；意在作为权重的数值边数据习惯上使用 'weight' 关键字，迪杰斯特拉最短路径等算法默认读取该关键字。<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup> 加权图即边携带此类值的图，路径的权重或长度是其各条边权重之和。<sup>[9](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec21/)</sup>

## 邻接矩阵表示

邻接矩阵用一个 \( n \times n \) 的布尔值矩阵 \( a \) 表示 \( n \) 个顶点的图，其中 \( a[i][j] \) 为真当且仅当 \( (i,j) \in E \)。<sup>[3](https://opendatastructures.org/ods-cpp/12_1_Representing_Graph_Mat.html)</sup> 由于每次边测试只是一次数组读取，addEdge、removeEdge 和 hasEdge 均以常数时间运行。弱点在于邻域列举：outEdges(i) 和 inEdges(i) 必须扫描一行或一列的全部 \( n \) 个条目，因此需要 \( O(n) \) 时间。空间为 \( O(n^2) \)；一个普通的 bool 值矩阵使用约 \( n^2 \) 字节，而每个机器字打包 \( w \) 个布尔值可将其降至 \( O(n^2/w) \) 个字。<sup>[3](https://opendatastructures.org/ods-cpp/12_1_Representing_Graph_Mat.html)</sup>

当图是稠密的——即边数接近 \( n^2 \)——且代数计算有用时，矩阵形式才物有所值。将条目视为整数并把 \( a \) 自乘得到 \( a^2 \)，其条目 \( a^2[i][j] \) 计数从 \( i \) 到 \( j \) 的长度为二的路径数；这一观察是仅使用 \( O(\log n) \) 次矩阵乘法的全对最短路径算法的基础。<sup>[3](https://opendatastructures.org/ods-cpp/12_1_Representing_Graph_Mat.html)</sup> 同样的矩阵视角可推广到加权图和二分图：邻接矩阵条目 \( A(v_1, v_2) = w_{12} \neq 0 \) 记录一条权重为 \( w_{12} \) 的边，而矩形矩阵表示行与列顶点集不同的二分图。<sup>[6](https://www.mit.edu/~kepner/GraphBLAS/GraphBLAS-Math-release.pdf)</sup>

## 邻接表表示

邻接表采取以顶点为中心的视角：图存储为列表的数组 adj，其中 adj[i] 包含所有满足 \( (i,j) \in E \) 的顶点 \( j \)。<sup>[4](https://opendatastructures.org/ods-java/12_2_AdjacencyLists_Graph_a.html)</sup> 在典型实现中，addEdge 以常数时间向列表追加；removeEdge 和 hasEdge 搜索列表，需 \( O(\deg(i)) \)，其中度计数以 \( i \) 为源的边；outEdges 以常数时间返回列表本身；inEdges 必须扫描每个顶点的列表，需 \( O(n+m) \)。总空间为 \( O(n+m) \)。<sup>[4](https://opendatastructures.org/ods-java/12_2_AdjacencyLists_Graph_a.html)</sup>

从这一骨架可衍生出若干实现选择。每顶点的集合可以是基于数组的列表、链表或哈希表；第二个结构 inadj 存储入边，可极大加速 inEdges，代价是边插入和删除时的额外工作；边可以是一等对象，携带自身数据，因此列表持有的是边而非顶点索引。<sup>[4](https://opendatastructures.org/ods-java/12_2_AdjacencyLists_Graph_a.html)</sup> 对于稠密图，邻接表的存储开销趋近于邻接矩阵：其最坏情况存储出现在图稠密时，空间复杂度与邻接矩阵相同。<sup>[10](https://courses.csail.mit.edu/6.006/spring11/exams/notes2-2.pdf)</sup>

**可配置实现。** Boost 图库的 adjacency_list 类将这些权衡显式化为模板参数。OutEdgeList 选择每顶点的边容器（vecS 求速度，setS 防止重复边），VertexList 选择顶点容器（vecS 求快速查找，listS 求稳定描述符），Directed 选择有向、无向或双向访问；双向选项同时暴露入边和出边，每条边的内存开销翻倍。使用基于序列的边列表时，add_edge 为摊还常数时间，但允许平行边；使用 setS 等关联容器时，add_edge 需 \( O(\log(E/V)) \) 且禁止平行边。<sup>[11](https://www.boost.org/doc/libs/latest/libs/graph/doc/html/graph/graph_classes/adjacency_list.html)</sup>

## 操作与算法

除构造外，图 ADT 通常还暴露结构查询，如 has_node、has_edge、order（节点数）、size（边数）、度、入度、出度、自环报告、子图提取、有向图反转以及有向与无向形式之间的转换。<sup>[2](https://networkx.org/documentation/networkx-1.7/reference/classes.digraph.html)</sup> 图的并、交等类集合操作也是标准配置。<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup>

两种最常见的遍历算法是广度优先搜索（BFS）和深度优先搜索（DFS）。两者均以 \( O(|V| + |E|) \) 时间运行，因为每个顶点至多入队或入栈一次，每条边至多被检查一次。两者均可占用 \( O(|V|) \) 状态：DFS 占用的状态与当前路径长度成正比，这发生在类链表图上；而 BFS 占用的状态与距离为 \( k \) 或 \( k+1 \) 的节点前沿成正比，这发生在高度分支的图上，此类图中距离为 \( k \) 的节点可多达 \( 2^k \) 个。在平衡二叉树上，DFS 仅需 \( O(\log |V|) \) 状态，与树高成正比。<sup>[5](https://courses.cs.cornell.edu/cs2110/2022fa/lectures/traversals/index.html)</sup>

## 库与接口

具体库以不同方式封装这些思想。NetworkX 提供四个基本类：Graph、DiGraph、MultiGraph 和 MultiDiGraph，由两个问题决定选择：边是否有向，是否允许多重边。其图视图报告节点、邻居、边和度，反映图的后续变化，并支持类似字典的属性查找，如 G.edges[u, v]['color']。<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup> 图可通过 erdos_renyi_graph 等生成器、导入 GML 和 [GraphML](https://www.edgepedia.org/zh/graphml) 等文件格式，或显式添加节点和边来构建。<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup>

Boost 图库在 C++ 中采取泛型的、基于模板的方法，顶点和边属性通过捆绑结构体或属性列表附加，并通过属性映射读写。<sup>[11](https://www.boost.org/doc/libs/latest/libs/graph/doc/html/graph/graph_classes/adjacency_list.html)</sup> 在数学一端，[GraphBLAS](https://www.edgepedia.org/zh/graphblas) 标准在二维矩阵（通常稀疏）上定义图操作，用邻接矩阵进行分析，用关联矩阵对多重图、超图和多部图进行面向数据的表示；两者通过矩阵-矩阵乘法相连，少量矩阵运算即可覆盖广泛的图算法。<sup>[6](https://www.mit.edu/~kepner/GraphBLAS/GraphBLAS-Math-release.pdf)</sup>

## 选择表示

选择取决于图的形态和主导操作。邻接矩阵适合稠密图以及以边存在性测试或矩阵代数为主的工作负载，空间为 \( O(n^2) \)。邻接表适合稀疏图以及遍历密集的工作负载，空间为 \( O(n+m) \)，但需注意反向边查询需要第二个入边结构或双向表示。<sup>[3](https://opendatastructures.org/ods-cpp/12_1_Representing_Graph_Mat.html)</sup><sup> • </sup><sup>[4](https://opendatastructures.org/ods-java/12_2_AdjacencyLists_Graph_a.html)</sup><sup> • </sup><sup>[11](https://www.boost.org/doc/libs/latest/libs/graph/doc/html/graph/graph_classes/adjacency_list.html)</sup> 对平行边和自环的支持以及附加属性的能力，进一步缩小了库所提供的具体类之间的选择范围。<sup>[1](https://networkx.org/documentation/stable/reference/introduction.html)</sup>

## References

1. [NetworkX Introduction, NetworkX documentation](https://networkx.org/documentation/stable/reference/introduction.html)
2. [DiGraph - Directed graphs with self loops, NetworkX 1.7 documentation](https://networkx.org/documentation/networkx-1.7/reference/classes.digraph.html)
3. [12.1 AdjacencyMatrix: Representing a Graph by a Matrix, Open Data Structures](https://opendatastructures.org/ods-cpp/12_1_Representing_Graph_Mat.html)
4. [12.2 AdjacencyLists: A Graph as a Collection of Lists, Open Data Structures](https://opendatastructures.org/ods-java/12_2_AdjacencyLists_Graph_a.html)
5. [Graph Traversals, CS 2110, Cornell University](https://courses.cs.cornell.edu/cs2110/2022fa/lectures/traversals/index.html)
6. [GraphBLAS Mathematics, Introduction: Graphs as Matrices](https://www.mit.edu/~kepner/GraphBLAS/GraphBLAS-Math-release.pdf)
7. [Self-loops example, NetworkX documentation](https://networkx.org/documentation/latest/auto_examples/drawing/plot_selfloops.html)
8. [Graph.java, Algorithms 4th edition, Princeton University](https://algs4.cs.princeton.edu/code/javadoc/edu/princeton/cs/algs4/Graph.html)
9. [Graphs, CS 2110, Cornell University](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec21/)
10. [6.006 Introduction to Algorithms, Graph notes, MIT](https://courses.csail.mit.edu/6.006/spring11/exams/notes2-2.pdf)
11. [adjacency_list, Boost Graph Library documentation](https://www.boost.org/doc/libs/latest/libs/graph/doc/html/graph/graph_classes/adjacency_list.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: 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/graph-abstract-data-type

English original: https://www.edgepedia.org/graph-abstract-data-type
