{
  "id": "graphs/bellman-ford",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/graphs/bellman-ford",
  "topic": {
    "slug": "graphs",
    "title": "Graphs"
  },
  "title": "Bellman–Ford",
  "tagline": "Slower than Dijkstra, and it can do the one thing Dijkstra can't.",
  "vizKind": "graph",
  "complexity": {
    "time": {
      "best": "O(E)",
      "average": "O(V × E)",
      "worst": "O(V × E)"
    },
    "space": "O(V)",
    "growth": "quadratic",
    "note": "V−1 rounds, each sweeping all E edges: O(V × E). That's markedly slower than Dijkstra's O(E log V). You pay that price for two things Dijkstra cannot give you: negative edges, and negative-cycle detection."
  },
  "code": {
    "javascript": "function bellmanFord(nodes, edges, start) {\n  const dist = {};\n  for (const v of nodes) dist[v] = Infinity;\n  dist[start] = 0;\n\n  // Relax EVERY edge, n-1 times. No cleverness at all.\n  for (let round = 1; round < nodes.length; round++) {\n    for (const [u, v, w] of edges) {\n      if (dist[u] + w < dist[v]) dist[v] = dist[u] + w;\n    }\n  }\n\n  // One more round. If anything still improves, a negative\n  // cycle exists — and \"shortest\" has no meaning.\n  for (const [u, v, w] of edges) {\n    if (dist[u] + w < dist[v]) return null;   // negative cycle\n  }\n\n  return dist;\n}",
    "python": "def bellman_ford(nodes, edges, start):\n    dist = {v: float('inf') for v in nodes}\n    dist[start] = 0\n\n    # Relax EVERY edge, n-1 times. No cleverness at all.\n    for _ in range(len(nodes) - 1):\n        for u, v, w in edges:\n            if dist[u] + w < dist[v]:\n                dist[v] = dist[u] + w\n\n\n    # One more round. If anything still improves, a negative\n    # cycle exists — and \"shortest\" has no meaning.\n    for u, v, w in edges:\n        if dist[u] + w < dist[v]:\n            return None                  # negative cycle\n\n    return dist\n",
    "java": "Map<String,Integer> bellmanFord(List<String> nodes, List<Edge> edges, String start) {\n  Map<String,Integer> dist = new HashMap<>();\n  for (String v : nodes) dist.put(v, INF);\n  dist.put(start, 0);\n\n  // Relax EVERY edge, n-1 times. No cleverness at all.\n  for (int round = 1; round < nodes.size(); round++) {\n    for (Edge e : edges) {\n      if (dist.get(e.u) + e.w < dist.get(e.v))\n        dist.put(e.v, dist.get(e.u) + e.w);\n    }\n  }\n\n  // One more round. If anything still improves, a negative\n  // cycle exists — and \"shortest\" has no meaning.\n  for (Edge e : edges) {\n    if (dist.get(e.u) + e.w < dist.get(e.v)) return null;\n  }\n\n  return dist;\n}",
    "cpp": "map<string,int> bellmanFord(vector<string>& nodes, vector<Edge>& edges, string start) {\n  map<string,int> dist;\n  for (auto& v : nodes) dist[v] = INF;\n  dist[start] = 0;\n\n  // Relax EVERY edge, n-1 times. No cleverness at all.\n  for (int round = 1; round < (int)nodes.size(); round++) {\n    for (auto& [u, v, w] : edges) {\n      if (dist[u] + w < dist[v]) dist[v] = dist[u] + w;\n    }\n  }\n\n  // One more round. If anything still improves, a negative\n  // cycle exists — and \"shortest\" has no meaning.\n  for (auto& [u, v, w] : edges) {\n    if (dist[u] + w < dist[v]) return {};   // negative cycle\n  }\n\n  return dist;\n}"
  },
  "defaultInput": [
    1
  ],
  "defaultTarget": null,
  "inputHint": "The pink edge C→B costs −4. Watch a negative edge make a longer route cheaper.",
  "pitfalls": [
    {
      "wrong": "Reaching for Dijkstra on a graph with negative edges.",
      "right": "It returns a wrong answer without any error — the worst failure mode there is. Negative weights mean Bellman–Ford."
    },
    {
      "wrong": "Skipping the final extra round.",
      "right": "That round is the negative-cycle detector, and it's half the reason to use this algorithm. Without it you'll happily report distances that don't exist."
    },
    {
      "wrong": "Running only a few rounds because 'it looks settled'.",
      "right": "You need V−1 rounds in the worst case. Do add the early exit — if a whole round changes nothing, stop — but don't guess a smaller number."
    },
    {
      "wrong": "Thinking a negative cycle just means 'a very cheap route'.",
      "right": "It means there is *no* shortest path. You could loop forever, getting cheaper each time. The right answer is 'this question has no answer'."
    }
  ],
  "quiz": [
    {
      "q": "Why does Dijkstra fail on negative edges?",
      "options": [
        "It crashes",
        "It finalises a node as soon as it's nearest, assuming a longer route can never be cheaper — which a negative edge makes false",
        "It runs forever",
        "It doesn't fail"
      ],
      "answer": 1,
      "why": "Dijkstra's proof depends on distances only ever growing as you travel. A negative edge breaks that, so the 'final' distance it commits to can later turn out to be beatable — and it never re-checks."
    },
    {
      "q": "How does Bellman–Ford detect a negative cycle?",
      "options": [
        "It counts the edges",
        "It runs one extra round — if anything STILL improves, a negative cycle exists",
        "It looks for negative weights",
        "It can't detect one"
      ],
      "answer": 1,
      "why": "After V−1 rounds everything should be final. If another sweep still finds an improvement, there must be a loop you can ride round to keep getting cheaper — so no shortest path exists."
    },
    {
      "q": "What do you give up by using Bellman–Ford instead of Dijkstra?",
      "options": [
        "Correctness",
        "Speed — O(V × E) instead of O(E log V)",
        "The ability to handle cycles",
        "Nothing"
      ],
      "answer": 1,
      "why": "It's markedly slower, because it brute-forces every edge V−1 times instead of being greedy. You pay that for negative edges and cycle detection."
    }
  ],
  "problems": [
    {
      "name": "Cheapest Flights Within K Stops",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/cheapest-flights-within-k-stops/"
    },
    {
      "name": "Network Delay Time",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/network-delay-time/"
    },
    {
      "name": "Find the City With the Smallest Number of Neighbors",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 17,
    "frames": [
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B ∞",
              "x": 0.38,
              "y": 0.2
            },
            {
              "id": "C",
              "label": "C ∞",
              "x": 0.38,
              "y": 0.8
            },
            {
              "id": "D",
              "label": "D ∞",
              "x": 0.66,
              "y": 0.5
            },
            {
              "id": "E",
              "label": "E ∞",
              "x": 0.9,
              "y": 0.5
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "start": "A",
          "negativeEdge": "C → B = −4"
        },
        "explanation": "Look at the pink edge: C → B costs **−4**. Dijkstra would go badly wrong here — it finalises B early on the cheap-looking direct route and never reconsiders. Bellman–Ford makes no assumptions at all, so it copes.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B ∞",
              "x": 0.38,
              "y": 0.2
            },
            {
              "id": "C",
              "label": "C ∞",
              "x": 0.38,
              "y": 0.8
            },
            {
              "id": "D",
              "label": "D ∞",
              "x": 0.66,
              "y": 0.5
            },
            {
              "id": "E",
              "label": "E ∞",
              "x": 0.9,
              "y": 0.5
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 7,
        "variables": {
          "round": 1,
          "of": 4
        },
        "explanation": "Round 1 of 4. Try to improve *every* edge, in order. No cleverness — just sweep the lot.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "pivot"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "active"
            },
            {
              "id": "B",
              "label": "B ∞",
              "x": 0.38,
              "y": 0.2,
              "role": "found"
            },
            {
              "id": "C",
              "label": "C ∞",
              "x": 0.38,
              "y": 0.8
            },
            {
              "id": "D",
              "label": "D ∞",
              "x": 0.66,
              "y": 0.5
            },
            {
              "id": "E",
              "label": "E ∞",
              "x": 0.9,
              "y": 0.5
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6,
              "role": "found"
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "A→B",
          "weight": 6,
          "via": 6,
          "known": "∞"
        },
        "explanation": "A→B: 0 + 6 = 6, better than ∞. Improve it.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "active"
            },
            {
              "id": "B",
              "label": "B 6",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C ∞",
              "x": 0.38,
              "y": 0.8,
              "role": "found"
            },
            {
              "id": "D",
              "label": "D ∞",
              "x": 0.66,
              "y": 0.5
            },
            {
              "id": "E",
              "label": "E ∞",
              "x": 0.9,
              "y": 0.5
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3,
              "role": "found"
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "A→C",
          "weight": 3,
          "via": 3,
          "known": "∞"
        },
        "explanation": "A→C: 0 + 3 = 3, better than ∞. Improve it.",
        "stats": {
          "comparisons": 2,
          "swaps": 1
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B 6",
              "x": 0.38,
              "y": 0.2,
              "role": "found"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "active"
            },
            {
              "id": "D",
              "label": "D ∞",
              "x": 0.66,
              "y": 0.5
            },
            {
              "id": "E",
              "label": "E ∞",
              "x": 0.9,
              "y": 0.5
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "found"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "C→B",
          "weight": -4,
          "via": -1,
          "known": "6"
        },
        "explanation": "C→B: 3 + -4 = -1, better than 6. Improve it. Notice a *negative* edge just made a route cheaper — this is precisely what Dijkstra cannot cope with.",
        "stats": {
          "comparisons": 3,
          "swaps": 2
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "active"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D ∞",
              "x": 0.66,
              "y": 0.5,
              "role": "found"
            },
            {
              "id": "E",
              "label": "E ∞",
              "x": 0.9,
              "y": 0.5
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2,
              "role": "found"
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "B→D",
          "weight": 2,
          "via": 1,
          "known": "∞"
        },
        "explanation": "B→D: -1 + 2 = 1, better than ∞. Improve it.",
        "stats": {
          "comparisons": 4,
          "swaps": 3
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "active"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "compare"
            },
            {
              "id": "E",
              "label": "E ∞",
              "x": 0.9,
              "y": 0.5
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8,
              "role": "compare"
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "C→D",
          "weight": 8,
          "via": 11,
          "known": "1"
        },
        "explanation": "C→D: 3 + 8 = 11, no better than 1. Leave it.",
        "stats": {
          "comparisons": 5,
          "swaps": 4
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "active"
            },
            {
              "id": "E",
              "label": "E ∞",
              "x": 0.9,
              "y": 0.5,
              "role": "found"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3,
              "role": "found"
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "D→E",
          "weight": 3,
          "via": 4,
          "known": "∞"
        },
        "explanation": "D→E: 1 + 3 = 4, better than ∞. Improve it.",
        "stats": {
          "comparisons": 6,
          "swaps": 4
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 7,
        "variables": {
          "round": 2,
          "of": 4
        },
        "explanation": "Round 2 of 4. Try to improve *every* edge, in order. No cleverness — just sweep the lot.",
        "stats": {
          "comparisons": 6,
          "swaps": 5
        },
        "accent": "pivot"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "active"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "compare"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6,
              "role": "compare"
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "A→B",
          "weight": 6,
          "via": 6,
          "known": "-1"
        },
        "explanation": "A→B: 0 + 6 = 6, no better than -1. Leave it.",
        "stats": {
          "comparisons": 7,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "active"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "compare"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3,
              "role": "compare"
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "A→C",
          "weight": 3,
          "via": 3,
          "known": "3"
        },
        "explanation": "A→C: 0 + 3 = 3, no better than 3. Leave it.",
        "stats": {
          "comparisons": 8,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "compare"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "active"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "compare"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "C→B",
          "weight": -4,
          "via": -1,
          "known": "-1"
        },
        "explanation": "C→B: 3 + -4 = -1, no better than -1. Leave it.",
        "stats": {
          "comparisons": 9,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "active"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "compare"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2,
              "role": "compare"
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "B→D",
          "weight": 2,
          "via": 1,
          "known": "1"
        },
        "explanation": "B→D: -1 + 2 = 1, no better than 1. Leave it.",
        "stats": {
          "comparisons": 10,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "active"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "compare"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8,
              "role": "compare"
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "C→D",
          "weight": 8,
          "via": 11,
          "known": "1"
        },
        "explanation": "C→D: 3 + 8 = 11, no better than 1. Leave it.",
        "stats": {
          "comparisons": 11,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "active"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "compare"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3,
              "role": "compare"
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "edge": "D→E",
          "weight": 3,
          "via": 4,
          "known": "4"
        },
        "explanation": "D→E: 1 + 3 = 4, no better than 4. Leave it.",
        "stats": {
          "comparisons": 12,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 7,
        "variables": {
          "round": 2,
          "settled": true
        },
        "explanation": "A whole round with nothing improved. Everything has settled, so we can stop early — the remaining rounds would change nothing.",
        "stats": {
          "comparisons": 12,
          "swaps": 5
        },
        "accent": "sorted"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A 0",
              "x": 0.12,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B -1",
              "x": 0.38,
              "y": 0.2,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C 3",
              "x": 0.38,
              "y": 0.8,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D 1",
              "x": 0.66,
              "y": 0.5,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E 4",
              "x": 0.9,
              "y": 0.5,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "weight": 6
            },
            {
              "from": "A",
              "to": "C",
              "weight": 3
            },
            {
              "from": "C",
              "to": "B",
              "weight": -4,
              "role": "pivot"
            },
            {
              "from": "B",
              "to": "D",
              "weight": 2
            },
            {
              "from": "C",
              "to": "D",
              "weight": 8
            },
            {
              "from": "D",
              "to": "E",
              "weight": 3
            }
          ],
          "directed": true
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 15,
        "variables": {
          "result": "A=0  B=-1  C=3  D=1  E=4",
          "negativeCycle": false
        },
        "explanation": "Done: A=0  B=-1  C=3  D=1  E=4. No negative cycle — one extra sweep improved nothing, which proves these distances are final. Bellman–Ford is slower than Dijkstra (O(V·E) against O(E log V)), and in exchange it handles negative edges and detects the impossible case.",
        "stats": {
          "comparisons": 12,
          "swaps": 5
        },
        "accent": "sorted"
      }
    ]
  }
}