From c1c953a19eeb2a610da57866d99590184bd62151 Mon Sep 17 00:00:00 2001 From: govindfinally Date: Mon, 15 Dec 2025 14:18:22 +0530 Subject: [PATCH 1/2] Add pybind11 bindings for GraphX (Graph and BFS) --- .gitignore | 15 +++++++++ python/pybind11/algonb/__init__.py | 0 python/pybind11/algonb/graph_bindings.cpp | 37 +++++++++++++++++++++++ python/pybind11/setup.py | 19 ++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 python/pybind11/algonb/__init__.py create mode 100644 python/pybind11/algonb/graph_bindings.cpp create mode 100644 python/pybind11/setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec0cf41 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# Python +venv/ +__pycache__/ +*.pyc + +# Build artifacts +build/ +dist/ +*.so +*.pyd +*.dll + +# OS +.DS_Store +Thumbs.db diff --git a/python/pybind11/algonb/__init__.py b/python/pybind11/algonb/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python/pybind11/algonb/graph_bindings.cpp b/python/pybind11/algonb/graph_bindings.cpp new file mode 100644 index 0000000..9b82a73 --- /dev/null +++ b/python/pybind11/algonb/graph_bindings.cpp @@ -0,0 +1,37 @@ +#include +#include + +// Include original algorithm code +#include "../../../GraphX.cpp" + +namespace py = pybind11; + +PYBIND11_MODULE(graph, m) { + m.doc() = "Python bindings for GraphX (Graph + BFS)"; + + py::class_(m, "Graph") + .def(py::init(), + py::arg("n"), + py::arg("is_directed") = true) + .def("add_edge", + py::overload_cast(&Graph::add_edge), + py::arg("u"), py::arg("v"), py::arg("cost") = 0) + .def("add_edge", + py::overload_cast< + std::tuple, + std::tuple, ll>(&Graph::add_edge), + py::arg("u"), py::arg("v"), py::arg("cost") = 0) + .def("add_edge", + py::overload_cast< + std::tuple, + std::tuple, ll>(&Graph::add_edge), + py::arg("u"), py::arg("v"), py::arg("cost") = 0); + + py::class_(m, "BFS") + .def(py::init()) + .def("run", py::overload_cast(&BFS::run)) + .def("run", py::overload_cast>(&BFS::run)) + .def("run", py::overload_cast>(&BFS::run)) + .def("min_dist", py::overload_cast(&BFS::min_dist)) + .def("is_visited", py::overload_cast(&BFS::is_visited)); +} diff --git a/python/pybind11/setup.py b/python/pybind11/setup.py new file mode 100644 index 0000000..9c12c8c --- /dev/null +++ b/python/pybind11/setup.py @@ -0,0 +1,19 @@ +from setuptools import setup, Extension +import pybind11 + +ext_modules = [ + Extension( + "algonb.graph", + ["algonb/graph_bindings.cpp"], + include_dirs=[pybind11.get_include()], + language="c++", + ) +] + +setup( + name="algonb", + version="0.1.0", + packages=["algonb"], + ext_modules=ext_modules, +) + \ No newline at end of file From 6cbcf1ae25b35d17a8e468216e1deeaeb107cf1c Mon Sep 17 00:00:00 2001 From: govindfinally Date: Mon, 15 Dec 2025 14:35:39 +0530 Subject: [PATCH 2/2] Add GraphX Python usage and build instructions --- DP_Opti : Divide & Conquer.cpp | 21 --- README.md | 200 ++++++++++++++++++++++++- unordered_map for pair as key | 19 --- 3 files changed, 199 insertions(+), 41 deletions(-) delete mode 100644 DP_Opti : Divide & Conquer.cpp delete mode 100644 unordered_map for pair as key diff --git a/DP_Opti : Divide & Conquer.cpp b/DP_Opti : Divide & Conquer.cpp deleted file mode 100644 index ac7c47b..0000000 --- a/DP_Opti : Divide & Conquer.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/*Given $a[i] = \min_{lo(i) \le k < hi(i)}(f(i, k))$ where the (minimal) optimal $k$ increases with $i$, computes $a[i]$ for $i = L..R-1$. - * Status: tested on http://codeforces.com/contest/321/problem/E - * Time: O((N + (hi-lo)) \log N) */ -struct DP { // Modify at will: - int lo(int ind) { return 0; } - int hi(int ind) { return ind; } - ll f(int ind, int k) { return dp[ind][k]; } - void store(int ind, int k, ll v) { res[ind] = pii(k, v); } - - void rec(int L, int R, int LO, int HI) { - if (L >= R) return; - int mid = (L + R) >> 1; - pair best(LLONG_MAX, LO); - rep(k, max(LO,lo(mid)), min(HI,hi(mid))) - best = min(best, make_pair(f(mid, k), k)); - store(mid, best.second, best.first); - rec(L, mid, LO, best.second+1); - rec(mid+1, R, best.second, HI); - } - void solve(int L, int R) { rec(L, R, INT_MIN, INT_MAX); } -}; diff --git a/README.md b/README.md index 76c6715..a2491df 100644 --- a/README.md +++ b/README.md @@ -1 +1,199 @@ -# Algorithms-Notebook \ No newline at end of file +# Algorithms-Notebook +# For Python users +Below is a **clean, copy-paste ready README.md** written for **your exact setup**. +No assumptions, no extra fluff. + +--- + +```md +# Algorithms Notebook β€” Python GraphX Bindings + +This repository contains C++ algorithm implementations with **Python bindings using pybind11**. +Currently, it exposes the **GraphX (Graph + BFS)** module for use directly in Python. + +--- + +## πŸ“ Relevant Structure + +``` + +Algorithms-Notebook/ +β”‚ +β”œβ”€β”€ GraphX.cpp # Original C++ Graph + BFS implementation +β”‚ +β”œβ”€β”€ python/ +β”‚ └── pybind11/ +β”‚ β”œβ”€β”€ algonb/ +β”‚ β”‚ β”œβ”€β”€ **init**.py +β”‚ β”‚ └── graph_bindings.cpp # pybind11 bindings for GraphX +β”‚ └── setup.py # Build script +β”‚ +└── README.md + +```` + +--- + +## 🧩 What Is Exposed to Python + +### Python Module +```python +algonb.graph +```` + +### Classes + +* `Graph` +* `BFS` + +--- + +## πŸ› οΈ Prerequisites + +### 1. Python + +* Python **3.10+** +* `pip` installed + +### 2. Compiler (IMPORTANT) + +#### Option A β€” Windows (Recommended) + +Install **Microsoft C++ Build Tools** + +* Download: [https://visualstudio.microsoft.com/visual-cpp-build-tools/](https://visualstudio.microsoft.com/visual-cpp-build-tools/) +* Select: + + * βœ” MSVC v14.x + * βœ” Windows 10/11 SDK + * βœ” C++ CMake tools + +#### Option B β€” Linux / WSL + +```bash +sudo apt update +sudo apt install build-essential python3-dev +``` + +--- + +## πŸ“¦ Build Instructions + +### Step 1 β€” Create Virtual Environment + +```bash +python -m venv venv +``` + +Activate: + +**Windows** + +```powershell +venv\Scripts\activate +``` + +**Linux / WSL** + +```bash +source venv/bin/activate +``` + +--- + +### Step 2 β€” Install Dependencies + +```bash +pip install pybind11 setuptools wheel +``` + +--- + +### Step 3 β€” Build the Extension + +```bash +cd python/pybind11 +python setup.py build_ext --inplace +``` + +After this, a compiled file like below will appear: + +``` +algonb/graph*.pyd (Windows) +algonb/graph*.so (Linux) +``` + +--- + +## πŸš€ Using GraphX in Python + +### Example Usage + +```python +from algonb.graph import Graph, BFS + +g = Graph(5, is_directed=False) + +g.add_edge(0, 1) +g.add_edge(1, 2) +g.add_edge(2, 3) + +bfs = BFS(g) +bfs.run(0) + +print(bfs.min_dist(3)) # shortest distance +print(bfs.is_visited(4)) # False +``` + +--- + +## 🧠 Design Notes + +* Original algorithm logic is **untouched** +* Python bindings are thin wrappers using **pybind11** +* No runtime Python overhead for BFS/Graph logic +* Designed for competitive programming & algorithm prototyping + +--- + +## ❌ What Is NOT Committed + +* `venv/` +* Compiled `.pyd / .so` files +* Local build artifacts + +These are intentionally excluded via `.gitignore`. + +--- + +## πŸ“Œ Future Extensions + +* DFS +* Dijkstra +* Union-Find +* Segment Trees +* Python-friendly algorithm notebook interface + +--- + +## πŸ‘€ Author + +**Govind Mohanty** +NIT Rourkela +Algorithms Notebook Project + +```` + +--- + +## NEXT STEP (ONLY THIS) +1. Paste this into `README.md` +2. Save +3. Run: +```bash +git add README.md +git commit -m "Add GraphX Python build and usage documentation" +git push origin pybind-graphx +```` + +Say **β€œREADME committed”** when done. diff --git a/unordered_map for pair as key b/unordered_map for pair as key deleted file mode 100644 index f465260..0000000 --- a/unordered_map for pair as key +++ /dev/null @@ -1,19 +0,0 @@ -struct hash_pair { - template - size_t operator()(const pair& p) const - { - auto hash1 = hash{}(p.first); - auto hash2 = hash{}(p.second); - - if (hash1 != hash2) { - return hash1 ^ hash2; - } - - // If hash1 == hash2, their XOR is zero. - return hash1; - } -}; - -int main() { - unordered_map, int, hash_pair> yo; -}