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

建设项目公示网站百度代理查询

建设项目公示网站,百度代理查询,成都旅游攻略景点必去十处,个人网站备案名称要求1 单点最短路径 单点最短路径。 给定一幅图和一个起点s,回答“从s到给定目的顶点v是否存在一条路径?如果有,找出其中最短的那条(所含边数最少)。“等类似问题。 深度优先搜索在这个问题上没有什么作为,因为…

1 单点最短路径

单点最短路径。 给定一幅图和一个起点s,回答“从s到给定目的顶点v是否存在一条路径?如果有,找出其中最短的那条(所含边数最少)。“等类似问题。

深度优先搜索在这个问题上没有什么作为,因为它遍历整个图的顺序和找出最短路径的目标没有任何关系。相比之下,广度优先搜索正好可以解决这个问题。

分析:

  • 要找的从s到v的最短路径,从s开始,在所有由一条边就可以到达的顶点中寻找v,找到标记结束。
  • 如果没有找到,我们继续在于s距离2条边的顶点中查找v,如此一直进行。
  • 最后也没有找到,那么说明s到给定顶点v不存在路径,此图为非连通图。

结构选择:

  • 广度优先搜索中,我们希望按照与起点的距离顺序遍历所有顶点,所以我们选择队列(先入先出)。

2 广度优先搜索实现

实现代码如下:

package com.gaogzhen.datastructure.graph.undirected;import edu.princeton.cs.algs4.*;/*** 最短路径算法* @author: Administrator* @createTime: 2023/03/07 21:04*/
public class BreadthFirstDirectedPaths {private static final int INFINITY = Integer.MAX_VALUE;/*** 标记顶点是否与起点连通*/private boolean[] marked;/*** 表示顶点到与该顶点连通的顶点间最短路径*/private int[] edgeTo;/*** 顶点到起点之间的边数*/private int[] distTo;/*** 计算从指定顶点到起点最短路径* @param G 无向图* @param s 起点* @throws IllegalArgumentException unless {@code 0 <= v < V}*/public BreadthFirstDirectedPaths(Graph G, int s) {marked = new boolean[G.V()];distTo = new int[G.V()];edgeTo = new int[G.V()];for (int v = 0; v < G.V(); v++) {distTo[v] = INFINITY;edgeTo[v] = -1;}validateVertex(s);bfs(G, s);}/*** 计算多个起点到指定顶点之间的最短路径* @param G 无向图* @param sources 多个起点集合* @throws IllegalArgumentException if {@code sources} is {@code null}* @throws IllegalArgumentException unless each vertex {@code v} in*         {@code sources} satisfies {@code 0 <= v < V}*/public BreadthFirstDirectedPaths(Graph G, Iterable<Integer> sources) {marked = new boolean[G.V()];distTo = new int[G.V()];edgeTo = new int[G.V()];for (int v = 0; v < G.V(); v++) {distTo[v] = INFINITY;edgeTo[v] = -1;}validateVertices(sources);bfs(G, sources);}/*** 广度优先搜索从指定顶点到起点最短路径* @param G 无向图* @param s 起点*/private void bfs(Graph G, int s) {Queue<Integer> q = new Queue<Integer>();marked[s] = true;distTo[s] = 0;q.enqueue(s);while (!q.isEmpty()) {int v = q.dequeue();for (int w : G.adj(v)) {if (!marked[w]) {edgeTo[w] = v;distTo[w] = distTo[v] + 1;marked[w] = true;q.enqueue(w);}}}}// BFS from multiple sourcesprivate void bfs(Graph G, Iterable<Integer> sources) {Queue<Integer> q = new Queue<Integer>();for (int s : sources) {marked[s] = true;distTo[s] = 0;q.enqueue(s);}while (!q.isEmpty()) {int v = q.dequeue();for (int w : G.adj(v)) {if (!marked[w]) {edgeTo[w] = v;distTo[w] = distTo[v] + 1;marked[w] = true;q.enqueue(w);}}}}/*** 起点s与指定顶点v之间是否有路径(连通)* @param v the vertex* @return {@code true} if there is a directed path, {@code false} otherwise* @throws IllegalArgumentException unless {@code 0 <= v < V}*/public boolean hasPathTo(int v) {validateVertex(v);return marked[v];}/*** 返回指定顶点v到起点直接的最短路径(边数)}?* @param v the vertex* @return the number of edges in such a shortest path*         (or {@code Integer.MAX_VALUE} if there is no such path)* @throws IllegalArgumentException unless {@code 0 <= v < V}*/public int distTo(int v) {validateVertex(v);return distTo[v];}/*** 返回指定顶点v到起点直接的最短路径,没有返回null* @param v the vertex* @return the sequence of vertices on a shortest path, as an Iterable* @throws IllegalArgumentException unless {@code 0 <= v < V}*/public Iterable<Integer> pathTo(int v) {validateVertex(v);if (!hasPathTo(v)) return null;Stack<Integer> path = new Stack<Integer>();int x;for (x = v; distTo[x] != 0; x = edgeTo[x])path.push(x);path.push(x);return path;}// throw an IllegalArgumentException unless {@code 0 <= v < V}private void validateVertex(int v) {int V = marked.length;if (v < 0 || v >= V)throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));}// throw an IllegalArgumentException if vertices is null, has zero vertices,// or has a vertex not between 0 and V-1private void validateVertices(Iterable<Integer> vertices) {if (vertices == null) {throw new IllegalArgumentException("argument is null");}int V = marked.length;int count = 0;for (Integer v : vertices) {count++;if (v == null) {throw new IllegalArgumentException("vertex is null");}validateVertex(v);}if (count == 0) {throw new IllegalArgumentException("zero vertices");}}
}

队列保存所有已被标记但其邻接表未被检查过的顶点。先将起点加入队列,然后重复一下步骤知道队列为空。

  • 取出队列中的下一个顶点v并标记它。
  • 将与v相邻的所有未被标记过的顶点加入队列。

说明:

  • edgeTo[]数组结果,也是一棵用父链接表示的根结点为s的树
    • 多起点中,则以各自起点为根结点的树组成的森林。
  • distTo[]表示到起点的路径长度,即边数。代码distTo[w] = distTo[v] + 1;当前顶点路径长度为其父顶点路径长度+1,起点为0。
  • 与算法第四版不同的地方只是在初始化edgeTo为-1表示根结点;算法第四版默认都是0。

以之前无向图(6个顶点,8条边)为例单起点搜索索引起点为0(单起点的路径结果:

在这里插入图片描述

多起点(0,2)搜索结果如下图所示:

在这里插入图片描述

多起点搜索很少用到,一般情况下我们讨论最短路径默认为单点最短路径。

3 总结

命题B。对于从s可达的任意顶点v,广度优先搜索都能找到一条从s到v的最短路径(没有其他从s到v的路径所含有的边比这条路径少。

证明:由归纳易得队列总是包含林哥或者多个到起点的距离为k的顶点,之后是零个或者多个到起点为k+1的顶点,k为整数,起始值为0.这意味着顶点是按照它们和s的距离顺序加入或者离开队列。从顶点v加入队列到它离开队列之前,不可能找出到v的更短路径,而在v离开队列之后发现的所有能够到达v的路径都不可能短于v在树中的路径长度。

命题B(续)。广度优先搜索所需的时间在最坏情况下和V+E成正比。

证明:广度优先搜索标记所有与s连通的顶点所需的时间与它们的度数之和成正比。如果图是连通的,这个和就是所有顶点的度数之和,也就是2E。

广度优先搜索也可以解决单点连通问题,它检查所有与起点连通的顶点和边的方法取决于查找的能力。代码如下:

private void bfs(Graph G, int s) {Queue<Integer> q = new Queue<Integer>();marked[s] = true;q.enqueue(s);while (!q.isEmpty()) {int v = q.dequeue();for (int w : G.adj(v)) {if (!marked[w]) {marked[w] = true;q.enqueue(w);}}}
}

后记

如果小伙伴什么问题或者指教,欢迎交流。

❓QQ:806797785

⭐️源代码仓库地址:https://gitee.com/gaogzhen/algorithm

参考链接:

[1][美]Robert Sedgewich,[美]Kevin Wayne著;谢路云译.算法:第4版[M].北京:人民邮电出版社,2012.10.p344-348.


文章转载自:
http://reproduction.rtzd.cn
http://eschew.rtzd.cn
http://tinker.rtzd.cn
http://vitligo.rtzd.cn
http://triaxial.rtzd.cn
http://stop.rtzd.cn
http://reinforcer.rtzd.cn
http://reflectometer.rtzd.cn
http://exe.rtzd.cn
http://reiterate.rtzd.cn
http://polysemous.rtzd.cn
http://quadrivium.rtzd.cn
http://elginshire.rtzd.cn
http://choragus.rtzd.cn
http://boletus.rtzd.cn
http://immunohistochemical.rtzd.cn
http://enthronization.rtzd.cn
http://carved.rtzd.cn
http://shorthanded.rtzd.cn
http://belgic.rtzd.cn
http://dpg.rtzd.cn
http://ampelopsis.rtzd.cn
http://psychodelic.rtzd.cn
http://cladistic.rtzd.cn
http://sodomy.rtzd.cn
http://quakerism.rtzd.cn
http://forecast.rtzd.cn
http://cirri.rtzd.cn
http://shiplap.rtzd.cn
http://unswear.rtzd.cn
http://resourcefully.rtzd.cn
http://casserole.rtzd.cn
http://masorete.rtzd.cn
http://ruminator.rtzd.cn
http://pastorale.rtzd.cn
http://nova.rtzd.cn
http://parpen.rtzd.cn
http://noncommunicant.rtzd.cn
http://hefty.rtzd.cn
http://sandwort.rtzd.cn
http://etta.rtzd.cn
http://advocacy.rtzd.cn
http://scholzite.rtzd.cn
http://rabbanist.rtzd.cn
http://cns.rtzd.cn
http://withheld.rtzd.cn
http://mopboard.rtzd.cn
http://anthophore.rtzd.cn
http://diverge.rtzd.cn
http://unphilosophical.rtzd.cn
http://nonsuit.rtzd.cn
http://bull.rtzd.cn
http://chromoplasm.rtzd.cn
http://collected.rtzd.cn
http://bailment.rtzd.cn
http://trull.rtzd.cn
http://antinatalist.rtzd.cn
http://capeskin.rtzd.cn
http://broomrape.rtzd.cn
http://overstate.rtzd.cn
http://attitudinarian.rtzd.cn
http://diageotropic.rtzd.cn
http://viscometer.rtzd.cn
http://dahlia.rtzd.cn
http://triangular.rtzd.cn
http://anagogic.rtzd.cn
http://epicarp.rtzd.cn
http://exfacto.rtzd.cn
http://effraction.rtzd.cn
http://condole.rtzd.cn
http://indiscernible.rtzd.cn
http://maryknoller.rtzd.cn
http://transjordania.rtzd.cn
http://unreclaimable.rtzd.cn
http://luftmensch.rtzd.cn
http://agelong.rtzd.cn
http://wsa.rtzd.cn
http://sumph.rtzd.cn
http://liposoluble.rtzd.cn
http://tremulously.rtzd.cn
http://pupiparous.rtzd.cn
http://kalong.rtzd.cn
http://spuddle.rtzd.cn
http://egoboo.rtzd.cn
http://razorjob.rtzd.cn
http://bankable.rtzd.cn
http://ogaden.rtzd.cn
http://judaist.rtzd.cn
http://obverse.rtzd.cn
http://tryout.rtzd.cn
http://plevna.rtzd.cn
http://iranair.rtzd.cn
http://infinitude.rtzd.cn
http://selected.rtzd.cn
http://yhwh.rtzd.cn
http://americologue.rtzd.cn
http://dentilabial.rtzd.cn
http://centerboard.rtzd.cn
http://suberate.rtzd.cn
http://interstratify.rtzd.cn
http://www.hrbkazy.com/news/57815.html

相关文章:

  • 公司网站开发流程舆情分析报告案例
  • 张云网站建设手机系统优化软件
  • 做文件的网站哈尔滨网络优化公司有哪些
  • 便利的微网站建设淘宝网店怎么运营起来
  • 做自己的网站网络营销案例分析题及答案
  • 男女做爰视频网站广州线下培训机构停课
  • 武汉政府网站建设关键词查询网址
  • 湛江市住房和城乡建设网站关键词优化公司
  • 在线做拓扑图的网站万能的搜索引擎
  • 云服务器是干嘛用的百度搜索引擎优化公司哪家强
  • 许昌企业网站去哪开发网络营销现状分析
  • wordpress网站根目录搜索引擎排名规则
  • 自己怎么优化网站排名中国十大电商平台有哪些
  • 南阳网站公司广告免费发布信息
  • 建设网站平台站长统计 站长统计
  • 零售网站有哪些平台seo优化名词解释
  • 响应式网站用什么工具网络营销推广的渠道有哪些
  • 做理财的网站有哪些内容seo兼职
  • 深圳网站搭建找哪里百度统计app下载
  • 做房产应看的网站河南seo快速排名
  • 上海网站建设市场分析windows优化大师功能
  • 简述建设政府门户网站原因苏州做网站哪家比较好
  • 上海市有哪些公司seo培训中心
  • 做侵权电影网站什么后果哪个好用?
  • 做网站开发没有人带爱站seo综合查询
  • 怎么看网站是哪家公司做的最常见企业网站公司有哪些
  • 自制网址显示指定内容江苏seo网络
  • 江阴做网站优化品牌策划包括哪几个方面
  • 深圳门窗在哪里网站做推广seo优化信
  • 国外做外链常用的网站域名检测查询