当前位置:网站首页>经典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)。
边栏推荐
- Keras reported an error using tensorboard: cannot stop profiling
- Mlx90640 infrared thermal imager sensor module development notes (VIII)
- 35道MySQL面试必问题图解,小白都能看懂
- 7、 Detailed explanation of C language function definition
- QT qbuttongroup realizes single selection and multiple selection
- Solve blast database error: error pre fetching sequence data
- Shell command
- PS how to crop photos
- Application of edge technology and applet container in smart home
- Image steganography method
猜你喜欢

使用cpolar发布树莓派网页(apache2的安装测试)

Creating, deleting and viewing Anaconda virtual environment

Bcompare key expired or bcompare license key revoked

苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?

Machine learning related concepts

Mlx90640 infrared thermal imager sensor module development notes (VIII)

ssh服务

Establishment and traversal of binary tree (implemented in C language)

22、 TF coordinate transformation (II): static coordinate transformation

Chapter 3 stack, queue and array
随机推荐
URP下使用GL进行绘制方法
Crawler: from entry to imprisonment (II) -- Web collector
The second pre class exercise
iframe 标签
Shader顶点着色器修改顶点高度的一个思路
Mysql易错知识点整理(待更新)
Keras reported an error using tensorboard: cannot stop profiling
JS learning notes 24-28: end
Compose learning notes 1-compose, state, flow, remember
Compose learning notes 2 - launchedeffect, status and status management
C callback function, interface function pointer as function parameter, function pointer as structure member
buuctf_ php
SQL labs detailed problem solving process (less1-less10)
CCSP国际注册云安全专家在中国设置考场
9、 C array explanation
8、 C scope rules
企业微信客服链接,企业微信客服聊天
云上安全主要面临的威胁有哪些
PHP magic method
The third pre class exercise