贪心算法——狄克斯特拉算法(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)
贪心算法——狄克斯特拉算法(Greedy Algorithm - Dijkstra's Algorithm)_第1张图片

路径追踪(Tracing paths)
We can draw a graph based on the figure above,
贪心算法——狄克斯特拉算法(Greedy Algorithm - Dijkstra's Algorithm)_第2张图片
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.

你可能感兴趣的:(算法)