Bellman-Ford算法

Bellman-Ford算法

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

Bellman-Ford算法也保证找到图中的最短路径,类似于Dijkstra的算法。虽然Bellman-Ford算法比Dijkstra算法慢,但它能够处理具有负边权重的图,这使得它更加通用。如果图中存在一个负环,则无法找到最短路径。如果我们继续绕负循环无限次,那么路径的成本将继续降低(即使路径的长度在增加)。因此,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-1是具有N个顶点的图中最短路径的最大可能长度。这允许算法正确地计算从源顶点到所有其他顶点的最短路径,假定不存在负权重循环。

为什么第N次弛豫中距离的减少表明负循环的存在?

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

Bellman-Ford算法的工作以检测图中的负循环:

让我们假设我们有一个图,这是下面给出的,我们想找到是否存在一个负循环或不使用贝尔曼福特。

图片[1]-Bellman-Ford算法-可能资源网

初始图

步骤1:初始化距离数组Dist[]以存储每个顶点到源顶点的最短距离。源的初始距离将为0,其他顶点的距离将为无限。

图片[2]-Bellman-Ford算法-可能资源网

初始化距离数组

步骤2:开始放松边缘,在第一次放松:

  • B的当前距离>(A的距离)+(A到B的权重),即无限>0 + 5
    • 因此,Dist[B] = 5
图片[3]-Bellman-Ford算法-可能资源网

第一次放松

第三步: 第二次放松:

  • D的当前距离>(B的距离)+(B到D的权重),即无限>5 + 2
    • 距离[D] = 7
  • C的当前距离>(B的距离)+(B到C的权重),即无限>5 + 1
    • 距离[C] = 6
图片[4]-Bellman-Ford算法-可能资源网

第二次放松

步骤4:第三次放松期间:

  • F的当前距离>(D的距离)+(D到F的权重),即无限>7 + 2
    • 距离[F] = 9
  • E的当前距离>(C的距离)+(C到E的权重),即无限>6 + 1
    • 距离[E] = 7
图片[5]-Bellman-Ford算法-可能资源网

第三次放松

第五步:第四次放松:

  • D的当前距离>(E的距离)+(E到D的权重),即7> 7 +(-1)
    • 距离[D] = 6
  • E的当前距离>(F的距离)+(F到E的权重),即7> 9 +(-3)
    • 距离[E] = 6
图片[6]-Bellman-Ford算法-可能资源网

第四次放松

第六步:第五次放松:

  • F的当前距离>(D的距离)+(D到F的权重),即九>六加二
    • 距离[F] = 8
  • D的当前距离>(E的距离)+(E到D的权重),即6> 6 +(-1)
    • 距离[E] = 5
  • 由于图具有6个顶点,所以在第5次松弛期间,所有顶点的最短距离应该已经被计算。
图片[7]-Bellman-Ford算法-可能资源网

第五次放松

第7步:最后的放松,即如果在第五次弛豫的距离阵列中存在任何变化,则第六次弛豫应指示负循环的存在。

在第六次放松期间,可以看到以下变化:

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

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

图片[8]-Bellman-Ford算法-可能资源网

第六次放松

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

Bellman-Ford在有向加权图中求负环的算法

  • 将每个顶点’ v ‘的距离数组dist[]初始化为dist[v] = INFINITY
  • 假设任何顶点(假设‘0’)作为源,并指定dist = 0
  • 放松所有的 边(u,v,weight)N-1 按以下条件计算:
    • dist[v] = minimum(dist[v],distance[u] + weight)
  • 现在,放松所有的边缘一次,即。的第n时间和基于以下两种情况,我们可以检测负循环:
    • 情况1(存在负循环):对于任意边(u,v,weight),如果dist[u] + weight< dist[v]
    • 情况2(无负循环):情况1对于所有边都失败。

在算法中处理断开的图形:

如果给定的图是断开的,则上述算法和程序可能不起作用。当所有顶点都可从源顶点0到达时,它起作用。
To handle disconnected graphs, we can repeat the above algorithm for vertices having distance = INFINITY, or simply for the vertices that are not visited.

以下是上述方法的实施: 

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实现代码

// A Java program for Bellman-Ford's single source shortest
// path algorithm.

import java.io.*;
import java.lang.*;
import java.util.*;

// A class to represent a connected, directed and weighted
// graph
class Graph {

	// A class to represent a weighted edge in graph
	class Edge {
		int src, dest, weight;
		Edge() { src = dest = weight = 0; }
	};

	int V, E;
	Edge edge[];

	// Creates a graph with V vertices and E edges
	Graph(int v, int e)
	{
		V = v;
		E = e;
		edge = new Edge[e];
		for (int i = 0; i < e; ++i)
			edge[i] = new Edge();
	}

	// 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(Graph graph, int src)
	{
		int V = graph.V, E = graph.E;
		int dist[] = new int[V];

		// Step 1: Initialize distances from src to all
		// other vertices as INFINITE
		for (int i = 0; i < V; ++i)
			dist[i] = Integer.MAX_VALUE;
		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; ++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] != Integer.MAX_VALUE
					&& 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 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] != Integer.MAX_VALUE
				&& dist[u] + weight < dist[v]) {
				System.out.println(
					"Graph contains negative weight cycle");
				return;
			}
		}
		printArr(dist, V);
	}

	// A utility function used to print the solution
	void printArr(int dist[], int V)
	{
		System.out.println("Vertex Distance from Source");
		for (int i = 0; i < V; ++i)
			System.out.println(i + "\t\t" + dist[i]);
	}

	// Driver's code
	public static void main(String[] args)
	{
		int V = 5; // Number of vertices in graph
		int E = 8; // Number of edges in graph

		Graph graph = new Graph(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
		graph.BellmanFord(graph, 0);
	}
}
// Contributed by Aakash Hasija

Python3实现代码

# Python3 program for Bellman-Ford's single source
# shortest path algorithm.

# Class to represent a graph

class Graph:

	def __init__(self, vertices):
		self.V = vertices # No. of vertices
		self.graph = []

	# function to add an edge to graph
	def addEdge(self, u, v, w):
		self.graph.append([u, v, w])

	# utility function used to print the solution
	def printArr(self, dist):
		print("Vertex Distance from Source")
		for i in range(self.V):
			print("{0}\t\t{1}".format(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
	def BellmanFord(self, src):

		# Step 1: Initialize distances from src to all other vertices
		# as INFINITE
		dist = [float("Inf")] * self.V
		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 _ in range(self.V - 1):
			# Update dist value and parent index of the adjacent vertices of
			# the picked vertex. Consider only those vertices which are still in
			# queue
			for u, v, w in self.graph:
				if dist[u] != float("Inf") and dist[u] + w < dist[v]:
					dist[v] = dist[u] + w

		# 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 u, v, w in self.graph:
			if dist[u] != float("Inf") and dist[u] + w < dist[v]:
				print("Graph contains negative weight cycle")
				return

		# print all distance
		self.printArr(dist)


# Driver's code
if __name__ == '__main__':
	g = Graph(5)
	g.addEdge(0, 1, -1)
	g.addEdge(0, 2, 4)
	g.addEdge(1, 2, 3)
	g.addEdge(1, 3, 2)
	g.addEdge(1, 4, 2)
	g.addEdge(3, 2, 5)
	g.addEdge(3, 1, 1)
	g.addEdge(4, 3, -3)

	# function call
	g.BellmanFord(0)

# Initially, Contributed by Neelam Yadav
# Later On, Edited by Himanshu Garg

C#实现代码

// C# program for Bellman-Ford's single source shortest
// path algorithm.

using System;

// A class to represent a connected, directed and weighted
// graph
class Graph {
	// A class to represent a weighted edge in graph
	class Edge {
		public int src, dest, weight;
		public Edge() { src = dest = weight = 0; }
	};

	int V, E;
	Edge[] edge;

	// Creates a graph with V vertices and E edges
	Graph(int v, int e)
	{
		V = v;
		E = e;
		edge = new Edge[e];
		for (int i = 0; i < e; ++i)
			edge[i] = new Edge();
	}

	// 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(Graph graph, int src)
	{
		int V = graph.V, E = graph.E;
		int[] dist = new int[V];

		// Step 1: Initialize distances from src to all
		// other vertices as INFINITE
		for (int i = 0; i < V; ++i)
			dist[i] = int.MaxValue;
		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; ++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.MaxValue
					&& 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 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.MaxValue
				&& dist[u] + weight < dist[v]) {
				Console.WriteLine(
					"Graph contains negative weight cycle");
				return;
			}
		}
		printArr(dist, V);
	}

	// A utility function used to print the solution
	void printArr(int[] dist, int V)
	{
		Console.WriteLine("Vertex Distance from Source");
		for (int i = 0; i < V; ++i)
			Console.WriteLine(i + "\t\t" + dist[i]);
	}

	// Driver's code
	public static void Main()
	{
		int V = 5; // Number of vertices in graph
		int E = 8; // Number of edges in graph

		Graph graph = new Graph(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
		graph.BellmanFord(graph, 0);
	}
	// This code is contributed by Ryuga
}

java script实现代码

// a structure to represent a connected, directed and
// weighted graph
class Edge {
constructor(src, dest, weight) {
	this.src = src;
	this.dest = dest;
	this.weight = weight;
}
}

class Graph {
constructor(V, E) {
	this.V = V;
	this.E = E;
	this.edge = [];
}
}

function createGraph(V, E) {
const graph = new Graph(V, E);
for (let i = 0; i < E; i++) {
	graph.edge[i] = new Edge();
}
return graph;
}

function printArr(dist, V) {
console.log("Vertex Distance from Source");
for (let i = 0; i < V; i++) {
	console.log(`${i} \t\t ${dist[i]}`);
}
}

function BellmanFord(graph, src) {
const V = graph.V;
const E = graph.E;
const dist = [];

for (let i = 0; i < V; i++) {
	dist[i] = Number.MAX_SAFE_INTEGER;
}
dist[src] = 0;

for (let i = 1; i <= V - 1; i++) {
	for (let j = 0; j < E; j++) {
	const u = graph.edge[j].src;
	const v = graph.edge[j].dest;
	const weight = graph.edge[j].weight;
	if (dist[u] !== Number.MAX_SAFE_INTEGER && dist[u] + weight < dist[v]) {
		dist[v] = dist[u] + weight;
	}
	}
}

for (let i = 0; i < E; i++) {
	const u = graph.edge[i].src;
	const v = graph.edge[i].dest;
	const weight = graph.edge[i].weight;
	if (dist[u] !== Number.MAX_SAFE_INTEGER && dist[u] + weight < dist[v]) {
	console.log("Graph contains negative weight cycle");
	return;
	}
}

printArr(dist, V);
}

// Driver program to test methods of graph class
	
// Create a graph given in the above diagram

const V = 5;
const E = 8;
const graph = createGraph(V, E);

graph.edge[0] = new Edge(0, 1, -1);
graph.edge[1] = new Edge(0, 2, 4);
graph.edge[2] = new Edge(1, 2, 3);
graph.edge[3] = new Edge(1, 3, 2);
graph.edge[4] = new Edge(1, 4, 2);
graph.edge[5] = new Edge(3, 2, 5);
graph.edge[6] = new Edge(3, 1, 1);
graph.edge[7] = new Edge(4, 3, -3);

BellmanFord(graph, 0);

输出

Vertex   Distance from Source
0          0
1          -1
2          2
3          -2
4          1

复杂性分析:

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

Bellman福特的算法应用:

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

Bellman福特算法:

  • 如果图中包含任何负边环,Bellman-Ford算法将失败。
THE END
抢沙发

请登录后发表评论

    暂无评论内容