图解算法学习笔记(七):狄克斯特拉算法

目录

1)使用狄克斯特拉算法

2)术语

3)实现

4)小结


本章内容;

  •        介绍加权图,提高或降低某些边的权重;
  •        介绍狄克斯特拉算法,找出加权图中前往X的最短路径;
  •        介绍图中的环,它导致狄克斯特拉算法不管用;

在上一篇博客中,我们找到了从A到B的路径,这是最短路径,只有三段,但不一定是最短路径。

广度优先搜索可以找出段数最少的路径,但如果要找出最快的路径,可使用狄克斯特拉算法。

1)使用狄克斯特拉算法

还是来看一个例子,如何对下面的图使用这种算法。图中每个数字表示的是时间,单位是分钟。如果使用广度优先搜索算法,将得到下面这条段数最少的路径。

狄克斯特拉算法包括4个步骤:

  1. 找出“最便宜”的节点,即可在最短时间内到达的节点。
  2. 更新该节点的邻居的开销,其含义稍后介绍。
  3. 重复这个过程,直到对图中的每个几点都这样做了。
  4. 计算最短路径。

第一步:找出最便宜的节点,你站在起点,不知道该前往节点A还是节点B。前往节点A需要6分钟,前往节点B需要2分钟,由于还不知道前往终点需要多长时间,因此假设为无穷大。

    

第二步:计算经节点B前往其各个邻居所需的时间。

对于节点B的邻居,如果找到前往它的更短路径,就更新其开销。在这里,我们找到了:

前往节点A的更短路径(时间从6分钟缩短到5分钟)。

前往终点的更短路径(时间从无穷大缩短到7分钟)。

第三步:重复!

现在更新节点A的所有邻居的开销。这时前往终点的时间缩短到6分钟。

2)术语

狄克斯特拉算法用于每条边都有关联数字的图,这些数字称为权重(weight)。

带权重的图称为加权图,不带权重的图称为非加权重。

要计算非加权图中的最短路径,可使用广度优先搜索,要计算加权图中的最短路径,可使用狄克斯特拉算法。这里需要指出的时,狄克斯特拉算法只适用与有向无环图

3)实现

下面来看看如何用代码来实现狄克斯特拉算法。要解决这个问题,需要用到散列表。

随着算法的进行,不断地更新散列表COSTS和PARENTS。Python实现代码如下:

# the graph
graph = {}
graph["start"] = {}
graph["start"]["a"] = 6
graph["start"]["b"] = 2graph["a"] = {}
graph["a"]["fin"] = 1graph["b"] = {}
graph["b"]["a"] = 3
graph["b"]["fin"] = 5graph["fin"] = {}# the costs table
infinity = float("inf")
costs = {}
costs["a"] = 6
costs["b"] = 2
costs["fin"] = infinity# the parents table
parents = {}
parents["a"] = "start"
parents["b"] = "start"
parents["fin"] = Noneprocessed = []def find_lowest_cost_node(costs):lowest_cost = float("inf")lowest_cost_node = None# Go through each node.for node in costs:cost = costs[node]# If it's the lowest cost so far and hasn't been processed yet...if cost < lowest_cost and node not in processed:# ... set it as the new lowest-cost node.lowest_cost = costlowest_cost_node = nodereturn lowest_cost_node# Find the lowest-cost node that you haven't processed yet.
node = find_lowest_cost_node(costs)
# If you've processed all the nodes, this while loop is done.
while node is not None:cost = costs[node]# Go through all the neighbors of this node.neighbors = graph[node]for n in neighbors.keys():new_cost = cost + neighbors[n]# If it's cheaper to get to this neighbor by going through this node...if costs[n] > new_cost:# ... update the cost for this node.costs[n] = new_cost# This node becomes the new parent for this neighbor.parents[n] = node# Mark the node as processed.processed.append(node)# Find the next node to process, and loop.node = find_lowest_cost_node(costs)print "Cost from the start to each node:"
print costs

4)小结

  1. 广度优先搜索用于在非加权图中查找最短路径。
  2. 狄克斯特拉算法用于在加权图中查找最短路径。
  3. 仅当权重为正时狄克斯特拉算法才管用。
  4. 如果图中包含负权边,请使用贝尔曼---福德算法。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440165.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 5477】A Sweet Journey(思维,水题)

题干&#xff1a; Master Di plans to take his girlfriend for a travel by bike. Their journey, which can be seen as a line segment of length L, is a road of swamps and flats. In the swamp, it takes A point strengths per meter for Master Di to ride; In the f…

图解算法学习笔记(八):贪婪算法

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;集合覆盖问题 &#xff08;3&#xff09;NP完全问题 &#xff08;4&#xff09;小结 本章内容&#xff1a; 学习如何处理没有快速算法的问题&#xff08;NP完全问题&#xff09;。学习近似算法&#xff…

图解算法学习笔记(九):动态规划

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;最长公共子串 &#xff08;3&#xff09;小结 本章内容&#xff1a; 学习动态规划&#xff0c;它将问题分成小问题&#xff0c;并先着手解决这些小问题。学习如何设计问题的动态规划解决方案。 &#xff…

Java(win10安装jdk,第一个hello world)

Java 第一步 &#xff1a;安装jdk 推荐默认安装。&#xff08;安装到C盘&#xff09;第二步 &#xff1a;配置jdk环境 JAVA_HOME C:\Program Files\Java\jdk1.8.0_191 JDK的安装路径 Path&#xff1a; C:\Program Files\Java\jdk1.8.0_191\bin JDK下bin目录的路径 &#xf…

#pragma 详解

#pragma 求助编辑 pragma - 必应词典美[prɡmə]英[prɡmə]n.〔计〕杂注网络编译指示&#xff1b;显示编译指示&#xff1b;特殊指令 百科名片 在所有的预处理指令中&#xff0c;#Pragma 指令可能是最复杂的了&#xff0c;它的作用是设定编译器的状态或者是指示编译器完成一些…

1.绪论

目录 &#xff08;1&#xff09;C语言传值与传地址变量 &#xff08;2&#xff09;算法效率的度量 &#xff08;3&#xff09;基本操作 &#xff08;4&#xff09;主函数 主要由实现基本操作和算法的程序构成。这些程序有6类&#xff1a; 数据存储结构&#xff0c;文件名第…

(ECC)椭圆曲线加密算法原理和C++实现源码

目录 &#xff08;1&#xff09;ECC加密原理&#xff1a; &#xff08;2&#xff09;编译生成LibTommath静态库 &#xff08;3&#xff09;ECC源码 今天介绍一下利用LibTommath数学库实现椭圆曲线加密算法的原理和源码。 &#xff08;1&#xff09;ECC加密原理&#xff1a;…

【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题)

题干&#xff1a; We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product VV, its elements being called edges. Then G(…

机器学习笔记(1):Introduction

目录 1&#xff09;welcome 2&#xff09;What is Machine Learning 3&#xff09;Supervised Learning 4&#xff09;Unsupervised Learning 1&#xff09;welcome 第一个视频主要介绍了机器学习目前的案例&#xff0c;主要有&#xff1a;数据库挖掘、医疗记录、生物工程…

【POJ - 3352】Road Construction(Tarjan,边双连通分量)

题干&#xff1a; Its almost summer time, and that means that its almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads…

机器学习简易入门-附推荐学习资料

目录 &#xff08;1&#xff09;机器学习正规学习路线 &#xff08;2&#xff09;机器学习快速入门 &#xff08;3&#xff09;总结 感谢黄海广博士的分享 原创&#xff1a; 机器学习初学者 机器学习初学者 今天 机器学习如何入门&#xff1f;目前没有明确的答案。本站面向…

C++11中的std::function

原文地址&#xff1a;http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码&#xff1a; std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::function<void(EventKeyboard::KeyCode, Event*)> onKeyReleased; 这两…

【HDU - 3394】Railway(点双连通分量,Tarjan算法,思维tricks)

题干&#xff1a; There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one …

飞机大战(简易版)

一、游戏分析 飞机大战中的主要“角色”有&#xff1a; 1.英雄 2.敌方飞机 3.英雄发射的子弹 我们需要控制的有&#xff1a; 1.绘制屏幕内的角色 2.控制角色的逻辑&#xff0c;比如&#xff1a;敌方飞机与我方飞机的碰撞检测&#xff0c;我方飞机发射的子弹与敌方飞机之间的碰撞…

在Ubuntu上安装Keras深度学习框架

目录 1&#xff09;安装pip 2&#xff09;安装Python科学套件 3&#xff09;安装TensorFlow 4&#xff09;安装keras 5&#xff09;安装Jupyter Notebook 6&#xff09;运行Keras 本文介绍如何在Ubuntu上安装Keras深度学习框架。 1&#xff09;安装pip 安装pip包&#…

【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)

题干&#xff1a; Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still…

机器学习笔记(2):单变量线性回归

目录 1&#xff09;Model representation 2&#xff09;Cost function 3&#xff09;Cost function intuition 1 4&#xff09;Cost function intuition2 5&#xff09;Gradient descent 6&#xff09;Gradient descent intuition 7&#xff09;Gradient descent for li…

安装VMware tools

点击“虚拟机” 安装VMware tools提取图中文件到“下载” 提取登入root 进入 cd 下载/vmware-tools-distrib 执行 ./vmware-install-pl 输入yes或者点击“enter”出现图中&#xff0c;即为成功安装

Keras入门实战(1):MNIST手写数字分类

目录 1)首先我们加载Keras中的数据集 2&#xff09;网络架构 3&#xff09;选择编译(compile参数) 4&#xff09;准备图像数据 5) 训练模型 6&#xff09;测试数据 前面的博客中已经介绍了如何在Ubuntu下安装Keras深度学习框架。 现在我们使用 Keras 库来学习手写数字分…

【BZOJ - 2574】[Poi1999] Store-Keeper(点双连通分量,求割点,记忆化bfs)

题干&#xff1a; 有一个仓库被分成n*m 个矩形区域&#xff0c;如果两个区域有一条公共边&#xff0c;则被认为这两个区域相邻。包裹都放在一个区域中&#xff0c;剩余的区域或者空闲或者被集装箱占有&#xff0c;这是因为集装箱太重&#xff0c;仓库管理员不能将集装箱搬走。…