Graph Traversal

Depth-First****Search

Idea

Let G= (V,E) be a directed or undirected graph. A depth-first search traversal of Gworks as follows. First, all vertices are marked unvisited. Next, a starting vertex is selected, say vV, and marked visited. Let wbe any vertex that is adjacent to v. We mark was visitedand advance to another vertex, say x, that is adjacent to wand is marked unvisited. Again, we mark xas visitedand advance to another vertex that is adjacent to xand is marked unvisited. This process of selecting an unvisited vertex adjacent to the current vertex continues as deep as possible until we find a vertex ywhose adjacent vertices have all been marked visited. At this point, we back upto the most recently visited vertex, say z, and visit an unvisited vertex that is adjacent to z, if any. Continuing this way, we finally return back to the starting vertex v. This method of traversal has been given the name depthfirst**search, as it continues the search in the forward (deeper) direction.


Algorithm 8.1 DFS
The case of undirected graphs

---- Tree edges

Edges in the depth-first search tree. An edge (v,w) is a tree edge if wwas first visited when exploring the edge (v,w).

---- Back edges

All other edges.

---- Example 8.1

Example 8.1Figure 8.1(b) illustrates the action of depth-first search traversal on the undirected graph shown in Fig. 8.1(a). Vertex ahas been selected as the start vertex. The depth-first search tree is shown in Fig. 8.1(b) with solid lines. Dotted lines represent back edges. Each vertex in the depth-first search tree is labeled with two numbers: predfn and postdfn. Note that since vertex ehas postdfn = 1, it is the first vertex whose depth-first search is complete. Note also that since the graph is connected, the start vertex is labeled with predfn = 1 and postdfn = 10, the number of vertices in the graph.

无向图

前序根节点为1

每往下一次加1

到底后倒序 底部后序为1退到再次有分叉的地方 注意当节点相邻节点有未访问时 后序还未开始计算 到最后回到根节点时 没有访问过的边也要让两节点相连 不过以虚线相连
Algorithm 8.1 DFS
The case of directed graphs

---- Tree edges, Back edges, Forward edges, Cross edges

Tree edges: edges in the depth-first search tree. An edge (v,w) is a tree edge if wwas first visited when exploring the edge (v,w).

Back edges: edges of the form (v,w) such that wis an ancestorof vin the depth-first search tree (constructed so far) and vertex wwas marked visitedwhen (v,w) was explored.

Forward edges: edges of the form (v,w) such that wis a descendantof vin the depth-first search tree (constructed so far) and vertex wwas marked visitedwhen (v,w) was explored.

---- Cross edges: All other edges.

---- Example****8.2

Example****8.2Figure 8.2(b) illustrates the action of depth-first search traversal on the directed graph shown in Fig. 8.2(a). The other result of the depth-first search traversal is shown in Fig. 8.2(c).

有向图

根据方向进行访问 其余与无向图相同

但是 当访问回根节点时 还没有访问完所有节点 此时挑选剩余节点中能往下走的起点开始 其先序为前面先序max加1 到底后(根据原有规则 先序一直加1)后序为根节点后序加1

方向为祖先指向子代的为forward edge

子代指向祖先的为back edge 直接祖先?直接看画出的序列图 通过实线直接或间接连接 就存在直接?祖先子代关系

其余的为cross edge
Time complexity of depth-first search
• Θ ( m + n ) ( adjacency list ) Θ ( n** 2 ) ( an adjacency matrix )

Applications of Depth-First Search
Finding articulation points in a graph

A vertex vin an undirected graph Gwith more than two vertices is called an articulation pointif there exist two vertices uand wdifferent from vsuch that any path between uand wmust pass through v. Thus, if Gis connected, the removal of vand its incident edges will result in a disconnected subgraph of G. A graph is called biconnected if it is connected and has no articulation points. To find the set of articulation points, we perform a depth-first search traversal on G. During the traversal, we maintain two labels with each vertex v ∈ V: α[v] and β[v]. α[v] is simply predfn in the depth-first search algorithm, which is incremented at each call to the depth-first search procedure. β[v] is initialized to α[v], but may change later on during the traversal. For each vertex vvisited, we let β[v] be the minimum of the following:

*•*α[v] = predfn.

• α[u] for each vertex usuch that (v, u) is a back edge.

*•*β[w] for each edge (v,w) in the depth-first search tree.

i.e.

b**[v]=min{a[v], min{b[w]|w is a son of v},**

min**{a[u]|(v,u) is a back edge} }****;**

n The articulation points are determined as follows:

*•*The root is an articulation point if and only if it has two or more children in the depth-first search tree.

*•*A vertex vother than the root is an articulation point if and only if vhas a child wwith β[w] ≥ α[v].

First, the algorithm performs the necessary initializations. In particular, count is the number of articulation points, and rootdegree is the degree of the root of the depth-first search tree. This is needed to decide later whether the root is an articulation point as mentioned above. Next the depth-first search commences starting at the root. For each vertex vvisited, α[v] and β[v] are initialized to predfn. When the search backs up from some vertex wto v, two actions take place. First, β[v] is set to β[w] if β[w] is found to be smaller then β[v]. Second, if β[w] ≥ α[v], then this is an indication that vis an articulation point.

This is because any path from wto an ancestor of vmust pass through v. This is illustrated in Fig. 8.4 in which any path from the subtree rooted at wto umust include v, and hence vis an articulation point. The subtree rooted at wcontains one or more connected components. In this figure, the root uis an articulation point since its degree is greater than 1.

Example 8.3 We illustrate the action of Algorithm ARTICPOINTS by finding the articulation points of the graph shown in Fig. 8.1(a). See Fig. 8.5. Each vertex vin the depth-first search tree is labeled with α[v] and β[v]. So vertices b, c, g and h are articulation points.
Strongly connected components

Given a directed graph G= (V,E), a strongly connected componentin Gis a maximalset of vertices in which there is a path between each pair of vertices. Algorithm STRONGCONNECTCOMP uses depth-first search in order to identify all the strongly connected components in a directed graph.Example 8.4 Consider the directed graph Gshown in Fig. 8.2(a). Applying depth-first search on this directed graph results in the forest shown in Fig. 8.2(b). Also shown in the figure is the postordering of the vertices, which is *e, f, b, a, d, c.*If we reverse the direction of the edges in G, we obtain G, which is shown in Fig. 8.6(a).

Starting from vertex cin G, a depth-first search traversal yields the tree consisting of vertex conly. Similarly, applying depth-first search on the remaining vertices starting at vertex dresults in the tree consisting of only vertex d. Finally, applying depth-first search on the remaining vertices starting at vertex ayields the tree whose vertices are a, b, e, and f. The resulting forest is shown in Fig. 8.6(b). Each tree in the forest corresponds to a strongly connected component. Thus, Gcontains three strongly connected components.

Idea

Unlike depth-first search, in breadth-first search when we visit a vertex v, we next visit all vertices adjacent to v. The resulting tree is called a breadthfirstsearchtree. This method of traversal can be implemented by a queue to store unexamined vertices. Algorithm BFS for breadth-first search can be applied to directed and undirected graphs. Initially, all vertices are marked unvisited. The counter bfn, which is initialized to zero, represents the order in which the vertices are removed from the queue. In the case of undirected graphs, an edge is either a tree edge or a cross edge. If the graph is directed, an edge is either a tree edge, a back edge, or a cross edge; there are no forward edges.

Example 8.5 Figure 8.7 illustrates the action of breadth-first search traversal when applied on the graph shown in Fig. 8.1(a) starting from vertex a.
Time complexity

The time complexity of breadth-first search when applied to a graph (directed or undirected) with nvertices and medges is the same as that of depth-first search, i.e., Θ(n+ m). If the graph is connected or *m ≥*n, then the time complexity is simply Θ(m).

Applications of Breadth-First Search

Problem

Let G= (V,E) be a connected undirected graph and sa vertex in V. Find the distance from s to any other vertex. (where the distance from sto a vertex vis defined to be the least number of edges in any path from sto v.)
Solution

This can easily be done by labeling each vertex with its distance priorto pushing it into the queue. Thus, the start vertex will be labeled 0, its adjacent vertices with 1, and so on. Clearly, the label of each vertex is its shortest distance from the start vertex. For instance, in Fig. 8.7, vertex awill be labeled 0, vertices band gwill be labeled 1, vertices c, f, and hwill be labeled 2, and finally vertices *d, e,*i, and jwill be labeled 3.

相关推荐
hsling松子4 小时前
使用PaddleHub智能生成,献上浓情国庆福
人工智能·算法·机器学习·语言模型·paddlepaddle
dengqingrui1234 小时前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
C++忠实粉丝4 小时前
前缀和(8)_矩阵区域和
数据结构·c++·线性代数·算法·矩阵
ZZZ_O^O5 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
CV-King5 小时前
opencv实战项目(三十):使用傅里叶变换进行图像边缘检测
人工智能·opencv·算法·计算机视觉
代码雕刻家6 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
雨中rain6 小时前
算法 | 位运算(哈希思想)
算法
Kalika0-07 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
sp_fyf_20248 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
我是哈哈hh9 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝