技术文档图(Graph)
图(Graph)
数据结构
内容
图形由顶点(vertices)/节点(nodes)和连接这些顶点的边(edges)/线(line)组成。
- 无向图(undirected):在与每个双向边相关联的两个顶点之间没有区别
- 指向图(directed):其边缘从一个顶点指向另一个顶点但不一定在另一个方向上
- 加权图(weigthed):通过向每个边缘分配权重,其表示与该连接相关联的数值
- 未加权(unweighted):所有边缘具有单位权重1或者所有边缘具有相同的恒定权重
术语
adjacent
- An undirected edge e: (u, v) is said to be incident with its two end-point vertices: u and v.==Two vertices are called adjacent (or neighbor)== if they are incident with a common edge.
在无向边e:(u,v)中,我们称其与两个端点顶点:u和v相邻。如果两个顶点与一个公共边相邻,我们称它们为相邻(或邻居)。
- 例如,边 (0, 2) 与顶点 0+2 相邻,顶点 0+2 是相邻的。
- In directed graph, If we have a directed edge ==e: (u → v)==, we say that ==v is adjacent to u== but not necessarily in the other direction.
- ==Two edges are called adjacent== if they are incident with a common vertex.
如果两条边与一个公共顶点相邻,我们称它们为相邻。
- 例如,边 (0, 2) 和 (2, 4) 是相邻的。
degree & subgraph & simple path
- ==The degree of a vertex v== in an undirected graph is ==the number of edges== incident with vertex v. A vertex of degree 0 is called an isolated vertex.
在无向图中,顶点v的度是与顶点v相邻的边的数量。度为0的顶点被称为孤立顶点。
- 例如,顶点 0/2/6 的度分别为 2/3/1。
- A ==subgraph== G' of a graph G is a (smaller) graph that contains subset of vertices and edges of G.
图G的子图G' 是一个包含G的顶点和边的子集的(较小的)图。
- 例如,三角形 {0, 1, 2} 是当前显示图的子图。
- If there is no repeated vertex along the path, we call such path as a ==simple path==.
Connected Component(SC) 连通分量
- 适用于:无向图
- 定义:如果在无向图中,任意两个顶点之间都存在一条路径,则称这个图是连通的。
- 一个连通分量是指一个最大连通子图,即不能再加入其他顶点而仍然保持连通。 An ==undirected graph== G is called ==connected== if there is a path between ==every pair== of distinct vertices of G. For example, the currently displayed graph is not a connected graph. An undirected graph C is called a ==connected component== of the undirected graph G if: 1). C is a subgraph of G; 2). C is connected; 3). no connected subgraph of G has C as a subgraph and contains vertices or edges that are not in C (i.e., C is the maximal subgraph that satisfies the other two criteria).
Strongly Connected Component(SCC) 强连通分量
- 适用于:有向图
- 定义:如果在有向图中,任意两个顶点之间都存在双向路径(即 A 到 B 和 B 到 A 都有路径),则称这个图是强连通的。
- 一个强连通分量是指一个最大强连通子图,即不能再加入其他顶点而仍然保持强连通。 In a directed graph, we extend the concept of Connected Component (CC) into ==Strongly Connected Component (SCC)==. A directed graph G is called ==strongly connected== if there is a path in each direction between every pair of distinct vertices of G. A directed graph SCC is called a strongly connected component of the directed graph G if: 1). SCC is a subgraph of G; 2). SCC is strongly connected; 3). no connected subgraph of G has SCC as a subgraph and contains vertices or edges that are not in SCCC (i.e., SCC is the maximal subgraph that satisfies the other two criteria).
In the currently displayed directed graph, we have {0}, {1, 2, 3}, and {4, 5, 6, 7} as its three SCCs.
转向图(Transpose graph)
翻转原有的所有边后得到的是转置图(transpose graph)。
特殊图
树
树的定义
- 树是一个具有V个顶点和E = V-1条边的连通图,无环,并且任意两个顶点之间有一个唯一的路径。通常,树是在无向图上定义的。
- 由于树只有V-1条边,它通常被认为是一个稀疏图。
- 并非所有的树都有相同的图形绘制布局,即顶部有一个特殊的根顶点,底部有叶顶点(度为1的顶点)。星形图也是一棵树,因为它满足树的属性。
- 将其中一个顶点指定为根顶点的树被称为==有根树==。
- 无根树转化成有根树:
- 指定一个特定的顶点(通常是顶点0)为根
- 从根运行DFS&BFS,将任何树转化为有根树。
- 无根树转化成有根树:
有根树的概念术语
- 0/1/7/9/4的父节点分别是无/0/1/8/3,parent
- 0/1/7的子节点分别是 {1,8}/{2,3,6,7}/无,children
- 4/6/8的祖先分别是 {3,1,0}/{1,0}/{0},ancestor
- 4和6的最低公共祖先是1。lowest common ancestor
- 1/8的后代分别是 {2,3,4,5,6,7}/{9},descendants
- 以1为根的子树包括1,它的后代和所有相关的边,
- ==0/1/2/3==级(level)成员分别是 {0}/{1,8}/{2,3,6,7,9}/{4,5},
- 这个有根树的高度是它的最大层级 = 3。height 对于有根树,我们还可以定义其他属性:二叉树,满二叉树,完全二叉树(堆)。
完全图-Complete Graph
- 无向完全图是一个具有V个顶点和E = V*(V-1)/2条边的图(或E = O(V^2)),即,任何一对顶点之间都有一条边。我们用KV表示具有V个顶点的完全图。
- 完全图是最密集的简单图。
二部图-Bipartite
- 无向图
- 具有V个顶点,可以划分为两个大小为m和n的不相交顶点集,其中V = m+n。同一集合的成员之间没有边。二部图也不包含奇数长度的循环。
- 二部图也可以是完全的,即,一个不相交集合中的所有m个顶点都与另一个不相交集合中的所有n个顶点相连。当m = n = V/2时,这样的完全二部图也有E = O(V2)。
- 树也是二部图,即,所有在偶数级别的顶点形成一个集合,所有在奇数级别的顶点形成另一个集合。
有向无环图-DAG(directed acyclic graph)
- 有向无环图 (DAG-directed acyclic graph) 是一种没有循环的有向图,这对于动态规划技术非常相关。
- DP在这里学:https://visualgo.net/zh/recursion?slide=1
- 每个 DAG 都至少有一个拓扑排序/顺序,可以通过对图遍历算法的简单调整找到。
- 动态规划用在DAG上的SSSP方法
图数据结构
- 邻接矩阵(adjacency Matrix) : 经常判断两点是否相连
- 矩阵:顶点(行)/ 顶点(列)
- 需要
V + 2E空间(每个节点 + 每条边的两个端点指针)。
- 邻接表(adjacency list):边很少的情况
顶点:[相邻顶点]- 空间复杂度V* V
-
边列表(edge list): 只关心边(比如最小生成树)
Adjacency Matrix 邻接矩阵 AM
- 用一个
V x V的二维数组(或矩阵)来存储。 - 如果顶点
u和v之间有边,则AM[u][v] = 1(无权图),或者存储权重w(有权图)。 - 我们通常设置
AM[i][j] = 0来表示没有边 (i, j)。然而,如果图包含0权重的边,我们必须使用另一个符号来表示"无边"(例如,-1,None,null,等等)。 - 空间复杂度分析:AM需要O(V^2)的大空间复杂度,即使图形实际上是稀疏的(边缘不多)。
V = 4
AM = [[0]*V for _ in range(V)]
AM[0][1] = 5 # 表示边 0->1 权重为5
Adjacency List 邻接列表 AL
- 每个顶点对应一个列表,存储所有邻接的顶点及其权重(如果是加权图)。
- 空间复杂度分析:AL具有O(V + E)的空间复杂度,比AM效率高得多,并且通常是大多数图形算法中的默认图形数据结构。
定义方法1
V = 5
AL = [[] for _ in range(V)]
---
定义方法2
from collections import defaultdict
AL = defaultdict(list)
---
# 添加边 0 -> 1,权重 2
AL[0].append((1, 2))
# 添加边 0 -> 3,权重 4
AL[0].append((3, 4))
# 输出邻接表
for u in AL:
print(f"{u}: {AL[u]}")
Edge List 边缘列表 EL
- 把图中所有边按
(u, v)或(u, v, w)(加权)形式放在一个列表中。 - 结构简单,适合用于图算法实现(如 Kruskal 最小生成树等)。
- 易于排序、处理边。
EL = [(0, 1, 5), (1, 2, 3)]