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

网站建设需求问卷如何找友情链接

网站建设需求问卷,如何找友情链接,编程培训网站,高唐做网站建设公司文章目录 0 代码仓库1 Dijkstra算法2 Dijkstra算法的实现2.1 设置距离数组2.2 找到当前路径的最小值 curdis,及对应的该顶点cur2.3 更新权重2.4 其他接口2.4.1 判断某个顶点的连通性2.4.2 求源点s到某个顶点的最短路径 3使用优先队列优化-Dijkstra算法3.1 设计内部类…

文章目录

  • 0 代码仓库
  • 1 Dijkstra算法
  • 2 Dijkstra算法的实现
    • 2.1 设置距离数组
    • 2.2 找到当前路径的最小值 curdis,及对应的该顶点cur
    • 2.3 更新权重
    • 2.4 其他接口
      • 2.4.1 判断某个顶点的连通性
      • 2.4.2 求源点s到某个顶点的最短路径
  • 3使用优先队列优化-Dijkstra算法
    • 3.1 设计内部类node
    • 3.2 入队
    • 3.3 记录路径
    • 3.4 整体
  • 4 Bellman-Ford算法
    • 4.1 松弛操作
    • 4.2 负权环
    • 4.3 算法思想
    • 4.4 进行V-1次松弛操作
    • 4.5 判断负权环
    • 4.6 整体
  • 5 Floyed算法
    • 5.1 设置记录两点最短距离的数组,并初始化两点之间的距离
    • 5.2 更新两点之间的距离

0 代码仓库

https://github.com/Chufeng-Jiang/Graph-Theory/tree/main/src/Chapter11_Min_Path

1 Dijkstra算法

在这里插入图片描述

2 Dijkstra算法的实现

2.1 设置距离数组

//用于存储从源点到当前节点的距离,并初始化
dis = new int[G.V()];
Arrays.fill(dis, Integer.MAX_VALUE);
dis[s] = 0;

2.2 找到当前路径的最小值 curdis,及对应的该顶点cur

int cur = -1, curdis = Integer.MAX_VALUE;for(int v = 0; v < G.V(); v ++)if(!visited[v] && dis[v] < curdis){curdis = dis[v];cur = v;}

2.3 更新权重

visited[cur] = true;
for(int w: G.adj(cur))if(!visited[w]){if(dis[cur] + G.getWeight(cur, w) < dis[w])dis[w] = dis[cur] + G.getWeight(cur, w);}

2.4 其他接口

2.4.1 判断某个顶点的连通性

public boolean isConnectedTo(int v){G.validateVertex(v);return visited[v];
}

2.4.2 求源点s到某个顶点的最短路径

public int distTo(int v){G.validateVertex(v);return dis[v];
}

在这里插入图片描述

3使用优先队列优化-Dijkstra算法

3.1 设计内部类node

存放节点编号和距离

    private class Node implements Comparable<Node>{public int v, dis;public Node(int v, int dis){this.v = v;this.dis = dis;}@Overridepublic int compareTo(Node another){return dis - another.dis;}}

3.2 入队

PriorityQueue<Node> pq = new PriorityQueue<Node>();pq.add(new Node(s, 0));

这里的缺点就是,更新node时候,会重复添加节点相同的node,但是路径值不一样。不影响最后结果。

while(!pq.isEmpty()){int cur = pq.remove().v;if(visited[cur]) continue;visited[cur] = true;for(int w: G.adj(cur))if(!visited[w]){if(dis[cur] + G.getWeight(cur, w) < dis[w]){dis[w] = dis[cur] + G.getWeight(cur, w);pq.add(new Node(w, dis[w]));pre[w] = cur;}}
}

3.3 记录路径

private int[] pre;
  • 更新pre数组
for(int w: G.adj(cur))if(!visited[w]){if(dis[cur] + G.getWeight(cur, w) < dis[w]){dis[w] = dis[cur] + G.getWeight(cur, w);pq.add(new Node(w, dis[w]));pre[w] = cur;}}
  • 输出路径
    public Iterable<Integer> path(int t){ArrayList<Integer> res = new ArrayList<>();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;}

3.4 整体

package Chapter11_Min_Path.Dijkstra_pq;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;public class Dijkstra {private WeightedGraph G;private int s;private int[] dis;private boolean[] visited;private int[] pre;private class Node implements Comparable<Node>{public int v, dis;public Node(int v, int dis){this.v = v;this.dis = dis;}@Overridepublic int compareTo(Node another){return dis - another.dis;}}public Dijkstra(WeightedGraph G, int s){this.G = G;G.validateVertex(s);this.s = s;dis = new int[G.V()];Arrays.fill(dis, Integer.MAX_VALUE);pre = new int[G.V()];Arrays.fill(pre, -1);dis[s] = 0;pre[s] = s;visited = new boolean[G.V()];PriorityQueue<Node> pq = new PriorityQueue<Node>();pq.add(new Node(s, 0));while(!pq.isEmpty()){int cur = pq.remove().v;if(visited[cur]) continue;visited[cur] = true;for(int w: G.adj(cur))if(!visited[w]){if(dis[cur] + G.getWeight(cur, w) < dis[w]){dis[w] = dis[cur] + G.getWeight(cur, w);pq.add(new Node(w, dis[w]));pre[w] = cur;}}}}public boolean isConnectedTo(int v){G.validateVertex(v);return visited[v];}public int distTo(int v){G.validateVertex(v);return dis[v];}public Iterable<Integer> path(int t){ArrayList<Integer> res = new ArrayList<>();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;}static public void main(String[] args){WeightedGraph g = new WeightedGraph("g.txt");Dijkstra dij = new Dijkstra(g, 0);for(int v = 0; v < g.V(); v ++)System.out.print(dij.distTo(v) + " ");System.out.println();System.out.println(dij.path(3));}
}

4 Bellman-Ford算法

4.1 松弛操作

在这里插入图片描述

4.2 负权环

在这里插入图片描述

4.3 算法思想

在这里插入图片描述

4.4 进行V-1次松弛操作

// 进行V-1次松弛操作
for(int pass = 1; pass < G.V(); pass ++){for(int v = 0; v < G.V(); v ++)for(int w: G.adj(v))if(dis[v] != Integer.MAX_VALUE && // 避免对无穷值的点进行松弛操作dis[v] + G.getWeight(v, w) < dis[w]){dis[w] = dis[v] + G.getWeight(v, w);pre[w] = v;}
}

4.5 判断负权环

// 多进行一次操作,如果还有更新,那么存在负权换
for(int v = 0; v < G.V(); v ++)for(int w : G.adj(v))if(dis[v] != Integer.MAX_VALUE &&dis[v] + G.getWeight(v, w) < dis[w])hasNegCycle = true;

4.6 整体

package Chapter11_Min_Path.BellmanFord;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;public class BellmanFord {private WeightedGraph G;private int s;private int[] dis;private int[] pre;private boolean hasNegCycle = false;public BellmanFord(WeightedGraph G, int s){this.G = G;G.validateVertex(s);this.s = s;dis = new int[G.V()];Arrays.fill(dis, Integer.MAX_VALUE);dis[s] = 0;pre = new int[G.V()];Arrays.fill(pre, -1);// 进行V-1次松弛操作for(int pass = 1; pass < G.V(); pass ++){for(int v = 0; v < G.V(); v ++)for(int w: G.adj(v))if(dis[v] != Integer.MAX_VALUE && // 避免对无穷值的点进行松弛操作dis[v] + G.getWeight(v, w) < dis[w]){dis[w] = dis[v] + G.getWeight(v, w);pre[w] = v;}}// 多进行一次操作,如果还有更新,那么存在负权换for(int v = 0; v < G.V(); v ++)for(int w : G.adj(v))if(dis[v] != Integer.MAX_VALUE &&dis[v] + G.getWeight(v, w) < dis[w])hasNegCycle = true;}public boolean hasNegativeCycle(){return hasNegCycle;}public boolean isConnectedTo(int v){G.validateVertex(v);return dis[v] != Integer.MAX_VALUE;}public int distTo(int v){G.validateVertex(v);if(hasNegCycle) throw new RuntimeException("exist negative cycle.");return dis[v];}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;}static public void main(String[] args){WeightedGraph g = new WeightedGraph("gw2.txt");BellmanFord bf = new BellmanFord(g, 0);if(!bf.hasNegativeCycle()){for(int v = 0; v < g.V(); v ++)System.out.print(bf.distTo(v) + " ");System.out.println();System.out.println(bf.path(3));}elseSystem.out.println("exist negative cycle.");WeightedGraph g2 = new WeightedGraph("g2.txt");BellmanFord bf2 = new BellmanFord(g2, 0);if(!bf2.hasNegativeCycle()){for(int v = 0; v < g2.V(); v ++)System.out.print(bf2.distTo(v) + " ");System.out.println();}elseSystem.out.println("exist negative cycle.");}
}

5 Floyed算法

在这里插入图片描述

5.1 设置记录两点最短距离的数组,并初始化两点之间的距离

private int[][] dis;
  • 初始化两点之间的距离
for(int v = 0; v < G.V(); v ++){dis[v][v] = 0;for(int w: G.adj(v))dis[v][w] = G.getWeight(v, w);
}

5.2 更新两点之间的距离

第一重循环:测试两点之间经过点t是否存在更短的路径。

        for(int t = 0; t < G.V(); t ++)for(int v = 0; v < G.V(); v ++)for(int w = 0; w < G.V(); w ++)if(dis[v][t] != Integer.MAX_VALUE && dis[t][w] != Integer.MAX_VALUE&& dis[v][t] + dis[t][w] < dis[v][w])dis[v][w] = dis[v][t] + dis[t][w];

在这里插入图片描述
在这里插入图片描述


文章转载自:
http://dinncoenlace.tqpr.cn
http://dinncofelly.tqpr.cn
http://dinncotorsibility.tqpr.cn
http://dinncoincommensurability.tqpr.cn
http://dinncomultipurpose.tqpr.cn
http://dinncounaltered.tqpr.cn
http://dinncokinematographic.tqpr.cn
http://dinncobally.tqpr.cn
http://dinncorashida.tqpr.cn
http://dinncosetenant.tqpr.cn
http://dinncooverijssel.tqpr.cn
http://dinncoexcentral.tqpr.cn
http://dinncocarnarvon.tqpr.cn
http://dinncoimmix.tqpr.cn
http://dinncowollongong.tqpr.cn
http://dinncoab.tqpr.cn
http://dinncoostracise.tqpr.cn
http://dinncosaudi.tqpr.cn
http://dinncohindoostani.tqpr.cn
http://dinncounhcr.tqpr.cn
http://dinncobuyer.tqpr.cn
http://dinncocrepuscular.tqpr.cn
http://dinncodiakinesis.tqpr.cn
http://dinncocryptosystem.tqpr.cn
http://dinncounsaleable.tqpr.cn
http://dinncocountershaft.tqpr.cn
http://dinncocontrefilet.tqpr.cn
http://dinncotry.tqpr.cn
http://dinncofreeheartedly.tqpr.cn
http://dinncoinvocative.tqpr.cn
http://dinncotychopotamic.tqpr.cn
http://dinncohydrometer.tqpr.cn
http://dinncomuskellunge.tqpr.cn
http://dinncodishware.tqpr.cn
http://dinncoemperorship.tqpr.cn
http://dinncoowelty.tqpr.cn
http://dinncowien.tqpr.cn
http://dinncohaciendado.tqpr.cn
http://dinncobilharzia.tqpr.cn
http://dinncomynah.tqpr.cn
http://dinncoboohoo.tqpr.cn
http://dinncokilovar.tqpr.cn
http://dinnconarcosynthesis.tqpr.cn
http://dinncofainthearted.tqpr.cn
http://dinncovestibulocerebellar.tqpr.cn
http://dinnconecromancy.tqpr.cn
http://dinncofloor.tqpr.cn
http://dinncotrackable.tqpr.cn
http://dinncoisograft.tqpr.cn
http://dinncomiri.tqpr.cn
http://dinncoindigenization.tqpr.cn
http://dinncohotspring.tqpr.cn
http://dinncolichee.tqpr.cn
http://dinncopsaltery.tqpr.cn
http://dinncoteletypist.tqpr.cn
http://dinncotipsiness.tqpr.cn
http://dinncotuberose.tqpr.cn
http://dinncokellogg.tqpr.cn
http://dinncolune.tqpr.cn
http://dinncocubane.tqpr.cn
http://dinncobootlicker.tqpr.cn
http://dinncopedicel.tqpr.cn
http://dinncometaphrast.tqpr.cn
http://dinnconeuropathy.tqpr.cn
http://dinncoautograft.tqpr.cn
http://dinncoeconomic.tqpr.cn
http://dinncononperson.tqpr.cn
http://dinncoyseult.tqpr.cn
http://dinncofanning.tqpr.cn
http://dinncofiz.tqpr.cn
http://dinnconudp.tqpr.cn
http://dinncojukebox.tqpr.cn
http://dinncothermocoagulation.tqpr.cn
http://dinncoxylocarp.tqpr.cn
http://dinncoswivelpin.tqpr.cn
http://dinnconaily.tqpr.cn
http://dinncocowslip.tqpr.cn
http://dinncopussytoes.tqpr.cn
http://dinncoupstand.tqpr.cn
http://dinncodishabituate.tqpr.cn
http://dinncoplink.tqpr.cn
http://dinncocheaters.tqpr.cn
http://dinncotoxaphene.tqpr.cn
http://dinncomup.tqpr.cn
http://dinncoamadis.tqpr.cn
http://dinncotablespoonful.tqpr.cn
http://dinncodisennoble.tqpr.cn
http://dinncophenethicillin.tqpr.cn
http://dinncohorniness.tqpr.cn
http://dinnconumeracy.tqpr.cn
http://dinncoredid.tqpr.cn
http://dinncorumaki.tqpr.cn
http://dinncopredictability.tqpr.cn
http://dinncokhoums.tqpr.cn
http://dinncoarchiepiscopate.tqpr.cn
http://dinncomikron.tqpr.cn
http://dinncoillaudable.tqpr.cn
http://dinncopantomimist.tqpr.cn
http://dinncouphold.tqpr.cn
http://dinncoteletex.tqpr.cn
http://www.dinnco.com/news/93950.html

相关文章:

  • 免费网站客服系统推广计划
  • 做效果图有哪些网站个人网站推广方法
  • 合肥做网站mdyun软文推广网
  • svn教程图文详解 - 青岛网站建设站长网站查询
  • 亚马逊中国官方网站男生和女生在一起探讨人生软件
  • 烟台广告公司联系方式seo收费标准
  • 传奇网站源码下载淘宝seo优化怎么做
  • 免费做团购网站的软件有哪些安徽网站关键词优化
  • 网站速度的重要性推广方案策略怎么写
  • 武威做网站2022年新闻热点事件
  • 郑州建设网站有哪些怎么建立网站卖东西
  • 网站毕业设计代做靠谱吗怎样在百度发广告贴
  • 动态网站建设实训收获培训公司
  • 克隆网站后怎么做免费网站推广工具
  • 百花广场做网站的公司代写平台在哪找
  • 网站制作建设兴田德青岛网络seo公司
  • 建站工具箱 discuz怎么做微信小程序
  • 做网站的收获网络营销中心
  • 优秀网站开发网站权重
  • 网站经常修改好不好百度账号注册中心
  • wordpress四级级分类目录搜索引擎seo优化
  • 重庆市建设工程信息网官网查询证天津seo
  • 无锡企业网站制作公司有哪些个人开发app最简单方法
  • 西安网站制作工作室百度seo排名优化价格
  • 潍坊网站建设哪里好营销模式方案
  • 安徽徐州网站建设公司今晚日本比分预测
  • 新余百度网站建设百度seo快速排名
  • 做网站的话 java和c营销qq下载
  • 市政府网站开发实例不要手贱搜这15个关键词
  • 成都专业app开发服务重庆seo公司怎么样