π netoptim-rs
A comprehensive Rust library for network optimization algorithms, built on top of the excellent petgraph library.
π Features
- Bellman-Ford Algorithm: Shortest paths with negative edge weight support
- Negative Cycle Detection: Efficient detection of negative cycles in directed graphs
- Parametric Optimization: Maximum parametric optimization for ratio-based problems
- Dijkstra's Algorithm: Fast shortest path computation for non-negative weights
- Howard's Algorithm: Polynomial-time negative cycle finding
- Graph Utilities: Comprehensive utilities for graph analysis and manipulation
- Serialization: JSON-based graph serialization and deserialization
- Visualization: DOT format export for Graphviz visualization
π Installation
Add this to your Cargo.toml:
[]
= "0.1"
Or install via cargo:
π Quick Start
Bellman-Ford Algorithm
use Graph;
use *;
use bellman_ford;
let mut g = new;
let a = g.add_node;
let b = g.add_node;
let c = g.add_node;
g.extend_with_edges;
match bellman_ford
Dijkstra's Algorithm
use Graph;
use *;
use ;
let mut g = new;
let a = g.add_node;
let b = g.add_node;
let c = g.add_node;
g.extend_with_edges;
// Compute all shortest paths from node A
let result = dijkstra.unwrap;
println!;
// Find shortest path from A to C
let path = dijkstra_path;
println!;
Negative Cycle Detection
use DiGraph;
use NegCycleFinder;
use Ratio;
let digraph = from_edges;
let mut ncf = new;
let mut dist = ;
if let Some = ncf.howard
Parametric Optimization
use DiGraph;
use ;
use Ratio;
use EdgeReference;
;
let digraph = from_edges;
let mut solver = new;
let mut dist = ;
let mut ratio = new;
let cycle = solver.run;
println!;
Graph Utilities
use *;
use Graph;
let g = from_edges;
// Check for cycles
println!;
// Serialize to JSON
let json = serialize_graph.unwrap;
println!;
// Export to DOT for visualization
let dot = to_dot;
println!;
π API Documentation
The complete API documentation is available on docs.rs.
Main Modules
bellman_ford: Bellman-Ford shortest path algorithmdijkstra: Dijkstra's shortest path algorithmneg_cycle: Negative cycle detection using Howard's algorithmparametric: Maximum parametric optimizationutils: Graph utility functionserror: Error types for the library
π Running Examples
The repository includes several examples demonstrating various features:
# Dijkstra algorithm examples
# Negative cycle detection examples
# Graph utilities examples
# Find negative cycles
# Property-based tests
π§ͺ Running Tests
Run the test suite:
# Run all tests (include --all-features for full CI)
# Run tests with output
# Run specific test
π Running Benchmarks
The library includes benchmarks using criterion:
# Run all benchmarks
# Run specific benchmark
π§ Features
Default Features
std is enabled by default (includes logging support).
Optional Features
std: Use standard library (enabled by default, includes logging)
π― Use Cases
- Network Routing: Finding optimal paths in communication networks
- Transportation Planning: Route optimization for logistics
- Currency Arbitrage: Detecting profitable currency exchange cycles
- Circuit Design: Finding negative cycles in circuit networks
- Game Development: Pathfinding and AI decision-making
- Scientific Computing: Network flow optimization problems
π Performance Characteristics
| Algorithm | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Bellman-Ford | O(VE) | O(V) | Handles negative weights |
| Dijkstra | O(E + V log V) | O(V) | Requires non-negative weights |
| Howard's Algorithm | O(VΒ³) worst case | O(V) | Fast in practice for negative cycles |
| Parametric Solver | O(k Β· VΒ³) | O(V) | k = number of iterations |
Where V = number of vertices, E = number of edges
π€ Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Development Setup
# Clone the repository
# Run tests
# Run clippy
# Format code
# Run benchmarks
π License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
π Acknowledgments
- Built on top of the excellent petgraph library
- Inspired by NetworkX and other graph algorithm libraries
- Uses num crate for numerical operations
π References
- Cormen, T. H., et al. "Introduction to Algorithms" (3rd ed.)
- Ahuja, R. K., Magnanti, T. L., & Orlin, J. B. "Network Flows"
- Karp, R. M. "A characterization of the minimum cycle mean in a digraph"
πΊοΈ Roadmap
- Additional shortest path algorithms (Floyd-Warshall, A*)
- Maximum flow algorithms (Edmonds-Karp, Push-Relabel)
- Minimum spanning tree algorithms (Kruskal, Prim)
- Parallel algorithm implementations
- More comprehensive examples and tutorials
- Performance optimizations for large graphs
π¬ Support
- π Documentation
- π Issue Tracker
- π¬ Discussions
Made with β€οΈ in Rust