# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
# ---

def bellmanFord(G, s):
      n = len(G)
  cost = [float('inf')]*n
  cost[s]=0
  path = [-1]*n

  #relax
  for _ in range(n-1):
    for u in range(n):
      for v, w in G[u]:
        if cost[u] + w < cost[v]:
          cost[v] = cost[u]+w
          path[v]=u
  
  #check negative cygroupse
  for u in range(n):
    for v, w in G[u]:
      if cost[u]+w<cost[v]:
        return None, None

  return path, cost

StarcoderdataJupyterScriptsDedupFiltered