{"version":1,"lectureId":"01M14TX77JPFEBWST5NE8CZPB8","attempt":1,"publication":{"slug":"dijkstras-algorithm-a-spreading-frontier","title":"Dijkstra's Algorithm as a Spreading Frontier","subject":"computer-science","summary":"Dijkstra's algorithm, taught as a frontier that spreads outward from a source across a weighted graph. We build a small six-node graph, give every node a tentative distance, and watch those numbers fall as edges are relaxed and the settled region grows one node at a time. Then we do the part that matters: we prove that the greedy choice — always settling the unsettled node with the smallest tentative distance — is safe, by tracing where any cheaper route would have to go, and showing exactly how that argument collapses the moment a single negative edge appears. We finish with the priority-queue implementation, the predecessor pointers that recover the routes themselves, and the O((V + E) log V) bound.","metaDescription":"Watch Dijkstra's algorithm as a frontier of settled nodes spreading outward, and see why the greedy choice is provably safe.","transcript":"Dijkstra's algorithm finds the cheapest route from one source to every node in a graph. The picture to keep is a filled region that spreads outward, with the edges crossing its boundary forming a live frontier. Here are six nodes. S is the source, and A, B, C, D and E are the other places. The gray connections are weighted edges. Their numbers measure cost, so a path is cheap when the sum of its edge weights is small. The weights are four, two, one, five, eight, ten, two again, six, and three. Every one is non-negative. That condition will carry the proof. This blue patch around S is the settled region we will grow. The two yellow edges leaving it are the frontier. Keep that boundary alive in your eye while the numbers decide which node the patch reaches next. First see why counting edges is not enough. Start at S, then B, then D, then E. This route uses only three edges. The three-hop route pays two, ten, and three. Together those weights total fifteen. The other route goes S, B, A, C, D, E. Its five weights are two, one, five, two, and three. Together they total thirteen. Fifteen is greater than thirteen. More hops can cost less, so every decision must use the accumulated weights rather than the hop count. Each node carries a tentative distance: the cheapest route found so far. Unreached nodes have no number yet. The source is different. Reaching S from S costs zero, so zero is final from the start. Relaxation is the only arithmetic move. On this picture, u is S, v is A, and w of u v is the four on their edge. A route through u costs d of u plus that edge weight. If that new cost is smaller than d of v, then replace d of v with it. The test comes first. The assignment happens only when the test wins. For A, the source contributes zero and the edge contributes four. Together they make four, so A receives four. For B, the source contributes zero and the edge contributes two. Together they make two, so B receives two. Now compare the unsettled rim. A carries four. B carries two. The smallest tentative distance belongs to B, so B is the greedy choice. Relax B to A. B contributes two and the edge contributes one. Together they make three, better than four, so A changes to three. A's predecessor changes from S to B at the same decision. The route record now agrees with the lower value. Next, B contributes two and the edge to C contributes eight. Together they make ten, so C receives ten. For D, B contributes two and the edge contributes ten. Together they make twelve, so D receives twelve. The live rim now reads A at three, C at ten, and D at twelve. Those are the finite candidates touching the blue region. The smallest is A at three. Settle A, grow the blue region, and update the crossing edges around the same weighted graph. Relax A to C. A contributes three and the edge contributes five. Together they make eight, so C falls from ten to eight. Now C has eight, D has twelve, and E is unreached. The smallest is C. Settle C and let the blue region reach it. Relax C to D. C contributes eight and the edge contributes two. Together they make ten, so D drops from twelve to ten immediately. Relax C to E. C contributes eight and the edge contributes six. Together they make fourteen, so E receives fourteen. The rim has D at ten and E at fourteen. Ten is smaller, so D is next. The region grows through D and the yellow boundary moves outward. Relax D to E. D contributes ten and the edge contributes three. Together they make thirteen, so E falls from fourteen to thirteen. Only E remains, carrying thirteen. Settle it. The blue region now covers every node, and no crossing edge remains. Read the final distances directly from the node labels: S zero, B two, A three, C eight, D ten, and E thirteen. The predecessor tree gives the route to E backwards: E to D, D to C, C to A, A to B, and B to S. Reverse that walk and its total is thirteen. The arithmetic explains every update, and the growing region shows every greedy commitment. What remains is to prove why the smallest label on the frontier is safe to make permanent. Freeze one generic greedy step. The blue polygon is the settled region. Its yellow crossing edges are the frontier, and C carries the smallest tentative distance there: eight. Suppose a cheaper route P to C exists. It begins at S inside the polygon and ends at C outside, so it must cross the frontier for the first time. Call the first unsettled node on that route x. The only part of P we need is the short local prefix from u to x, ending exactly at x. Every edge leaving a settled node was relaxed when that node settled. Therefore the cost already spent along the prefix P sub x cannot be less than the tentative distance written at x. The greedy choice points to C because eight is the smallest frontier label. Since x is also on that frontier, d of x is at least d of C, which is eight. So the proposed cheaper route has already spent at least eight when it reaches x. It still has the remaining segment from x to C ahead of it. Every remaining edge has non-negative weight. Continuing from x cannot reduce the amount already spent, so P cannot reach C for less than eight. That contradiction makes eight final. The same frontier-crossing argument applies at every greedy step, which is why settled labels never need to reopen. Now change only one fact. This three-node graph has S to X of weight one, S to Y of weight four, and a red edge from Y to X of weight minus four. Relaxing S gives X one and Y four. The greedy rule points at X because one is smaller, so it settles X and grows the region there. But the other route walks S to Y and then Y to X. Its two weights are four and minus four. Together they total zero. Zero is smaller than one. The negative edge discounts the route after X was declared final, so the non-negative step in the proof has failed. Dijkstra's algorithm therefore requires non-negative weights. When a graph really contains negative edges, Bellman-Ford is the algorithm to reach for instead. The whole algorithm fits into four numbered steps. The graph is in the state just after B settles: the blue region contains S and B, and its yellow crossing edges form the current frontier. First, initialize the labels; the source gets zero. Second, choose the smallest unsettled label; here A at three is next. Third, relax its outgoing edges. Fourth, repeat while the frontier still has a node. A plain array finds the minimum by scanning every key: three, ten, twelve, infinity. One scan costs V, and V choices give V times V, which is order V squared. A priority queue stores exactly the unsettled rim. Its smallest key is A's three. Pop A, settle it, and let the filled region grow through A. Relaxing A improves C. Decrease C's queue key from ten to eight, and the node label beside C changes to eight with the queue key. Those are the two heap operations Dijkstra needs: pop-min chooses the next settled node, and decrease-key records a better route without scanning the whole graph. Every node is popped once, giving V pop-min operations. Every edge can trigger a decrease-key, giving at most E lowerings. A binary heap makes each operation cost log V. The total is order V plus E, multiplied by log V. Finish the same run. C settles at eight. Its relaxation lowers D to ten. D then settles, which makes E reachable at fourteen. Relaxing D lowers E to thirteen, and E settles last. The blue region grows at each commitment. Each improvement also records a predecessor. The surviving choices form the shortest-path tree: S to B, B to A, A to C, C to D, and D to E. The final state carries the whole algorithm: a filled settled region, the tree that recovers every route, and no frontier edge left to cross. Non-negative weights are what make each greedy step permanent.","watch":{"version":1,"scenes":[{"title":"The Frontier Spreads","start":0,"end":315.774125,"objects":{"answers":"a Table [text] that says \"Node $S$ $B$ $A$ $C$ $D$ $E$ Distance 0 2 3 8 10 13 Via start $S$ $B$ $A$ $C$ $D$\" (rows=(('Node', '$S$', '$B$', '$A$', '$C$', '$D$', '$E$'), ('Distance…, header=True)","calc_ac":"an Arithmetic [text] that says \"$3 5 8$\" (operator='+', operands=('3', '5'), result='8')","calc_ba":"an Arithmetic [text] that says \"$2 1 3$\" (operator='+', operands=('2', '1'), result='3')","calc_bc":"an Arithmetic [text] that says \"$2 8 10$\" (operator='+', operands=('2', '8'), result='10')","calc_bd":"an Arithmetic [text] that says \"$2 10 12$\" (operator='+', operands=('2', '10'), result='12')","calc_cd":"an Arithmetic [text] that says \"$8 2 10$\" (operator='+', operands=('8', '2'), result='10')","calc_ce":"an Arithmetic [text] that says \"$8 6 14$\" (operator='+', operands=('8', '6'), result='14')","calc_de":"an Arithmetic [text] that says \"$10 3 13$\" (operator='+', operands=('10', '3'), result='13')","calc_sa":"an Arithmetic [text] that says \"$0 4 4$\" (operator='+', operands=('0', '4'), result='4')","calc_sb":"an Arithmetic [text] that says \"$0 2 2$\" (operator='+', operands=('0', '2'), result='2')","card":"a Title that says \"Algorithms — Dijkstra's Algorithm as a Spreading Frontier\"","cheap_route":"an Arithmetic [text] that says \"$2 1 5 2 3 13$\" (operator='+', operands=('2', '1', '5', '2', '3'), result='13')","cost":"a Math [text] that says \"$w(P) = sum_(e in P) w(e)$\"","d_a":"a VariableNumber (initial_value=4.0, format_spec='.0f')","d_c":"a VariableNumber (initial_value=10.0, format_spec='.0f')","d_d":"a VariableNumber (initial_value=12.0, format_spec='.0f')","d_e":"a VariableNumber (initial_value=14.0, format_spec='.0f')","edge_ab":"a Line [gray] labelled \"1\" drawn in plane (start=(3.6, 6.2), end=(3.6, 1.4))","edge_ac":"a Line [gray] labelled \"5\" drawn in plane (start=(3.6, 6.2), end=(6.6, 4.9))","edge_bc":"a Line [gray] labelled \"8\" drawn in plane (start=(3.6, 1.4), end=(6.6, 4.9))","edge_bd":"a Line [gray] labelled \"10\" drawn in plane (start=(3.6, 1.4), end=(6.6, 1.9))","edge_cd":"a Line [gray] labelled \"2\" drawn in plane (start=(6.6, 4.9), end=(6.6, 1.9))","edge_ce":"a Line [gray] labelled \"6\" drawn in plane (start=(6.6, 4.9), end=(9.0, 3.6))","edge_de":"a Line [gray] labelled \"3\" drawn in plane (start=(6.6, 1.9), end=(9.0, 3.6))","edge_sa":"a Line [gray] labelled \"4\" drawn in plane (start=(1.1, 3.7), end=(3.6, 6.2))","edge_sb":"a Line [gray] labelled \"2\" drawn in plane (start=(1.1, 3.7), end=(3.6, 1.4))","heading_answers":"a Heading that says \"The Finished Shortest-Path Tree\"","heading_routes":"a Heading that says \"Weighted Paths Need Weighted Reasoning\"","heading_run":"a Heading that says \"Settle the Smallest, Then Relax\"","node_a":"a Point [text] labelled \"A\" drawn in plane (location=(3.6, 6.2))","node_a_live":"a Point [text] labelled \"4\" drawn in plane (location=(3.6, 6.2), show_marker=False)","node_b":"a Point [text] labelled \"B\" drawn in plane (location=(3.6, 1.4))","node_b_live":"a Point [text] labelled \"2\" drawn in plane (location=(3.6, 1.4), show_marker=False)","node_c":"a Point [text] labelled \"C\" drawn in plane (location=(6.6, 4.9))","node_c_live":"a Point [text] labelled \"10\" drawn in plane (location=(6.6, 4.9), show_marker=False)","node_d":"a Point [text] labelled \"D\" drawn in plane (location=(6.6, 1.9))","node_d_live":"a Point [text] labelled \"12\" drawn in plane (location=(6.6, 1.9), show_marker=False)","node_e":"a Point [text] labelled \"E\" drawn in plane (location=(9.0, 3.6))","node_e_live":"a Point [text] labelled \"14\" drawn in plane (location=(9.0, 3.6), show_marker=False)","node_s":"a Point [text] labelled \"S\" drawn in plane (location=(1.1, 3.7))","node_s_live":"a Point [text] labelled \"0\" drawn in plane (location=(1.1, 3.7), show_marker=False)","plane":"a Figure (x_range=(0.0, 10.4), y_range=(0.0, 7.4), aspect=(10.4, 7.4))","point":"a Point [yellow] drawn in plane (location=(1.1, 3.7))","point_10":"a Point [yellow] drawn in plane (location=(6.6, 4.9))","point_11":"a Point [yellow] drawn in plane (location=(6.6, 1.9))","point_12":"a Point [yellow] drawn in plane (location=(6.6, 4.9))","point_13":"a Point [yellow] drawn in plane (location=(6.6, 1.9))","point_14":"a Point [yellow] drawn in plane (location=(9.0, 3.6))","point_15":"a Point [yellow] drawn in plane (location=(6.6, 1.9))","point_16":"a Point [yellow] drawn in plane (location=(9.0, 3.6))","point_17":"a Point [yellow] drawn in plane (location=(9.0, 3.6))","point_18":"a Point [yellow] drawn in plane (location=(9.0, 3.6))","point_19":"a Point [yellow] drawn in plane (location=(6.6, 1.9))","point_2":"a Point [yellow] drawn in plane (location=(3.6, 1.4))","point_20":"a Point [yellow] drawn in plane (location=(6.6, 4.9))","point_21":"a Point [yellow] drawn in plane (location=(3.6, 6.2))","point_22":"a Point [yellow] drawn in plane (location=(3.6, 1.4))","point_23":"a Point [yellow] drawn in plane (location=(1.1, 3.7))","point_3":"a Point [yellow] drawn in plane (location=(6.6, 1.9))","point_4":"a Point [yellow] drawn in plane (location=(9.0, 3.6))","point_5":"a Point [yellow] drawn in plane (location=(1.1, 3.7))","point_6":"a Point [yellow] drawn in plane (location=(3.6, 6.2))","point_7":"a Point [yellow] drawn in plane (location=(3.6, 6.2))","point_8":"a Point [yellow] drawn in plane (location=(3.6, 1.4))","point_9":"a Point [yellow] drawn in plane (location=(3.6, 6.2))","region_all":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (7.0, 1.15), (9.6, 3.05), (9.55, 4.…, fill_opacity=0.2)","region_s":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.35), (1.1, 4.35), (1.8, 3.35)), fill_opacity=0.2)","region_sb":"a Polygon [blue] drawn in plane (vertices=((0.45, 4.1), (0.55, 3.15), (3.15, 0.7), (4.25, 0.95), (4.2, 2.…, fill_opacity=0.2)","region_sba":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (4.25, 0.9), (4.25, 6.45), (3.05, 6…, fill_opacity=0.2)","region_sbac":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (4.25, 0.9), (7.2, 4.35), (7.1, 5.5…, fill_opacity=0.2)","region_sbacd":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (7.15, 1.2), (7.3, 5.35), (3.05, 6.…, fill_opacity=0.2)","relax_if":"a Math [text] that says \"$upright(\"if\") quad d[u] + w(u, v) < d[v]$\"","relax_then":"a Math [text] that says \"$upright(\"then\") quad d[v] arrow.l d[u] + w(u, v)$\"","route_compare":"a Math [text] that says \"$15 > 13$\"","short_route":"an Arithmetic [text] that says \"$2 10 3 15$\" (operator='+', operands=('2', '10', '3'), result='15')"},"beats":[{"start":0,"say":"Dijkstra's algorithm finds the cheapest route from one source to every node in a graph. The picture to keep is a filled region that spreads outward, with the edges crossing its boundary forming a live frontier.","live":[],"does":[[0,"card is shown on the screen, written out."],[1.5,"card: enter:write-left-to-right."],[11.366,"card is hidden from the screen — left the board."]]},{"start":12.565999999999999,"say":"Here are six nodes. S is the source, and A, B, C, D and E are the other places. The gray connections are weighted edges. Their numbers measure cost, so a path is cheap when the sum of its edge weights is small.","live":null,"does":[[12.565999999999999,"heading_routes is shown on the screen, written out."],[12.565999999999999,"plane is shown on the screen, written out."],[15.491999999999999,"node_s is shown on the screen, written out."],[19.044999999999998,"node_a is shown on the screen, written out."],[19.224999999999998,"node_b is shown on the screen, written out."],[19.404999999999998,"node_c is shown on the screen, written out."],[19.584999999999997,"node_d is shown on the screen, written out."],[19.764999999999997,"node_e is shown on the screen, written out."],[20.798,"edge_sa is shown on the screen, drawn."],[20.938,"edge_sb is shown on the screen, drawn."],[21.078,"edge_ab is shown on the screen, drawn."],[21.218,"edge_ac is shown on the screen, drawn."],[21.357999999999997,"edge_bc is shown on the screen, drawn."],[21.497999999999998,"edge_bd is shown on the screen, drawn."],[21.637999999999998,"edge_cd is shown on the screen, drawn."],[21.778,"edge_ce is shown on the screen, drawn."],[21.918,"edge_de is shown on the screen, drawn."],[26.278,"plane moves to a new place on the board."],[26.278,"cost is shown on the screen, written out."]]},{"start":28.898,"say":"The weights are four, two, one, five, eight, ten, two again, six, and three. Every one is non-negative. That condition will carry the proof.","live":["cost","plane","heading_routes","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de"],"does":[[29.861,"edge_sa is emphasized."],[30.303,"edge_sa is no longer emphasized."],[30.303,"edge_sb is emphasized."],[30.755000000000003,"edge_sb is no longer emphasized."],[30.755000000000003,"edge_ab is emphasized."],[31.103,"edge_ab is no longer emphasized."],[31.103,"edge_ac is emphasized."],[31.556,"edge_ac is no longer emphasized."],[31.556,"edge_bc is emphasized."],[31.869000000000003,"edge_bc is no longer emphasized."],[31.869000000000003,"edge_bd is emphasized."],[32.496,"edge_bd is no longer emphasized."],[32.496,"edge_cd is emphasized."],[33.251000000000005,"edge_cd is no longer emphasized."],[33.251000000000005,"edge_ce is emphasized."],[34.06400000000001,"edge_ce is no longer emphasized."],[34.06400000000001,"edge_de is emphasized."],[38.754,"edge_de is no longer emphasized."]]},{"start":39.354,"say":"This blue patch around S is the settled region we will grow. The two yellow edges leaving it are the frontier. Keep that boundary alive in your eye while the numbers decide which node the patch reaches next.","live":null,"does":[[39.923,"region_s is shown on the screen, faded in."],[43.963,"edge_sa is emphasized."],[43.963,"edge_sb is emphasized."]]},{"start":52.4,"say":"First see why counting edges is not enough. Start at S, then B, then D, then E. This route uses only three edges.","live":["cost","plane","heading_routes","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","region_s"],"does":[[56.394000000000005,"point is shown on the screen, grown."],[56.394000000000005,"The segment (1.1, 3.7) to (3.6, 1.4) in plane is lit up."],[57.45,"point_2 is shown on the screen, grown."],[58.394000000000005,"point is hidden from the screen."],[58.414,"point_3 is shown on the screen, grown."],[58.414,"The segment (3.6, 1.4) to (6.6, 1.9) in plane is lit up."],[59.285000000000004,"point_4 is shown on the screen, grown."],[59.285000000000004,"The segment (6.6, 1.9) to (9.0, 3.6) in plane is lit up."],[59.45,"point_2 is hidden from the screen."],[60.414,"point_3 is hidden from the screen."],[61.285000000000004,"point_4 is hidden from the screen."]]},{"start":63.4835,"say":"The three-hop route pays two, ten, and three. Together those weights total fifteen.","live":null,"does":[[65.26,"short_route is shown on the screen, written out."],[65.26,"short_route (the \"2\" part) is emphasized."],[65.875,"short_route is shown on the screen, written out."],[65.875,"short_route (the \"2\" part) is no longer emphasized."],[65.875,"short_route (the \"10\" part) is emphasized."],[66.572,"short_route is shown on the screen, written out."],[66.572,"short_route (the \"10\" part) is no longer emphasized."],[66.572,"short_route (the \"3\" part) is emphasized."],[67.501,"short_route is shown on the screen, drawn."],[67.501,"short_route (the \"3\" part) is no longer emphasized."],[68.301,"short_route is shown on the screen, drawn."],[68.917,"short_route is shown on the screen, written out."]]},{"start":70.5505,"say":"The other route goes S, B, A, C, D, E. Its five weights are two, one, five, two, and three. Together they total thirteen.","live":null,"does":[[71.07300000000001,"short_route is hidden from the screen."],[71.07300000000001,"plane: retire a lit segment (unemphasize_line)."],[72.571,"The segment (3.6, 1.4) to (3.6, 6.2) in plane is lit up."],[72.872,"The segment (3.6, 6.2) to (6.6, 4.9) in plane is lit up."],[73.186,"The segment (6.6, 4.9) to (6.6, 1.9) in plane is lit up."],[74.67200000000001,"cheap_route is shown on the screen, written out."],[74.67200000000001,"cheap_route (the \"5\" part) is emphasized."],[75.403,"cheap_route is shown on the screen, written out."],[75.403,"cheap_route (the \"2\" part) is emphasized."],[75.403,"cheap_route (the \"5\" part) is no longer emphasized."],[76.03,"cheap_route is shown on the screen, written out."],[76.03,"cheap_route (the \"2\" part) is no longer emphasized."],[76.03,"cheap_route (the \"1\" part) is emphasized."],[76.90100000000001,"cheap_route is shown on the screen, written out."],[76.90100000000001,"cheap_route (the \"1\" part) is no longer emphasized."],[76.90100000000001,"cheap_route (the \"2\" part) is emphasized."],[77.447,"cheap_route is shown on the screen, written out."],[77.447,"cheap_route (the \"2\" part) is no longer emphasized."],[77.447,"cheap_route (the \"3\" part) is emphasized."],[78.631,"cheap_route is shown on the screen, drawn."],[78.631,"cheap_route (the \"3\" part) is no longer emphasized."],[79.431,"cheap_route is shown on the screen, drawn."],[79.595,"cheap_route is shown on the screen, written out."]]},{"start":81.286,"say":"Fifteen is greater than thirteen. More hops can cost less, so every decision must use the accumulated weights rather than the hop count.","live":null,"does":[[81.634,"route_compare is shown on the screen, written out."],[81.634,"route_compare (the \"15\" part) is emphasized."],[82.784,"route_compare (the \"13\" part) is emphasized."],[82.784,"route_compare (the \"15\" part) is no longer emphasized."],[90.13250000000001,"cheap_route is hidden from the screen — left the board."],[90.13250000000001,"cost is hidden from the screen — left the board."],[90.13250000000001,"heading_routes is hidden from the screen — left the board."],[90.13250000000001,"route_compare is hidden from the screen — left the board."],[90.13250000000001,"route_compare (the \"13\" part) is no longer emphasized."],[90.13250000000001,"plane: retire a lit segment (unemphasize_line)."],[90.13250000000001,"plane: retire a lit segment (unemphasize_line)."],[90.13250000000001,"plane: retire a lit segment (unemphasize_line)."],[90.13250000000001,"plane: retire a lit segment (unemphasize_line)."],[90.13250000000001,"plane: retire a lit segment (unemphasize_line)."]]},{"start":90.7325,"say":"Each node carries a tentative distance: the cheapest route found so far. Unreached nodes have no number yet. The source is different. Reaching S from S costs zero, so zero is final from the start.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","region_s"],"does":[[90.7325,"heading_run is shown on the screen, written out."],[102.412,"node_s_live is shown on the screen, written out."],[103.886,"node_s is indicated — a transient flash."]]},{"start":105.85,"say":"Relaxation is the only arithmetic move. On this picture, u is S, v is A, and w of u v is the four on their edge. A route through u costs d of u plus that edge weight.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","region_s","heading_run","node_s_live"],"does":[[110.872,"point_5 is shown on the screen, grown."],[111.893,"point_6 is shown on the screen, grown."],[112.872,"point_5 is hidden from the screen."],[113.893,"point_6 is hidden from the screen."],[114.11099999999999,"edge_sa is indicated — a transient flash."]]},{"start":120.103,"say":"If that new cost is smaller than d of v, then replace d of v with it. The test comes first. The assignment happens only when the test wins.","live":null,"does":[[120.434,"relax_if is shown on the screen, written out."],[120.434,"relax_if (the \"upright(\"if\")\" part) is emphasized."],[122.628,"relax_then is shown on the screen, written out."],[122.628,"relax_if (the \"upright(\"if\")\" part) is no longer emphasized."],[122.628,"relax_then (the \"upright(\"then\")\" part) is emphasized."],[129.80349999999999,"relax_then (the \"upright(\"then\")\" part) is no longer emphasized."]]},{"start":130.4035,"say":"For A, the source contributes zero and the edge contributes four. Together they make four, so A receives four.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","region_s","relax_if","relax_then","heading_run","node_s_live"],"does":[[132.72500000000002,"calc_sa is shown on the screen, written out."],[132.72500000000002,"calc_sa (the \"0\" part) is emphasized."],[134.49,"calc_sa is shown on the screen, written out."],[134.49,"calc_sa (the \"0\" part) is no longer emphasized."],[134.49,"calc_sa (the \"4\" part) is emphasized."],[135.628,"calc_sa is shown on the screen, drawn."],[135.628,"calc_sa (the \"4\" part) is no longer emphasized."],[136.42799999999997,"calc_sa is shown on the screen, drawn."],[136.522,"node_a_live is shown on the screen, written out."],[136.522,"calc_sa is shown on the screen, written out."],[137.57800000000003,"edge_sa is indicated — a transient flash."]]},{"start":139.39700000000002,"say":"For B, the source contributes zero and the edge contributes two. Together they make two, so B receives two.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","region_s","relax_if","relax_then","heading_run","node_s_live","node_a_live"],"does":[[139.39700000000002,"calc_sa is hidden from the screen."],[141.03400000000002,"calc_sb is shown on the screen, written out."],[141.03400000000002,"calc_sb (the \"0\" part) is emphasized."],[142.70600000000002,"calc_sb is shown on the screen, written out."],[142.70600000000002,"calc_sb (the \"0\" part) is no longer emphasized."],[142.70600000000002,"calc_sb (the \"2\" part) is emphasized."],[143.74,"calc_sb is shown on the screen, drawn."],[143.74,"calc_sb (the \"2\" part) is no longer emphasized."],[144.52900000000002,"node_b_live is shown on the screen, written out."],[144.52900000000002,"calc_sb is shown on the screen, written out."],[144.54,"calc_sb is shown on the screen, drawn."],[145.50400000000002,"edge_sb is indicated — a transient flash."]]},{"start":147.25950000000003,"say":"Now compare the unsettled rim. A carries four. B carries two. The smallest tentative distance belongs to B, so B is the greedy choice.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","region_s","relax_if","relax_then","heading_run","node_s_live","node_a_live","node_b_live"],"does":[[150.79500000000004,"point_7 is shown on the screen, grown."],[152.55900000000005,"point_8 is shown on the screen, grown."],[152.79500000000004,"point_7 is hidden from the screen."],[153.82500000000005,"node_b is indicated — a transient flash."],[154.55900000000005,"point_8 is hidden from the screen."],[157.41200000000003,"region_s becomes a Polygon [blue] drawn in plane (vertices=((0.45, 4.1), (0.55, 3.15), (3.15, 0.7), (4.25, 0.95), (4.2, 2.…, fill_opacity=0.2)."],[157.41200000000003,"edge_sb is no longer emphasized."],[157.41200000000003,"edge_ab is emphasized."],[157.41200000000003,"edge_bc is emphasized."],[157.41200000000003,"edge_bd is emphasized."]]},{"start":158.70350000000002,"say":"Relax B to A. B contributes two and the edge contributes one. Together they make three, better than four, so A changes to three.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","relax_if","relax_then","heading_run","node_s_live","node_a_live","node_b_live","region_sb"],"does":[[158.70350000000002,"calc_sb is hidden from the screen."],[161.32100000000003,"calc_ba is shown on the screen, written out."],[161.32100000000003,"calc_ba (the \"2\" part) is emphasized."],[162.93500000000003,"calc_ba is shown on the screen, written out."],[162.93500000000003,"calc_ba (the \"2\" part) is no longer emphasized."],[162.93500000000003,"calc_ba (the \"1\" part) is emphasized."],[163.73600000000002,"calc_ba is shown on the screen, drawn."],[163.73600000000002,"calc_ba (the \"1\" part) is no longer emphasized."],[164.536,"calc_ba is shown on the screen, drawn."],[164.54900000000004,"node_a_live is redrawn as the numbers it depends on change."],[164.54900000000004,"calc_ba is shown on the screen, written out."],[164.54900000000004,"d_a ticks to 3.0."]]},{"start":168.95700000000002,"say":"A's predecessor changes from S to B at the same decision. The route record now agrees with the lower value.","live":null,"does":[[170.32700000000003,"edge_sa is indicated — a transient flash."],[170.32700000000003,"edge_ab is indicated — a transient flash."]]},{"start":176.7315,"say":"Next, B contributes two and the edge to C contributes eight. Together they make ten, so C receives ten.","live":null,"does":[[176.7315,"calc_ba is hidden from the screen."],[178.078,"calc_bc is shown on the screen, written out."],[178.078,"calc_bc (the \"2\" part) is emphasized."],[180.05200000000002,"calc_bc is shown on the screen, written out."],[180.05200000000002,"calc_bc (the \"2\" part) is no longer emphasized."],[180.05200000000002,"calc_bc (the \"8\" part) is emphasized."],[180.99300000000002,"calc_bc is shown on the screen, drawn."],[180.99300000000002,"calc_bc (the \"8\" part) is no longer emphasized."],[181.793,"calc_bc is shown on the screen, drawn."],[181.852,"node_c_live is shown on the screen, written out."],[181.852,"calc_bc is shown on the screen, written out."],[182.92000000000002,"edge_bc is indicated — a transient flash."]]},{"start":184.79700000000003,"say":"For D, B contributes two and the edge contributes ten. Together they make twelve, so D receives twelve.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","relax_if","relax_then","heading_run","node_s_live","node_a_live","node_b_live","region_sb","node_c_live"],"does":[[184.79700000000003,"calc_bc is hidden from the screen."],[186.538,"calc_bd is shown on the screen, written out."],[186.538,"calc_bd (the \"2\" part) is emphasized."],[188.222,"calc_bd is shown on the screen, written out."],[188.222,"calc_bd (the \"2\" part) is no longer emphasized."],[188.222,"calc_bd (the \"10\" part) is emphasized."],[189.267,"calc_bd is shown on the screen, drawn."],[189.267,"calc_bd (the \"10\" part) is no longer emphasized."],[190.06699999999998,"calc_bd is shown on the screen, drawn."],[190.115,"node_d_live is shown on the screen, written out."],[190.115,"calc_bd is shown on the screen, written out."],[191.20600000000002,"edge_bd is indicated — a transient flash."]]},{"start":193.211,"say":"The live rim now reads A at three, C at ten, and D at twelve. Those are the finite candidates touching the blue region.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","relax_if","relax_then","heading_run","node_s_live","node_a_live","node_b_live","region_sb","node_c_live","node_d_live"],"does":[[195.347,"point_9 is shown on the screen, grown."],[196.25300000000001,"point_10 is shown on the screen, grown."],[197.347,"point_9 is hidden from the screen."],[197.448,"point_11 is shown on the screen, grown."],[198.25300000000001,"point_10 is hidden from the screen."],[199.448,"point_11 is hidden from the screen."]]},{"start":201.8625,"say":"The smallest is A at three. Settle A, grow the blue region, and update the crossing edges around the same weighted graph.","live":null,"does":[[203.528,"node_a is indicated — a transient flash."],[205.63,"region_s becomes a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (4.25, 0.9), (4.25, 6.45), (3.05, 6…, fill_opacity=0.2)."],[207.453,"edge_sa is no longer emphasized."],[207.453,"edge_ab is no longer emphasized."],[207.453,"edge_ac is emphasized."]]},{"start":210.6185,"say":"Relax A to C. A contributes three and the edge contributes five. Together they make eight, so C falls from ten to eight.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","relax_if","relax_then","heading_run","node_s_live","node_a_live","node_b_live","region_sb","node_c_live","node_d_live","region_sba"],"does":[[210.6185,"calc_bd is hidden from the screen."],[213.57900000000004,"calc_ac is shown on the screen, written out."],[213.57900000000004,"calc_ac (the \"3\" part) is emphasized."],[215.23900000000003,"calc_ac is shown on the screen, written out."],[215.23900000000003,"calc_ac (the \"3\" part) is no longer emphasized."],[215.23900000000003,"calc_ac (the \"5\" part) is emphasized."],[216.44700000000003,"calc_ac is shown on the screen, drawn."],[216.44700000000003,"calc_ac (the \"5\" part) is no longer emphasized."],[217.247,"calc_ac is shown on the screen, drawn."],[217.49200000000002,"node_c_live is redrawn as the numbers it depends on change."],[217.49200000000002,"calc_ac is shown on the screen, written out."],[217.49200000000002,"d_c ticks to 8.0."],[218.65300000000002,"edge_bc is indicated — a transient flash."],[218.65300000000002,"edge_ac is indicated — a transient flash."]]},{"start":220.8435,"say":"Now C has eight, D has twelve, and E is unreached. The smallest is C. Settle C and let the blue region reach it.","live":null,"does":[[222.09700000000004,"point_12 is shown on the screen, grown."],[223.27,"point_13 is shown on the screen, grown."],[224.09700000000004,"point_12 is hidden from the screen."],[224.24500000000003,"point_14 is shown on the screen, grown."],[225.27,"point_13 is hidden from the screen."],[226.24500000000003,"point_14 is hidden from the screen."],[226.358,"node_c is indicated — a transient flash."],[229.69000000000003,"region_s becomes a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (4.25, 0.9), (7.2, 4.35), (7.1, 5.5…, fill_opacity=0.2)."],[229.69000000000003,"edge_ac is no longer emphasized."],[229.69000000000003,"edge_bc is no longer emphasized."],[229.69000000000003,"edge_cd is emphasized."],[229.69000000000003,"edge_ce is emphasized."]]},{"start":231.1205,"say":"Relax C to D. C contributes eight and the edge contributes two. Together they make ten, so D drops from twelve to ten immediately.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","relax_if","relax_then","heading_run","node_s_live","node_a_live","node_b_live","region_sb","node_c_live","node_d_live","region_sba","region_sbac"],"does":[[231.1205,"calc_ac is hidden from the screen."],[234.09799999999998,"calc_cd is shown on the screen, written out."],[234.09799999999998,"calc_cd (the \"8\" part) is emphasized."],[235.724,"calc_cd is shown on the screen, written out."],[235.724,"calc_cd (the \"8\" part) is no longer emphasized."],[235.724,"calc_cd (the \"2\" part) is emphasized."],[236.95399999999998,"calc_cd is shown on the screen, drawn."],[236.95399999999998,"calc_cd (the \"2\" part) is no longer emphasized."],[237.75399999999996,"calc_cd is shown on the screen, drawn."],[237.837,"node_d_live is redrawn as the numbers it depends on change."],[237.837,"calc_cd is shown on the screen, written out."],[237.837,"d_d ticks to 10.0."],[239.03199999999998,"edge_bd is indicated — a transient flash."],[239.03199999999998,"edge_cd is indicated — a transient flash."]]},{"start":242.1985,"say":"Relax C to E. C contributes eight and the edge contributes six. Together they make fourteen, so E receives fourteen.","live":null,"does":[[242.1985,"calc_cd is hidden from the screen."],[245.12400000000002,"calc_ce is shown on the screen, written out."],[245.12400000000002,"calc_ce (the \"8\" part) is emphasized."],[246.76100000000002,"calc_ce is shown on the screen, written out."],[246.76100000000002,"calc_ce (the \"8\" part) is no longer emphasized."],[246.76100000000002,"calc_ce (the \"6\" part) is emphasized."],[248.084,"calc_ce is shown on the screen, drawn."],[248.084,"calc_ce (the \"6\" part) is no longer emphasized."],[248.884,"calc_ce is shown on the screen, drawn."],[248.89700000000002,"node_e_live is shown on the screen, written out."],[248.89700000000002,"calc_ce is shown on the screen, written out."],[250.08100000000002,"edge_ce is indicated — a transient flash."]]},{"start":252.0625,"say":"The rim has D at ten and E at fourteen. Ten is smaller, so D is next. The region grows through D and the yellow boundary moves outward.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","relax_if","relax_then","heading_run","node_s_live","node_a_live","node_b_live","region_sb","node_c_live","node_d_live","region_sba","region_sbac","node_e_live"],"does":[[253.467,"point_15 is shown on the screen, grown."],[254.768,"point_16 is shown on the screen, grown."],[255.467,"point_15 is hidden from the screen."],[256.544,"node_d is indicated — a transient flash."],[256.76800000000003,"point_16 is hidden from the screen."],[259.168,"region_s becomes a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (7.15, 1.2), (7.3, 5.35), (3.05, 6.…, fill_opacity=0.2)."],[260.654,"edge_bd is no longer emphasized."],[260.654,"edge_cd is no longer emphasized."],[260.654,"edge_de is emphasized."]]},{"start":262.891,"say":"Relax D to E. D contributes ten and the edge contributes three. Together they make thirteen, so E falls from fourteen to thirteen.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","relax_if","relax_then","heading_run","node_s_live","node_a_live","node_b_live","region_sb","node_c_live","node_d_live","region_sba","region_sbac","node_e_live","region_sbacd"],"does":[[262.891,"calc_ce is hidden from the screen."],[265.851,"calc_de is shown on the screen, written out."],[265.851,"calc_de (the \"10\" part) is emphasized."],[267.337,"calc_de is shown on the screen, written out."],[267.337,"calc_de (the \"10\" part) is no longer emphasized."],[267.337,"calc_de (the \"3\" part) is emphasized."],[268.44000000000005,"calc_de is shown on the screen, drawn."],[268.44000000000005,"calc_de (the \"3\" part) is no longer emphasized."],[269.24000000000007,"calc_de is shown on the screen, drawn."],[269.3,"node_e_live is redrawn as the numbers it depends on change."],[269.3,"calc_de is shown on the screen, written out."],[269.3,"d_e ticks to 13.0."],[270.635,"edge_ce is indicated — a transient flash."],[270.635,"edge_de is indicated — a transient flash."]]},{"start":273.4055,"say":"Only E remains, carrying thirteen. Settle it. The blue region now covers every node, and no crossing edge remains.","live":null,"does":[[275.57700000000006,"point_17 is shown on the screen, grown."],[276.97,"node_e is indicated — a transient flash."],[277.57700000000006,"point_17 is hidden from the screen."],[278.90900000000005,"region_s becomes a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (7.0, 1.15), (9.6, 3.05), (9.55, 4.…, fill_opacity=0.2)."],[280.42900000000003,"edge_ce is no longer emphasized."],[280.42900000000003,"edge_de is no longer emphasized."],[282.2525,"calc_de is hidden from the screen."],[282.2525,"heading_run is hidden from the screen — left the board."],[282.2525,"relax_if is hidden from the screen — left the board."],[282.2525,"relax_then is hidden from the screen — left the board."]]},{"start":282.8525,"say":"Read the final distances directly from the node labels: S zero, B two, A three, C eight, D ten, and E thirteen.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s_live","node_a_live","node_b_live","region_sb","node_c_live","node_d_live","region_sba","region_sbac","node_e_live","region_sbacd","region_all"],"does":[[282.8525,"heading_answers is shown on the screen, written out."],[282.8525,"answers is shown on the screen, written out."],[286.637,"node_s_live is emphasized."],[287.29900000000004,"node_s_live is no longer emphasized."],[287.29900000000004,"node_b_live is emphasized."],[288.04200000000003,"node_b_live is no longer emphasized."],[288.04200000000003,"node_a_live is emphasized."],[288.75,"node_a_live is no longer emphasized."],[288.75,"node_c_live is emphasized."],[289.54,"node_c_live is no longer emphasized."],[289.54,"node_d_live is emphasized."],[290.724,"node_d_live is no longer emphasized."],[290.724,"node_e_live is emphasized."]]},{"start":292.415,"say":"The predecessor tree gives the route to E backwards: E to D, D to C, C to A, A to B, and B to S. Reverse that walk and its total is thirteen.","live":["plane","node_s","node_a","node_b","node_c","node_d","node_e","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s_live","node_a_live","node_b_live","region_sb","node_c_live","node_d_live","region_sba","region_sbac","node_e_live","region_sbacd","region_all","answers","heading_answers"],"does":[[292.415,"node_e_live is no longer emphasized."],[296.13000000000005,"point_18 is shown on the screen, grown."],[296.56000000000006,"point_19 is shown on the screen, grown."],[296.56000000000006,"The segment (6.6, 1.9) to (9.0, 3.6) in plane is lit up."],[297.24500000000006,"point_20 is shown on the screen, grown."],[297.24500000000006,"The segment (6.6, 4.9) to (6.6, 1.9) in plane is lit up."],[297.98800000000006,"point_21 is shown on the screen, grown."],[297.98800000000006,"The segment (3.6, 6.2) to (6.6, 4.9) in plane is lit up."],[298.13000000000005,"point_18 is hidden from the screen."],[298.56000000000006,"point_19 is hidden from the screen."],[298.66100000000006,"point_22 is shown on the screen, grown."],[298.66100000000006,"The segment (3.6, 1.4) to (3.6, 6.2) in plane is lit up."],[299.24500000000006,"point_20 is hidden from the screen."],[299.451,"point_23 is shown on the screen, grown."],[299.451,"The segment (1.1, 3.7) to (3.6, 1.4) in plane is lit up."],[299.98800000000006,"point_21 is hidden from the screen."],[300.66100000000006,"point_22 is hidden from the screen."],[301.451,"point_23 is hidden from the screen."]]},{"start":304.5095,"say":"The arithmetic explains every update, and the growing region shows every greedy commitment. What remains is to prove why the smallest label on the frontier is safe to make permanent.","live":null,"does":[]}]},{"title":"Why Settling Is Safe","start":315.774125,"end":457.8747708333333,"objects":{"bound":"a Derivation [text] that says \"$w(P_x) &>= d[x] \\ &>= d[C] = 8$\"","claim":"a Math [text] that says \"$d[C] = 8 quad upright(\"is final\")$\"","frontier_uc":"a Line [gray] drawn in proof (start=(3.4, 4.7), end=(8.7, 3.2))","frontier_ux":"a Line [gray] drawn in proof (start=(3.4, 4.7), end=(6.1, 4.0))","frontier_vx":"a Line [gray] drawn in proof (start=(3.5, 1.5), end=(6.1, 4.0))","heading_negative":"a Heading that says \"One Negative Edge Breaks the Proof\"","heading_proof":"a Heading that says \"Why the Greedy Step Is Safe\"","inside_su":"a Line [gray] drawn in proof (start=(1.0, 3.2), end=(3.4, 4.7))","inside_sv":"a Line [gray] drawn in proof (start=(1.0, 3.2), end=(3.5, 1.5))","neg":"a Figure (x_range=(0.0, 6.4), y_range=(0.0, 4.4), aspect=(6.4, 4.4))","neg_region_s":"a Polygon [blue] drawn in neg (vertices=((0.35, 1.8), (1.05, 3.0), (1.75, 1.8)), fill_opacity=0.22)","neg_region_sx":"a Polygon [blue] drawn in neg (vertices=((0.3, 1.8), (0.8, 2.85), (4.45, 4.05), (5.3, 3.5), (4.55, 2.8)), fill_opacity=0.22)","neg_s":"a Point [text] labelled \"S: 0\" drawn in neg (location=(1.0, 2.2))","neg_sx":"a Line [gray] labelled \"1\" drawn in neg (start=(1.0, 2.2), end=(4.7, 3.45))","neg_sy":"a Line [gray] labelled \"4\" drawn in neg (start=(1.0, 2.2), end=(4.7, 0.85))","neg_x":"a Point [text] labelled \"X\" drawn in neg (location=(4.7, 3.45))","neg_x_live":"a Point [text] labelled \"1\" drawn in neg (location=(4.7, 3.45), show_marker=False)","neg_y":"a Point [text] labelled \"Y\" drawn in neg (location=(4.7, 0.85))","neg_y_live":"a Point [text] labelled \"4\" drawn in neg (location=(4.7, 0.85), show_marker=False)","neg_yx":"a Line [red] labelled \"-4\" drawn in neg (start=(4.7, 0.85), end=(4.7, 3.45))","negative_compare":"a Math [text] that says \"$0 < 1$\"","negative_lesson":"a Math [text] that says \"$w(e) < 0 quad arrow.r quad upright(\"use Bellman-Ford\")$\"","negative_total":"an Arithmetic [text] that says \"$4 -4 0$\" (operator='+', operands=('4', '-4'), result='0')","nonnegative":"a Math [text] that says \"$w(e) >= 0$\"","point":"a Point [yellow] drawn in proof (location=(8.7, 3.2))","point_10":"a Point [yellow] drawn in neg (location=(4.7, 3.45))","point_11":"a Point [yellow] drawn in neg (location=(1.0, 2.2))","point_12":"a Point [yellow] drawn in neg (location=(4.7, 0.85))","point_13":"a Point [yellow] drawn in neg (location=(4.7, 3.45))","point_2":"a Point [yellow] drawn in proof (location=(1.0, 3.2))","point_3":"a Point [yellow] drawn in proof (location=(8.7, 3.2))","point_4":"a Point [yellow] drawn in proof (location=(3.4, 4.7))","point_5":"a Point [yellow] drawn in proof (location=(6.1, 4.0))","point_6":"a Point [yellow] drawn in proof (location=(8.7, 3.2))","point_7":"a Point [yellow] drawn in proof (location=(6.1, 4.0))","point_8":"a Point [yellow] drawn in proof (location=(6.1, 4.0))","point_9":"a Point [yellow] drawn in proof (location=(8.7, 3.2))","prefix":"a Line [red] labelled \"$P_x$\" drawn in proof (start=(3.4, 4.7), end=(6.1, 4.0))","proof":"a Figure (x_range=(0.0, 10.0), y_range=(0.0, 6.5), aspect=(10.0, 6.5))","proof_c":"a Point [text] labelled \"C: 8\" drawn in proof (location=(8.7, 3.2))","proof_s":"a Point [text] labelled \"S: 0\" drawn in proof (location=(1.0, 3.2))","proof_u":"a Point [text] labelled \"u\" drawn in proof (location=(3.4, 4.7))","proof_v":"a Point [text] labelled \"upright(\"settled\")\" drawn in proof (location=(3.5, 1.5))","proof_x":"a Point [text] labelled \"x: 10\" drawn in proof (location=(6.1, 4.0))","remaining_xc":"a Line [gray] labelled \"upright(\"remainder\")\" drawn in proof (start=(6.1, 4.0), end=(8.7, 3.2))","settled_region":"a Polygon [blue] drawn in proof (vertices=((0.35, 3.2), (2.8, 0.7), (4.2, 1.0), (4.15, 5.25), (2.8, 5.75)), fill_opacity=0.22)"},"beats":[{"start":315.774125,"say":"Freeze one generic greedy step. The blue polygon is the settled region. Its yellow crossing edges are the frontier, and C carries the smallest tentative distance there: eight.","live":[],"does":[[315.774125,"heading_proof is shown on the screen, written out."],[315.774125,"proof is shown on the screen, written out."],[315.774125,"inside_su is shown on the screen, drawn."],[315.774125,"proof_s is shown on the screen, written out."],[318.72312500000004,"settled_region is shown on the screen, faded in."],[319.861125,"inside_sv is shown on the screen, drawn."],[319.861125,"proof_u is shown on the screen, written out."],[319.861125,"proof_v is shown on the screen, written out."],[321.602125,"frontier_ux is shown on the screen, drawn."],[321.602125,"frontier_uc is shown on the screen, drawn."],[321.602125,"frontier_vx is shown on the screen, drawn."],[321.602125,"frontier_ux is emphasized."],[321.602125,"frontier_uc is emphasized."],[321.602125,"frontier_vx is emphasized."],[323.053125,"proof_x is shown on the screen, written out."],[323.053125,"remaining_xc is shown on the screen, drawn."],[324.238125,"proof_c is shown on the screen, written out."],[324.238125,"point is shown on the screen, grown."],[326.238125,"point is hidden from the screen."],[327.407125,"proof moves to a new place on the board."],[327.407125,"claim is shown on the screen, written out."]]},{"start":328.762125,"say":"Suppose a cheaper route P to C exists. It begins at S inside the polygon and ends at C outside, so it must cross the frontier for the first time.","live":["claim","proof","heading_proof","settled_region","inside_su","inside_sv","proof_s","proof_u","proof_v","proof_x","proof_c","frontier_ux","frontier_uc","frontier_vx","remaining_xc"],"does":[[330.68912500000005,"point_3 is shown on the screen, grown."],[332.68912500000005,"point_3 is hidden from the screen."],[333.09312500000004,"point_2 is shown on the screen, grown."],[333.429125,"settled_region is indicated — a transient flash."],[335.09312500000004,"point_2 is hidden from the screen."],[337.296125,"point_4 is shown on the screen, grown."],[339.296125,"point_4 is hidden from the screen."]]},{"start":340.12462500000004,"say":"Call the first unsettled node on that route x. The only part of P we need is the short local prefix from u to x, ending exactly at x.","live":null,"does":[[343.06212500000004,"point_5 is shown on the screen, grown."],[345.06212500000004,"point_5 is hidden from the screen."],[346.06912500000004,"prefix is shown on the screen, drawn."]]},{"start":351.197125,"say":"Every edge leaving a settled node was relaxed when that node settled. Therefore the cost already spent along the prefix P sub x cannot be less than the tentative distance written at x.","live":["claim","proof","heading_proof","settled_region","inside_su","inside_sv","proof_s","proof_u","proof_v","proof_x","proof_c","frontier_ux","frontier_uc","frontier_vx","remaining_xc","prefix"],"does":[[351.928125,"prefix is indicated — a transient flash."],[352.59012500000006,"proof_u is indicated — a transient flash."],[355.678125,"bound is shown on the screen, written out."],[358.198125,"bound (the \"w(P_x)\" part) is emphasized."],[359.61412500000006,"bound (the \"d[x]\" part) is emphasized."],[359.61412500000006,"bound (the \"w(P_x)\" part) is no longer emphasized."]]},{"start":364.208125,"say":"The greedy choice points to C because eight is the smallest frontier label. Since x is also on that frontier, d of x is at least d of C, which is eight.","live":null,"does":[[365.78712500000006,"point_6 is shown on the screen, grown."],[367.78712500000006,"point_6 is hidden from the screen."],[369.236125,"bound is shown on the screen, written out."],[369.700125,"point_7 is shown on the screen, grown."],[371.700125,"point_7 is hidden from the screen."],[373.31112500000006,"bound (the \"d[x]\" part) is no longer emphasized."],[373.31112500000006,"bound (the \"d[C]\" part) is emphasized."],[373.94912500000004,"bound (the \"8\" part) is emphasized."],[373.94912500000004,"bound (the \"d[C]\" part) is no longer emphasized."]]},{"start":375.29262500000004,"say":"So the proposed cheaper route has already spent at least eight when it reaches x. It still has the remaining segment from x to C ahead of it.","live":null,"does":[[379.79712500000005,"point_8 is shown on the screen, grown."],[381.654125,"remaining_xc is emphasized."],[381.79712500000005,"point_8 is hidden from the screen."],[383.094125,"point_9 is shown on the screen, grown."]]},{"start":384.948125,"say":"Every remaining edge has non-negative weight. Continuing from x cannot reduce the amount already spent, so P cannot reach C for less than eight.","live":["claim","proof","heading_proof","settled_region","inside_su","inside_sv","proof_s","proof_u","proof_v","proof_x","proof_c","frontier_ux","frontier_uc","frontier_vx","remaining_xc","prefix","point_9"],"does":[[385.094125,"point_9 is hidden from the screen."],[386.62012500000003,"nonnegative is shown on the screen, written out."],[386.62012500000003,"nonnegative (the \"0\" part) is emphasized."],[388.338125,"remaining_xc is indicated — a transient flash."],[395.09512500000005,"nonnegative (the \"0\" part) is no longer emphasized."]]},{"start":395.695125,"say":"That contradiction makes eight final. The same frontier-crossing argument applies at every greedy step, which is why settled labels never need to reopen.","live":["claim","nonnegative","proof","heading_proof","settled_region","inside_su","inside_sv","proof_s","proof_u","proof_v","proof_x","proof_c","frontier_ux","frontier_uc","frontier_vx","remaining_xc","prefix"],"does":[[397.63412500000004,"proof_c is indicated — a transient flash."],[397.63412500000004,"claim (the \"upright(\"is final\")\" part) is emphasized."],[405.07612500000005,"bound is hidden from the screen — left the board."],[405.07612500000005,"claim is hidden from the screen — left the board."],[405.07612500000005,"heading_proof is hidden from the screen — left the board."],[405.07612500000005,"nonnegative is hidden from the screen — left the board."],[405.07612500000005,"proof is hidden from the screen — left the board."],[405.07612500000005,"settled_region is hidden from the screen — proof left the board."],[405.07612500000005,"inside_su is hidden from the screen — proof left the board."],[405.07612500000005,"inside_sv is hidden from the screen — proof left the board."],[405.07612500000005,"proof_s is hidden from the screen — proof left the board."],[405.07612500000005,"proof_u is hidden from the screen — proof left the board."],[405.07612500000005,"proof_v is hidden from the screen — proof left the board."],[405.07612500000005,"proof_x is hidden from the screen — proof left the board."],[405.07612500000005,"proof_c is hidden from the screen — proof left the board."],[405.07612500000005,"frontier_ux is hidden from the screen — proof left the board."],[405.07612500000005,"frontier_uc is hidden from the screen — proof left the board."],[405.07612500000005,"frontier_vx is hidden from the screen — proof left the board."],[405.07612500000005,"remaining_xc is hidden from the screen — proof left the board."],[405.07612500000005,"prefix is hidden from the screen — proof left the board."],[405.07612500000005,"claim (the \"upright(\"is final\")\" part) is no longer emphasized."]]},{"start":405.676125,"say":"Now change only one fact. This three-node graph has S to X of weight one, S to Y of weight four, and a red edge from Y to X of weight minus four.","live":[],"does":[[405.676125,"heading_negative is shown on the screen, written out."],[405.676125,"neg is shown on the screen, written out."],[405.676125,"neg_region_s is shown on the screen, faded in."],[405.676125,"neg_s is shown on the screen, written out."],[405.676125,"neg_x is shown on the screen, written out."],[405.676125,"neg_y is shown on the screen, written out."],[406.965125,"neg_sx is shown on the screen, drawn."],[406.965125,"neg_sx is emphasized."],[413.037125,"neg_sy is shown on the screen, drawn."],[413.037125,"neg_sx is no longer emphasized."],[413.037125,"neg_sy is emphasized."],[414.035125,"neg_yx is shown on the screen, drawn."],[414.035125,"neg_sy is no longer emphasized."],[415.753125,"neg_yx is emphasized."],[416.914625,"neg_yx is no longer emphasized."]]},{"start":417.514625,"say":"Relaxing S gives X one and Y four. The greedy rule points at X because one is smaller, so it settles X and grows the region there.","live":["neg","heading_negative","neg_region_s","neg_s","neg_x","neg_y","neg_sx","neg_sy","neg_yx"],"does":[[419.186125,"point_10 is shown on the screen, grown."],[419.511125,"neg_x_live is shown on the screen, written out."],[420.196125,"neg_y_live is shown on the screen, written out."],[421.186125,"point_10 is hidden from the screen."],[423.633125,"neg_x is indicated — a transient flash."],[425.699125,"neg_region_s becomes a Polygon [blue] drawn in neg (vertices=((0.3, 1.8), (0.8, 2.85), (4.45, 4.05), (5.3, 3.5), (4.55, 2.8)), fill_opacity=0.22)."]]},{"start":427.820625,"say":"But the other route walks S to Y and then Y to X. Its two weights are four and minus four. Together they total zero.","live":["neg","heading_negative","neg_s","neg_x","neg_y","neg_sx","neg_sy","neg_yx","neg_x_live","neg_y_live","neg_region_sx"],"does":[[429.330125,"point_11 is shown on the screen, grown."],[429.782125,"point_12 is shown on the screen, grown."],[429.782125,"neg_sy is emphasized."],[430.85012500000005,"point_13 is shown on the screen, grown."],[430.85012500000005,"neg_yx is emphasized."],[431.330125,"point_11 is hidden from the screen."],[431.782125,"point_12 is hidden from the screen."],[432.650125,"neg moves to a new place on the board."],[432.650125,"negative_total is shown on the screen, written out."],[432.650125,"negative_total (the \"4\" part) is emphasized."],[432.85012500000005,"point_13 is hidden from the screen."],[433.196125,"negative_total is shown on the screen, written out."],[433.196125,"negative_total (the \"4\" part) is no longer emphasized."],[433.196125,"negative_total (the \"-4\" part) is emphasized."],[434.368125,"negative_total is shown on the screen, drawn."],[434.368125,"negative_total (the \"-4\" part) is no longer emphasized."],[435.16812500000003,"negative_total is shown on the screen, drawn."],[435.599125,"negative_total is shown on the screen, written out."]]},{"start":437.046125,"say":"Zero is smaller than one. The negative edge discounts the route after X was declared final, so the non-negative step in the proof has failed.","live":null,"does":[[437.348125,"negative_compare is shown on the screen, written out."],[437.348125,"negative_compare (the \"0\" part) is emphasized."],[438.416125,"negative_compare (the \"0\" part) is no longer emphasized."],[438.416125,"negative_compare (the \"1\" part) is emphasized."],[439.960125,"neg_yx is indicated — a transient flash."],[446.787125,"negative_compare (the \"1\" part) is no longer emphasized."]]},{"start":447.38712499999997,"say":"Dijkstra's algorithm therefore requires non-negative weights. When a graph really contains negative edges, Bellman-Ford is the algorithm to reach for instead.","live":["negative_compare","neg","heading_negative","neg_s","neg_x","neg_y","neg_sx","neg_sy","neg_yx","neg_x_live","neg_y_live","neg_region_sx"],"does":[[447.38712499999997,"neg_sy is no longer emphasized."],[447.38712499999997,"neg_yx is no longer emphasized."],[453.227125,"negative_lesson is shown on the screen, written out."],[454.446125,"negative_lesson (the \"upright(\"use Bellman-Ford\")\" part) is emphasized."]]}]},{"title":"From Frontier to Priority Queue","start":457.8747708333333,"end":593.4649999999999,"objects":{"cost":"a Derivation [text] that says \"$upright(\"work\") &= V dot upright(\"pop-min\") + E dot upright(\"decrease-key\") \\ &= O((V + E) log V)$\"","d_c":"a VariableNumber (initial_value=10.0, format_spec='.0f')","d_d":"a VariableNumber (initial_value=12.0, format_spec='.0f')","d_e":"a VariableNumber (initial_value=14.0, format_spec='.0f')","edge_ab":"a Line [gray] labelled \"1\" drawn in plane (start=(3.6, 6.2), end=(3.6, 1.4))","edge_ac":"a Line [gray] labelled \"5\" drawn in plane (start=(3.6, 6.2), end=(6.6, 4.9))","edge_bc":"a Line [gray] labelled \"8\" drawn in plane (start=(3.6, 1.4), end=(6.6, 4.9))","edge_bd":"a Line [gray] labelled \"10\" drawn in plane (start=(3.6, 1.4), end=(6.6, 1.9))","edge_cd":"a Line [gray] labelled \"2\" drawn in plane (start=(6.6, 4.9), end=(6.6, 1.9))","edge_ce":"a Line [gray] labelled \"6\" drawn in plane (start=(6.6, 4.9), end=(9.0, 3.6))","edge_de":"a Line [gray] labelled \"3\" drawn in plane (start=(6.6, 1.9), end=(9.0, 3.6))","edge_sa":"a Line [gray] labelled \"4\" drawn in plane (start=(1.1, 3.7), end=(3.6, 6.2))","edge_sb":"a Line [gray] labelled \"2\" drawn in plane (start=(1.1, 3.7), end=(3.6, 1.4))","heading_cost":"a Heading that says \"Heap Cost and the Route Tree\"","heading_queue":"a Heading that says \"The Frontier as a Changing Queue\"","heading_steps":"a Heading that says \"The Algorithm, in Order\"","node_a":"a Point [text] labelled \"A\" drawn in plane (location=(3.6, 6.2))","node_b":"a Point [text] labelled \"B\" drawn in plane (location=(3.6, 1.4))","node_c":"a Point [text] labelled \"C\" drawn in plane (location=(6.6, 4.9))","node_d":"a Point [text] labelled \"D\" drawn in plane (location=(6.6, 1.9))","node_e":"a Point [text] labelled \"E\" drawn in plane (location=(9.0, 3.6))","node_s":"a Point [text] labelled \"S\" drawn in plane (location=(1.1, 3.7))","plane":"a Figure (x_range=(0.0, 10.4), y_range=(0.0, 7.4), aspect=(10.4, 7.4))","point":"a Point [yellow] drawn in plane (location=(3.6, 6.2))","point_2":"a Point [yellow] drawn in plane (location=(3.6, 6.2))","point_3":"a Point [yellow] drawn in plane (location=(6.6, 4.9))","point_4":"a Point [yellow] drawn in plane (location=(6.6, 4.9))","point_5":"a Point [yellow] drawn in plane (location=(6.6, 1.9))","point_6":"a Point [yellow] drawn in plane (location=(9.0, 3.6))","queue_0":"a Table [text] that says \"Node Key $A$ 3 $C$ 10 $D$ 12 $E$ $infinity$\" (rows=(('Node', 'Key'), ('$A$', '3'), ('$C$', '10'), ('$D$', '12'), (…, header=True)","queue_1":"a Table [text] that says \"Node Key $C$ 10 $D$ 12 $E$ $infinity$\" (rows=(('Node', 'Key'), ('$C$', '10'), ('$D$', '12'), ('$E$', '$infin…, header=True)","queue_2":"a Table [text] that says \"Node Key $C$ 8 $D$ 12 $E$ $infinity$\" (rows=(('Node', 'Key'), ('$C$', '8'), ('$D$', '12'), ('$E$', '$infini…, header=True)","region_all":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (7.0, 1.15), (9.6, 3.05), (9.55, 4.…, fill_opacity=0.2)","region_sb":"a Polygon [blue] drawn in plane (vertices=((0.45, 4.1), (0.55, 3.15), (3.15, 0.7), (4.25, 0.95), (4.2, 2.…, fill_opacity=0.2)","region_sba":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (4.25, 0.9), (4.25, 6.45), (3.05, 6…, fill_opacity=0.2)","region_sbac":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (4.25, 0.9), (7.2, 4.35), (7.1, 5.5…, fill_opacity=0.2)","region_sbacd":"a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (7.15, 1.2), (7.3, 5.35), (3.05, 6.…, fill_opacity=0.2)","scan_array":"a Table [text] that says \"$A$ $C$ $D$ $E$ 3 10 12 $infinity$\" (rows=(('$A$', '$C$', '$D$', '$E$'), ('3', '10', '12', '$infinity$')))","scan_cost":"a Math [text] that says \"$V dot V = O(V^2)$\"","steps":"a Block [text] that says \"Set $d[S] = 0$; leave every other node unreached. Choose and settle the unsettled node with smallest $d$. Relax every edge leaving that node and record predecessors. Repeat while an unsettled reachable node remains.\"","tree_statement":"a Math [text] that says \"$upright(\"predecessors\") arrow.r upright(\"shortest-path tree\")$\"","value_a":"a Point [text] labelled \"3\" drawn in plane (location=(3.6, 6.2), show_marker=False)","value_b":"a Point [text] labelled \"2\" drawn in plane (location=(3.6, 1.4), show_marker=False)","value_c":"a Point [text] labelled \"10\" drawn in plane (location=(6.6, 4.9), show_marker=False)","value_d":"a Point [text] labelled \"12\" drawn in plane (location=(6.6, 1.9), show_marker=False)","value_e":"a Point [text] labelled \"14\" drawn in plane (location=(9.0, 3.6), show_marker=False)","value_s":"a Point [text] labelled \"0\" drawn in plane (location=(1.1, 3.7), show_marker=False)"},"beats":[{"start":457.8747708333333,"say":"The whole algorithm fits into four numbered steps. The graph is in the state just after B settles: the blue region contains S and B, and its yellow crossing edges form the current frontier.","live":[],"does":[[457.8747708333333,"heading_steps is shown on the screen, written out."],[457.8747708333333,"plane is shown on the screen, written out."],[457.8747708333333,"edge_sa is shown on the screen, faded in."],[457.8747708333333,"edge_sb is shown on the screen, faded in."],[457.8747708333333,"edge_ab is shown on the screen, faded in."],[457.8747708333333,"edge_ac is shown on the screen, faded in."],[457.8747708333333,"edge_bc is shown on the screen, faded in."],[457.8747708333333,"edge_bd is shown on the screen, faded in."],[457.8747708333333,"edge_cd is shown on the screen, faded in."],[457.8747708333333,"edge_ce is shown on the screen, faded in."],[457.8747708333333,"edge_de is shown on the screen, faded in."],[457.8747708333333,"node_s is shown on the screen, faded in."],[457.8747708333333,"node_a is shown on the screen, faded in."],[457.8747708333333,"node_b is shown on the screen, faded in."],[457.8747708333333,"node_c is shown on the screen, faded in."],[457.8747708333333,"node_d is shown on the screen, faded in."],[457.8747708333333,"node_e is shown on the screen, faded in."],[457.8747708333333,"value_s is shown on the screen, faded in."],[457.8747708333333,"value_a is shown on the screen, faded in."],[457.8747708333333,"value_b is shown on the screen, faded in."],[457.8747708333333,"value_c is shown on the screen, faded in."],[457.8747708333333,"value_d is shown on the screen, faded in."],[459.48877083333326,"plane moves to a new place on the board."],[459.48877083333326,"steps is shown on the screen, written out."],[464.8287708333333,"region_sb is shown on the screen, faded in."],[467.5227708333333,"edge_sa is emphasized."],[467.5227708333333,"edge_ab is emphasized."],[467.5227708333333,"edge_bc is emphasized."],[467.5227708333333,"edge_bd is emphasized."]]},{"start":470.96727083333326,"say":"First, initialize the labels; the source gets zero. Second, choose the smallest unsettled label; here A at three is next. Third, relax its outgoing edges. Fourth, repeat while the frontier still has a node.","live":["steps","plane","heading_steps","region_sb","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s","node_a","node_b","node_c","node_d","node_e","value_s","value_a","value_b","value_c","value_d"],"does":[[471.31577083333326,"steps (the \"Set\" part) is emphasized."],[474.2057708333333,"node_s is indicated — a transient flash."],[475.2967708333333,"steps (the \"Choose\" part) is emphasized."],[475.2967708333333,"steps (the \"Set\" part) is no longer emphasized."],[479.0467708333333,"point is shown on the screen, grown."],[480.59177083333327,"steps (the \"Choose\" part) is no longer emphasized."],[480.59177083333327,"steps (the \"Relax\" part) is emphasized."],[481.0467708333333,"point is hidden from the screen."],[481.9147708333333,"edge_ab is indicated — a transient flash."],[481.9147708333333,"edge_ac is indicated — a transient flash."],[483.6797708333333,"steps (the \"Relax\" part) is no longer emphasized."],[483.6797708333333,"steps (the \"Repeat\" part) is emphasized."],[485.26977083333327,"region_sb is indicated — a transient flash."]]},{"start":487.6292708333333,"say":"A plain array finds the minimum by scanning every key: three, ten, twelve, infinity. One scan costs V, and V choices give V times V, which is order V squared.","live":null,"does":[[488.43577083333327,"scan_array is shown on the screen, written out."],[489.7707708333333,"scan_array is shown on the screen, written out."],[491.4777708333333,"scan_array (the \"3\" part) is emphasized."],[491.9537708333333,"scan_array (the \"10\" part) is emphasized."],[491.9537708333333,"scan_array (the \"3\" part) is no longer emphasized."],[492.3717708333333,"scan_array (the \"10\" part) is no longer emphasized."],[492.3717708333333,"scan_array (the \"12\" part) is emphasized."],[492.8247708333333,"scan_array (the \"$infinity$\" part) is emphasized."],[492.8247708333333,"scan_array (the \"12\" part) is no longer emphasized."],[496.8767708333333,"scan_cost is shown on the screen, written out."],[496.8767708333333,"scan_cost (the \"V dot V\" part) is emphasized."],[498.6177708333333,"scan_cost (the \"O(V^2)\" part) is emphasized."],[498.6177708333333,"scan_cost (the \"V dot V\" part) is no longer emphasized."],[499.4712708333333,"heading_steps is hidden from the screen — left the board."],[499.4712708333333,"scan_array is hidden from the screen — left the board."],[499.4712708333333,"scan_cost is hidden from the screen — left the board."],[499.4712708333333,"steps is hidden from the screen — left the board."]]},{"start":500.0712708333333,"say":"A priority queue stores exactly the unsettled rim. Its smallest key is A's three. Pop A, settle it, and let the filled region grow through A.","live":["plane","region_sb","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s","node_a","node_b","node_c","node_d","node_e","value_s","value_a","value_b","value_c","value_d"],"does":[[500.0712708333333,"heading_queue is shown on the screen, written out."],[500.0712708333333,"queue_0 is shown on the screen, written out."],[500.1912708333333,"queue_0 is shown on the screen, written out."],[500.3112708333333,"queue_0 is shown on the screen, written out."],[500.4312708333333,"queue_0 is shown on the screen, written out."],[500.55127083333326,"queue_0 is shown on the screen, written out."],[505.7547708333333,"point_2 is shown on the screen, grown."],[505.7547708333333,"queue_0 (the \"3\" part) is emphasized."],[506.9267708333333,"queue_0 is hidden from the screen."],[506.9267708333333,"queue_1 is shown on the screen, written out."],[507.63577083333325,"region_sb becomes a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (4.25, 0.9), (4.25, 6.45), (3.05, 6…, fill_opacity=0.2)."],[507.7547708333333,"point_2 is hidden from the screen."],[509.4467708333333,"edge_sa is no longer emphasized."],[509.4467708333333,"edge_ab is no longer emphasized."],[509.4467708333333,"edge_ac is emphasized."]]},{"start":511.2592708333333,"say":"Relaxing A improves C. Decrease C's queue key from ten to eight, and the node label beside C changes to eight with the queue key.","live":["plane","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s","node_a","node_b","node_c","node_d","node_e","value_s","value_a","value_b","value_c","value_d","queue_1","heading_queue","region_sba"],"does":[[512.6817708333333,"edge_bc is indicated — a transient flash."],[512.6817708333333,"edge_ac is indicated — a transient flash."],[515.5607708333333,"point_3 is shown on the screen, grown."],[515.9557708333333,"value_c is redrawn as the numbers it depends on change."],[515.9557708333333,"queue_1 is hidden from the screen."],[515.9557708333333,"queue_2 is shown on the screen, written out."],[515.9557708333333,"d_c ticks to 8.0."],[517.5607708333333,"point_3 is hidden from the screen."]]},{"start":520.9212708333333,"say":"Those are the two heap operations Dijkstra needs: pop-min chooses the next settled node, and decrease-key records a better route without scanning the whole graph.","live":["plane","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s","node_a","node_b","node_c","node_d","node_e","value_s","value_a","value_b","value_c","value_d","queue_2","heading_queue","region_sba"],"does":[[527.8397708333333,"node_c is indicated — a transient flash."],[530.1387708333333,"plane moves to a new place on the board."],[530.1387708333333,"heading_queue is hidden from the screen — left the board."],[530.1387708333333,"queue_2 is hidden from the screen — left the board."]]},{"start":530.7387708333333,"say":"Every node is popped once, giving V pop-min operations. Every edge can trigger a decrease-key, giving at most E lowerings.","live":["plane","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s","node_a","node_b","node_c","node_d","node_e","value_s","value_a","value_b","value_c","value_d","region_sba"],"does":[[530.7387708333333,"heading_cost is shown on the screen, written out."],[531.7137708333333,"cost is shown on the screen, written out."],[533.0957708333333,"cost (the \"V dot upright(\"pop-min\")\" part) is emphasized."],[538.5987708333333,"cost (the \"E dot upright(\"decrease-key\")\" part) is emphasized."],[538.5987708333333,"cost (the \"V dot upright(\"pop-min\")\" part) is no longer emphasized."]]},{"start":540.3717708333332,"say":"A binary heap makes each operation cost log V. The total is order V plus E, multiplied by log V.","live":["plane","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s","node_a","node_b","node_c","node_d","node_e","value_s","value_a","value_b","value_c","value_d","region_sba","heading_cost"],"does":[[544.1217708333332,"cost is shown on the screen, written out."],[544.6447708333333,"cost (the \"E dot upright(\"decrease-key\")\" part) is no longer emphasized."],[544.6447708333333,"cost (the \"O((V + E) log V)\" part) is emphasized."],[547.6862708333333,"cost (the \"O((V + E) log V)\" part) is no longer emphasized."]]},{"start":548.2862708333333,"say":"Finish the same run. C settles at eight. Its relaxation lowers D to ten. D then settles, which makes E reachable at fourteen. Relaxing D lowers E to thirteen, and E settles last. The blue region grows at each commitment.","live":null,"does":[[550.5617708333333,"region_sb becomes a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (4.25, 0.9), (7.2, 4.35), (7.1, 5.5…, fill_opacity=0.2)."],[550.5617708333333,"edge_ac is no longer emphasized."],[550.5617708333333,"edge_bc is no longer emphasized."],[550.5617708333333,"edge_cd is emphasized."],[550.5617708333333,"edge_ce is emphasized."],[551.0847708333333,"point_4 is shown on the screen, grown."],[553.0847708333333,"point_4 is hidden from the screen."],[553.8237708333332,"value_d is redrawn as the numbers it depends on change."],[553.8237708333332,"point_5 is shown on the screen, grown."],[553.8237708333332,"d_d ticks to 10.0."],[555.4377708333333,"region_sb becomes a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (7.15, 1.2), (7.3, 5.35), (3.05, 6.…, fill_opacity=0.2)."],[555.4377708333333,"edge_bd is no longer emphasized."],[555.4377708333333,"edge_cd is no longer emphasized."],[555.4377708333333,"edge_de is emphasized."],[555.8237708333332,"point_5 is hidden from the screen."],[557.5277708333333,"value_e is shown on the screen, written out."],[560.3377708333333,"value_e is redrawn as the numbers it depends on change."],[560.3377708333333,"point_6 is shown on the screen, grown."],[560.3377708333333,"d_e ticks to 13.0."],[561.5567708333333,"region_sb becomes a Polygon [blue] drawn in plane (vertices=((0.45, 3.7), (3.05, 0.65), (7.0, 1.15), (9.6, 3.05), (9.55, 4.…, fill_opacity=0.2)."],[561.5567708333333,"edge_ce is no longer emphasized."],[561.5567708333333,"edge_de is no longer emphasized."],[562.3377708333333,"point_6 is hidden from the screen."]]},{"start":565.9182708333333,"say":"Each improvement also records a predecessor. The surviving choices form the shortest-path tree: S to B, B to A, A to C, C to D, and D to E.","live":["plane","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s","node_a","node_b","node_c","node_d","node_e","value_s","value_a","value_b","value_c","value_d","region_sba","heading_cost","region_sbac","region_sbacd","value_e","region_all"],"does":[[568.0087708333333,"tree_statement is shown on the screen, written out."],[572.6527708333333,"The segment (1.1, 3.7) to (3.6, 1.4) in plane is lit up."],[573.7667708333333,"The segment (3.6, 1.4) to (3.6, 6.2) in plane is lit up."],[575.0327708333333,"The segment (3.6, 6.2) to (6.6, 4.9) in plane is lit up."],[576.0307708333332,"The segment (6.6, 4.9) to (6.6, 1.9) in plane is lit up."],[577.3077708333333,"The segment (6.6, 1.9) to (9.0, 3.6) in plane is lit up."]]},{"start":578.9992708333333,"say":"The final state carries the whole algorithm: a filled settled region, the tree that recovers every route, and no frontier edge left to cross. Non-negative weights are what make each greedy step permanent.","live":["plane","edge_sa","edge_sb","edge_ab","edge_ac","edge_bc","edge_bd","edge_cd","edge_ce","edge_de","node_s","node_a","node_b","node_c","node_d","node_e","value_s","value_a","value_b","value_c","value_d","region_sba","tree_statement","heading_cost","region_sbac","region_sbacd","value_e","region_all"],"does":[[582.0407708333333,"region_sb is indicated — a transient flash."],[583.8747708333333,"tree_statement (the \"upright(\"shortest-path tree\")\" part) is emphasized."],[592.4233333333333,"tree_statement (the \"upright(\"shortest-path tree\")\" part) is no longer emphasized."]]}]}]},"durationSeconds":593,"chapters":[{"title":"The Frontier Spreads","startSeconds":0,"narration":"Dijkstra's algorithm finds the cheapest route from one source to every node in a graph. The picture to keep is a filled region that spreads outward, with the edges crossing its boundary forming a live frontier. Here are six nodes. S is the source, and A, B, C, D and E are the other places. The gray connections are weighted edges. Their numbers measure cost, so a path is cheap when the sum of its edge weights is small. The weights are four, two, one, five, eight, ten, two again, six, and three. Every one is non-negative. That condition will carry the proof. This blue patch around S is the settled region we will grow. The two yellow edges leaving it are the frontier. Keep that boundary alive in your eye while the numbers decide which node the patch reaches next. First see why counting edges is not enough. Start at S, then B, then D, then E. This route uses only three edges. The three-hop route pays two, ten, and three. Together those weights total fifteen. The other route goes S, B, A, C, D, E. Its five weights are two, one, five, two, and three. Together they total thirteen. Fifteen is greater than thirteen. More hops can cost less, so every decision must use the accumulated weights rather than the hop count. Each node carries a tentative distance: the cheapest route found so far. Unreached nodes have no number yet. The source is different. Reaching S from S costs zero, so zero is final from the start. Relaxation is the only arithmetic move. On this picture, u is S, v is A, and w of u v is the four on their edge. A route through u costs d of u plus that edge weight. If that new cost is smaller than d of v, then replace d of v with it. The test comes first. The assignment happens only when the test wins. For A, the source contributes zero and the edge contributes four. Together they make four, so A receives four. For B, the source contributes zero and the edge contributes two. Together they make two, so B receives two. Now compare the unsettled rim. A carries four. B carries two. The smallest tentative distance belongs to B, so B is the greedy choice. Relax B to A. B contributes two and the edge contributes one. Together they make three, better than four, so A changes to three. A's predecessor changes from S to B at the same decision. The route record now agrees with the lower value. Next, B contributes two and the edge to C contributes eight. Together they make ten, so C receives ten. For D, B contributes two and the edge contributes ten. Together they make twelve, so D receives twelve. The live rim now reads A at three, C at ten, and D at twelve. Those are the finite candidates touching the blue region. The smallest is A at three. Settle A, grow the blue region, and update the crossing edges around the same weighted graph. Relax A to C. A contributes three and the edge contributes five. Together they make eight, so C falls from ten to eight. Now C has eight, D has twelve, and E is unreached. The smallest is C. Settle C and let the blue region reach it. Relax C to D. C contributes eight and the edge contributes two. Together they make ten, so D drops from twelve to ten immediately. Relax C to E. C contributes eight and the edge contributes six. Together they make fourteen, so E receives fourteen. The rim has D at ten and E at fourteen. Ten is smaller, so D is next. The region grows through D and the yellow boundary moves outward. Relax D to E. D contributes ten and the edge contributes three. Together they make thirteen, so E falls from fourteen to thirteen. Only E remains, carrying thirteen. Settle it. The blue region now covers every node, and no crossing edge remains. Read the final distances directly from the node labels: S zero, B two, A three, C eight, D ten, and E thirteen. The predecessor tree gives the route to E backwards: E to D, D to C, C to A, A to B, and B to S. Reverse that walk and its total is thirteen. The arithmetic explains every update, and the growing region shows every greedy commitment. What remains is to prove why the smallest label on the frontier is safe to make permanent."},{"title":"Why Settling Is Safe","startSeconds":315.774125,"narration":"Freeze one generic greedy step. The blue polygon is the settled region. Its yellow crossing edges are the frontier, and C carries the smallest tentative distance there: eight. Suppose a cheaper route P to C exists. It begins at S inside the polygon and ends at C outside, so it must cross the frontier for the first time. Call the first unsettled node on that route x. The only part of P we need is the short local prefix from u to x, ending exactly at x. Every edge leaving a settled node was relaxed when that node settled. Therefore the cost already spent along the prefix P sub x cannot be less than the tentative distance written at x. The greedy choice points to C because eight is the smallest frontier label. Since x is also on that frontier, d of x is at least d of C, which is eight. So the proposed cheaper route has already spent at least eight when it reaches x. It still has the remaining segment from x to C ahead of it. Every remaining edge has non-negative weight. Continuing from x cannot reduce the amount already spent, so P cannot reach C for less than eight. That contradiction makes eight final. The same frontier-crossing argument applies at every greedy step, which is why settled labels never need to reopen. Now change only one fact. This three-node graph has S to X of weight one, S to Y of weight four, and a red edge from Y to X of weight minus four. Relaxing S gives X one and Y four. The greedy rule points at X because one is smaller, so it settles X and grows the region there. But the other route walks S to Y and then Y to X. Its two weights are four and minus four. Together they total zero. Zero is smaller than one. The negative edge discounts the route after X was declared final, so the non-negative step in the proof has failed. Dijkstra's algorithm therefore requires non-negative weights. When a graph really contains negative edges, Bellman-Ford is the algorithm to reach for instead."},{"title":"From Frontier to Priority Queue","startSeconds":457.8747708333333,"narration":"The whole algorithm fits into four numbered steps. The graph is in the state just after B settles: the blue region contains S and B, and its yellow crossing edges form the current frontier. First, initialize the labels; the source gets zero. Second, choose the smallest unsettled label; here A at three is next. Third, relax its outgoing edges. Fourth, repeat while the frontier still has a node. A plain array finds the minimum by scanning every key: three, ten, twelve, infinity. One scan costs V, and V choices give V times V, which is order V squared. A priority queue stores exactly the unsettled rim. Its smallest key is A's three. Pop A, settle it, and let the filled region grow through A. Relaxing A improves C. Decrease C's queue key from ten to eight, and the node label beside C changes to eight with the queue key. Those are the two heap operations Dijkstra needs: pop-min chooses the next settled node, and decrease-key records a better route without scanning the whole graph. Every node is popped once, giving V pop-min operations. Every edge can trigger a decrease-key, giving at most E lowerings. A binary heap makes each operation cost log V. The total is order V plus E, multiplied by log V. Finish the same run. C settles at eight. Its relaxation lowers D to ten. D then settles, which makes E reachable at fourteen. Relaxing D lowers E to thirteen, and E settles last. The blue region grows at each commitment. Each improvement also records a predecessor. The surviving choices form the shortest-path tree: S to B, B to A, A to C, C to D, and D to E. The final state carries the whole algorithm: a filled settled region, the tree that recovers every route, and no frontier edge left to cross. Non-negative weights are what make each greedy step permanent."}]}}
