DSAA之图论DFS生成树和查找强连通分支(十)

1. 回顾

DSAA之图论DFS算法,初识回溯(八)的重点扩展了回溯算法,忽略了DFS生成树的若干问题。研究DFS生成树是必要的,因为其有些比较重要的应用,如:Biconnectivity。

2. 无向图生成树

  • We graphically illustrate these steps with a depth-first spanning tree.
  • The root of the tree is A, the first vertex visited. Each edge (v, w) in the graph is present in the tree. If, when we process (v, w), we find that w is unmarked, or if, when we process (w, v), we find that v is unmarked, we indicate this with a tree edge.
  • If when we process (v, w), we find that w is already marked, and when processing (w, v), we find that v is already marked, we draw a dashed line, which we will call a back edge, to indicate that this “edge” is not really part of the tree.

DFS生成树是DFS搜索过程的直观反映,区分于无向图的最小生成树。顺便回顾下只有连通的无向图才存在最小生成树,当各边权重唯一的时候,该生成树唯一。然后下图为一个DFS生成树(可以自己动手画出来DFS生成树):
  这里写图片描述
  这里写图片描述

2. 有向图生成树

  • If the graph is not strongly connected, a depth-first search starting at some node might not visit all nodes. In this case we repeatedly perform depth-first searches, starting at some unmarked node, until all vertices have been visited. 如果有向图不是强连通,那么就会生成DFS森林。
  • The dashed arrows in the depth-first spanning forest are edges (v, w) for which w was already marked at the time of consideration.
  • there are three types of edges that do not lead to new vertices.
  • back edges.
  • forward edges that lead from a tree node to a descendant.
  • cross edges which connect two tree nodes that are not directly related.

在非强连通的有向图中,虚线有三种类型:背向、前向、横跨边。直接看一个有向图的例子如下:
  这里写图片描述
  这里写图片描述

DFS的几个重要应用都需要DFS生成树的概念:

  • One use of depth-first search is to test whether or not a directed graph is acyclic.
  • The rule is that a directed graph is acyclic if and only if it has no back edges. (The graph above has back edges, and thus is not acyclic.)
  • The alert reader may remember that a topological sort can also be used to determine whether a graph is acyclic.
  • Another way to perform topological sorting is to assign the vertices topological numbers n, n - 1, . . . ,1 by postorder traversal of the depth-first spanning forest. As long as the graph is acyclic, this ordering will be consistent.

上面几点高度浓缩,但是想要理解,恐怕需要回顾很多知识了,首先第一点指出深搜可以用于测试一个有向图是否有环,因为背边代表了当前节点与目标节点是连通的,但目标节点已经访问过了。如果含有背边,则该有向图一定存在环。
  第二点
拓扑排序
只能用于DAG,但是如果该图中有环,则拓扑排序提前结束(未打印出所有的节点)。
  第三点给出了拓扑排序另外的实现手段,在DFS遍历过程中将其产生的DFS生成树(森林)按照后续遍历的方式输出节点并从n到1的递减顺序编号,此时产生唯一的拓扑排序结果。这里如何编程实现?笔者暂时没有思考出结果。

3. 查找强连通分支

使用DFS可以测试有向图是否为强连通,如果不是还可以找到其强连通分支。使用的算法如下步骤:

  • 对原图生成DFS生成树或者森林
  • 对DFS生成森林后序遍历,并将节点赋予编号(从n到1)。
  • 将原图的所有边都反向形成新图
  • 在新图中从编号最大的节点进行深搜,生成相应的DFS生成树。
  • 重复第四步直到遍历所有的节点
  该算法的正确性,DSAA有些简单的逻辑证明。这里不赘述,同样看一个基于上面有向图的例子: ![这里写图片描述](https://img-blog.csdn.net/2018061516321010?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvdmVTdGFja292ZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) ![这里写图片描述](https://img-blog.csdn.net/20180615163231468?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvdmVTdGFja292ZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) 在该例子中最大强连通分支为BACF。

4. 总结

这些内容DSAA没有给出任何实现,也是点到为止。笔者觉得在实现上一个最大的问题就是如何实现DFS生成的森林,这里就需要别的教材的帮助了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值