当前位置: 首页 > news >正文

图的深度优先遍历的六种应用附Java代码

目录

无向图的连通分量个数

单纯求出了连通分量个数 

能具体返回哪几个点是同一个连通分量

路径问题

单源路径问题

从某个顶点到另一个顶点的路径问题

检测无向图中的环

二分图的检测


无向图的连通分量个数

单纯求出了连通分量个数 

import java.util.ArrayList;public class CC {private Graph G;private int[] visited;private int cccount = 0;public CC(Graph G){this.G = G;visited = new int[G.V()];for(int i = 0; i < visited.length; i ++)visited[i] = -1;for(int v = 0; v < G.V(); v ++)if(visited[v] == -1){dfs(v, cccount);cccount ++;}}private void dfs(int v, int ccid){visited[v] = ccid;for(int w: G.adj(v))if(visited[w] == -1)dfs(w, ccid);}public int count(){
//        for(int e: visited)
//            System.out.print(e + " ");
//        System.out.println();return cccount;}public boolean isConnected(int v, int w){G.validateVertex(v);G.validateVertex(w);return visited[v] == visited[w];}public ArrayList<Integer>[] components(){ArrayList<Integer>[] res = new ArrayList[cccount];for(int i = 0; i < cccount; i ++)res[i] = new ArrayList<Integer>();for(int v = 0; v < G.V(); v ++)res[visited[v]].add(v);return res;}public static void main(String[] args){Graph g = new Graph("g.txt");CC cc = new CC(g);System.out.println(cc.count());System.out.println(cc.isConnected(0, 6));System.out.println(cc.isConnected(5, 6));ArrayList<Integer>[] comp = cc.components();for(int ccid = 0; ccid < comp.length; ccid ++){System.out.print(ccid + " : ");for(int w: comp[ccid])System.out.print(w + " ");System.out.println();}}
}

能具体返回哪几个点是同一个连通分量

import java.util.ArrayList;public class CC{private Graph G;private boolean[] visited;private int cccount = 0;
public CC(Graph G){this.G = G;visited = new int[G.V()];
for(int i = 0; i < visited.length; i++)
{visited[i] = -1;
}
for(int v = 0; v < G.V(); v++){if(visited[V] == -1){dfs(v, ccount);cccount++;}}
private void dfs(int v, int ccid){visited[v] = ccid;
for(int w : G.adj(v)){if(visited[w] == -1){dfs(w, ccid);}}}
public int count(){for(int e : visited){System.out.print(e + " ");
}System.out.println();return cccount;}
public static void main(String[] args){Graph g = new Graph("g.txt");CC cc = new GraphDFS(g);System.out.println(cc.count());}}
}

路径问题

单源路径问题

import java.util.ArrayList;
import java.util.Collections;public class SingleSourcePath {private Graph G;private int s;private int[] pre;public SingleSourcePath(Graph G, int s){this.G = G;this.s = s;pre = new int[G.V()];for(int i = 0; i < pre.length; i ++)pre[i] = -1;dfs(s, s);}private void dfs(int v, int parent){pre[v] = parent;for(int w: G.adj(v))if(pre[w] == -1)dfs(w, v);}public boolean isConnectedTo(int t){G.validateVertex(t);return pre[t] != -1;}public Iterable<Integer> path(int t){ArrayList<Integer> res = new ArrayList<Integer>();if(!isConnectedTo(t)) return res;int cur = t;while(cur != s){res.add(cur);cur = pre[cur];}res.add(s);Collections.reverse(res);return res;}public static void main(String[] args){Graph g = new Graph("g.txt");SingleSourcePath sspath = new SingleSourcePath(g, 0);System.out.println("0 -> 6 : " + sspath.path(6));System.out.println("0 -> 5 : " + sspath.path(5));}
}

从某个顶点到另一个顶点的路径问题

import java.util.ArrayList;
import java.util.Collections;public class Path {private Graph G;private int s, t;private int[] pre;private boolean[] visited;public Path(Graph G, int s, int t){G.validateVertex(s);G.validateVertex(t);this.G = G;this.s = s;this.t = t;visited = new boolean[G.V()];pre = new int[G.V()];for(int i = 0; i < pre.length; i ++)pre[i] = -1;dfs(s, s);for(boolean e: visited)System.out.print(e + " ");System.out.println();}private boolean dfs(int v, int parent){visited[v] = true;pre[v] = parent;if(v == t) return true;for(int w: G.adj(v))if(!visited[w])if(dfs(w, v))return true;return false;}public boolean isConnected(){return visited[t];}public Iterable<Integer> path(){ArrayList<Integer> res = new ArrayList<Integer>();if(!isConnected()) return res;int cur = t;while(cur != s){res.add(cur);cur = pre[cur];}res.add(s);Collections.reverse(res);return res;}public static void main(String[] args){Graph g = new Graph("g.txt");Path path = new Path(g, 0, 6);System.out.println("0 -> 6 : " + path.path());Path path2 = new Path(g, 0, 5);System.out.println("0 -> 5 : " + path2.path());Path path3 = new Path(g, 0, 1);System.out.println("0 -> 1 : " + path3.path());}
}

检测无向图中的环

思路:找到一个已经被访问过的结点,并且这个结点不是当前节点的上一个结点。

public class CycleDetection {private Graph G;private boolean[] visited;private boolean hasCycle = false;public CycleDetection(Graph G){this.G = G;visited = new boolean[G.V()];for(int v = 0; v < G.V(); v ++)if(!visited[v])if(dfs(v, v)){hasCycle = true;break;}}// 从顶点 v 开始,判断图中是否有环private boolean dfs(int v, int parent){visited[v] = true;for(int w: G.adj(v))if(!visited[w]){if(dfs(w, v)) return true;}else if(w != parent)return true;return false;}public boolean hasCycle(){return hasCycle;}public static void main(String[] args){Graph g = new Graph("g.txt");CycleDetection cycleDetection = new CycleDetection(g);System.out.println(cycleDetection.hasCycle());Graph g2 = new Graph("g2.txt");CycleDetection cycleDetection2 = new CycleDetection(g2);System.out.println(cycleDetection2.hasCycle());}
}

二分图的检测

思路:从某一点开始逐个染色。

 

 

import java.util.ArrayList;public class BipartitionDetection {private Graph G;private boolean[] visited;private int[] colors;private boolean isBipartite = true;public BipartitionDetection(Graph G){this.G = G;visited = new boolean[G.V()];colors = new int[G.V()];for(int i = 0; i < G.V(); i ++)colors[i] = -1;for(int v = 0; v < G.V(); v ++)if(!visited[v])if(!dfs(v, 0)){isBipartite = false;break;}}private boolean dfs(int v, int color){visited[v] = true;colors[v] = color;for(int w: G.adj(v))if(!visited[w]){if(!dfs(w, 1 - color)) return false;}else if(colors[w] == colors[v])return false;return true;}public boolean isBipartite(){return isBipartite;}public static void main(String[] args){Graph g = new Graph("g.txt");BipartitionDetection bipartitionDetection = new BipartitionDetection(g);System.out.println(bipartitionDetection.isBipartite);// trueGraph g2 = new Graph("g2.txt");BipartitionDetection bipartitionDetection2 = new BipartitionDetection(g2);System.out.println(bipartitionDetection2.isBipartite);// falseGraph g3 = new Graph("g3.txt");BipartitionDetection bipartitionDetection3 = new BipartitionDetection(g3);System.out.println(bipartitionDetection3.isBipartite);// true}
}

相关文章:

  • 一体化模型图像去雨+图像去噪+图像去模糊(图像处理-图像复原-代码+部署运行教程)
  • (SpringBoot)第二章:Spring创建和使用
  • 矢量图形编辑软件illustrator 2023 mac中文软件特点
  • 微信开放平台账号
  • 0037【Edabit ★☆☆☆☆☆】【修改Bug 2】Buggy Code (Part 2)
  • redis6.0源码分析:简单动态字符串sds
  • 贝叶斯神经网络用于学习曲线的概率预测【ICLR 2017】
  • 【C++指针】类的指针
  • Android 处理多个TextView, 文案过长时前面文本省略的问题
  • 4.8 内部类
  • Vue之样式绑定
  • pg数据库插入数据的时候,进行数据去重
  • Capacitor 打包 h5 到 Android 应用,uniapp https http net::ERR_CLEARTEXT_NOT_PERMITTED
  • 09、SpringCloud -- 利用redis的原子性控制高并发请求访问到service层、本地标识
  • 2023年【R1快开门式压力容器操作】考试题及R1快开门式压力容器操作模拟考试
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • 【EOS】Cleos基础
  • 【mysql】环境安装、服务启动、密码设置
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  •  D - 粉碎叛乱F - 其他起义
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • JavaScript设计模式与开发实践系列之策略模式
  • JavaScript学习总结——原型
  • Less 日常用法
  • MYSQL 的 IF 函数
  • python docx文档转html页面
  • Spring声明式事务管理之一:五大属性分析
  • SQL 难点解决:记录的引用
  • tab.js分享及浏览器兼容性问题汇总
  • uva 10370 Above Average
  • vue-cli在webpack的配置文件探究
  • 百度小程序遇到的问题
  • 创建一个Struts2项目maven 方式
  • 从重复到重用
  • 关于 Cirru Editor 存储格式
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 回顾2016
  • 前端工程化(Gulp、Webpack)-webpack
  • 驱动程序原理
  • 如何优雅地使用 Sublime Text
  • 设计模式(12)迭代器模式(讲解+应用)
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 我建了一个叫Hello World的项目
  • Prometheus VS InfluxDB
  • #1014 : Trie树
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • $ git push -u origin master 推送到远程库出错
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (11)MSP430F5529 定时器B
  • (2)(2.10) LTM telemetry
  • (2)STM32单片机上位机
  • (4) PIVOT 和 UPIVOT 的使用
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (Python) SOAP Web Service (HTTP POST)
  • (附源码)python房屋租赁管理系统 毕业设计 745613