-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbellman_ford.py
More file actions
79 lines (63 loc) · 2.58 KB
/
bellman_ford.py
File metadata and controls
79 lines (63 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Bellman-Ford Shortest Path Algorithm
Bellman-Ford algorithm finds shortest paths from a source vertex to all other
vertices in a weighted graph. Unlike Dijkstra's, it can handle negative edge
weights and can detect negative cycles.
Time Complexity: O(V * E) where V is vertices and E is edges
Space Complexity: O(V)
Note: Slower than Dijkstra's but more versatile (handles negative weights).
"""
def bellman_ford(graph, start):
"""
Finds shortest paths from start vertex to all other vertices.
Args:
graph: Dictionary representing weighted graph {node: [(neighbor, weight), ...]}
start: Starting vertex
Returns:
Tuple (distances, has_negative_cycle)
- distances: Dictionary of shortest distances {vertex: distance}
- has_negative_cycle: Boolean indicating if negative cycle was detected
"""
# Get all vertices
vertices = set(graph.keys())
for neighbors in graph.values():
vertices.update(neighbor for neighbor, _ in neighbors)
# Initialize distances: all vertices start at infinity except start (0)
distances = {vertex: float('infinity') for vertex in vertices}
distances[start] = 0
# Relax edges V-1 times
for _ in range(len(vertices) - 1):
for vertex in graph:
for neighbor, weight in graph.get(vertex, []):
if distances[vertex] != float('infinity'):
if distances[vertex] + weight < distances[neighbor]:
distances[neighbor] = distances[vertex] + weight
# Check for negative cycles
# If we can still relax edges, there's a negative cycle
has_negative_cycle = False
for vertex in graph:
for neighbor, weight in graph.get(vertex, []):
if distances[vertex] != float('infinity'):
if distances[vertex] + weight < distances[neighbor]:
has_negative_cycle = True
break
if has_negative_cycle:
break
return distances, has_negative_cycle
# Example usage
if __name__ == "__main__":
# Example weighted graph: {node: [(neighbor, weight), ...]}
graph = {
'A': [('B', 4), ('C', 2)],
'B': [('C', 1), ('D', 5)],
'C': [('D', 8), ('E', 10)],
'D': [('E', 2)],
'E': []
}
print("Shortest distances from 'A' using Bellman-Ford:")
distances, has_cycle = bellman_ford(graph, 'A')
if has_cycle:
print("Warning: Negative cycle detected!")
else:
for vertex, distance in distances.items():
print(f" {vertex}: {distance}")