跳到正文

算法·理论

【图论 - 01】图的基本概念

Ydch约 20 分钟
原文

这是 2026年4月25日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月14日
收录
2026年4月8日
更新
2026年11月16日
篇幅
约 20 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

目录

P0. 图论の简介 + 前言

P1. 图的基本概念

P1.1 图的基本概念

P1.2 有向图和无向图

P1.3 度数

P1.4 简单图的判定

P1.5 完全图

P1.6 子图

P1.7 补图

P1.8 同构

P1.9 通路、回路、环、路径、迹、距离

P1.10 连通

P2. 图的存储方式

P2.1 邻接矩阵

P2.2 邻接表

P3.

P0. 图论の简介 + 前言

前言

因为图论可以说是一大难点,我的掌握也不透彻,所以写了这篇。

另:个人认为,先看字,再在不懂的地方看视频讲解,掌握会更好。

生活中,我们经常会遇到图,导航地图,数学坐标,一张图可以描述不同的道路,可以让我们快速的知道两地的通行路线。

P1. 图的基本概念

图的基本概念
  • 一个图,是由点集 V\textup{\textmd{V}} 和边集 E\textup{\textmd{E}} 组成的。

我们一般将一张图 G\textup{\textmd{G}} 记作 G = <V, E>\textup{\textmd{G = <V, E>}},一条边连接两个顶点。

  • 点集 V\textup{\textmd{V}} 中包含了所有顶点,边集 E\textup{\textmd{E}} 中包含了所有边,当 V\textup{\textmd{V}} 为空时,称该图为空图。

有向图和无向图

图分为有向图和无向图:

无向图即为一条边可以从点 A\textup{\textmd{A}} 走到 点 B\textup{\textmd{B}},也可以从点 B\textup{\textmd{B}} 走到 点 A\textup{\textmd{A}}。

有向图即为一条边有一个规定的方向(单行道,单方向),当然,一条边可以有两个规定的方向,变成一条无向边。


度数

度数

无向图中的度数
对于无向图中的顶点 v\textup{\textmd{v}},v\textup{\textmd{v}} 作为端点的次数即为 v\textup{\textmd{v}} 的度数(也就是被几条线段连接)。

有向图中的度数
对于有向图中的顶点 v\textup{\textmd{v}},v\textup{\textmd{v}} 作为边的起点的次数称为出度,记为 d+(v)d^{+}(\textup{\textmd{v}}),作为边的终点的次数称为入度,记为 d−(v)d^{-}(\textup{\textmd{v}})。

每个图 G\textup{\textmd{G}} 的最大度为所有顶点度数的最大值,记作 △(G)\textup{\textmd{△(G)}};最小度为所有顶点度数的最小值,记作 ɖ(G)\textup{\textmd{ɖ(G)}}。

一张图 G\textup{\textmd{G}} 的度数和为边数的两倍,有向图所有出度和等于入度和。


简单图的判定

简单图的判定

自环

边连接的两个点是同一个点。

重边

无向图中指两个点有 ≥2\ge 2 条边连接。

孤点

没有边连接的点称为孤点。

简单图

没有自环和重边的图即为简单图。


完全图

完全图

无向完全图:
设 G\textup{\textmd{G}} 为一个有 nn 个顶点的无向简单图,若 G\textup{\textmd{G}} 中每个顶点都与其余 n−1n - 1 个顶点有边相连,则称 G\textup{\textmd{G}} 为 nn 阶无向完全图,记作 Kn\textup{\textmd{K}}_n。

有向完全图:
设 G\textup{\textmd{G}} 为一个有 nn 个顶点的有向简单图,若 G\textup{\textmd{G}} 中每个顶点都有与其余 n−1n - 1 个顶点有边相连,且都有这些顶点连向它的边,则称 G\textup{\textmd{G}} 为 nn 阶有向完全图,记作 Kn\textup{\textmd{K}}_n。
简单理解:
将一个无向完全图的每条边都变成两条边,一条从 AA 指向 BB,另一条从 BB 指向 AA。

竞赛图:
基于 nn 阶无向完全图,给每条边任意确定一个方向形成的图称为 nn 阶竞赛图。


子图

子图

设 G = <V, E>\textup{\textmd{G = <V, E>}},G\textup{\textmd{G}}'=<V = \textup{\textmd{<V}}' , E\textup{\textmd{, E}}‘>>,如果 V\textup{\textmd{V}}’ ⊆V\subseteq \textup{\textmd{V}} 且 E\textup{\textmd{E}}’ ⊆E\subseteq \textup{\textmd{E}},我们称 G\textup{\textmd{G}}’ 是 G\textup{\textmd{G}} 的子图,记作 G\textup{\textmd{G}}’ ⊆G\subseteq \textup{\textmd{G}}。

如果 V\textup{\textmd{V}}’ =V= \textup{\textmd{V}},则称 G\textup{\textmd{G}}’ 是 G\textup{\textmd{G}} 的生成子图。

如果 V\textup{\textmd{V}}’ ⊂V\subset \textup{\textmd{V}} 或 E\textup{\textmd{E}}’ ⊂E\subset \textup{\textmd{E}},则称 G\textup{\textmd{G}}’ 是 G\textup{\textmd{G}} 的真子图。


补图

补图

设 G = <V, E>\textup{\textmd{G = <V, E>}} 是一个 nn 阶无向简单图,以 V\textup{\textmd{V}} 为顶点集,以所有使 G\textup{\textmd{G}} 成为完全图 KnK_{n} 需要添加的边的集合为边集的图,称为 GG 的补图,记作 G‾\overline{\textup{\textmd{G}}}。

简单理解:
一个 nn 阶无向简单图 G\textup{\textmd{G}},与完全图 KnK_n 做对比,点集不变,边集为与完全图对比后没有的边。


同构

同构

设 G\textup{\textmd{G}} 和 G\textup{\textmd{G}}’ 是分别具有顶点集 V\textup{\textmd{V}} 和 V\textup{\textmd{V}}’ 的两个图。如果存在一个双射 hh : V→V\textup{\textmd{V}} \rightarrow \textup{\textmd{V}}‘,满足当且仅当 (vi,vj)(v_{i}, v_{j}) 是 G\textup{\textmd{G}} 的边时,(h(vi),h(vj))(h(v_{i}), h(v_{j})) 是 G\textup{\textmd{G}}’ 的边,则称图 G\textup{\textmd{G}} 和 G\textup{\textmd{G}}’ 同构。

上面的我一点没看懂。

简单理解:设 G\textup{\textmd{G}} 和 G\textup{\textmd{G}}’ 是两个图,G\textup{\textmd{G}}’ 如果可以通过:改这个点的名字、挪这个点的位置、变这个点的大小,来变成 G\textup{\textmd{G}},则两个图同构。


通路、回路、路径、距离

通路、回路、环、路径、迹、距离

通路

对于一个图 G\textup{\textmd{G}},G\textup{\textmd{G}} 中顶点与边的交替序列 v0,e1,v1,e2,v2…en,vnv_{0}, e_{1}, v_{1}, e_{2}, v_{2} … e_{n}, v_{n} 称 v0v_{0} 到 vnv_{n} 的通路,其中我们称 v0v_{0} 是通路的起点,vnv_{n} 是通路的终点,通路中边的条数称为它的长度。在有向图中,要保持边的方向一致。

上面能看懂了一半(?

简单理解:对于一个图 G\textup{\textmd{G}},从一个点出发,沿着边走,走到另一个点中间走过的一整条路线,路线的边数称为它的长度,称第一个点 v0v_{0} 为该通路的起点,最后一个点 vnv_{n} 为该通路的终点。

回路

按照上面的理解,如果这个通路的起点也是这个通路的终点,称这个通路是一条回路,类似自环。

如果这是一条回路,且除了 S=TS = T,我们称这是一个环。

路径、迹

如果通路中的所有边均不相同,称为迹,如果通路中所有的顶点不相同,边也不相同,那么称为路径。

距离

在两点之间所有的通路中最短的路径长度称为距离。


连通性、连通图、连通块

连通

铺垫

极大连通子图

连通图只有一个极大连通子图,就是它本身。
非连通图有多个极大连通子图。
称为极大是因为如果此时加入任何一个不在 V\textup{\textmd{V}} 中的点都会导致它不再连通。

无向图

连通性

设无向图 G= <V,E>\textup{\textmd{G}} =\ < \textup{\textmd{V}}, \textup{\textmd{E}}>,u,v∈Vu, v \in \textup{\textmd{V}},如果 u,vu, v 之间存在通路或路径,则称 u,vu, v 是连通的。特别的,如果 u=vu = v,他们也是连通的。

连通图

对于任意非空无向图 G\textup{\textmd{G}},如果任意两个顶点之间都连通,那么称这是一张连通图。

连通块

无向图的极大连通子图就叫做连通块。 简单理解 图被分成了好几块,每一块内部互相连通,任意两块之间不连通,每一块都是一个连通块。

有向图

连通性

设有向图 G = <V, E>\textup{\textmd{G = <V, E>}},u,v∈Vu, v \in \textup{\textmd{V}},如果存在从 uu 到 vv 的通路,则称 uu 可达 vv,如果 uu 和 vv 互相可达,则称 u,vu, v 强连通。

强连通块

与无向图的相似


P2. 图的存储方式

因一个无向图可以变成一个有向图,接下来我都将使用有向图讲解。

邻接矩阵

邻接矩阵

邻接矩阵使用二维数组存储,下面,我会使用 aa 当做邻接矩阵

在一个有向图中,u,v∈Vu, v \in \textup{\textmd{V}},如果 uu 可达 vv,那么 au,v=1a_{u, v } = 1,如果 uu 连通 vv,那么 au,v=av,u=1a_{u, v} = a_{v, u} = 1。

例:一个有 55 个顶点,66 条边的无向图:

1−2,1−3,4−5,3−2,1−51 - 2, 1 - 3, 4 - 5, 3 - 2, 1 - 5。

邻接矩阵的存储
C++
0 1 1 0 1
1 0 1 0 0
1 1 0 0 0
0 0 0 0 1
1 0 0 1 0

例:一个有 55 个顶点,66 条边的有向图:

1→2,1→3,4→5,3→2,1→51 \rightarrow 2, 1 \rightarrow 3, 4 \rightarrow 5, 3 \rightarrow 2, 1 \rightarrow 5。

邻接矩阵的存储
C++
0 1 1 0 1
0 0 0 0 0
0 1 0 0 0
0 0 0 0 1
0 0 0 0 0

读入方式

输入有 nn 个节点,mm 条边,每条边连接 u,vu, v 的无向简单图。

  • 1≤n,m≤1001 \le n, m \le 100
  • 1≤l,r≤n1 \le l, r \le n
Code
C++
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define ios ios::sync_with_stdio(false), cin.tie(nullptr)               
#define double long double
#define int long long
#define endl '\n'
#define all(x) x.begin(), x.end()
using namespace std;

int n, m, a[105][105];

signed main() {
    ios;
    cin >> n >> m;
    for (int i = 1, u, v; i <= m; i++) {
        cin >> u >> v;
        a[u][v] = a[v][u] = 1;
    }
    return 0;
}
邻接表

邻接表

邻接表,顾名思义,在每个顶点上放一张链表,将与这个点关联的其他点存在这个链表里,适用于邻接矩阵的空间不够时,邻接矩阵占用的空间是 n2n^{2},大一点就会爆掉,所以就有了邻接表。

骗你的,不是我讲的不是表

谁说非要用链表,vector 也是存储邻接表的数据结构。

在一个有向图中,u,v∈Vu, v \in \textup{\textmd{V}},如果 uu 可达 vv,那么 a[u]push_back(v),如果 uu 连通 vv,那么 a[u]push_back(v) 并 a[v]push_back(u)。

例:一个有 55 个顶点,66 条边的无向图:

1−2,1−3,4−5,3−2,1−51 - 2, 1 - 3, 4 - 5, 3 - 2, 1 - 5。

邻接表的存储
C++
2 3 5
1 3
1 2
5
1 4

例:一个有 55 个顶点,66 条边的有向图:

1→2,1→3,4→5,3→2,1→51 \rightarrow 2, 1 \rightarrow 3, 4 \rightarrow 5, 3 \rightarrow 2, 1 \rightarrow 5。

邻接表的存储
C++
2 3 5
NULL
2
5
NULL

读入方式

输入有 nn 个节点,mm 条边,每条边连接 u,vu, v 的无向简单图。

  • 1≤n,m≤1001 \le n, m \le 100
  • 1≤l,r≤n1 \le l, r \le n
Code
C++
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define ios ios::sync_with_stdio(false), cin.tie(nullptr)               
#define double long double
#define int long long
#define endl '\n'
#define all(x) x.begin(), x.end()
using namespace std;

int n, m;
vector<vector<int> > a(105);

signed main() {
    ios;
    cin >> n >> m;
    for (int i = 1, u, v; i <= m; i++) {
        cin >> u >> v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    return 0;
}
图的存储

按照之前的输入方法输入,存储邻接表和邻接矩阵,输出注意的事情:

  • 邻接矩阵正常输出即可。
  • 邻接表要先输出度数,即为连接了几条边,即为 b[u].size。
  • 邻接表在输出时节点要按需要从小到大输出。
Accepted Code
C++
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define ios ios::sync_with_stdio(false), cin.tie(nullptr)               
#define double long double
#define int long long
#define endl '\n'
#define all(x) x.begin(), x.end()
using namespace std;

int n, m;
vector<vector<int> > a(1005, vector<int>(1005, 0));
vector<vector<int> > b(1005, vector<int>());

signed main() {
    ios;
    cin >> n >> m;
    for (int i = 1, u, v; i <= m; i++) {
        cin >> u >> v;
        a[u][v] = a[v][u] = 1;
        b[u].push_back(v);
        b[v].push_back(u);
    }
    for (int u = 1; u <= n; u++) {
        for (int v = 1; v <= n; v++)
            cout << a[u][v] << ' ';
        cout << '\n';
    }
    for (int u = 1; u <= n; u++) {
        sort(b[u].begin(), b[u].end());
        cout << b[u].size() << ' ';
        for (int v : b[u])
            cout << v << ' ';
        cout << '\n';
    }
    return 0;
}

P3. 记号含义

各种记号
  • G = <V, E>\textup{\textmd{G = <V, E>}} 代表我们要处理的简单有向图;
  • n=∣V∣,m=∣E∣n = |\textup{\textmd{V}}|, m = |\textup{\textmd{E}}| 分别代表顶点数和边数;
  • l(u,v)l(u, v) 代表从 uu 到 vv 的边的长度,也就是边权;
  • 如果有起点和终点,S\textup{\textmd{S}} 表示起点, T\textup{\textmd{T}} 表示终点;
  • dist(u)dist(u) 代表我们现在求出的从 S\textup{\textmd{S}} 到 uu 的最短路长度,简称为 uu 的距离。