A Python package to quickly decompose weighted graphs into weights paths, under various models.
Project description
The flowpaths Python Package
This package implements fast solvers for decomposing weighted directed graphs into weighted paths or walks, based on (Mixed) Integer Linear Programming ((M)ILP) formulations. It supports both acyclic graphs (DAGs, decomposed into paths) and general graphs with cycles (decomposed into walks), and makes it easy to create new decomposition models.
Installation
pip install flowpaths
Documentation
The documentation is available at algbio.github.io/flowpaths/.
Requirements
- Python >= 3.8
- Dependencies installed automatically: networkx, highspy, graphviz, numpy
- Optional: gurobipy (to use Gurobi instead of the default HiGHS)
Basic usage example
For more examples, see the examples/ folder.
import flowpaths as fp
import networkx as nx
# Create a simple graph
graph = nx.DiGraph()
graph.add_edge("s", "a", flow=2)
graph.add_edge("a", "t", flow=2)
graph.add_edge("s", "b", flow=5)
graph.add_edge("b", "t", flow=5)
# ...
# Create a Minimum Flow Decomposition solver
mfd_solver = fp.MinFlowDecomp(graph, flow_attr="flow")
mfd_solver.solve() # We solve it
if mfd_solver.is_solved(): # We get the solution
print(mfd_solver.get_solution())
# {'paths': [['s', 'b', 't'], ['s', 'a', 't']], 'weights': [5, 2]}
For graphs with cycles, use the cyclic variants which return walks rather than simple paths:
import flowpaths as fp
import networkx as nx
G = nx.DiGraph()
G.add_edge("s", "a", flow=1)
G.add_edge("a", "b", flow=2) # part of a cycle
G.add_edge("b", "a", flow=2) # part of a cycle
G.add_edge("a", "t", flow=1)
mfd_solver = fp.MinFlowDecompCycles(G, flow_attr="flow")
mfd_solver.solve()
if mfd_solver.is_solved():
print(mfd_solver.get_solution())
# {'walks': [['s', 'a', 'b', 'a', 'b', 'a', 't']], 'weights': [1]}
Design principles
-
Easy to use: You pass a directed graph (as a networkx DiGraph), and the solvers return optimal weighted paths (or walks for cyclic models). See the examples/ folder.
-
It just works: You do not need to install an (M)ILP solver. This is possible thanks to the fast open source solver HiGHS, which gets installed once you install this package.
- If you have a Gurobi license (free for academic users), you can install the gurobipy Python package, and then you can run the Gurobi solver instead of the default HiGHS solver by just passing the entry
"external_solver": "gurobi"in thesolver_optionsdictionary.
- If you have a Gurobi license (free for academic users), you can install the gurobipy Python package, and then you can run the Gurobi solver instead of the default HiGHS solver by just passing the entry
-
Easy to implement other decomposition models:
- For DAGs, use the abstract class
AbstractPathModelDAG, which encodes a given number of paths. See docs: Abstract Path Model. - For general directed graphs with cycles, use
AbstractWalkModelDiGraph, which encodes a given number of walks. See docs: Abstract Walk Model.
You can inherit from these classes to add weights and model-specific constraints/objectives. See a basic example. These abstract classes interface with a wrapper for popular MILP solvers, so you don't need to worry about solver-specific details.
- For DAGs, use the abstract class
-
Fast: Having solvers implemented using
AbstractPathModelDAGorAbstractWalkModelDiGraphmeans that any optimization to the path-/walk-finding mechanisms benefits all solvers that inherit from these classes. In particular, in these abstract classes we implement "safety optimizations" described in this paper (for DAGs) and this paper (for general graphs), based on ideas first introduced in this paper. These can provide up to 1000x speedups, depending on the graph instance, while preserving global optimality (under some simple assumptions). -
Flexible inputs: The models support graphs with flows/weights on either edges or nodes, and additional real use-case input features, such as subpathconstraints or subset constraints.
Models currently implemented
-
Minimum Flow Decomposition: Given a graph with flow values on its edges (i.e. at every node different from source or sink the flow entering the node is equal to the flow exiting the node), find the minimum number of weighted paths / walks such that, for every edge, the sum of the weights of the paths going through the edge equals the flow value of the edge.
-
k-Least Absolute Errors: Given a graph with weights on its edges, and a number $k$, find $k$ weighted paths / walks such that the sum of the absolute errors of each edge is minimized.
- The error of an edge is defined as the weight of the edge minus the sum of the weights of the paths / walks going through it.
-
k-Minimum Path Error: Given a graph with weights on its edges, and a number $k$, find $k$ weighted paths / walks, with associated slack values, such that:
- The error of each edge (defined as in $k$-Least Absolute Errors above) is at most the sum of the slacks of the paths / walks going through the edge, and
- The sum of path / walk slacks is minimized.
-
k-Minimum Discordant Nodes: Given a graph with flows on nodes and a number $k$, find $k$ weighted paths / walks minimizing the number of nodes whose explained flow deviates from the observed node flow beyond a tolerance.
-
Minimum-Paths Minimum Discordant Nodes: Automatically optimize over $k$ (with
NumPathsOptimization) for the k-Minimum Discordant Nodes objective, in both DAG and cyclic-graph variants. -
Minimum Path Cover: Given a graph and node sets S and T, find a minimum number of S-T paths (if the graph is acyclic) or S-T walks (if the graph has cycles) such that every edge appears in at least one path or walk.
Contributing
Contributions are welcome! Please read the CONTRIBUTING.md guide for how to set up a dev environment, run tests locally, and build/preview the documentation with Material for MkDocs.
License and support
- License: MIT
- Issues: https://github.com/algbio/flowpaths/issues
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flowpaths-0.2.16.tar.gz.
File metadata
- Download URL: flowpaths-0.2.16.tar.gz
- Upload date:
- Size: 152.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60b6a09d9ec739330727a104c793190ce533fe028d857f58d58ad617552cd315
|
|
| MD5 |
89d76538d5af865bcb8e3abbe7f0b091
|
|
| BLAKE2b-256 |
4ea045563d6929ca036328dac1cdb252835f695fb353bef102ac90490861b5cf
|
Provenance
The following attestation bundles were made for flowpaths-0.2.16.tar.gz:
Publisher:
python-publish.yml on algbio/flowpaths
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flowpaths-0.2.16.tar.gz -
Subject digest:
60b6a09d9ec739330727a104c793190ce533fe028d857f58d58ad617552cd315 - Sigstore transparency entry: 1459982984
- Sigstore integration time:
-
Permalink:
algbio/flowpaths@38d734d1b1efa1894a4dee49818970d59bb6a0c4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/algbio
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@38d734d1b1efa1894a4dee49818970d59bb6a0c4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file flowpaths-0.2.16-py3-none-any.whl.
File metadata
- Download URL: flowpaths-0.2.16-py3-none-any.whl
- Upload date:
- Size: 178.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
419fe6815586a068f6e39aec0b1ccacdd86544e12f591c909d0f60e3c8242702
|
|
| MD5 |
a44a1146a60a0a4cd5aed6a6c3557f06
|
|
| BLAKE2b-256 |
541b62d170d820678c1c5591cea4a253a0996fe78305e7b1560ccce5025be519
|
Provenance
The following attestation bundles were made for flowpaths-0.2.16-py3-none-any.whl:
Publisher:
python-publish.yml on algbio/flowpaths
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flowpaths-0.2.16-py3-none-any.whl -
Subject digest:
419fe6815586a068f6e39aec0b1ccacdd86544e12f591c909d0f60e3c8242702 - Sigstore transparency entry: 1459983116
- Sigstore integration time:
-
Permalink:
algbio/flowpaths@38d734d1b1efa1894a4dee49818970d59bb6a0c4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/algbio
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@38d734d1b1efa1894a4dee49818970d59bb6a0c4 -
Trigger Event:
push
-
Statement type: