当前位置:网站首页>经典Dijkstra与最长路
经典Dijkstra与最长路
2022-07-28 14:15:00 【追寻远方的人】
前言
有一题求最短路 和最长路的题,由于基础不够扎实,天真地以为改一下松弛操作、重载一下优先队列即可。事实上,经典Dijkstra并不能求最长路。
经典Dijkstra不能求最长路原理
在经典Dijkstra中,根据贪心思想,我们每次使用一个距离源点最短的点A来松弛其他点,并且保证了这个操作每个点只会进行一次,也即每个点有且仅有一次作为点A(除了距离最远的点,因为最后一点被松弛完整个算法就结束了)。
众所周知,迪杰斯特拉的使用条件是图中不存在负边。这是因为在不存在负边权的图中,最短路有子结构,子路径最短能保证总路径最短。根据这一性质,也有了一个结论,上面提到的用来松弛其他点的那个点A,距离源点的距离已经是最短的(这也是Dijsktra每个点只用来松弛一次的前提)。然而,以下两种情况不存在子结构:
- 存在负边权,例如

- 求最长路,例如

在不存在子结构的图中,一个点用来松弛其他点的点,未必已经是最优解。所以,我们可以使用允许多次松弛的算法(SPFA、经典Dijsktra改成允许一个点松弛其他点多次,事实上就应该称为SPFA了)来求最长路。
总结
- 正权边图中求最长路可使用SPFA
- 经典Dijsktra可在全负权边图中跑最长路、全正权边图中跑最短路
- Dijsktra+堆优化的时间复杂度为O(ElogV)、SPFA(Bellman Ford)+堆优化的时间复杂度是O(VE)。
边栏推荐
猜你喜欢

Wonderful frog -- how simple can it be to abandon the float and use the navigation bar set by the elastic box

Foundation of knowledge atlas (II) - knowledge expression system of knowledge atlas

边缘技术和小程序容器在智能家居中的应用

Solution to the problem of high collapse caused by float (including all methods)

Instructions for common symbols in kotlin

The second 1024, come on!

Machine learning related concepts
![SQL error [1810] [22008]: ora-01810: format code occurs twice](/img/3b/4cbc0efe23f6f71163a115cd098ea9.png)
SQL error [1810] [22008]: ora-01810: format code occurs twice

Simple data analysis using Weka and excel
Node.js+express realizes the operation of MySQL database
随机推荐
Mysql使用left join连表查询时,因连接条件未加索引导致查询很慢
Using keras to visualize the network model, failed to import pydot appears
Establishment and traversal of binary tree (implemented in C language)
Install biological sequence de redundancy software CD hit
2021-09-02
Install pytorch geometric on colab, and libcudart.so.10.2 appears when importing the package
Partition and index of Oracle Database
Robot mathematics foundation 3D space position representation space position
QT environment cannot run error set
URP下使用GL进行绘制方法
UTF-8、UTF-16 和 UTF-32 字符编码之间的区别?[图文详解]
8、 C scope rules
软件开发三大痛点!小程序容器如何解决?
Talk about low code / zero code tools
Machine learning related concepts
RepVGG论文详解以及使用Pytorch进行模型复现
苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?
即刻体验 | 借助 CTS-D 进一步提升应用设备兼容性
Deploy flask on Alibaba cloud server
5、 C language judgment statement