貪心算法——狄克斯特拉算法(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.

發佈了37 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章