Skip to main content

Island graph solutions (grid graphs) with python

Connected grids can be thought of as grid graphs, where each grid point is a node either positive (island) or negative (void/water), although the categories can be readily inverted so in practice only one of the above is defined as the positive node. Connections between each grid point (node) is then an edge. For a given valid connection type (say up down left right, or including diagonals) each positive node gets defined between 0 and maximum direction number of edges.

Common problems are:

  • count the number of islands
  • find the size of largest, smallest, average, median, mode island.
  • Use depth-first or breadth-first traversal.

The code is fully typed and makes use of composition with Protocol classes so that valid directions, traversal strategies, and even 2D graph types can be readily exchanged with minimal refactoring.

Requires Python > 3.10.
Only dependency is a new version of numpy for convenience (and typing support), although the operations are mostly in base python data structures like list and set.

To run

Clone and run: python -m main.py

Some details and possible expansion

Grid graphs in this code can all be considered undirected but are not acyclical, therefore visited nodes are marked to avoid infinite recursion. In practice, directed 2D grid graphs are also possible to make. In that case, the neighbors should be pre-calculated as a python dict of {node: [list of connected nodes]} using the provided get_neighbors function, which can be done by specifying per node valid Directions, instead of a global valid_directions per graphs.

The two exploration algorithms, Depth-first and Breadth-first, also known as BFS and DFS, will always give the same end-result but with different algorithmic time and space complexity, and are good for different usecases. For the island questions answered here, depth first is ideal but breadth first is also provided. However, for example, depth-first can be particularly poor for checking if a connection exists between given node and destination node, especially if the graph tree is big and the destination is relatively close (yet by chance we go off in another direction). So while their worst case senarios are equal, the average for breadth-first will be much better for has path problems. In comparison, using the call-stack depth-first traversal algorithms can be very fast, although this code is in pure python so both could have been further optimized using lower-level (or other) languages.

Depth-first traversal

Using recursion and a first in first out (stack) data structure, each connected node is explored going as far as possible in one direction first.

Breadth-first traversal

Using iteration and a first in last out (queue) data structure, each connected node is explored going to its nearest neighbors first, then the nearest neighbors of the nearest neighbors, and so on.

We make use of list comprehension, dataclasses, enums, protocols, and structural typing so the code may be unfamiliar to some. Finally, the breadh-first algorithm, and the neighbor-tree generation algorithm can be made faster by pre-computing the neighbors and having the algorithm explore the pre-computed neighbors (edges) instead of the current version. The code for doing this is provided but unused in grid_graph.py to lower coupling (as neighbors cannot be calculated without knowing valid directions, which your graph ideally is independent of).

Metadata

Release files for gridgraphs 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gridgraphs 0.1.0
File Size Uploaded
gridgraphs-0.1.0.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gridgraphs 0.1.0
File Interpreter ABI Platform
gridgraphs-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / gridgraphs-0.1.0.tar.gz

Download URL gridgraphs-0.1.0.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
bd66d26b05d82334c7340fbe7336217f442c66ef8c186dc9173b63206211e747
BLAKE2b-256 checksum
How to use checksums
e5cb3b786c36c3a27faa7d7845e576c46c3ba1e42d68f96a5e19ab796cb61fb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.27.1

Release files / gridgraphs-0.1.0-py3-none-any.whl

Download URL gridgraphs-0.1.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d475cb1ff7c1a7788fda038a1c64257c1c78d7ef5d0435b9597d2f9769f85fa6
BLAKE2b-256 checksum
How to use checksums
0a554f4e970c4ccb8cd1d514efbeedd144e0683864fd3c11e5dca34ed10f1602
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.27.1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page