Skip to main content

Grid graphs and island solutions in 2D and 3D, object-oriented and pure python/numpy.

Project description

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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gridgraphs-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gridgraphs-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file gridgraphs-0.1.0.tar.gz.

File metadata

  • Download URL: gridgraphs-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for gridgraphs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bd66d26b05d82334c7340fbe7336217f442c66ef8c186dc9173b63206211e747
MD5 bed5bc467d11c87705e11a1725b6c625
BLAKE2b-256 e5cb3b786c36c3a27faa7d7845e576c46c3ba1e42d68f96a5e19ab796cb61fb5

See more details on using hashes here.

File details

Details for the file gridgraphs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: gridgraphs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for gridgraphs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d475cb1ff7c1a7788fda038a1c64257c1c78d7ef5d0435b9597d2f9769f85fa6
MD5 aabae08b0ce82fcb6f3a08b9c1202e6b
BLAKE2b-256 0a554f4e970c4ccb8cd1d514efbeedd144e0683864fd3c11e5dca34ed10f1602

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page