bellman ford algorithm
TRANSCRIPT
Bellman-Ford Algorithm
Dr.Jeevani GoonetillakeUCSC
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 Bellman-Ford algorithm returns a boolean value indicating whether or not there is a negative-weight 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 FALSEreturn TRUE
Bellman-Ford Algorithm5
-3
6
9
7
8
-2
7
2
x∞
t∞
z∞
y∞
-4s0
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
-3
6
9
7
8
-2
7
2
x∞
t6
z∞
y7
s0
-4
Bellman-Ford Algorithm
5
-3
6
9
7
8
-2
7
2
x4
t6
z2
y7
s0
-4
Bellman-Ford Algorithm
5
-3
6
9
7
8
-2
7
2
x4
t2
z2
y7
s0
-4
Bellman-Ford Algorithm
-3
6
9
7
8
-2
7
2
x4
t2
z-2
y7
s0
-4
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.