A. 概述
一、邻接 + 关联
- 基本术语
- 邻接(adjacency):v-v,同一条边的两个顶点彼此邻接
- 关联(incidence):v-e,顶点与其所属的边彼此关联
- 度(degree/valency):与同一顶点关联的边数
- 自环(self-loop):同一顶点自我邻接
- 简单图(simple graph):不含自环及重边;非简单图(non-simple graph)
二、无向图 + 有向图
- 无向边(undirected edge):若邻接顶点u和v的次序无所谓,则(u,v)为无向边
- 无向图(undigraph):所有边均无方向的图
- 有向图(digraph):所有边均为有向边(directed edge),u,v分别称作边(u,v)的尾(tail)、头(head)
- 混合图(mixed graph):无向边、有向边并存的图
三、路径 + 环路
- 路径π:顶点按一定的序列进行组织<v0, v1, …, vk>
- 长度|π|:k
- 简单路径:V0 ≠ Vk,除非 i = j
- 环/环路:V0 = Vk
- 有向无环图(DAG)
- 欧拉环路:|π| = |E|,各边恰好出现一次
- 哈密尔顿环路:|π| = |V|,各顶点恰好出现一次
四、支撑树 + 带权网络 + 最小支撑树*
- 图G = (V;;E) 的子图T = (V;F)若是树,即为其支撑树(spanning tree),通常并不唯一
- 各边e均有对应的权值wt(e),则为带权网络(weighted network)
- 同一网络的支撑树中,总权重最小者为最小支撑树(MST)
五、作业
- The relationship between two vertices connected by a edge is called:两个通过一条边连起来的顶点之间的关系称为:adjacent 邻接
- The one-touch-drawing question is to find out: 一笔画问题即要找出:Euler path 欧拉路径
B. 邻接矩阵
构思
- 算法
- 邻接矩阵(adjacency matrix):记录顶点之间的邻接关系;空间复杂度Θ(n2),与图中实际的边数无关
- 关联矩阵(incidence matrix):记录顶点与边之间的关联关系;空间复杂度Θ(n*e) = O(n3);空间利用率2e/ne = 2/n
二、模板实现
- 顶点与边
- 图矩阵
三、静态操作
- 顶点的读写
- 边的读写
- 邻点的枚举
四、动态操作
- 点的操作
- 边的操作
五、性能分析
- 优点
- 直观,易于理解和实现
- 适用广泛,eg:digraph、network、cyclic、尤其是稠密图(dense graph)
- 判断两点之间是否存在联边:O(1)
- 获取顶点的(出/入)度数:O(1)
- 添加、删除边后更新度数:O(1)
- 缺点
- 空间复杂的Θ(n2),与边数无关,且e << n2
- 欧拉定理(Euler’s formula): v - e +f - c =1,(v顶点数目,e一维元素:边,f二维元素:压缩面片的总数,c连通域总数)
- 平面图(planar graph):可嵌入于平面的图。e ≤ 3n - 6 = O(n) << n2,空间利用率 ≈ 1 / n → 0
- 稀疏图(sparse graph)
六、作业
- A graph with an (undirected) edge between any two vertices is called a complete graph, and a complete graph containing n vertices is represented by Kn. Which of the following figures must not be a plan? 任何两个顶点间都有一条(无向)边的图称为完全图,包含n个顶点的完全图用 Kn表示。下列哪个图一定不是平面图?K5 【Obviously the other three are all plans. K_5 is not a plan that can be proved by Euler’s formula and the fact that a face in a plan corresponds to a maximum of 3/2 edges.显然其余三个都是平面图。K_5不是平面图可以用欧拉公式以及平面图中的一个面最多平均对应3/2条边这一事实来证明。】
- The adjacency matrix of the above digraph is (in order of A, B, C, D) 以上有向图的邻接矩阵为(以A、B、C、D为顺序)
- In the graph implemented with adjacency matrices with n vertices, the vertex v has m neighbors, and the time complexity of traversing all m neighbors is:在包含n个顶点的用邻接矩阵实现的图中,顶点v有m个邻居,遍历所有m个邻居的时间复杂度为:O(n) 【Need to access a row in the adjacency list 需要访问邻接表中的一行】
- The graph G contains n vertices (n>0), implemented with an adjacency matrix. How many items have been added to the adjacency matrix after adding a new vertex?图G包含n个顶点(n>0),用邻接矩阵实现。在其中加入一个新的顶点后邻接矩阵增加了多少项?2n+1 【Adjacency matrix adds one row and one column邻接矩阵增加了一行一列】
D. 广度优先搜索BFS(Breadth-First Search)
一、算法
- 化繁为简:traversal使图变为非线性结构
- 策略
- 算法实现
二、实例-无向图
三、推广
四、性质及应用*
五、作业
- Traversing graphs in a sense is to translate the graph into: 对图进行遍历某种意义上是将图转化为:Tree树
- The breadth-first search of a graph is similar to that of a binary tree: 图的广度优先搜索访问各顶点的模式类似于二叉树的:Level-order traversal 层次遍历
- The BFS is performed on the above undigraph with the vertex s as the starting point. The neighbors of the same vertex are in the order of a~z. When the vertex c is just out of the queue, the vertices in the queue from the head of the queue to the end of the queue are: 以顶点s为起点对以上无向图进行BFS,同一顶点的邻居之间以a-z为顺序,顶点c刚出队时队列中顶点从队头到队尾为:d,e
- For graphs with n vertices and e edges implemented with adjacency list, the time complexity of BFS is:对于用邻接表实现的包含n个顶点e条边的图,BFS的时间复杂度为:O(n + e)
E. 深度优先搜索DFS(Depth-First Search)
一、算法
- 策略:
- 算法实现:
二、实例:无向图
三、实例:有向图
四、性质
- 从顶点s出发的DFS:
· 在无向图中将访问与s连通的所有顶点(connectivity)
· 在有向图中将访问由s可达的所有顶点(reachability) - 经DFS确定的树边,不会构成回路
- DFS树及森林由parent指针描述(只不过所有边去反向)
- 括号引理
- 边分类
五、作业
- DFS on the above undigraph with A as the starting point, and the neighbors of the same vertex are in order of a~z. The order in which the vertices are accessed is: 以A为起点对以上无向图进行DFS,同一顶点的邻居以a-z为序,各顶点被访问的顺序为:a, b, c, f, g, d, i, h, j, e
- u and v are two vertices in the graph. After performing DFS on the graph, dTime(u) < dTime(v) < fTime(v) < fTime(u), then the relationship between u and v in the DFS forest is: u 和 v 为图中两个顶点,对图进行 DFS 后,dTime(u) < dTime(v) < fTime(v) < fTime(u),则 u 和 v 在 DFS 森林中的关系是:u is the ancestor of v u 为 v 的祖先
- Run breadth-first search (BFS) and depth-first search (DFS) respectively on the same undirected graph, the numbers of TREE edges satisfy: 对同一个无向图分别运行广度优先算法和深度优先算法,得到的树边数量:they result in same number of TREE edges两种算法得到的树边一样多 【The number of TREE edges is always equal to the number of vertices minus the number of connected components.TREE 边的数量总是等于顶点数减去连通分量的数量】
- DFS on a graph, which situation means that the graph contains a loop 对图进行DFS,一下哪种情况意味着该图包含环:There has a BACKWARD edge 有BACKWARD边
遍历算法应用举例
F1. 拓扑排序之零入度算法
一、拓扑排序
- 有向无环图(Directed Acyclic Graph)
- 任给有向图G(不一定是DAG),尝试将所有顶点排成一个线性序列,使其次序须与原图相容(每一个顶点都不会通过边指向前驱顶点)
- 接口要求:若原图存在回路(即并非DAG),检查并报告;否则给出一个相容的线性序列
二、算法
从零入度的点开始顺序输出
三、实例
F2. 拓扑排序之零初度算法
一、算法
- 基于DFS,借住栈s,逆序输出零出度顶点
- 对图做DFS:每当有顶点被标记为VISITED,则将其压入s;一旦发现有后向边,则报告"NOT_A_DAG"并退出;DFS结束后,顺序弹出s中的各个顶点
- 实现
二、实例
无向图的边数 = 各顶点度数的一半
测验
- In a simple undigraph with 20 vertices, the maximum number of edges is:在含20个顶点的简单无向图中,边的数量最多为:190
The degree of the vertex with the smallest degree at this time is:此时度最小的顶点的度为:19 - A total of 7 people took part in the banquet and a friendly handshake took place among the participants. The number of hands-on handshakes known to each of them is:3, 1, 2, 2, 3, 1, 2。How many handshakes have occurred at the banquet?某宴会一共有7个人参加,与会者之间进行了亲切的握手。已知他们中的每个人进行握手的次数分别为:3, 1, 2, 2, 3, 1, 2。请问宴会上总共发生了多少次握手?7
- (接上题)In the long history of humanity, everyone may have to shake hands with other people. If someone makes an odd number of handshakes in his life, he is called a Class A person, otherwise he is called a Class B person. The number of people of type A since ancient times is: (assuming that humans can only shake hands with humans)在人类的历史长河中,每个人都可能要与其他人握手。如果某人在他的一生中进行握手的次数为奇数,则称他为A类人,否则称为B类人。试问从古至今A类人的个数是:(假设人类只能和人类握手) Even number偶数 【无向图的边数等于各顶点度数之和的一半】
- The adjacency matrix of the above digraph is (in order of A, B, C, D) 以上有向图的邻接矩阵为(图中顶点以A、B、C、D为顺序)
- For a simple undigraph with n vertices and e edges, which of the following argument for its adjacency matrix A is wrong: 对于包含n个顶点e条边的简单无向图,以下关于它的邻接矩阵A的说法中错误的是:A has n rows and e columns, where the elements take a value of {0, 1} A有n行e列,其中元素取值于{0, 1} 【n rows and n columns n行n列】
- G is a simple undigraph, A is an adjacency matrix of G, M is an associative matrix of G, and D is a diagonal matrix of the degree of the i-th element of the vertex on the diagonal. Their relationship is: G是简单无向图,A为G的邻接矩阵,M为G的关联矩阵,D是对角线上第i个元素为顶点i的度的对角矩阵,它们的关系是:A + D = MMT 【Use the example to verify, or see the analysis of the textbook matching exercises 6-1 用例子验证,或见教材配套习题解析6-1】
- Using the adjacency matrix to implement a graph with n vertices and e edges: 用邻接矩阵实现含n个顶点e条边的图:Space complexity: 空间复杂度:O(n2)
- Time complexity of deleting edge(i,j): 删除边(i, j)的时间复杂度:O(1)
- Time complexity of traversing all the neighbors of vertex v: 遍历顶点v的所有邻居的时间复杂度:O(n)
- Time complexity of accessing data stored in vertex v: 访问顶点v中存储的数据的时间复杂度:O(1)
- G is a directed acyclic graph, and (u, v) is an edge in G that points from u to v. The result of DFS on G is: G是有向无环图,(u, v)是G中的一条由u指向v的边。对G进行DFS的结果是:fTime(u) > fTime(v) 【G does not contain a loop, (u, v) cannot be BACKWARD, and access to v must have ended when access to u ends G不含环路,(u, v)不可能是BACKWARD,对u的访问结束时对v的访问必然已经结束】
- The following is the dTime and fTime of each vertex after performing a DFS on a simple undigraph: 下面是对一个简单无向图进行DFS后得到各顶点的dTime和fTime:The DFS tree is: 得到的DFS树为:
- Starting from s, perform BFS on the above undigraph, order a-z between the neighbors of the same vertex, and find the dTime of the vertex 从s开始,对以上无向图进行BFS,同一顶点的邻居之间以a~z为序,求顶点的dTime,dTime of s =1,
dtime of a = 2,
dtime of b = 6,
dtime of e = 5,
dtime of f = 7 - Starting from s, perform DFS on the above undigraph, order a~z between the neighbors of the same vertex, and find the dTime and fTime of each vertex
从s开始,对以上无向图进行DFS,同一顶点的邻居之间以a~z为序,求各顶点的dTime和fTime。dTime of s = 1,fTime of s = 16
dTime of c = 3
fTime of c = 14
dTime of g = 7
fTime of g = 12