22

私は3つを研究してきました、そして私はそれらからの私の推論を以下に述べています。私がそれらを十分に正確に理解したかどうか誰かに教えてもらえますか?ありがとうございました。

  1. ダイクストラのアルゴリズムは、単一のソースがあり、あるノードから別のノードへの最小パスを知りたい場合にのみ使用されますが、このような場合は失敗します

  2. Floyd-Warshallのアルゴリズムは、すべてのノードのいずれかがソースになる可能性がある場合に使用されるため、任意のソースノードから任意の宛先ノードに到達するための最短距離が必要です。これは、負のサイクルがある場合にのみ失敗します

(これは最も重要なものです。つまり、これは私が最も確信が持てないものです:)

3.Bellman-Fordは、ソースが1つしかない場合、ダイクストラのように使用されます。これは負の重みを処理でき、その動作は1つのソースを除いてFloyd-Warshallと同じですよね?

確認する必要がある場合、対応するアルゴリズムは次のとおりです(提供:ウィキペディア)。

ベルマンフォード:

 procedure BellmanFord(list vertices, list edges, vertex source)
   // This implementation takes in a graph, represented as lists of vertices
   // and edges, and modifies the vertices so that their distance and
   // predecessor attributes store the shortest paths.

   // Step 1: initialize graph
   for each vertex v in vertices:
       if v is source then v.distance := 0
       else v.distance := infinity
       v.predecessor := null

   // Step 2: relax edges repeatedly
   for i from 1 to size(vertices)-1:
       for each edge uv in edges: // uv is the edge from u to v
           u := uv.source
           v := uv.destination
           if u.distance + uv.weight < v.distance:
               v.distance := u.distance + uv.weight
               v.predecessor := u

   // Step 3: check for negative-weight cycles
   for each edge uv in edges:
       u := uv.source
       v := uv.destination
       if u.distance + uv.weight < v.distance:
           error "Graph contains a negative-weight cycle"

ダイクストラ:

 1  function Dijkstra(Graph, source):
 2      for each vertex v in Graph:                                // Initializations
 3          dist[v] := infinity ;                                  // Unknown distance function from 
 4                                                                 // source to v
 5          previous[v] := undefined ;                             // Previous node in optimal path
 6                                                                 // from source
 7      
 8      dist[source] := 0 ;                                        // Distance from source to source
 9      Q := the set of all nodes in Graph ;                       // All nodes in the graph are
10                                                                 // unoptimized - thus are in Q
11      while Q is not empty:                                      // The main loop
12          u := vertex in Q with smallest distance in dist[] ;    // Start node in first case
13          if dist[u] = infinity:
14              break ;                                            // all remaining vertices are
15                                                                 // inaccessible from source
16          
17          remove u from Q ;
18          for each neighbor v of u:                              // where v has not yet been 
19                                                                                 removed from Q.
20              alt := dist[u] + dist_between(u, v) ;
21              if alt < dist[v]:                                  // Relax (u,v,a)
22                  dist[v] := alt ;
23                  previous[v] := u ;
24                  decrease-key v in Q;                           // Reorder v in the Queue
25      return dist;

フロイド-ウォーシャル:

 1 /* Assume a function edgeCost(i,j) which returns the cost of the edge from i to j
 2    (infinity if there is none).
 3    Also assume that n is the number of vertices and edgeCost(i,i) = 0
 4 */
 5
 6 int path[][];
 7 /* A 2-dimensional matrix. At each step in the algorithm, path[i][j] is the shortest path
 8    from i to j using intermediate vertices (1..k−1).  Each path[i][j] is initialized to
 9    edgeCost(i,j).
10 */
11
12 procedure FloydWarshall ()
13    for k := 1 to n
14       for i := 1 to n
15          for j := 1 to n
16             path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );
4

2 に答える 2

21

最初の 2 つの質問と、Floyd-Warshall の目標 (すべてのペア間の最短経路を見つける) については正しいですが、Bellman-Ford と Floyd-Warshall の関係については正しくありません。どちらのアルゴリズムも動的計画法を使用して最短経路を見つけます。ただし、FW は、各開始ノードから他のすべてのノードへの BF の実行と同じではありません。

BF では、問題は次のとおりです。最大 k ステップを使用して、ソースからターゲットまでの最短経路は何ですか。実行時間は O(E V) です。これを他のノードごとに実行すると、実行時間は O(E V^2) になります。

FW では、問題は次のとおりです。すべてのノード i、j、k について、i から k を介して j までの最短経路は何ですか。これにより、実行時間が O(V^3) になります。これは、各開始ノードの BF よりも優れています (高密度グラフの場合、最大 |V| 倍)。

負のサイクル/重みに関するもう 1 つの注意事項: Dijkstra は単に正しい結果を提供できない可能性があります。BF と FW は失敗しません。負の重みには制限がないため、最小重みパスが存在しないことを正しく示します。

于 2012-07-28T21:23:03.843 に答える
9

単一ソースの最短パス:

ダイクストラ アルゴリズム - 負の重みは許可されません - O(E+Vlg(V))

Bellman ford Algorithm - 負の重みが許可されます。しかし、負のサイクルが存在する場合、ベルマンフォードは -ve サイクルを検出します - O(VE)

有向非巡回グラフ - 名前が示すように、DAG でのみ機能します - O(V+E)

すべてのペアの最短経路:

ダイクストラ アルゴリズム - 負の重みは許可されません - O(VE + V^2lg(V))

Bellman ford アルゴリズム - O(V^2E)

行列連鎖乗算法 - Bellman ford アルゴリズムと同じ複雑さ

Floyd Warshall アルゴリズム - 動的計画法を使用 - 複雑さは O(V^3)

于 2012-07-29T10:05:36.160 に答える