diff --git a/bellman_ford/DEMO.ipynb b/bellman_ford/DEMO.ipynb new file mode 100644 index 0000000..46f720c --- /dev/null +++ b/bellman_ford/DEMO.ipynb @@ -0,0 +1,137 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "7f235e2e", + "metadata": {}, + "outputs": [], + "source": [ + "import networkx as nx\n", + "from scipy.sparse.csgraph import bellman_ford" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "7d256792", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shortest distances from vertex A (0): [0, 2, 1, 3, 4]\n" + ] + } + ], + "source": [ + "# edge (u, v, w) means an edge from u to v with weight w\n", + "edges = [\n", + " (0, 1, 3),\n", + " (0, 2, 1),\n", + " (1, 2, 7),\n", + " (1, 3, 5),\n", + " (2, 3, 2),\n", + " (3, 2, -1),\n", + " (3, 4, 1),\n", + " (4, 1, -2),\n", + "]\n", + "\n", + "# assuming A-E are indexed as 0-4\n", + "distances = [float('inf')] * 5\n", + "distances[0] = 0\n", + "\n", + "for _ in range(4): # v-1 iterations\n", + " for u, v, w in edges: # 'w' is the 'new' weight\n", + " if distances[u] < float('inf'):\n", + " distances[v] = min(distances[v], distances[u] + w)\n", + "\n", + "print(\"Shortest distances from vertex A (0):\", distances)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9afe182f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "nodes: ['A', 'B', 'C', 'D', 'E']\n", + "distances from source (A): [0. 2. 1. 3. 4.]\n", + "predecessors: [-9999 4 0 2 3]\n", + " A: distance=0.0, predecessor=None\n", + " B: distance=2.0, predecessor=E\n", + " C: distance=1.0, predecessor=A\n", + " D: distance=3.0, predecessor=C\n", + " E: distance=4.0, predecessor=D\n" + ] + } + ], + "source": [ + "# build a small directed graph with weights (same as in example)\n", + "G = nx.DiGraph()\n", + "G.add_weighted_edges_from([\n", + " ('A', 'B', 3),\n", + " ('A', 'C', 1),\n", + " ('B', 'C', 7),\n", + " ('B', 'D', 5),\n", + " ('C', 'D', 2),\n", + " ('D', 'C', -1),\n", + " ('D', 'E', 1),\n", + " ('E', 'B', -2),\n", + "])\n", + "\n", + "# convert to an adjacency/weight matrix\n", + "W = nx.to_numpy_array(G, weight='weight')\n", + "\n", + "# run scipy's bellman_ford on the weight matrix from source index 0\n", + "distances, predecessors = bellman_ford(W, indices=0, return_predecessors=True)\n", + "\n", + "nodes = list(G.nodes())\n", + "print(\"nodes:\", nodes)\n", + "print(\"distances from source (A):\", distances)\n", + "print(\"predecessors:\", predecessors)\n", + "\n", + "# interpret results in terms of node names\n", + "for i, d in enumerate(distances):\n", + " pred = predecessors[i]\n", + " pred_name = nodes[pred] if pred != -9999 else None\n", + " print(f\" {nodes[i]}: distance={d}, predecessor={pred_name}\")" + ] + }, + { + "cell_type": "markdown", + "id": "358750df", + "metadata": {}, + "source": [ + "### References\n", + "* [algorithms for cp](https://cp-algorithms.com/graph/bellman_ford.html)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv (3.11.0)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}