Metadata-Version: 2.4
Name: untangle-rs
Version: 0.2.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: maturin>=1.9.1,<2.0.0
License-File: LICENSE
Summary: Library that minimises edge crossings in a hierarchical multipartite graph
Author-email: Matthijs Wesseling <matthijswesseling@gmail.com>
License: MIT
Requires-Python: >=3.11, <4
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/Bladieblah/untangle-rs
Project-URL: Repository, https://github.com/Bladieblah/untangle-rs.git
Project-URL: Bug Tracker, https://github.com/Bladieblah/untangle-rs/issues

[![PyPI version](https://img.shields.io/pypi/v/untangle-rs.svg)](https://pypi.org/project/untangle-rs/)
[![PyPI downloads](https://img.shields.io/pypi/dm/untangle-rs.svg)](https://pypi.org/project/untangle-rs/)
[![Build](https://github.com/Bladieblah/untangle-rs/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/Bladieblah/untangle-rs/actions)
[![Coverage](https://codecov.io/gh/<your-username>/<your-repo>/branch/main/graph/badge.svg)](https://codecov.io/gh/Bladieblah/untangle-rs)

# untangle-rs
Library that minimises edge crossings in weighted multipartite graphs, with support for respecting node hierarchy. It uses a simple metropolis-hastings algorithm that iteratitely swaps neighboring nodes in order to minimize the number of crossings. It is unlikely to find a global minimum but does converge to optimal solutions.

![Basic example](docs/images/basic.png)
*Basic example*

## Basic layouts

You can minimize the crossings in arbitrary multipartite graphs, as long as you provide the parts of the graph:

```python
from untanglers import LayoutOptimizerInt

nodes = [
  [0,1,2],
  [3,4,5],
  [6,7,8],
]

edges = [
  # node_a, node_b, edge_weight
  [(0, 4, 1), (1, 3, 5)],
  [(4, 8, 2), (5, 6, 1)]
]

optimizer = LayoutOptimizerInt(nodes, edges)
edge_crossings = optimizer.count_crossings()
new_crossings = optimizer.optimize(
  start_temp = 1.0,
  end_temp = 0.1,
  steps = 3,
  max_iterations = 20,
  passes = 5,
)
```

![Complicated example](docs/images/complex.png)
*More complicated graph*

## Hierarchical layouts

In case certain nodes need to remain grouped together, the optimizer also supports hierarchy. This is useful for visualizing e.g. data lineage where columns in a table should remain together.

```python
from untanglers import HierarchyOptimizerInt

nodes = ...
edges = ...

hierarchy = [
  # 2 levels, note that the coarser level is aligned with the finer level
  [[4,5,6,5], [9, 11]],
  [[7, 20, 13], [27, 13]],
  [[8, 9, 6, 7], [17, 13]],
]

optimizer = HierarchyOptimizerInt(nodes, edges, hierarchy)
new_crossings = optimizer.optimize(...)
```

![Hierarchical example](docs/images/hierarchy.png)
*Graph with 2 levels of node hierarchy*

