0% found this document useful (0 votes)
3 views24 pages

Bellman Ford Algorithm

The Bellman-Ford algorithm finds the shortest paths from a source node to all other nodes in a weighted graph, accommodating negative edge weights and detecting negative weight cycles. It operates by relaxing all edges for V−1 iterations, where V is the number of vertices, and is applicable in various fields such as traffic networks, financial arbitrage, and routing protocols. The document includes detailed examples and Python code implementations demonstrating the algorithm's functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views24 pages

Bellman Ford Algorithm

The Bellman-Ford algorithm finds the shortest paths from a source node to all other nodes in a weighted graph, accommodating negative edge weights and detecting negative weight cycles. It operates by relaxing all edges for V−1 iterations, where V is the number of vertices, and is applicable in various fields such as traffic networks, financial arbitrage, and routing protocols. The document includes detailed examples and Python code implementations demonstrating the algorithm's functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

The Bellman-Ford algorithm is used to find the shortest paths from a source node to all other

nodes in a weighted graph. It is capable of handling graphs with negative edge weights and can
detect negative weight cycles. The algorithm works by relaxing all edges repeatedly for V−1
iterations, where V is the number of vertices in the graph.
Here is a numerical example of the Bellman-Ford Algorithm applied to find the shortest path in
a network flow problem, from one node to all nodes, with negative edges allowed. The Bellman-
Ford algorithm is particularly useful for graphs with negative edge weights, unlike Dijkstra’s
algorithm, which assumes all edge weights are non-negative.

Applications of Bellman-Ford in Network Flow


1. Traffic Networks: Finding the shortest path considering time delays (negative weights).
2. Financial Arbitrage: Detecting negative cycles in currency exchange rates.
3. Routing Protocols: Used in Distance Vector Routing (e.g., RIP protocol) to find paths in
networks.

Example 1
Consider the following directed graph with weights (some edges have negative weights):

From To Weight

S A 10

S E 8

A C 2

B A 1

C B -2

D A -4

D C -1

E D 1

1
Goal
Find the shortest path from S (source node) to all other nodes.

Step-by-Step Execution
Step 1: Initialization
1. Set the distance to the source A as 0.
2. Set the distance to all other nodes as ∞\infinity (indicating they are initially unreachable).
3. Create a table to track distances and predecessors:

Node Distance Predecessor

S 0 -

A ∞ -

B ∞ -

C ∞ -

D ∞ -

E ∞ -

Step 2: Relax Edges Repeatedly


Relax each edge V→U repeatedly over V−1 iterations (where V is the number of vertices).

Iteration 1
 Relax edge S→A:
dist(A) = min(∞,0+10) = 10, predecessor = S.
 Relax edge S→E:
dist(E) = min(∞,0+8) = 8, predecessor = S.
 Relax edge A→C:
dist(C) = min(∞,10+2) = 12, predecessor = A.
 Relax edge E→D:
dist(D) = min(∞,8+1) = 9, predecessor = E.

2
 Relax edge C→B:
dist(B) = min(∞,12-2) = 10, predecessor = C.

Node Distance Predecessor

S 0 -

A 10 S

B 10 C
C 12 D

D 9 E

E 8 S

Iteration 2
 Relax all edges again.

Node Distance Predecessor

S 0 -

A 5 S

B 10 C
C 8 D

D 9 E

E 8 S

Iteration 3
 Relax all edges again.

Node Distance Predecessor

S 0 -

A 5 S

3
Node Distance Predecessor

B 5 C
C 7 D

D 9 E

E 8 S

Iteration 4
 Relax all edges again.
No changes occur.

Node Distance Predecessor

S 0 -

A 5 S

B 5 C
C 7 D

D 9 E

E 8 S

Iteration 5
 Relax all edges again.
No changes occur.

Node Distance Predecessor

S 0 -

A 5 S

B 5 C
C 7 D

D 9 E

E 8 S

4
Node Distance Predecessor

Step 3: Check for Negative Weight Cycles


After V−1 iterations, perform one additional iteration to check for negative weight cycles. If any
edge can still be relaxed, the graph contains a negative weight cycle.
In this case, no further relaxation is possible, so no negative weight cycle exists.

Shortest Paths
 S → A: Distance = 5.
 S → B: Distance = 5.
 S → C: Distance = 7.
 S → D: Distance = 9.
 S → E: Distance = 8.

Applications of Bellman-Ford in Network Flow


4. Traffic Networks: Finding the shortest path considering time delays (negative weights).
5. Financial Arbitrage: Detecting negative cycles in currency exchange rates.
6. Routing Protocols: Used in Distance Vector Routing (e.g., RIP protocol) to find paths in
networks.

Python Code
class Graph:
def __init__(self, vertices):
self.V = vertices
self.edges = []

def add_edge(self, u, v, w):


self.edges.append((u, v, w))

def bellman_ford(self, src):

5
# Step 1: Initialize distances from the source to all vertices as
infinite
dist = {i: float("Inf") for i in range(self.V)}
dist[src] = 0 # Distance to source is 0

# Step 2: Relax all edges |V| - 1 times


for _ in range(self.V - 1):
for u, v, w in self.edges:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
dist[v] = dist[u] + w

# Step 3: Check for negative weight cycles


for u, v, w in self.edges:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
print("Graph contains a negative weight cycle!")
return

# Print shortest distance from source


print("Vertex Distance from Source:")
for node in dist:
print(f"{node} \t\t {dist[node]}")

# Define the graph based on the given problem


g = Graph(6) # 6 vertices (S=0, A=1, B=2, C=3, D=4, E=5)

# Adding edges based on the given input


g.add_edge(0, 1, 10) # S → A (10)
g.add_edge(0, 5, 8) # S → E (8)
g.add_edge(1, 3, 2) # A → C (2)
g.add_edge(2, 1, 1) # B → A (1)
g.add_edge(3, 2, -2) # C → B (-2)
g.add_edge(4, 1, -4) # D → A (-4)
g.add_edge(4, 3, -1) # D → C (-1)
g.add_edge(5, 4, 1) # E → D (1)

# Run Bellman-Ford from source S (0)


g.bellman_ford(0)

Python Output

csharp
CopyEdit
Vertex Distance from Source:
0 0
1 5
2 5
3 7
4 9
5 8

Example 2
Consider the following directed graph with weights (some edges have negative weights):
6
From To Weight

S A 10

S E 8

A C 2

B A 1

C B -2

D A -12

D C -1

Goal
Find the shortest path from S (source node) to all other nodes.

Step-by-Step Execution

Step 1: Given Graph Representation

From To Weight
S A 10
S E 8
A C 2
B A 1
C B -2
D A -12
D C -1
E D 1

Vertex Mapping

 S=0
 A=1
 B=2
 C=3
 D=4
 E=5

7
Step 2: Initialize Distances

 Distance to S (0) = 0
 Distance to all others = ∞

Vertex Distance from S


S (0) 0
A (1) ∞
B (2) ∞
C (3) ∞
D (4) ∞
E (5) ∞

Step 3: Relax All Edges (|V| - 1 = 5 times)

Iteration 1

Relax each edge:

 S → A: 0 + 10 = 10 (Update A)
 S → E: 0 + 8 = 8 (Update E)
 A → C: 10 + 2 = 12 (Update C)
 C → B: 12 + (-2) = 10 (Update B)
 E → D: 8 + 1 = 9 (Update D)
 D → A: 9 + (-12) = -3 (Update A)
 D → C: 9 + (-1) = 8 (Update C)
 B → A: 10 + 1 = 11 (No update since A = -3)

Vertex Distance
S (0) 0
A (1) -3
B (2) 10
C (3) 8
D (4) 9
E (5) 8

Iteration 2

Relax each edge:

8
 A → C: -3 + 2 = -1 (Update C)
 C → B: -1 + (-2) = -3 (Update B)
 B → A: -3 + 1 = -2 (No update)
 D → A: 9 + (-12) = -3 (No update)
 D → C: 9 + (-1) = 8 (No update)

Vertex Distance
S (0) 0
A (1) -3
B (2) -3
C (3) -1
D (4) 9
E (5) 8

Iteration 3

Relax each edge:

 A → C: -3 + 2 = -1 (No update)
 C → B: -1 + (-2) = -3 (No update)
 B → A: -3 + 1 = -2 (No update)
 D → A: 9 + (-12) = -3 (No update)

Vertex Distance
S (0) 0
A (1) -3
B (2) -3
C (3) -1
D (4) 9
E (5) 8

Iteration 4 & 5

 No further updates occur.

Step 4: Check for Negative Weight Cycles

Perform one extra iteration:

 No further updates occur, so no negative weight cycle exists!

9
Final Corrected Shortest Distances from S

Vertex Shortest Distance


S (0) 0
A (1) -3
B (2) -3
C (3) -1
D (4) 9
E (5) 8

✔ Final paths are now correct!


✔ No negative weight cycle detected!

Python Code

python
CopyEdit
class Graph:
def __init__(self, vertices):
self.V = vertices
self.edges = []

def add_edge(self, u, v, w):


self.edges.append((u, v, w))

def bellman_ford(self, src):


dist = {i: float("Inf") for i in range(self.V)}
dist[src] = 0

for _ in range(self.V - 1):


for u, v, w in self.edges:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
dist[v] = dist[u] + w

for u, v, w in self.edges:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
print("Graph contains a negative weight cycle!")
return

print("Vertex Distance from Source:")


for node in dist:
print(f"{node} \t\t {dist[node]}")

# Define the graph with 6 vertices (S=0, A=1, B=2, C=3, D=4, E=5)
g = Graph(6)

# Adding edges based on the input

10
g.add_edge(0, 1, 10) # S → A
g.add_edge(0, 5, 8) # S → E
g.add_edge(1, 3, 2) # A → C
g.add_edge(2, 1, 1) # B → A
g.add_edge(3, 2, -2) # C → B
g.add_edge(4, 1, -12) # D → A
g.add_edge(4, 3, -1) # D → C
g.add_edge(5, 4, 1) # E → D

# Run Bellman-Ford from source S (0)


g.bellman_ford(0)

Python Output

csharp
CopyEdit
Vertex Distance from Source:
0 0
1 -3
2 -3
3 -1
4 9
5 8

Example 3
Consider the following directed graph with weights (some edges have negative weights):

From To Weight

A B 4

A C 1

B C -3

B D 2

C D 3

D E 2

E B -5

Goal
Find the shortest path from S (source node) to all other nodes.

11
Step-by-Step Execution

Step 1: Graph Representation

We represent the given directed weighted graph:

Edge Weight
A→B 4
A→C 1
B → C -3
B→D 2
C→D 3
D→E 2
E→B -5

Vertices: A (0), B (1), C (2), D (3), E (4)

Step 2: Initialize Distances

We select A (0) as the source:

 Distance to A = 0
 Distance to all other vertices = ∞

Vertex Distance from A


A (0) 0
B (1) ∞
C (2) ∞
D (3) ∞
E (4) ∞

Step 3: Relax All Edges (|V| - 1 = 4 times)

Iteration 1

Relax each edge:

 A → B: 0 + 4 = 4 (Update B)
 A → C: 0 + 1 = 1 (Update C)

12
 B → C: 4 + (-3) = 1 (No update)
 B → D: 4 + 2 = 6 (Update D)
 C → D: 1 + 3 = 4 (Update D)
 D → E: 4 + 2 = 6 (Update E)
 E → B: 6 + (-5) = 1 (Update B)

Vertex Distance
A (0) 0
B (1) 1
C (2) 1
D (3) 4
E (4) 6

Iteration 2

Relax each edge:

 A → B: No update
 A → C: No update
 B → C: 1 + (-3) = -2 (Update C)
 B → D: 1 + 2 = 3 (Update D)
 C → D: -2 + 3 = 1 (Update D)
 D → E: 1 + 2 = 3 (Update E)
 E → B: 3 + (-5) = -2 (Update B)

Vertex Distance
A (0) 0
B (1) -2
C (2) -2
D (3) 1
E (4) 3

Iteration 3

Relax each edge:

 B → C: -2 + (-3) = -5 (Update C)
 B → D: -2 + 2 = 0 (Update D)
 C → D: -5 + 3 = -2 (Update D)
 D → E: -2 + 2 = 0 (Update E)
 E → B: 0 + (-5) = -5 (Update B)

13
Vertex Distance
A (0) 0
B (1) -5
C (2) -5
D (3) -2
E (4) 0

Iteration 4

Relax each edge:

 B → C: -5 + (-3) = -8 (Update C)
 B → D: -5 + 2 = -3 (Update D)
 C → D: -8 + 3 = -5 (Update D)
 D → E: -5 + 2 = -3 (Update E)
 E → B: -3 + (-5) = -8 (Update B)

Vertex Distance
A (0) 0
B (1) -8
C (2) -8
D (3) -5
E (4) -3

Step 4: Check for Negative Weight Cycles

We perform one extra iteration:

 B → C: -8 + (-3) = -11 (Update C → Negative cycle detected!)

Negative weight cycle exists!

Final Conclusion

Since a negative weight cycle was detected (B → C → B keeps decreasing), this is an infinite
loop and shortest paths cannot be determined.

Python Code

14
class Graph:
def __init__(self, vertices):
self.V = vertices # Number of vertices
self.edges = [] # List of edges

def add_edge(self, u, v, w):


"""Function to add an edge to the graph"""
self.edges.append((u, v, w))

def bellman_ford(self, src):


"""Function to find shortest paths from the source vertex"""
# Step 1: Initialize distances
dist = {i: float("Inf") for i in range(self.V)}
dist[src] = 0 # Distance to source is 0

# Step 2: Relax all edges |V| - 1 times


for _ in range(self.V - 1):
for u, v, w in self.edges:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
dist[v] = dist[u] + w

# Step 3: Check for negative weight cycles


for u, v, w in self.edges:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
print("Graph contains a negative weight cycle!")
return

# Step 4: Print the results


print("Vertex Distance from Source:")
for node in range(self.V):
print(f"{chr(65+node)} \t\t {dist[node]}")
# Define graph with 5 vertices (A=0, B=1, C=2, D=3, E=4)
g = Graph(5)

# Adding edges (From, To, Weight)


g.add_edge(0, 1, 4) # A → B
g.add_edge(0, 2, 1) # A → C
g.add_edge(1, 2, -3) # B → C
g.add_edge(1, 3, 2) # B → D
g.add_edge(2, 3, 3) # C → D
g.add_edge(3, 4, 2) # D → E
g.add_edge(4, 1, -5) # E → B

# Run Bellman-Ford from source A (0)


g.bellman_ford(0)

Python Output

Graph contains a negative weight cycle!

Example 4
mathematica

15
Copy code
Vertices: {A, B, C, D, E}

Edges and weights:


1. A - B: 4
2. A - C: 1
3. B - C: -2
4. B - D: 5
5. C - D: 8
6. C - E: -3
7. D - E: 3

Steps of Bellman-Ford Algorithm

1. Initialization:
o Set the distance to the source node (A) to 0.
o Set the distance to all other nodes to infinity (∞).
2. Relax all edges:
o Repeat V−1 times (where V is the number of vertices).
o For each edge (u,v), if the distance to u plus the edge weight is smaller than the
current distance to v, update the distance to v.
3. Check for Negative Weight Cycles:
o After V−1 iterations, check for negative weight cycles by going through each
edge again.
o If any edge can still be relaxed, it indicates a negative weight cycle.

Python Code for Bellman-Ford Algorithm

python
Copy code
# Bellman-Ford Algorithm Implementation

def bellman_ford(edges, vertices, source):


# Step 1: Initialize distances from source to all other vertices
distances = {vertex: float('inf') for vertex in vertices}
distances[source] = 0

# Step 2: Relax all edges |V| - 1 times


for _ in range(len(vertices) - 1):
for u, v, weight in edges:
if distances[u] + weight < distances[v]:
distances[v] = distances[u] + weight

# Step 3: Check for negative-weight cycles


for u, v, weight in edges:
if distances[u] + weight < distances[v]:
print("Graph contains negative weight cycle")
return None

return distances

16
# Define the graph edges and vertices
edges = [
('A', 'B', 4),
('A', 'C', 1),
('B', 'C', -2),
('B', 'D', 5),
('C', 'D', 8),
('C', 'E', -3),
('D', 'E', 3)
]

vertices = ['A', 'B', 'C', 'D', 'E']

# Call the Bellman-Ford algorithm with 'A' as the source


source = 'A'
distances = bellman_ford(edges, vertices, source)

if distances:
print(f"Shortest distances from {source}:")
for vertex, distance in distances.items():
print(f"Distance from {source} to {vertex}: {distance}")

Explanation of the Code

1. Initialization: We initialize the distances from the source vertex (A) to all other vertices
as infinity, except for the source vertex which is set to 0.
2. Relaxation: We perform V−1 iterations, and in each iteration, we check all edges and
update the shortest distances to each vertex.
3. Negative Weight Cycle Detection: After the relaxation steps, we go through the edges
once more to check if any distance can still be updated. If so, it means the graph contains
a negative weight cycle.

Output:
vbnet
Copy code
Shortest distances from A:
Distance from A to A: 0
Distance from A to B: 4
Distance from A to C: 1
Distance from A to D: 9
Distance from A to E: -2

Detailed Step-by-Step Execution

17
1. Initialization:

mathematica
Copy code
Distances: A=0, B=∞, C=∞, D=∞, E=∞

2. Relaxation (1st Iteration):


o A → B: 0 + 4 = 4, so update distance to B: A=0, B=4, C=∞, D=∞, E=∞
o A → C: 0 + 1 = 1, so update distance to C: A=0, B=4, C=1, D=∞, E=∞
o B → C: 4 + (-2) = 2, but 2 is greater than the current distance to C (which is 1), so
no update.
o B → D: 4 + 5 = 9, so update distance to D: A=0, B=4, C=1, D=9, E=∞
o C → D: 1 + 8 = 9, no update since the distance to D is already 9.
o C → E: 1 + (-3) = -2, so update distance to E: A=0, B=4, C=1, D=9, E=-2
o D → E: 9 + 3 = 12, but 12 is greater than the current distance to E (which is -2),
so no update.
3. Relaxation (2nd to 4th Iterations): These iterations will not change the distances because
we have already found the shortest paths.
4. Final Distances:

mathematica
Copy code
Distances: A=0, B=4, C=1, D=9, E=-2

Negative Weight Cycle Detection

If there were any negative weight cycles, the algorithm would detect them after the relaxation
steps and print:

sql
Copy code
Graph contains negative weight cycle

Conclusion

The Bellman-Ford algorithm is capable of handling graphs with negative weights and can detect
negative weight cycles, making it more flexible than Dijkstra's algorithm for certain types of
graphs.

Example 5
Consider a directed graph with 5 vertices (0, 1, 2, 3, 4) and the following edges with weights:

18
 (0, 1, -1)
 (0, 2, 4)
 (1, 2, 3)
 (1, 3, 2)
 (1, 4, 2)
 (3, 2, 5)
 (3, 1, 1)
 (4, 3, -3)

Bellman-Ford Algorithm Steps:

1. Initialize distances: Set the distance to the source vertex (0) to 0, and all other vertices to
infinity (∞).

bash
CopyEdit
Distance from source (0): [0, ∞, ∞, ∞, ∞]

2. Relax all edges: For each vertex, iterate through all the edges and update the distances if
a shorter path is found.

Repeat this process for V-1 times (where V is the number of vertices, so in this case, we
will repeat the process 4 times).

3. Check for negative weight cycles: After V-1 iterations, if any distance can be updated,
then there is a negative weight cycle in the graph.

We have a graph with 5 vertices (0, 1, 2, 3, 4) and the following weighted directed edges:

 (0, 1, -1)
 (0, 2, 4)
 (1, 2, 3)
 (1, 3, 2)
 (1, 4, 2)
 (3, 2, 5)
 (3, 1, 1)
 (4, 3, -3)

We will use vertex 0 as the source vertex to calculate the shortest paths to all other vertices.

Step 1: Initialization

19
We initialize the distances to all vertices from the source as follows:

 Distance from source (vertex 0): [0, ∞, ∞, ∞, ∞]

Here, 0 means the distance to the source vertex itself is 0, and infinity (∞) means that initially, all
other vertices are unreachable.

Step 2: Relax Edges (V-1 times)

The main part of the Bellman-Ford algorithm is relaxing the edges. Relaxing an edge means
updating the distance of a vertex if we find a shorter path via an edge.

We repeat the relaxation process for V-1 iterations (where V is the number of vertices). In this
case, V = 5, so we perform the relaxation for 4 iterations.

Iteration 1:

We go through all edges and relax them:

1. Edge (0, 1, -1):


o Current distance to vertex 1: ∞
o Distance to vertex 1 via vertex 0: 0 + (-1) = -1
o Since -1 < ∞, we update the distance to vertex 1.
o New distance array: [0, -1, ∞, ∞, ∞]
2. Edge (0, 2, 4):
o Current distance to vertex 2: ∞
o Distance to vertex 2 via vertex 0: 0 + 4 = 4
o Since 4 < ∞, we update the distance to vertex 2.
o New distance array: [0, -1, 4, ∞, ∞]
3. Edge (1, 2, 3):
o Current distance to vertex 2: 4
o Distance to vertex 2 via vertex 1: -1 + 3 = 2
o Since 2 < 4, we update the distance to vertex 2.
o New distance array: [0, -1, 2, ∞, ∞]
4. Edge (1, 3, 2):
o Current distance to vertex 3: ∞
o Distance to vertex 3 via vertex 1: -1 + 2 = 1
o Since 1 < ∞, we update the distance to vertex 3.
o New distance array: [0, -1, 2, 1, ∞]
5. Edge (1, 4, 2):
o Current distance to vertex 4: ∞
o Distance to vertex 4 via vertex 1: -1 + 2 = 1
o Since 1 < ∞, we update the distance to vertex 4.
o New distance array: [0, -1, 2, 1, 1]

20
6. Edge (3, 2, 5):
o Current distance to vertex 2: 2
o Distance to vertex 2 via vertex 3: 1 + 5 = 6
o Since 6 > 2, no update is made.
o Distance array remains the same: [0, -1, 2, 1, 1]
7. Edge (3, 1, 1):
o Current distance to vertex 1: -1
o Distance to vertex 1 via vertex 3: 1 + 1 = 2
o Since 2 > -1, no update is made.
o Distance array remains the same: [0, -1, 2, 1, 1]
8. Edge (4, 3, -3):
o Current distance to vertex 3: 1
o Distance to vertex 3 via vertex 4: 1 + (-3) = -2
o Since -2 < 1, we update the distance to vertex 3.
o New distance array: [0, -1, 2, -2, 1]

After the first iteration, the distances are:

csharp
CopyEdit
[0, -1, 2, -2, 1]

Iteration 2:

We perform another round of relaxing the edges:

1. Edge (0, 1, -1): No update needed since -1 < -1 is false.


2. Edge (0, 2, 4): No update needed since 4 < 2 is false.
3. Edge (1, 2, 3): No update needed since 2 < 2 is false.
4. Edge (1, 3, 2): No update needed since -2 + 2 = 0 is greater than -2.
5. Edge (1, 4, 2): No update needed since 1 + 2 = 3 is greater than 1.
6. Edge (3, 2, 5): No update needed since -2 + 5 = 3 is greater than 2.
7. Edge (3, 1, 1): No update needed since -2 + 1 = -1 is greater than -1.
8. Edge (4, 3, -3): No update needed since 1 - 3 = -2 is greater than -2.

After the second iteration, the distances remain the same:

csharp
CopyEdit
[0, -1, 2, -2, 1]

Iterations 3 and 4:

Since no further updates are made in these iterations (the distances stay the same), the algorithm
terminates early.

21
Step 3: Check for Negative Weight Cycles

After the V-1 iterations (4 iterations in this case), we check whether any distance can still be
updated. If we find an edge where the distance to the destination can still be improved, it
indicates the presence of a negative weight cycle.

In our example, there are no such updates, so the graph does not contain a negative weight cycle.

Final Output

The shortest distances from the source vertex 0 are:

yaml
CopyEdit
Vertex 0: 0
Vertex 1: -1
Vertex 2: 2
Vertex 3: -2
Vertex 4: 1

Conclusion

 The Bellman-Ford algorithm successfully computes the shortest paths from the source
vertex (0) to all other vertices.
 The algorithm handles negative edge weights and does not detect any negative weight
cycles in this graph.

Python Code

python
CopyEdit
class Edge:
def __init__(self, u, v, weight):
self.u = u
self.v = v
self.weight = weight

def bellman_ford(vertices, edges, source):


# Step 1: Initialize distances
distances = [float("inf")] * vertices
distances[source] = 0

# Step 2: Relax edges repeatedly

22
for _ in range(vertices - 1):
for edge in edges:
if distances[edge.u] != float("inf") and distances[edge.u] +
edge.weight < distances[edge.v]:
distances[edge.v] = distances[edge.u] + edge.weight

# Step 3: Check for negative weight cycles


for edge in edges:
if distances[edge.u] != float("inf") and distances[edge.u] +
edge.weight < distances[edge.v]:
print("Graph contains negative weight cycle")
return None

return distances

# Define edges of the graph


edges = [
Edge(0, 1, -1),
Edge(0, 2, 4),
Edge(1, 2, 3),
Edge(1, 3, 2),
Edge(1, 4, 2),
Edge(3, 2, 5),
Edge(3, 1, 1),
Edge(4, 3, -3)
]

vertices = 5 # Number of vertices


source = 0 # Source vertex

# Run Bellman-Ford
distances = bellman_ford(vertices, edges, source)

if distances:
print("Shortest distances from source vertex 0:")
for i in range(vertices):
print(f"Vertex {i}: {distances[i]}")

Python Output

yaml
CopyEdit
Shortest distances from source vertex 0:
Vertex 0: 0
Vertex 1: -1
Vertex 2: 2
Vertex 3: -2
Vertex 4: 1

Explanation

 The algorithm calculates the shortest distances from vertex 0 to all other vertices.

23
 The final distances are:
o From 0 to 0: 0
o From 0 to 1: -1
o From 0 to 2: 2
o From 0 to 3: -2
o From 0 to 4: 1

The algorithm correctly handles negative weights, and after V-1 iterations, the shortest paths are
found. If there were any negative weight cycles, the algorithm would report it.

24

You might also like