Hoshen–Kopelman, one tested step at a time
Hoshen–Kopelman, one tested step at a time
I begin with a small grid, then add label lookup, merging, scanning, final labels and periodic connections. I test each new part before continuing. The public download contains the Python Jupyter notebook only. If you would like a similar tutorial in Julia or C, email me with the lesson title and your intended use. I can share a Google Colab notebook with step-by-step explanations and worked examples. Email: saswata_at_msme.iith.ac.in (replace at with @).
Run the notebook
Install NumPy and Jupyter with python -m pip install numpy jupyter, then open hoshen-kopelman-python.ipynb in Jupyter. Run its cells in order. No separate Python script or C compiler is required for this notebook.
Meaning of the result
Cells connect through shared edges, not corners. Occupancy values, labels and counts are dimensionless. If physical grid spacings are dx and dy, a cluster area is its count times dx times dy. Periodic connections join matching opposite-edge cells. They do not establish wrapping. Component counts also do not give hole counts. The topology tutorial explains these additional measurements.
Checks and limitations
The notebook contains stage checks. The historical check-results.txt also records checks of other language versions, which are not included in this public bundle. Label numbers are arbitrary; comparisons use the partitions they define. This small teaching implementation does not establish the scalability of a full-volume analysis.
References and credits
J. Hoshen and R. Kopelman, Physical Review B 14, 3438 (1976), https://doi.org/10.1103/PhysRevB.14.3438.
Tobin Fricke, The Hoshen–Kopelman Algorithm, https://www.ocf.berkeley.edu/~fricke/projects/hoshenkopelman/hoshenkopelman.html.
The downloadable Python notebook is a new teaching implementation. Historical C files retain their existing credits, including Prof. T. A. Abinandanan, my PhD adviser (Abi in the original source comments). A separate Fricke C source retains his copyright and GPL notice; it is not included in this download or relicensed here.
I permit free noncommercial teaching, learning and demonstrations with acknowledgment. Research (including academic research) and commercial use require my explicit written permission. Earlier MIT and GPL releases retain their existing permissions. See CODE-USE-TERMS.txt. Original prose follows CC BY 4.0.
