Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions bellman_ford/DEMO.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"cells": [
Comment on lines +1 to +2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Typo in directory name: "belmann" → "bellman"

The directory is named belmann_ford but the algorithm is named after Richard Bellman (single 'n' after 'bell'): bellman_ford.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@belmann_ford/DEMO.ipynb` around lines 1 - 2, The repository uses the
misspelled directory name "belmann_ford" (and file DEMO.ipynb) but the algorithm
is Bellman–Ford; rename the directory to "bellman_ford" and update all
references to it (imports, module/package names, relative paths, notebook links,
README/documentation, tests, and CI scripts) so nothing breaks. Search for the
symbol "belmann_ford" and replace with "bellman_ford", update any Python imports
or module paths that reference that package, and ensure the notebook filename
DEMO.ipynb and any references to it are moved/updated accordingly; run tests and
CI to verify no leftover references remain.

{
"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
}
Loading