广度优先算法之狄克斯特拉算法

package cn.wizzer.common.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class A {// the graphprivate static Map<String, Map<String, Double>> graph = new HashMap<>();private static List<String> processed = new ArrayList<>();private static String findLowestCostNode(Map<String, Double> costs) {Double lowestCost = Double.POSITIVE_INFINITY;String lowestCostNode = null;// Go through each nodefor (Map.Entry<String, Double> node : costs.entrySet()) {Double cost = node.getValue();// If it's the lowest cost so far and hasn't been processed yet...if (cost < lowestCost && !processed.contains(node.getKey())) {// ... set it as the new lowest-cost node.lowestCost = cost;lowestCostNode = node.getKey();}}return lowestCostNode;}public static void main(String[] args) {graph.put("start", new HashMap<>());graph.get("start").put("a", 5.0);graph.get("start").put("b", 2.0);graph.put("a", new HashMap<>());graph.get("a").put("fin", 1.0);//增加c点graph.get("a").put("c", 3.0);graph.put("b", new HashMap<>());graph.get("b").put("a", 3.0);graph.get("b").put("c", 5.0);//graph.get("b").put("fin", 5.0);graph.put("c", new HashMap<>());graph.get("c").put("fin", 4.0);// graph.get("b").put("c", 5.0);graph.put("fin", new HashMap<>());// The costs tableMap<String, Double> costs = new HashMap<>();costs.put("a", 5.0);costs.put("b", 2.0);costs.put("c", Double.POSITIVE_INFINITY);costs.put("fin", Double.POSITIVE_INFINITY);// the parents tableMap<String, String> parents = new HashMap<>();parents.put("a", "start");parents.put("b", "start");parents.put("fin", null);String node = findLowestCostNode(costs);while (node != null) {Double cost = costs.get(node);// Go through all the neighbors of this nodeMap<String, Double> neighbors = graph.get(node);for (String n : neighbors.keySet()) {double newCost = cost + neighbors.get(n);// If it's cheaper to get to this neighbor by going through this nodeif (newCost<costs.get(n)  ) {// ... update the cost for this nodecosts.put(n, newCost);// This node becomes the new parent for this neighbor.parents.put(n, node);}}// Mark the node as processedprocessed.add(node);// Find the next node to process, and loopnode = findLowestCostNode(costs);}System.out.println("Cost from the start to each node:");System.out.println(costs); // { a: 5, b: 2, fin: 6 }System.out.println(parents); // { a: 5, b: 2, fin: 6 }}
}

广度优先算法之狄克斯特拉算法相关推荐

  1. Python查找算法之狄克斯特拉算法

    目录 简介 加权图 非加权图 思路 实例 代码步骤 代码示例 运行结果 简介 狄克斯特拉算法解决了耗时最短(总权重最小)问题狄克斯特拉算法适用于加权图,并且图为有向无环图(DAG),而且权重不能为负数 ...

  2. 算法图解-狄克斯特拉算法

    本章内容: 加权图-提高或者降低某些边的权重 狄克斯特拉算法,能找出加权图中前往x的最短路径 图中的环,它导致狄克斯特拉算不管用 7.1狄克斯特拉算法 4个步骤: 找出最便宜的节点,即最短时间内前往的 ...

  3. 算法之狄克斯特拉算法

    [算法定义] 是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题.迪克斯特拉算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. [算法图示] [程序设计] /****** ...

  4. 算法之狄克斯特拉算法 --《图解算法》

    2019你好!好好生活,好好工作! 狄克斯特拉算法 狄克斯特拉算法(Dijkstra )用于计算出不存在非负权重的情况下,起点到各个节点的最短距离 可用于解决2类问题: 从A出发是否存在到达B的路径: ...

  5. 算法(四):图解狄克斯特拉算法

    算法简介 狄克斯特拉算法(Dijkstra )用于计算出不存在非负权重的情况下,起点到各个节点的最短距离 可用于解决2类问题: 从A出发是否存在到达B的路径: 从A出发到达B的最短路径(时间最少.或者 ...

  6. 《算法图解》——狄克斯特拉算法

    前面文章提到,找出段数最少的路径,使用广度优先搜索. 现在要找出最快的路径,(花费最少)使用狄克斯特拉算法. 狄克斯特拉算法包含的四个步骤: (1)找出最便宜的节点,即可在最短时间内前往的节点. (2 ...

  7. 算法学习之狄克斯特拉算法

    加权图 在了解狄克斯特拉算法之前,先介绍一下加权图. 如图,假设你要从起点出发到达终点,如果只考虑换乘少,即最短路径.那么可以使用广度优先搜索算法,该算法我之前简单的写过,链接点这里.但是,现在你要找 ...

  8. Python图算法之狄克斯特拉算法

    可用于类似公交线路用时最短的案例. 图算法之狄克斯特拉算法(Dijkstra's algorithm),包含4个步骤: (1) 找出"最便宜"的节点(权重最小),即可在最短时间内前 ...

  9. 算法详解之狄克斯特拉算法

    上一篇文章,我们了解了广度优先搜索算法(BFS),BFS主要用来解决图的可达路径验证和最小路径问题,即从一个顶点A到另一个顶点B,是否有可达路径,如果有那么求出其到达的最少步骤.那么这里的最短路径就如 ...

最新文章

  1. 从0到1,Airbnb的深度学习实践经验总结
  2. DQL、DML、DDL、DCL的概念
  3. Nature Cancer | 发现非肿瘤药物的抗癌潜力
  4. boost::python模块实现使用任意 Python 序列创建 ndarray 的示例
  5. 《Linux4.0设备驱动开发详解》笔记--第三章:Linux下的C编程特点
  6. POJ 2456 - Aggressive cows(二分)
  7. jquery实用方法
  8. sql server分页_SQL Server中的分页简介
  9. 画质与性能双重加持,Unity超越游戏的“炫技”
  10. 机器学习练习----神经网络的标准BP算法(误差逆传播算法)
  11. SVG线条动画实现蚂蚁线
  12. 什么是HyperText Transfer Protocol 超文本传输协议
  13. 如何解决DNS解析错误故障
  14. win10网络适配器不见了_恢复消失的win10网络适配器的方法
  15. STM32:利用VM8978和I2S实现录音的频率分析
  16. Android 一个简单手机响铃功能实现
  17. 项目中的用户鉴权是如何实现的?
  18. Mysql之using用法
  19. 【LeeCode】赛题02:Python解答大衍数列题目
  20. MATLAB——画图(经典)

热门文章

  1. Android Framework 电源子系统(04)核心方法updatePowerStateLocked分析-2 循环处理  更新显示设备状态
  2. 88steamCSGO即开即取回的开箱网站,CSGO皮肤交易平台
  3. FPGA—多路选择器(简单逻辑组合电路)
  4. 在vue项目中使用高德地图
  5. 穿山甲别于传统广告联盟,造势创建新角色
  6. 科学计算基础软件包Numpy学习 02
  7. TSINGSEE青犀视频构建pion webrtc运行broadcast示例的步骤
  8. oracle缓存文件,oracle的缓存
  9. python 处理snp的vcf文件,统计snp在基因的intron、exon还是上游、下游还是不在基因及基因附近
  10. linux常用命令(轻松入门linux)