图的深度优先遍历的六种应用附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}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/154995.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库分库分表的原则

目录 1、数据库分库分表是什么 2、为什么要对数据库分库分表 3、何时选择分库分表 4、⭐分库分表遵循的原则 5、分库分表的方式 6、数据存放在表和库中的规则&#xff08;算法&#xff09; 7、分库分表的架构模式 8、分库分表的问题 小结 1、数据库分库分表是什么 数…

Simulink的To Workspace

To Workspace模块将Simulink产生的数据存储到matlab的工作区。 用To Workspace模块中的数据进行绘图。 参见Matlab/simulink/simscape multibody-to wotkspace模块使用_to workspace模块_五VV的博客-CSDN博客

AGRCZO-A-20/210先导比例减压阀控制器

AGRCZO-A-10/100、AGRCZO-A-20/210、AGRCZO-A-10/50、AGRCZO-A-20/350、AGRCZO-A-10/315、AGRCZO-A-20/100数字型比例减压阀&#xff0c;先导式&#xff0c;用于压力开环控制。A型&#xff0c;与分体式放大器配合使用AEB型&#xff0c;带基本型集成式数字放大器&#xff0c;模拟…

518抽奖软件,为什么说比别的抽奖软件更美观精美?

518抽奖软件简介 518抽奖软件&#xff0c;518我要发&#xff0c;超好用的年会抽奖软件&#xff0c;简约设计风格。 包含文字号码抽奖、照片抽奖两种模式&#xff0c;支持姓名抽奖、号码抽奖、数字抽奖、照片抽奖。(www.518cj.net) 精致美观功能 字体平滑无锯齿图片放大后清晰…

红队专题-从零开始VC++C/S远程控制软件RAT-MFC-远程桌面屏幕监控

红队专题 招募六边形战士队员[24]屏幕监控-(1)屏幕查看与控制技术的讲解图像压缩算法图像数据转换其他 [25]---屏幕监控(2)查看屏幕的实现7.1 屏幕抓图显示7.7 完善主控端 招募六边形战士队员 一起学习 代码审计、安全开发、web攻防、逆向等。。。 私信联系 [24]屏幕监控-(1…

【已解决】VSCode运行C#控制台乱码显示

问题描述 如上图所示&#xff0c;最近在学习C#突然发现我在运行Hello World的时候出现这样的乱码情况。 分析原因 主要是因为VS Code 是UTF-8的编码格式&#xff0c;而我们的PC是Unicode编码&#xff0c;所以我们需要对其进行一个统一即可解决问题。那么知道这个的问题那就开…

Redis队列Stream

1 缘起 项目中处理文件的场景&#xff1a; 将文件处理请求放入队列&#xff0c; 一方面&#xff0c;缓解服务器文件处理压力&#xff1b; 另一方面&#xff0c;可以根据文件大小拆分到不同的队列&#xff0c;提高文件处理效率。 这是Java开发组Leader佳汇提出的文件处理方案&a…

创建并启动华为HarmonyOS本地与远程模拟器及远程真机

1.打开设备管理器 2.选择要添加的手机设备,然后点击安装 3.正在下载华为手机模拟器 4.下载完成 5.创建新模拟器 下载系统镜像 点击下一步,创建模拟器 创建成功 启动模拟器 华为模拟器启动成功 6.登陆华为账号并使用远程模拟器 7.使用远程真机

成绩不公开,如何发成绩

亲爱的老师们&#xff0c;有没有在学期中疯狂整理成绩单&#xff0c;又担心成绩私发引起混乱的烦恼&#xff1f;今天就让我们一起探索如何利用各种工具和代码&#xff0c;实现学生自主查询成绩的便捷方式吧&#xff01; 成绩查询系统简介 成绩查询系统是一款方便学生和老师查询…

如果一定要在C++和JAVA中选择,是C++还是java?

如果一定要在C和JAVA中选择&#xff0c;是C还是java&#xff1f; 计算机专业的同学对这个问题有疑惑的&#xff0c;- 定要看一下这个回答! 上来直接给出最中肯的建议: 如果你是刚刚步入大学的大一时间非常充裕的同学&#xff0c;猪学长强烈建议先学C/C.因为C 非常 最近很多…

unity性能优化__Statistic状态分析

在Unity的Game视图右上角&#xff0c;我们会看到有Stats选项&#xff0c;点击会出现这样的信息 我使用的Unity版本是2019.4.16 一、Audio&#xff0c;顾名思义是声音信息 1&#xff1a;Level:-74.8dB 声音的相对强度或音量。通常&#xff0c;音量级别以分贝&#xff08;dB&a…

15、SpringCloud -- 延迟消息、异步下单失败处理方案

目录 延迟消息需求理解:思路:代码:发送延迟消息消费延迟消息:1、订单支付状态:2、回补真实库存:3、回补预库存:4、修改本地标识:测试:清除MQ数据:期望结果:实际结果:问题:异步下单失败需求1:代码:发送消息:消费消息:测试:需求2:延迟消息 需求理解: 用户成…