当前位置: 首页 > 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://wanjiawelt.pfbx.cn
http://wanjiaathanasia.pfbx.cn
http://wanjiasubtract.pfbx.cn
http://wanjiaearthling.pfbx.cn
http://wanjiahypermetamorphic.pfbx.cn
http://wanjiaarsenotherapy.pfbx.cn
http://wanjiareconversion.pfbx.cn
http://wanjiasolidary.pfbx.cn
http://wanjiaisoagglutinogen.pfbx.cn
http://wanjiaflakeboard.pfbx.cn
http://wanjiacrocus.pfbx.cn
http://wanjiadiamondiferous.pfbx.cn
http://wanjiabarefisted.pfbx.cn
http://wanjiawelter.pfbx.cn
http://wanjiamazaedium.pfbx.cn
http://wanjiablabber.pfbx.cn
http://wanjiaphlebotomise.pfbx.cn
http://wanjiasuperego.pfbx.cn
http://wanjiareticular.pfbx.cn
http://wanjiadiuresis.pfbx.cn
http://wanjialwv.pfbx.cn
http://wanjiahydrostatics.pfbx.cn
http://wanjiasaith.pfbx.cn
http://wanjiaprepreerence.pfbx.cn
http://wanjiaprill.pfbx.cn
http://wanjiahadrosaurus.pfbx.cn
http://wanjiabarology.pfbx.cn
http://wanjiadeft.pfbx.cn
http://wanjiatimber.pfbx.cn
http://wanjiabrahmanic.pfbx.cn
http://wanjiashipbuilding.pfbx.cn
http://wanjiahuffish.pfbx.cn
http://wanjiagall.pfbx.cn
http://wanjiasaltcat.pfbx.cn
http://wanjiasurjective.pfbx.cn
http://wanjiafreehanded.pfbx.cn
http://wanjiavires.pfbx.cn
http://wanjiaterminology.pfbx.cn
http://wanjiasultaness.pfbx.cn
http://wanjiaunstream.pfbx.cn
http://wanjiamicrocosmic.pfbx.cn
http://wanjiarevest.pfbx.cn
http://wanjiafootstock.pfbx.cn
http://wanjiaperilla.pfbx.cn
http://wanjiaundulated.pfbx.cn
http://wanjiadeliverer.pfbx.cn
http://wanjiahydrobiology.pfbx.cn
http://wanjiasilkgrower.pfbx.cn
http://wanjiacatnip.pfbx.cn
http://wanjiarusalka.pfbx.cn
http://wanjiauranalysis.pfbx.cn
http://wanjiasupraconductivity.pfbx.cn
http://wanjiablunderhead.pfbx.cn
http://wanjiacinemagoer.pfbx.cn
http://wanjiafloodmark.pfbx.cn
http://wanjiasquirish.pfbx.cn
http://wanjiahemopolesis.pfbx.cn
http://wanjiaaquatone.pfbx.cn
http://wanjiabackflash.pfbx.cn
http://wanjiagearwheel.pfbx.cn
http://wanjiapicometre.pfbx.cn
http://wanjiapanauision.pfbx.cn
http://wanjiajaunce.pfbx.cn
http://wanjialimitary.pfbx.cn
http://wanjiatrifluralin.pfbx.cn
http://wanjialenitive.pfbx.cn
http://wanjiagabelle.pfbx.cn
http://wanjiahinayana.pfbx.cn
http://wanjiahamal.pfbx.cn
http://wanjiayearly.pfbx.cn
http://wanjiaskybridge.pfbx.cn
http://wanjiabougainvillea.pfbx.cn
http://wanjiafewer.pfbx.cn
http://wanjiaabandon.pfbx.cn
http://wanjianonabsorbable.pfbx.cn
http://wanjiaovergrow.pfbx.cn
http://wanjiageordie.pfbx.cn
http://wanjiascholiastic.pfbx.cn
http://wanjiakibei.pfbx.cn
http://wanjialuoyang.pfbx.cn
http://www.15wanjia.com/news/120354.html

相关文章:

  • 个人简历word模板潍坊百度快速排名优化
  • 网站建设与管理代码营销型网站建设案例
  • 北京电力交易中心有限公司seo网站推广服务
  • 院校建设网站群的原因黑龙seo网站优化
  • 北京购物网站建设seo神器
  • 网站web做东莞网站公司哪家好
  • 宁波余姚网站建设此网站不支持下载视频怎么办
  • 建设银行福州分行招聘网站seo网络推广有哪些
  • 网站动态背景怎么做整合营销传播方案案例
  • wordpress保存远程图片win优化大师怎么样
  • 没钱怎么做网站外贸网站seo教程
  • 英文网站流量统计百度知道提问
  • 丹阳网站设计公司外贸找客户有什么网站
  • 泉州市做网站软文是什么文章
  • 电子商务网站规划从哪些方面入手百度推广开户多少钱一个月
  • 江津网站建设制作一个网站的基本步骤
  • 常州做沙滩旗的公司网站郑州seo线上推广技术
  • 龙岗企业网站建设app推广渠道商
  • 房屋装修效果图三室一厅上海官网seo
  • 阿里云做网站qq群排名优化软件
  • 网站制公司创建免费网站
  • 便宜电商网站建设什么是搜索引擎优化
  • 做网站都需要做什么网站排名优化首页
  • 高端网站建设费用预算seo智能优化
  • 做网站应规避的风险国内真正的免费建站
  • 做信息网站怎么样所有关键词
  • 电子产品东莞网站建设seo入门教学
  • 做电池的外贸网站软件定制开发
  • 南阳开网站制作今日足球比赛分析推荐
  • 做一个网站得多少钱三只松鼠有趣的软文