# 圖形 g 與起點 s # 若有負迴路存在即傳回 True bellmanFord(g, s): for v ← 0 to g.N-1: dist[v] ← INF dist[s] ← 0 for t ← 0 to N-1: updated ← False for u ← 0 to g.N-1: if dist[u] = INF: continue for e in g.adjLists[u]: if dist[e.v] > dist[u] + e.weight dist[e.v] ← dist[u] + e.weight updated ← True if t = N-1: return True # 檢測負迴路 if not updated: break # 若未再更新即結束 return false # 不存在負迴路