def bellman_ford(edges, vertex_count, start):
distances = [float('inf')] * vertex_count
distances[start] = 0
for _ in range(vertex_count - 1):
for u, v, w in edges:
if distances[u] != float('inf') and distances[u] + w < distances[v]:
distances[v] = distances[u] + w
for u, v, w in edges:
if distances[u] != float('inf') and distances[u] + w < distances[v]:
return None # Negative cycle detected
return distances
# Example graph edges: (from, to, weight)
edges = [
(0, 1, 6),
(0, 2, 7),
(1, 2, 8),
(1, 3, 5),
(1, 4, -4),
(2, 3, -3),
(2, 4, 9),
(3, 1, -2),
(4, 0, 2),
(4, 3, 7)
]
result = bellman_ford(edges, 5, 0)
print(result if result is not None else 'Negative cycle detected')