图的广度优先遍历的单源路径、无权图的最短路径问题、BFS性质附Java代码

news/2025/3/6 3:33:53/

目录

使用BFS求解单源路径问题

BFS重要性质

无权图的最短路径问题


使用BFS求解单源路径问题

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;public class SingleSourcePath {private Graph G;private int s;//在SingleSourcePath中传入的源private boolean[] visited;private int[] pre;public SingleSourcePath(Graph G, int s){this.G = G;this.s = s;visited = new boolean[G.V()];pre = new int[G.V()];//图有多少个顶点就开多少空间for(int i = 0; i < pre.length; i ++)pre[i] = -1;bfs(s);//从s到其他连通分量是不可达的}private void bfs(int s){Queue<Integer> queue = new LinkedList<>();queue.add(s);visited[s] = true;pre[s] = s;while(!queue.isEmpty()){int v = queue.remove();for(int w: G.adj(v))if(!visited[w]){queue.add(w);visited[w] = true;pre[w] = v;//w的上一个顶点是v}}}public boolean isConnectedTo(int t){G.validateVertex(t);//看用户传入的t是否合法return visited[t];//若t被遍历到了,说明t和s属于同一个连通分量,从s肯定有条路径到达t}public Iterable<Integer> path(int t){ArrayList<Integer> res = new ArrayList<Integer>();if(!isConnectedTo(t)) return res;//若从s无法到达t则返回一个空的ArrayList,说明没有路径能从s到tint cur = t;while(cur != s){res.add(cur);cur = pre[cur];}res.add(s);Collections.reverse(res);//把ArrayList所有元素颠倒出来成为正序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));}
}

BFS重要性质

BFS求解的路径不是一般路,而是最短路,而且要注意BFS求最短路径必须是无权图! 广度优先遍历先遍历离源顶点近的,先遍历的顶点的距离小于等于后遍历的顶点离源顶点的距离。其实这个和树的广度优先遍历的顺序是一模一样的。

无权图的最短路径问题

用一个dis数组来记录距离。

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;// Unweighted Single Source Shortest Path
public class USSSPath {private Graph G;private int s;private boolean[] visited;private int[] pre;private int[] dis;public USSSPath(Graph G, int s){this.G = G;this.s = s;visited = new boolean[G.V()];pre = new int[G.V()];dis = new int[G.V()];for(int i = 0; i < G.V(); i ++) {pre[i] = -1;dis[i] = -1;}bfs(s);for(int i = 0; i < G.V(); i ++)System.out.print(dis[i] + " ");System.out.println();}private void bfs(int s){Queue<Integer> queue = new LinkedList<>();queue.add(s);visited[s] = true;pre[s] = s;dis[s] = 0;while(!queue.isEmpty()){int v = queue.remove();for(int w: G.adj(v))if(!visited[w]){queue.add(w);visited[w] = true;pre[w] = v;dis[w] = dis[v] + 1;}}}public boolean isConnectedTo(int t){G.validateVertex(t);return visited[t];}public int dis(int t){G.validateVertex(t);return dis[t];//返回从源点到某个目标顶点的长度}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");USSSPath ussspath = new USSSPath(g, 0);System.out.println("0 -> 6 : " + ussspath.path(6));System.out.println("0 -> 6 : " + ussspath.dis(6));}
}


http://www.ppmy.cn/news/1194462.html

相关文章

QML WebEngineView 调用 JavaScript

作者: 一去、二三里 个人微信号: iwaleon 微信公众号: 高效程序员 在 QML 与 Web 混合开发时,除了使用 WebEngineView 加载网页之外,我们还可以在 QML 层运行 JavaScript 代码,这样就能更灵活地操作浏览器窗口和网页内容,从而实现丰富的交互功能了。例如:获取网页标题、…

10kb的照片尺寸怎么弄?三个方法值得一试!

为了方便存储和传输&#xff0c;同时还能保证一定的清晰度。10kb的照片在清晰度和尺寸之间达到了平衡&#xff0c;既能保证照片的细节和色彩&#xff0c;又不会占用太多的存储空间。那么如何把照片弄成10kb呢&#xff1f;下面介绍了三种方法。 方法一&#xff1a;嗨格式压缩大师…

git的命令操作

1、基本命令 目录 1、基本命令 创建 Git 存储库 添加文件/目录到索引 将更改提交到本地存储库 撤消上一次提交的更改 显示工作树状态 显示对工作树和索引的更改 显示提交日志 显示提交详细信息 重命名文件 从工作树和索引中移除文件 从工作树中移除未跟踪文件 将…

农业中的机器学习

机器学习训练模型推荐&#xff1a; UnrealSynth虚幻合成数据生成器 - NSDT 机器学习是一个不断发展的领域&#xff0c;在农业中有许多潜在的应用。农民和农业科学家正在探索如何转向机器学习开发来提高作物产量、减少用水量和预测病虫害。未来&#xff0c;机器学习可以帮助农民…

手写一个uniapp的步骤条组件

在template实现 <template><view class"process_more"><!-- 步骤条 --><view class"set-2" :key"index" v-for"(item,index) in options"><!-- 图片 --><view class"img-border"><…

在钣金加工领域,迅镭激光切割机广泛使用的原因和优点何在?

激光切割工艺和激光切割设备正在被广泛的板材加工企业逐渐理解并接受&#xff0c;凭借其高效率的加工、高精度的加工、优质的切割断面、三维切割能力等诸多优势&#xff0c;逐步取代了传统的钣金切割设备。 苏州迅镭激光科技有限公司推出的激光切割设备的柔性化程度高&#xff…

Mac电脑怎么运行 Office 办公软件

虽然 Office 软件也有 Mac 版本的&#xff0c;但是有蛮多小伙伴用起来还是感觉不得劲&#xff0c;毕竟接触了太久的 Windows&#xff0c;所以想要使用 Windows 版本的 Office 软件。 今天就给大家介绍一下怎么在 Mac 电脑中运行 Windows 版本的办公软件&#xff0c;在这里就需…

基于仿真的飞机ICD工具测试

机载电子系统是飞机完成飞行任务的核心保障之一。从1949年新中国建立至今70余年的发展过程中&#xff0c;随着我国在航空航天领域的投资逐年增多&#xff0c;机载电子系统大致经历了四个发展过程阶段&#xff0c;按照出现的先后顺序进行排序&#xff0c;分别为&#xff1a; 1、…