圖演算法(2):Bellman-Ford演算法

來源:互聯網
上載者:User

標籤:圖   單源最短路   貝爾曼-福特演算法   

  Bellman-Ford演算法是由理查德?貝爾曼(Richard Bellman) 和 萊斯特?福特 創立的,求解單源最短路徑問題的一種演算法。有時候這種演算法也被稱為 Moore-Bellman-Ford 演算法,因為 Edward F. Moore 也為這個演算法的發展做出了貢獻。它的原理是對圖進行V-1次鬆弛操作,得到所有可能的最短路徑。其優於迪科斯徹演算法的方面是邊的權值可以為負數、實現簡單,缺點是時間複雜度過高,高達O(VE)。

負邊權操作:
  與迪科斯徹演算法不同的是,迪科斯徹演算法的基本操作“拓展”是在深度上尋路,而“鬆弛”操作則是在廣度上尋路,這就確定了貝爾曼-福特演算法可以對負邊進行操作而不會影響結果。
負權環判定:
  因為負權環可以無限制的降低總花費,所以如果發現第n次操作仍可降低花銷,就一定存在負權環。

Bellman-Ford 演算法描述:
  1)建立源頂點 v 到圖中所有頂點的距離的集合 distSet,為圖中的所有頂點指定一個距離值,初始均為 Infinite,源頂點距離為 0;
  2)計算最短路徑,執行 V - 1 次遍曆;
對於圖中的每條邊:如果起點 u 的距離 d 加上邊的權值 w 小於終點 v 的距離 d,則更新終點 v 的距離值 d;
  3)檢測圖中是否有負權邊形成了環,遍曆圖中的所有邊,計算 u 至 v 的距離,如果對於 v 存在更小的距離,則說明存在環;

虛擬碼表示:

procedure BellmanFord(list vertices, list edges, vertex source)   // 該實現讀入邊和節點的列表,並向兩個數組(distance和predecessor)中寫入最短路徑資訊   // 步驟1:初始化圖   for each vertex v in vertices:       if v is source then distance[v] := 0       else distance[v] := infinity       predecessor[v] := null   // 步驟2:重複對每一條邊進行鬆弛操作   for i from 1 to size(vertices)-1:       for each edge (u, v) with weight w in edges:           if distance[u] + w < distance[v]:               distance[v] := distance[u] + w               predecessor[v] := u   // 步驟3:檢查負權環   for each edge (u, v) with weight w in edges:       if distance[u] + w < distance[v]:           error "圖包含了負權環"
// A C / C++ program for Bellman-Ford‘s single source shortest path algorithm.#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>// a structure to represent a weighted edge in graphstruct Edge{    int src, dest, weight;};// a structure to represent a connected, directed and weighted graphstruct Graph{    // V-> Number of vertices, E-> Number of edges    int V, E;    // graph is represented as an array of edges.    struct Edge* edge;};// Creates a graph with V vertices and E edgesstruct Graph* createGraph(int V, int E){    struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) );    graph->V = V;    graph->E = E;    graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) );    return graph;}// A utility function used to print the solutionvoid printArr(int dist[], int n){    printf("Vertex   Distance from Source\n");    for (int i = 0; i < n; ++i)        printf("%d \t\t %d\n", i, dist[i]);}// The main function that finds shortest distances from src to all other// vertices using Bellman-Ford algorithm.  The function also detects negative// weight cyclevoid BellmanFord(struct Graph* graph, int src){    int V = graph->V;    int E = graph->E;    int dist[V];    // Step 1: Initialize distances from src to all other vertices as INFINITE    for (int i = 0; i < V; i++)        dist[i]   = INT_MAX;    dist[src] = 0;    // Step 2: Relax all edges |V| - 1 times. A simple shortest path from src    // to any other vertex can have at-most |V| - 1 edges    for (int i = 1; i <= V-1; i++)    {        for (int j = 0; j < E; j++)        {            int u = graph->edge[j].src;            int v = graph->edge[j].dest;            int weight = graph->edge[j].weight;            if (dist[u] != INT_MAX && dist[u] + weight < dist[v])                dist[v] = dist[u] + weight;        }    }    // Step 3: check for negative-weight cycles.  The above step guarantees    // shortest distances if graph doesn‘t contain negative weight cycle.    // If we get a shorter path, then there is a cycle.    for (int i = 0; i < E; i++)    {        int u = graph->edge[i].src;        int v = graph->edge[i].dest;        int weight = graph->edge[i].weight;        if (dist[u] != INT_MAX && dist[u] + weight < dist[v])            printf("Graph contains negative weight cycle");    }    printArr(dist, V);    return;}// Driver program to test above functionsint main(){    /* Let us create the graph given in above example */    int V = 5;  // Number of vertices in graph    int E = 8;  // Number of edges in graph    struct Graph* graph = createGraph(V, E);    // add edge 0-1 (or A-B in above figure)    graph->edge[0].src = 0;    graph->edge[0].dest = 1;    graph->edge[0].weight = -1;    // add edge 0-2 (or A-C in above figure)    graph->edge[1].src = 0;    graph->edge[1].dest = 2;    graph->edge[1].weight = 4;    // add edge 1-2 (or B-C in above figure)    graph->edge[2].src = 1;    graph->edge[2].dest = 2;    graph->edge[2].weight = 3;    // add edge 1-3 (or B-D in above figure)    graph->edge[3].src = 1;    graph->edge[3].dest = 3;    graph->edge[3].weight = 2;    // add edge 1-4 (or A-E in above figure)    graph->edge[4].src = 1;    graph->edge[4].dest = 4;    graph->edge[4].weight = 2;    // add edge 3-2 (or D-C in above figure)    graph->edge[5].src = 3;    graph->edge[5].dest = 2;    graph->edge[5].weight = 5;    // add edge 3-1 (or D-B in above figure)    graph->edge[6].src = 3;    graph->edge[6].dest = 1;    graph->edge[6].weight = 1;    // add edge 4-3 (or E-D in above figure)    graph->edge[7].src = 4;    graph->edge[7].dest = 3;    graph->edge[7].weight = -3;    BellmanFord(graph, 0);    return 0;}

最佳化:
1)迴圈的提前跳出:
  在實際操作中,貝爾曼-福特演算法經常會在未達到V-1次前就出解,V-1其實是最大值。於是可以在迴圈中設定判定,在某次迴圈不再進行鬆弛時,直接退出迴圈,進行負權環判定。

2)隊列最佳化:
  求單源最短路的SPFA演算法的全稱是:Shortest Path Faster Algorithm。 SPFA演算法是西南交通大學段凡丁於1994年發表的。鬆弛操作必定只會發生在最短路徑前置節點鬆弛成功過的節點上,用一個隊列記錄鬆弛過的節點,可以避免了冗餘計算。複雜度可以降低到O(kE),k是個比較小的係數(並且在絕大多數的圖中,k<=2,然而在一些精心構造的圖中可能會上升到很高)

Begin  initialize-single-source(G,s);  initialize-queue(Q);  enqueue(Q,s);  while not empty(Q) do     begin      u:=dequeue(Q);      for each v∈adj[u] do         begin          tmp:=d[v];          relax(u,v);          if (tmp<>d[v]) and (not v in Q) then            enqueue(Q,v);        end;    end;End;

參考:
https://zh.wikipedia.org/w/index.php?title=%E8%B4%9D%E5%B0%94%E6%9B%BC-%E7%A6%8F%E7%89%B9%E7%AE%97%E6%B3%95&redirect=no
http://www.cnblogs.com/hxsyl/p/3248391.html
http://www.geeksforgeeks.org/dynamic-programming-set-23-bellman-ford-algorithm/
http://www.nocow.cn/index.php/%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

圖演算法(2):Bellman-Ford演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.