贪心算法——狄克斯特拉算法(Greedy Algorithm - Dijkstra's Algorithm) - Go语言中文社区

贪心算法——狄克斯特拉算法(Greedy Algorithm - Dijkstra's Algorithm)


贪心算法——狄克斯特拉算法(Greedy Algorithm - Dijkstra’s Algorithm)

最短路径问题(The Single-Source Shortest Path Problem)
这里写图片描述

In real life, the single-source shortest path problem is everywhere. For example, you bought a blow-up doll on Taobao. You are anxiously waiting for your “girlfriend”. You hope deliver company can deliver your parcel to your home as soon as possible. The deliver company understand your anxious mood. The problem they are facing is finding out a way that is shortest and has minimum transportation cost.

That’s the single-source shortest path problem. Single-Source means the source is fixed. Find shortest paths to all destinations, which mean not only one shortest path but a set of shortest paths.

A variety of practical applications of the shortest-paths problems have been widely used, such as transportation planning, packet routing on the internet, airline crew scheduling, puzzle problem etc.

算法简介(Introduction)
The best well-known algorithm for single-source shortest path problem is Dijkstra’s algorithm. This algorithm is applicable for undirected or directed weighted graph with nonnegative weights only.

It starting from a given node in a graph, finds the shortest path from the node to another node nearest to it among its neighbour. Then go to the nearest node, find the second nearest node. Do the same operations until traverses all nodes. At the end, each node in the graph has a smallest length from starting node to it, which is our focus. This algorithm do not pay attention to what the shortest paths look like. If you want to, you can draw a graph made up of all nodes and their shortest paths. From this graph you can find the shorest path from any source to any destination.

伪代码(Pseudocode)

function Dijkstra((V, E), v0)
    for each v ∈ V do
        dist[v] ⟵ infinity
        prev[v] ⟵ null
    dist[v0] ⟵ 0
    Q ⟵ InitPriorityQueue(V)//Priorities are distances
    while Q is non-empty do
        u ⟵ EjectMin(Q)
        while each (u, w) ∈ E do
            if dist[u] + weight(u, w) < weight[w] then
                dist[w] ⟵ dist[u] + weight(u, w)
                prev[w] ⟵ u
                update(Q, w, dist[w])//Rearranges priority Queue

InitPriorityQueue(V): generate the min-heap of nodes in graph. The priority of a node is the distance from source to its.

EjectMin(Q): eject the node with minimum priority.

Update(Q, w, dist[w]): update the priority of node w with dist[w].

时间复杂度(Time Complexity)
The time complexity is the same with Prim’s algorithm. O(|E|log|V|).
The prerequisites are using adjacency list to represent graph and min-heap for priority queue implementation.

示例(Example)
这里写图片描述

路径追踪(Tracing paths)
We can draw a graph based on the figure above,
这里写图片描述
Suppose you want to find the shortest path from a to f. The path is,
a —— d —— c —— f
length=1+1+3=5.

Java Code

I will update ASAP.

写在最后的话(PS)
Dijkstra’s algorithm is one of the greatest application of greedy algorithmic design techniques. Hope you guys absorb and obtain this algorithm.
Welcome questions always and forever.

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/UoM_XiaoShuaiShuai/article/details/72830091
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 22:57:58
  • 阅读 ( 1939 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢