Démonstration animée de l'algorithme de Bellman-Ford Visualisez votre code avec des animations

图码-数据结构可视化动画版

假设你有一张地图,地图上不同的城市通过道路连接,每条道路都有一定距离。Bellman -Ford 算法就像一本指南,可以帮助你找到从一个城市到所有其他城市的最短路径,即使有些道路的长度为负数。它就像计算机的GPS,可用于找出网络中从一个点到另一个点的最快路径。在本文中,我们将仔细研究该算法的工作原理以及它为何在解决日常问题时如此方便。

Bellman-Ford 算法

目录

  • Bellman-Ford 算法
  • Bellman Ford 算法背后的思想
  • Bellman-Ford 的边缘松弛原理
  • 为什么放松边 N-1 次可以给我们单源最短路径?
  • 为什么第N次松弛中距离的减少预示着负循环的存在?
  • 使用 Bellman-Ford 算法检测图中的负循环
  • 使用 Bellman-Ford 算法查找有向加权图中的负循环
  • 处理算法中的不连通图
  • Bellman-Ford 算法的复杂度分析
  • Bellman Ford 算法的应用
  • Bellman Ford 算法的缺点

Bellman-Ford 算法

Bellman-Ford是一种单源最短路径算法,用于确定给定源顶点与图中每个其他顶点之间的最短路径。此算法可用于加权非加权图。

距源的距离(Bellman-Ford 算法)
公司:
亚马逊微软
显示主题
Topics:
Dynamic ProgrammingGraph
+2 more
Data StructuresAlgorithms
解决问题
中等的
48.11%
1.4升

Bellman-Ford算法也能保证找到图中的最短路径,类似于Dijkstra 算法。尽管 Bellman-Ford 算法比Dijkstra 算法慢,但它能够处理具有负边权重的图,这使其更加通用。如果图中存在负循环,则无法找到最短路径。如果我们继续无限次地绕负循环,那么路径的成本将继续下降(即使路径的长度在增加)。因此,Bellman-Ford还能够检测负循环,这是一个重要特性。

Bellman Ford 算法背后的想法:

Bellman-Ford 算法的主要原理是从一个源开始,计算到每个节点的距离。距离最初是未知的,并假设为无限的,但随着时间的推移,该算法通过识别一些较短的路径来放宽这些路径。因此,人们说 Bellman-Ford 是基于“放宽原则”的。

Bellman-Ford 的边缘松弛原理:

  • 它指出,对于具有N 个顶点的图,应将所有边放宽N-1次以计算单源最短路径。
  • 为了检测是否存在负循环,请再一次放松所有边,如果任何节点的最短距离减少,那么我们可以说存在负循环。简而言之,如果我们将边放​​松N次,并且第N-1 次第 N 次放松之间任何节点的最短距离有任何变化,则存在负循环,否则不存在。

为什么放松边 N-1 次可以给我们单源最短路径?

在最坏的情况下,两个顶点之间的最短路径最多可以有N-1条边,其中N是顶点的数量。这是因为在具有N 个顶点的图中,一条简单路径最多可以有N-1条边,因为如果不重新访问顶点,就不可能形成闭环。

通过放宽边N-1次,Bellman-Ford 算法可确保所有顶点的距离估计值都已更新为最佳值(假设图不包含任何可从源顶点到达的负权重循环)。如果图包含可从源顶点到达的负权重循环,则算法可以在N-1次迭代后检测到它,因为负循环会破坏最短路径长度。

总之,在 Bellman-Ford 算法中,将边放宽N-1次可确保算法已探索长度不超过N-1的所有可能路径,这是具有N 个顶点的图中最短路径的最大可能长度。这允许算法正确计算从源顶点到所有其他顶点的最短路径(假设不存在负权重循环)。

为什么第N次松弛中距离的减少预示着负循环的存在?

如前所述,实现到所有其他节点的单源最短路径需要N-1 次松弛。如果第 N 次松弛进一步缩短了任何节点的最短距离,则意味着某条具有负权重的边又被遍历了一次。值得注意的是,在N-1 次松弛期间,我们假设每个顶点只被遍历一次。然而,第 N 次松弛期间距离的缩短表示重新访问了顶点。

使用Bellman-Ford算法检测图中的负循环:

假设我们有如下所示的图表,我们想使用 Bellman-Ford 来查找是否存在负循环。

初始图表

步骤 1:初始化距离数组 Dist[],用于存储每个顶点与源顶点之间的最短距离。最初源顶点的距离为 0,其他顶点的距离为无穷大。

初始化距离数组

第 2 步:在第一次放松期间开始放松边缘:

  • B 的当前距离 > (A 的距离) + (A 到 B 的权重),即无穷大 > 0 + 5
    • 因此,Dist[B] = 5

第一次放松

步骤 3:第二次放松时:
  • D 的当前距离 > (B 的距离) + (B 到 D 的权重) 即无穷大 > 5 + 2
    • 距离[D] = 7
  • C 的当前距离 > (B 的距离) + (B 到 C 的权重),即无穷大 > 5 + 1
    • 距离[C] = 6

第二次放松

步骤 4:第三次放松时:

  • F 的当前距离 > (D 的距离) + (D 到 F 的权重),即无穷大 > 7 + 2
    • 距离[F] = 9
  • E 的当前距离 > (C 的距离) + (C 到 E 的权重),即无穷大 > 6 + 1
    • 距离[E] = 7

第三次放松

步骤5:第四次放松时:

  • D 的当前距离 > (E 的距离) + (E 到 D 的权重) 即 7 > 7 + (-1)
    • 距离[D] = 6
  • E 的当前距离 > (F 的距离) + (F 到 E 的权重) 即 7 > 9 + (-3)
    • 距离[E] = 6

第四放松

步骤 6:第五次放松时:

  • F 的当前距离 > (D 的距离) + (D 到 F 的权重),即 9 > 6 + 2
    • 距离[F] = 8
  • D 的当前距离 > (E 的距离) + (E 到 D 的权重) 即 6 > 6 + (-1)
    • 距离[D] = 5
  • 由于图有 6 个顶点,因此在第 5 次松弛期间应该计算出所有顶点的最短距离。

第五放松

步骤 7:现在,最后的放松,即第 6 次放松,如果第 5 次放松的距离数组有任何变化,则应该指示存在负循环。

在第6次放松时,可以看到以下变化:

  • E 的当前距离 > (F 的距离) + (F 到 E 的权重) 即 6 > 8 + (-3)
    • 距离[E]=5
  • F 的当前距离 > (D 的距离) + (D 到 F 的权重),即 8 > 5 + 2
    • 距离[F]=7

因为我们观察到距离数组的变化,因此我们可以得出图中存在负循环的结论。

第六次放松

结果:图中存在负循环(D->F->E)。

使用 Bellman-Ford 在有向加权图中查找负循环的算法:

  • 为每个顶点 ' v '初始化距离数组 dist[]dist[v] = INFINITY
  • 假设任何顶点(假设为“0”)作为源并分配dist = 0
  • 根据以下条件放松所有边(u,v,权重)N-1次:
    • dist[v] = 最小值(dist[v],距离[u] + 权重)
  • 现在,再次放松所有边,即第 N次,并基于以下两种情况,我们可以检测负循环:
    • 情况 1(存在负循环):对于任何边(u,v,权重),如果 dist[u] + weight < dist[v]
    • 情况 2(无负循环):情况 1 对于所有边均失败。

处理算法中的不连通图:

如果给定的图是不连通的,则上述算法和程序可能无法工作。当所有顶点都可以从源顶点0到达时,它才有效。
为了处理不连通的图,我们可以对距离 = INFINITY 的顶点重复上述算法或者仅对未访问的顶点重复上述算法。

下面是上述方法的实现: 

C++
// A C++ program for Bellman-Ford's single source
// shortest path algorithm.
#include <bits/stdc++.h>
using namespace std;

// 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 = new Graph;
    graph->V = V;
    graph->E = E;
    graph->edge = new Edge[E];
    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");
            return; // If negative cycle is detected, simply
                    // return
        }
    }

    printArr(dist, V);

    return;
}

// Driver's code
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 B-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;
    
      // Function call
    BellmanFord(graph, 0);

    return 0;
}
Java Python C# JavaScript

输出
顶点与源的距离
0 0
1 -1
2 2
3 -2
4 1

Bellman-Ford算法的复杂度分析

  • 图连通时的时间复杂度:
    • 最佳情况: O(E),当第一次和第二次松弛后的距离数组相同时,我们可以简单地停止进一步处理
    • 平均情况: O(V*E)
    • 最坏情况: O(V*E)
  • 图断开连接时的时间复杂度
    • 所有情况: O(E*(V^2))
  • 辅助空间: O(V),其中V是图中的顶点数。

Bellman Ford 算法的应用:

  • 网络路由: Bellman-Ford 用于计算机网络中查找路由表内的最短路径,帮助数据包在网络中高效导航。
  • GPS 导航:GPS 设备使用 Bellman-Ford 计算位置之间最短或最快路线,从而辅助导航应用程序和设备。
  • 运输和物流: Bellman-Ford算法可用于确定运输和物流中车辆的最佳路径,从而最大限度地减少燃料消耗和行程时间。
  • 游戏开发: Bellman-Ford 可用于在游戏开发中为虚拟世界中的移动和导航建模,其中不同的路径可能具有不同的成本或障碍。
  • 机器人和自动驾驶汽车:该算法可帮助机器人或自动驾驶汽车进行路径规划,同时考虑障碍物、地形和能源消耗。

Bellman Ford 算法的缺点:

  • Bellman-Ford algorithm will fail if the graph contains any negative edge cycle.

Related articles on single source shortest path algorithms:

  • Dijkstra’s Algorithm
  • Floyd-Warshall Algorithm

Previous Article
How to find Shortest Paths from Source to all Vertices using Dijkstra's Algorithm
Next Article
Floyd Warshall Algorithm

Que votre objectif soit la réussite d'un examen, le développement professionnel ou un intérêt purement personnel, ce site de visualisation des structures de données et des algorithmes sera une ressource inestimable.

Rendez-vous sur ce site et commencez votre voyage d'apprentissage !

Voici les plus courants : 【Description en langage C】Structures de données et algorithmes Implémentation JAVA des structures de données Fondamentaux des structures de données et algorithmes (Université de Qingdao - Wang Zhuo) Structures de données et algorithmes Structures de données selon Wang Dao Implémentation en langage C des structures de données Cours intensif de structures de données pour les examens de fin de semestre Tutoriel vidéo sur les structures de données en langage C Yan Weimin sur les structures de données Hao Bin sur les structures de données Examen d'entrée en master sur les structures de données Algorithmes et fondamentaux des structures de données en JAVA Structures de données selon Wang Dao 2022 Apprentissage des structures de données Structures de données selon Xiao Jiayu Wang Zhuo Apprentissage des structures de données Structures de données à l'Université du Zhejiang Révision des structures de données Structures de données selon Ma Shibin Cours zéro sur les structures de données Structures de données et algorithmes Introduction aux structures de données Explication des exercices de structures de données pour l'examen d'entrée en master Révision de fin de semestre des structures de données Niveau 2 en informatique