Bellman Ford Negative Cycle Detect

Detect negative cycles in weighted graphs using Bellman-Ford for arbitrage detection.

PythonData Integrity

Code

snippet
def bellman_ford(edges, vertices, source):
    dist = {v: float('inf') for v in vertices}
    dist[source] = 0
    for _ in range(len(vertices) - 1):
        for u, v, w in edges:
            if dist[u] + w < dist[v]:
                dist[v] = dist[u] + w
    for u, v, w in edges:
        if dist[u] + w < dist[v]:
            return None, True
    return dist, False