Planarity - Graph Planarity Tools
raw JSON → 0.6 verified Fri May 01 auth: no python maintenance
Planarity is a Python package for testing and manipulating the planarity of undirected graphs. It wraps the Boyer-Myrvold planarity algorithm (C library) and provides functions like is_planar, kuratowski_subgraph, and planar_embedding. The current version is 0.6, released with Cython 3 support, but the library is in maintenance mode with infrequent updates.
pip install planarity Common errors
error ImportError: No module named 'planarity' ↓
cause The package is not installed or the Python environment is incorrect.
fix
Run 'pip install planarity' in the correct environment.
error FileNotFoundError: [Errno 2] No such file or directory: 'planarity.c' ↓
cause The Cython-generated C file is missing from the source distribution (common with older pip or setuptools).
fix
Install with '--no-cache-dir' or upgrade pip: 'pip install --upgrade pip && pip install planarity'
error KeyError: 0 ↓
cause The planarity library expects integer vertices starting from 0. Non-consecutive or non-integer vertices (like strings) may cause this error.
fix
Remap vertices to consecutive integers starting at 0 before calling planarity functions.
Warnings
breaking In version 0.4, inputs with parallel edges are silently dropped with a warning. This can cause incorrect results or confusion. Always deduplicate edges before calling planarity functions. ↓
fix Remove parallel edges using set operations: edges = list(set((min(u,v), max(u,v)) for u,v in edges))
broken The library uses Cython and requires compilation. On some platforms (e.g., Windows without a C compiler), installation may fail with a 'Unable to find vcvarsall.bat' error. ↓
fix Install from a precompiled wheel if available, or use conda; alternatively, use the Cython source and ensure a C compiler is properly configured.
gotcha The function kuratowski_subgraph() may raise a KeyError if the graph is planar. It is designed to be called only for non-planar graphs, but the documentation is unclear. Always check is_planar first. ↓
fix if not planarity.is_planar(edges): sub = planarity.kuratowski_subgraph(edges)
Imports
- planarity
import planarity
Quickstart
import planarity
def test_planarity():
# Example: complete graph K5 is non-planar
edges = [(0, 1), (0, 2), (0, 3), (0, 4),
(1, 2), (1, 3), (1, 4),
(2, 3), (2, 4),
(3, 4)]
# Check planarity
result = planarity.is_planar(edges)
print('Is K5 planar?', result) # False
test_planarity()