0%
图
基本定义与应用
无向图
- Undirected graphs
- \(G=(V,E)\)
- 结点/节点:nodes/vertices
- 边:edges/arcs
- \(n=\vert{V}\vert,m=\vert{E}\vert\)
- \(degree(u)=\vert
B\vert,B=\{v:edge(u,v)\in E\}\)
应用
communication |
telephone, computer |
fiber optic cable |
circuit |
gate, register, processor |
wire |
mechanical |
joint |
rod, beam, spring |
financial |
stock, currency |
transactions |
transportation |
street intersection, airport |
highway, airway route |
internet |
class C network |
connection |
game |
board |
position |
social relationship |
person, actor |
friendship, movie cast |
neural network |
neuron |
synapse |
protein network |
protein |
protein-protein interaction |
molecule |
atom |
bond |
图的表示

邻接矩阵

邻接表

路径与连通性
- paths and connectivity
- 简单路径(simple)
- 连通的(connected)
环
- cycle
- 首尾结点相同的路径,长度大于等于 2
- 简单环(simple)
树
- tree
- 无向图:连通图 + 无环
- 如下 3 个结论,任意两个都可以推导得到剩下一个
- G is connected.
- G does not contain a cycle.
- G has n – 1 edges.
有根树

应用
- Phylogeny trees:系统发育树
- GUI containment hierarchy:GUI 层次结构树
图的连通性与遍历
连通性
- s-t connectivity problem
- s-t shortest path problem
BFS
- Breadth-first search:宽度优先遍历
- Explore outward from s in all possible directions, adding nodes
one “layer” at a time.

- 性质
- 如果 s-t 存在路径,则 t 一定出现在某一层上
- 如果 (x, y) 是图上的一条边,则它们深度最多相差 1
- 时间复杂度
- 邻接表:\(O(m+n)\)
- 无向图:每一条边都会被检查 2 次
连通分支
- Connected component
- Find all nodes reachable from s.
Flood Fill
Testing bipartiteness
二部图
- 二部图/二分图(Bipartite graphs)

引理
- G 是连通图,\(L_0,\cdots,L_k\) 是
BFS 形成的层级

- 如果同层之内没有边,则是二部图
- 回顾:BFS 形成的层次结构,edge(u, v) 的两个结点层数最多相差 1
- 否则存在奇数环
- z 表示 u,v 的最近公共祖先
- \(z-L_{i+1}-\cdots-L_{j-1}-u-v-L_{j-1}-\cdots-L_{i+1}-z\)
- 边数:\(2(i-j+2)+1\)
推论
- 无向图 G 是二部图当且仅当图 G
不含有奇数长度的环
- 不包含奇回路 \(\Rightarrow\)
上面奇数层级为一部,偶数层级为一部,证明部的内部没有点相邻
- G 是二部图 \(\Rightarrow\)
反证法,假设存在奇回路,则无法二着色
有向图中的连通性
- 有向图(directed graph)
- 有向边:edge(u,v) 表示 \(u\to
v\)
- 应用
图搜索
- Directed reachability
- Directed s\(\to\)t shortest path problem
强连通性
- strong connectivity
- 一个有向图是强连通的 \(\Leftrightarrow\) 任意两点相互可达
- 推论:
- s 是 G 中任意一点
- G 是强连通的 \(\Leftrightarrow\)
任意一点可达 s,而且 s 可达任意一点
- 根据推论判断一个图是否强连通
- 选择任意点 s
- 在 G 和 G‘ 上判断 s 能够到达任意点
- G':将 G 上的所有边逆向之后得到的新图
- \(O(m+n)\)
强连通分量
- Strong components
- 最大的互相可达的顶点集合

有向无环图与拓扑排序
- 有向无环图
- Directed acyclic graphs
- DAG
- 拓扑排序
DAG
- 有向图 + 无环
- 拓扑序:节点的一个排序 \(v_1,v_2,\cdots,v_n\),是的不存在一条边
\(edge(v_i,v_j),i>j\)
- Precedence constraints:优先级的约束
- 有向图 G 是 DAG \(\Rightarrow\) G
存在一个结点没有入边
- 有向图 G 是 DAG \(\Leftrightarrow\)
G 存在一个拓扑排序
- \(\Leftarrow\):反证法,假设有环则无拓扑排序
- \(\Rightarrow\):归纳法,根据上面的定理
- 找到一个拓扑排序
- 依次取没有入边的点(除去它的相关边)
- \(O(m+n)\)