-
Notifications
You must be signed in to change notification settings - Fork 0
/
modified_dijkstra.py
54 lines (40 loc) · 1.46 KB
/
modified_dijkstra.py
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
from heapq import heappush, heappop
def modified_dijkstra(graph: dict, src: int, V: int) -> list:
"""Modified Dijkstra's algorithm that handles negative weights.
Args:
graph (dict): Graph as adjacency list with (vertex, weight) pairs
src (int): Source vertex
V (int): Number of vertices
Returns:
list: Shortest distances or None if negative cycle exists
"""
dist = [float("inf")] * V
dist[src] = 0
# Priority queue: (distance, vertex)
pq = [(0, src)]
# Track number of relaxations per vertex
relax_count = [0] * V
while pq:
d, u = heappop(pq)
# Check for negative cycle
relax_count[u] += 1
if relax_count[u] >= V:
return None # Negative cycle detected
# Process neighbors
for v, weight in graph[u]:
if dist[u] + weight < dist[v]:
dist[v] = dist[u] + weight
heappush(pq, (dist[v], v))
return dist
# Example usage
if __name__ == "__main__":
# Graph with negative edges but no negative cycles
graph = {0: [(1, 4), (2, 1)], 1: [(3, 1)], 2: [(1, -2)], 3: []} # Negative edge
# Graph with negative cycle
graph_cycle = {
0: [(1, 1)],
1: [(2, -3)],
2: [(1, 1)], # Creates cycle: 1->2->1 with total weight -2
}
print("Graph without negative cycle:", modified_dijkstra(graph, 0, 4))
print("Graph with negative cycle:", modified_dijkstra(graph_cycle, 0, 3))