图算法(2):Bellman-Ford算法

  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 graph
struct Edge
{
    int src, dest, weight;
};

// a structure to represent a connected, directed and weighted graph
struct 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 edges
struct 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 solution
void 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 cycle
void 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 functions
int 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Bellman-Ford算法是一种用于解决单源最短路径问题的算法。它可以处理带有负权边的,并且可以检测到负权环。下面我将给出一个具体的案例来说明Bellman-Ford算法的实现过程。 假设有一个带有负权边的有向,如下所示: ![Bellman-Ford算法具体案例及算法](https://img-blog.csdnimg.cn/20211201171019322.png) 我们要求从源点S到其它各个顶点的最短路径。首先,我们需要初始化距离数组dist,将源点S的距离设为,其它顶点的距离设为无穷大。同时,我们需要初始化前驱数组prev,将源点S的前驱设为自身,其它顶点的前驱设为null。 接下来,我们需要进行n-1次松弛操作,其中n为中顶点的个数。每次松弛操作都会遍历中的所有边,对每条边进行松弛操作。松弛操作的具体过程如下: 对于一条边(u, v),如果dist[u]+w(u, v)<dist[v],则更新dist[v]为dist[u]+w(u, v),同时更新prev[v]为u。 其中,w(u, v)表示边(u, v)的权值。 第一次松弛操作后,距离数组和前驱数组的值如下所示: ![Bellman-Ford算法具体案例及算法](https://img-blog.csdnimg.cn/20211201171019321.png) 第二次松弛操作后,距离数组和前驱数组的值如下所示: ![Bellman-Ford算法具体案例及算法](https://img-blog.csdnimg.cn/20211201171019320.png) 第三次松弛操作后,距离数组和前驱数组的值如下所示: ![Bellman-Ford算法具体案例及算法](https://img-blog.csdnimg.cn/20211201171019319.png) 第四次松弛操作后,距离数组和前驱数组的值如下所示: ![Bellman-Ford算法具体案例及算法](https://img-blog.csdnimg.cn/20211201171019318.png) 第五次松弛操作后,距离数组和前驱数组的值如下所示: ![Bellman-Ford算法具体案例及算法](https://img-blog.csdnimg.cn/20211201171019317.png) 最终,我们得到了从源点S到其它各个顶点的最短路径。其中,如果距离数组中存在负数,则说明中存在负权环,Bellman-Ford算法无法处理这种情况。 以上就是Bellman-Ford算法的具体案例及算法。 ### 回答2: Bellman-Ford算法是解决单源最短路径问题的一种算法。它可以用于有向和带有负权边的算法具体案例如下:假设有一个有向,其中包含5个节点(A、B、C、D、E)和6条边,边的权重如下:AB(6),AC(3),BC(2),BD(5),CD(-2),DE(1)。 Bellman-Ford算法如下: 1. 初始化距离数组dist[],将起点到所有其他点的距离初始化为无穷大,除了起点本身的距离初始化为0。 2. 对于每个节点进行n-1次迭代,其中n是节点的数量。(本例中进行4次迭代) 3. 在每次迭代中,遍历所有的边,如果从当前节点u到边的终点v的距离加上边的权重w小于dist[v],则更新dist[v]为新的距离值。 4. 完成n-1次迭代后,得出的dist[]数组中存储的就是起点到其他节点的最短路径距离。 在本例中,按照上述算法进行4次迭代后,得到的最终dist[]数组为[0, 3, 5, 7, 8],表示起点A到其他节点的最短路径距离分别为:A到B为0,A到C为3,A到D为5,A到E为7。 Bellman-Ford算法的时间复杂度为O(V * E),其中V是节点数量,E是边数量。这是由于该算法需要遍历所有的边,并进行n-1次迭代。当出现负权环的情况时,算法无法得出最短路径结果。但可以通过检测负权环的存在来判断是否存在该问题。 ### 回答3: Bellman-Ford算法是一种用于计算带负权边的单源最短路径的算法。它可以解决一般的有向和带有负权边的最短路径问题。算法的时间复杂度为O(VE),其中V是中的顶点数,E是中的边数。 下面以一个具体案例来说明Bellman-Ford算法的执行过程: 假设有一个有向,其中包含5个顶点和6条边。我们要求从顶点A到其他所有顶点的最短路径。 顶点 边 权重 A AB 1 A AC -3 B CD 2 C BD -2 D AE 3 E BC 2 首先,我们初始化源顶点A到其他所有顶点的距离为无穷大,除了A自身到A的距离为0。同时,我们初始化一个辅助数组distance用于存储当前已知的最短距离。 然后,我们开始执行Bellman-Ford算法的主循环。在每一轮循环中,我们遍历所有边,计算通过当前边能够获得的更短路径。如果发现了一条更短的路径,则更新距离数组distance。一共需要执行V-1轮循环,其中V是中的顶点数。 第一轮循环: 假设distance[A] = 0,distance[B] = ∞,distance[C] = ∞,distance[D] = ∞,distance[E] = ∞。 遍历边AB,发现distance[B] > distance[A] + 1,更新distance[B] = distance[A] + 1 = 1。 遍历边AC,发现distance[C] > distance[A] - 3,更新distance[C] = distance[A] - 3 = -3。 其他边没有更新。 第二轮循环: 假设distance[A] = 0,distance[B] = 1,distance[C] = -3,distance[D] = ∞,distance[E] = ∞。 遍历边CD,发现distance[D] > distance[C] + 2,更新distance[D] = distance[C] + 2 = -1。 遍历边BD,发现distance[D] > distance[B] - 2,更新distance[D] = distance[B] - 2 = -1。 其他边没有更新。 第三轮循环: 假设distance[A] = 0,distance[B] = 1,distance[C] = -3,distance[D] = -1,distance[E] = ∞。 遍历边AE,发现distance[E] > distance[A] + 3,更新distance[E] = distance[A] + 3 = 3。 其他边没有更新。 第四轮循环: 假设distance[A] = 0,distance[B] = 1,distance[C] = -3,distance[D] = -1,distance[E] = 3。 遍历边BC,发现distance[C] > distance[B] + 2,更新distance[C] = distance[B] + 2 = 3。 其他边没有更新。 最后,经过V-1=4轮循环,我们得到了从顶点A到其他所有顶点的最短路径。结果为distance[A] = 0,distance[B] = 1,distance[C] = 3,distance[D] = -1,distance[E] = 3。 Bellman-Ford算法的核心思想是通过松弛操作不断更新当前已知的最短距离,直到达到最优解。当算法结束后,如果存在从源点可达的顶点无法通过松弛操作更新,则说明中存在负权回路。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

One2zeror

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值