最短路径
![2966caf3b176753ef4ef43b65d88ec94.png](https://img-blog.csdnimg.cn/img_convert/2966caf3b176753ef4ef43b65d88ec94.png)
![24f51ba0d0c93a1a653a7479a6f80398.png](https://img-blog.csdnimg.cn/img_convert/24f51ba0d0c93a1a653a7479a6f80398.png)
算法特点:
迪科斯彻算法使用了广度优先搜索解决赋权有向图或者无向图的单源最短路径问题,算法最终得到一个最短路径树。该算法常用于路由算法或者作为其他图算法的一个子模块。
算法思路:
Dijkstra算法采用的是一种贪心的策略,声明一个数组dis来保存源点到各个顶点的最短距离和一个保存已经找到了最短路径的顶点的集合:T,初始时,原点 s 的路径权重被赋为 0 (dis[s] = 0)。若对于顶点 s 存在能直接到达的边(s,m),则把dis[m]设为w(s, m),同时把所有其他(s不能直接到达的)顶点的路径长度设为无穷大。初始时,集合T只有顶点s。
然后,从dis数组选择最小值,则该值就是源点s到该值对应的顶点的最短路径,并且把该点加入到T中,OK,此时完成一个顶点,
然后,我们需要看看新加入的顶点是否可以到达其他顶点并且看看通过该顶点到达其他点的路径长度是否比源点直接到达短,如果是,那么就替换这些顶点在dis中的值。
然后,又从dis中找出最小值,重复上述动作,直到T中包含了图的所有顶点。
原文链接:https://blog.csdn.net/qq_35644234/article/details/60870719
![06ce2db8c19dbf8182d17643d5468dbc.png](https://img-blog.csdnimg.cn/img_convert/06ce2db8c19dbf8182d17643d5468dbc.png)
![f90762164003a4fb046d121ab0de7adb.png](https://img-blog.csdnimg.cn/img_convert/f90762164003a4fb046d121ab0de7adb.png)
![812e3a145f3ced83611bd889fc280027.png](https://img-blog.csdnimg.cn/img_convert/812e3a145f3ced83611bd889fc280027.png)
![9f0191744523a7590b705718e37f27aa.png](https://img-blog.csdnimg.cn/img_convert/9f0191744523a7590b705718e37f27aa.png)
![28a12db8db1a48b31867db60e692a0c2.png](https://img-blog.csdnimg.cn/img_convert/28a12db8db1a48b31867db60e692a0c2.png)
![e1c011b779b0d2900885e8979195fd71.png](https://img-blog.csdnimg.cn/img_convert/e1c011b779b0d2900885e8979195fd71.png)
![47c0669f6b0548c777aef076c1331eff.png](https://img-blog.csdnimg.cn/img_convert/47c0669f6b0548c777aef076c1331eff.png)
![b8b937320df6a407306e861246e2239e.png](https://img-blog.csdnimg.cn/img_convert/b8b937320df6a407306e861246e2239e.png)
![80784139327c0dd82eafc8b9c82b471c.png](https://img-blog.csdnimg.cn/img_convert/80784139327c0dd82eafc8b9c82b471c.png)
![15bc4276ff222782dca02b3b274ed17f.png](https://img-blog.csdnimg.cn/img_convert/15bc4276ff222782dca02b3b274ed17f.png)
![021903a984f57228448e5324755c34ae.png](https://img-blog.csdnimg.cn/img_convert/021903a984f57228448e5324755c34ae.png)
![b0538e94eec19f4519bd5adadcfed414.png](https://img-blog.csdnimg.cn/img_convert/b0538e94eec19f4519bd5adadcfed414.png)
![806719abe21b25dd86e541cf05738282.png](https://img-blog.csdnimg.cn/img_convert/806719abe21b25dd86e541cf05738282.png)
![027a1181db8f06b72462b2ff0031b251.png](https://img-blog.csdnimg.cn/img_convert/027a1181db8f06b72462b2ff0031b251.png)
python版本:https://zhuanlan.zhihu.com/p/35976233
![8f9b273aa48caba662c053dba3dddbaa.png](https://img-blog.csdnimg.cn/img_convert/8f9b273aa48caba662c053dba3dddbaa.png)
![c7964bd280f14d1bfc5394f8acc39511.png](https://img-blog.csdnimg.cn/img_convert/c7964bd280f14d1bfc5394f8acc39511.png)
![038351edd606f1cf950536e55493f782.png](https://img-blog.csdnimg.cn/img_convert/038351edd606f1cf950536e55493f782.png)
为什么dijkstra算法处理不了带有负权值的边的图?
下面说法有点道理但也有漏洞,供参考:
![c9c26e8193077c23acbd779005be61bb.png](https://img-blog.csdnimg.cn/img_convert/c9c26e8193077c23acbd779005be61bb.png)
例子:
![256b22b20314286422e45337829b31a9.png](https://img-blog.csdnimg.cn/img_convert/256b22b20314286422e45337829b31a9.png)
思考题:Dijkstra可以求带权无向图最短路径么
可以