Dijkstra's Algorithm 狄杰斯特拉算法

       计算机的一门专业课--数据结构里的一个非常重要的算法--"Dijkstra's Algorithm",中文叫做狄杰斯特拉算法.

我们先看看中文关于这个算法的描述:

算法基本思想:
Dijkstra算法是解单源最短路径问题的一个贪心算法。其基本思想是,设置一个基点集合 S ,并不断地作贪心选择来扩充这个集合。一个项点属于集合 S 当且仅当从源到该项点的最短路径长度已知。初始时,S中仅含有源。设 u 是 G 的某一个项点,
我们把从源到 u 且中间只有经过 S 中项点的路称为从源到 u 的特殊路径,并且数组 dist 来记录当前每个项点所对应的最短特殊路径长度。Dijkstra算法每次从 V-S 中取出具有最短特殊路径长度的项点 u ,将 u 添加到 S 中,同时对数组 dist 作必要
的修改。

呵呵,看着这一段是不是感到很晕呢?没有关系,咱们结合下边的英文介绍和图例仔细看看这个算法.

   There are many different operations that can be done on graphs. Methods such as Kruskal's algorithm and Prim's algorithm find the most efficient way to traverse an entire graph. However, if the distance (cost) between two given vertices needed to be calculated, an alternate method would be required. Dijkstra's algorithm determines the distances (costs) between a given vertex and all other vertices in a graph. This may be useful to determine alternatives in decision making. For example, a telephone company may forgo the decision to install a new telephone cable in a rural area when presented with the option of installing the same cable in a city, reaching twice the people at half the cost.

   Dijkstra's algorithm is almost identical to that of Prim's. The algorithm begins at a specific vertex and extends outward within the graph, until all vertices have been reached. The only distinction is that Prim's algorithm stores a minimum cost edge whereas Dijkstra's algorithm stores the total cost from a source vertex to the current vertex. More simply, Dijkstra's algorithm stores a summation of minimum cost edges whereas Prim's algorithm stores at most one minimum cost edge.

   Dijkstra's algorithm creates labels associated with vertices.  These labels represent the distance (cost) from the source vertex to that particular vertex. Within the graph, there exists two kinds of labels: temporary and permanent. The temporary labels are given to vertices that have not been reached. The value given to these temporary labels can vary. Permanent labels are given to vertices that have been reached and their distance (cost) to the source vertex is known. The value given to these labels is the distance (cost) of that vertex to the source vertex. For any given vertex, there must be a permanent label or a temporary label, but not both.   

Vertex A has a temporary label with a distance of 0       Vertex B has a permanent label with a distance of 5 

 The algorithm begins by initializing any vertex in the graph (vertex A, for example) a permanent label with the value of 0, and all other vertices a temporary label with the value of 0.

The algorithm then proceeds to select the least cost edge connecting a vertex with a permanent label (currently vertex A) to a vertex with a temporary label (vertex B, for example).   Vertex B's label is then updated from a temporary to a permanent label. Vertex B's value is then determined by the addition of the cost of the edge with vertex A's value.

The next step is to find the next least cost edge extending to a vertex with a temporary label from either vertex A or vertex B (vertex C, for example), change vertex C's label to permanent, and determine its distance to vertex A.

This process is repeated until the labels of all vertices in the graph are permanent.


呵呵,现在大家通过这个例子明白了些什么吧,其实,这个算法就是一个寻求最短路径遍历所有节点的算法

posted on 2007-02-08 15:48  许维光  阅读(2230)  评论(2)    收藏  举报