在这篇采用的是union-find算法实现的连通性检查,本篇我们将采用深度优先搜索的方式来找出图中的所有连通分量

 图算法系列之深度优先搜索(基于图搜索的深度优先搜索) 深度 优先 搜索 第1张

本文转载自微信公众号「贝塔学JAVA」,作者Silently9527。转载本文请联系贝塔学JAVA公众号。

在上篇中我们学习了深度优先搜索,知道了如何通过深度优先搜索在图中寻找路径;本篇我们继续一起来学习深度优先搜索算法的其他应用场景

连通分量

从一幅图中找出所有的连通分量,这是也是深度优先搜索的一个应用场景。什么是连通分量?这个定义在之前的文章中已有提到《如何检测社交网络中两个人是否是朋友关系(union-find算法)》

在这篇采用的是union-find算法实现的连通性检查,本篇我们将采用深度优先搜索的方式来找出图中的所有连通分量

连通分量的API定义

  1. publicclassDepthFirstCC{
  2. publicDepthFirstCC(Graphgraph);
  3. publicbooleanconnected(intv,intw);//检查两个顶点是否连通
  4. publicintcount();//统计连通分量的总数
  5. publicintid(intv);//顶点v所在连通分量的标识
  6. }

连通分量的API实现

与之前一样没扫描到一个顶点我们就需要标记这个顶点,所以依然需要定义一个marked[]数组

为了统计出图中总共有多少连通分量,所以需要定义一个变量count

为了判断两个顶点是否相连,我们需要把相连的顶点对应的标识值记录成相同值,当在调用connected方法的时候直接取出两个顶点的标识值比较,如果相同就是连通的,否则就是非连通;

这个的标识值我们使用的是count的值,每个顶点都需要存一个标识值,所以还需要一个ids[]数组。

  1. publicclassDepthFirstCC{
  2. privatebooleanmarked[];
  3. privateintcount;
  4. privateint[]ids;
  5. publicDepthFirstCC(Graphgraph){
  6. this.marked=newboolean[graph.V()];
  7. this.ids=newint[graph.V()];
  8. for(intv=0;v<graph.V();v++){
  9. if(!this.marked[v]){
  10. dfs(graph,v);
  11. count++;
  12. }
  13. }
  14. }
  15. privatevoiddfs(Graphgraph,intv){
  16. this.marked[v]=true;
  17. this.ids[v]=count;
  18. for(intw:graph.adj(v)){
  19. if(!this.marked[w]){
  20. dfs(graph,w);
  21. }
  22. }
  23. }
  24. publicbooleanconnected(intv,intw){
  25. returnid(v)==id(w);
  26. }
  27. publicintcount(){
  28. returncount;
  29. }
  30. publicintid(intv){
  31. returnids[v];
  32. }
  33. }

单元测试

 图算法系列之深度优先搜索(基于图搜索的深度优先搜索) 深度 优先 搜索 第2张

构造这样一个图,连通分量的总数应该是3

  1. @Test
  2. publicvoidtest(){
  3. Graphgraph=newGraph(10);
  4. graph.addEdge(0,1);
  5. graph.addEdge(0,2);
  6. graph.addEdge(0,5);
  7. graph.addEdge(1,3);
  8. graph.addEdge(2,4);
  9. graph.addEdge(4,3);
  10. graph.addEdge(5,3);
  11. graph.addEdge(6,7);
  12. graph.addEdge(8,9);
  13. DepthFirstCCcc=newDepthFirstCC(graph);
  14. System.out.println(cc.connected(0,5));
  15. System.out.println(cc.connected(1,2));
  16. System.out.println(cc.count());
  17. }

 图算法系列之深度优先搜索(基于图搜索的深度优先搜索) 深度 优先 搜索 第3张

基于深度优先搜索实现的连通性检查理论上说要比以前实现的union-find算法更快,因为检查连通性深度优先搜索实现的版本能够保证在常量时间内完成,而union-find算法不行;

但是union-find也有自己的优势: 不需要把完整的构造并表示一张图,更重要的是union-find算法是动态的添加节点。

检查无向图中是否有环

为了减小实现的复杂度,我们假设图中不存在自环和平行边;

假如从顶点v出发存在环,表示从顶点v出发的连通分量中某个顶点的邻接顶点是v,那么在搜索的过程中必定会再次遇到顶点v

实现的思路:

  1. 标记已经搜索过的每个顶点
  2. 当遇到了一个已经被标记过的顶点,表示已经图中存在环;
  3. 由于图是无向图,如果v-w相连,那么顶点v中的邻接表中有w,w邻接表中也会有v,但是他们没有构成环,所以需要排除掉该情况。
  1. publicclassCycle{
  2. privatebooleanmarked[];
  3. privatebooleanhashCycle;
  4. publicCycle(Graphgraph){
  5. this.marked=newboolean[graph.V()];
  6. for(ints=0;s<graph.V();s++){
  7. if(!this.marked[s]){
  8. dfs(graph,s,s);
  9. }
  10. }
  11. }
  12. privatevoiddfs(Graphgraph,intv,intpV){
  13. this.marked[v]=true;
  14. for(intw:graph.adj(v)){
  15. if(!this.marked[w]){
  16. this.dfs(graph,w,v);
  17. }elseif(w!=pV){
  18. this.hashCycle=true;
  19. return;
  20. }
  21. }
  22. }
  23. publicbooleanhasCycle(){
  24. returnhashCycle;
  25. }
  26. }

方法dfs的参数v表示需要待搜索的顶点,pV表示的是到达v的顶点,所以如果v的邻接表中有个顶点已被标记过并且该顶点不等于到达v的顶点,那么表示图中有环

检查无向图是否是二分图

何为二分图? 图中每条边所连接的顶点都属于不同的部分;如下图:

 图算法系列之深度优先搜索(基于图搜索的深度优先搜索) 深度 优先 搜索 第4张

其中红色节点表示一个集合,白色节点是另一个集合,每条边连接的两个顶点属于不同的集合;

举个实际的例子就很好理解,电影与演员的关系,电影作为一个顶点,演员作为一个顶点,电影与电影直接是不会有边,演员与演员直接也不会有边,这就是一张二分图。

  1. publicclassTwoColorGraph{
  2. privatebooleantwoColor=true;
  3. privateboolean[]marked;
  4. privateboolean[]color;
  5. publicTwoColorGraph(Graphgraph){
  6. this.marked=newboolean[graph.V()];
  7. this.color=newboolean[graph.V()];
  8. for(intv=0;v<graph.V();v++){
  9. if(!this.marked[v]){
  10. dfs(graph,v);
  11. }
  12. }
  13. }
  14. privatevoiddfs(Graphgraph,intv){
  15. this.marked[v]=true;
  16. for(intw:graph.adj(v)){
  17. if(!this.marked[w]){
  18. this.color[w]=!this.color[v];
  19. dfs(graph,w);
  20. }elseif(this.color[w]==this.color[v]){
  21. this.twoColor=false;
  22. return;
  23. }
  24. }
  25. }
  26. publicbooleanisTwoColor(){
  27. returntwoColor;
  28. }
  29. }

文中所有源码已放入到了github仓库:https://github.com/silently9527/JavaCore

转载请说明出处
知优网 » 图算法系列之深度优先搜索(基于图搜索的深度优先搜索)

发表评论

您需要后才能发表评论