Sie sind auf Seite 1von 12

Bellman-Ford Algorithm

Dr.Jeevani Goonetillake UCSC

Bellman-Ford Algorithm
Solves the single-source shortest paths problem in the general case in which the edge weights may be negative.

Given a weighted directed graph G = (V,E) with source s and weight function w, the BellmanFord algorithm returns a boolean value indicating whether or not there is a negativeweight cycle that is reachable from the source.

Bellman-Ford Algorithm
If there such a cycle, the algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortest paths and their weights.

Bellman-Ford Algorithm
Uses relaxation progressively decreasing an estimate d[v] on the weight of a shortest path from the source s to each vertex v V until it achieves the actual shortest path. Returns true if and only if the graph contains no negative weight cycles that are reachable from the source.

Bellman-Ford Algorithm
Bellman-Ford (G,w,s)
INITIALIZE-SINGLE-SOURCE(G,s) for i 1 to |V[G]| - 1 do for each edge (u,v) E[G] do RELAX (u,v,w) for each edge (u,v) E[G] do if d[v] > d[u] + w(u,v) then return FALSE return TRUE

Bellman-Ford Algorithm
5 6 s 0 7 8 2 y 9 z

-2
-4 -3

x 7

Each pass relaxes the edges in the order (t,x) , (t,y), (t,z), (x,t), (y,x),(y,z),(z,x),(z,s),(s,t),(s,y).

Bellman-Ford Algorithm
5 -2
-4 s 0 7

t 6
6

8
2 y 7 9

-3

Bellman-Ford Algorithm
5 -2
-4 s 0 7

t 6
6

x 4

8
2 y 7 9

-3

z 2

Bellman-Ford Algorithm
5 -2
-4 s 0 7

t 6
2

x 4

8
2 y 7 9

-3

z 2

Bellman-Ford Algorithm

t 6 s 0 7
2

-2 -4

x 4

8
2 y 7 9

-3

z -2

Bellman-Ford Algorithm
Running Time is O(VE)

Linear Programming
In the general linear programming problem , we are given an m x n matrix A, an m-vector b, and an n-vector c. We wish to find a vector x of n elements that maximizes the objective function CT xi subject to m constraints given by Ax b.

Das könnte Ihnen auch gefallen