| Crates.io | vf3 |
| lib.rs | vf3 |
| version | 0.1.0 |
| created_at | 2025-12-25 05:29:35.194043+00 |
| updated_at | 2025-12-25 05:29:35.194043+00 |
| description | Rust implementation of VF3/VF3L subgraph isomorphism algorithms |
| homepage | https://github.com/krzysztofwos/vf3 |
| repository | https://github.com/krzysztofwos/vf3 |
| max_upload_size | |
| id | 2004227 |
| size | 205,736 |
A pure Rust implementation of the VF3/VF3L subgraph isomorphism algorithms, inspired by the canonical C++ implementation from MIVIA Lab.
This is a functional implementation that works well on small to medium graphs. It implements:
Not yet implemented:
Add this to your Cargo.toml:
[dependencies]
vf3 = "0.1"
use vf3::{Graph, VF3Options, find_subgraph_matches_with_options};
// Create a pattern graph (triangle)
let mut pattern = Graph::new(3);
pattern.add_edge(0, 1)?;
pattern.add_edge(1, 2)?;
pattern.add_edge(2, 0)?;
// Create a target graph (square)
let mut target = Graph::new(4);
target.add_edge(0, 1)?;
target.add_edge(1, 2)?;
target.add_edge(2, 3)?;
target.add_edge(3, 0)?;
// Find all matches (node-induced by default)
let opts = VF3Options::default();
let matches = find_subgraph_matches_with_options(&pattern, &target, None, &opts);
println!("Found {} matches", matches.len());
pub struct VF3Options {
/// Use node-induced subgraph isomorphism (true) or edge-induced/monomorphism (false)
pub node_induced: bool,
/// Treat graphs as undirected (ignores edge direction)
pub undirected: bool,
/// Require matching node labels
pub use_node_labels: bool,
/// Require matching edge labels
pub use_edge_labels: bool,
}
// Node labels
let mut g = Graph::new(3);
g.set_label(0, 1); // Node 0 has label 1
g.set_label(1, 2); // Node 1 has label 2
// Edge labels
g.add_edge_with_label(0, 1, 42)?; // Edge with label 42
g.add_edge(1, 2)?; // Edge with default label 0
// Enable label matching
let opts = VF3Options {
use_node_labels: true,
use_edge_labels: true,
..Default::default()
};
For best performance, compile with release optimizations:
cargo build --release
# With CPU-specific optimizations
RUSTFLAGS="-C target-cpu=native" cargo build --release
This crate uses unsafe code in performance-critical sections to achieve competitive performance with C++ implementations. The unsafe code is:
lookahead_neighbor_capacity function which is called millions of times during graph matchingThe unsafe optimizations include:
get_unchecked for array access where bounds are guaranteed by constructionAll unsafe code follows Rust's safety guidelines and the invariants are maintained by the VF3State structure. The codebase has been thoroughly tested and follows patterns used in other high-performance Rust graph libraries.
This crate includes an optional feature to compare against the C++ VF3 implementation via FFI:
# Run comparison tests
cargo test --features compare-cpp
# Run benchmarks comparing both implementations
cargo bench vf3_vs_cpp --features compare-cpp
# Basic usage example
cargo run --example basic
# Performance testing harness
cargo run --example bench-harness --release
The crate includes a command-line tool for running VF3 on JSON graph files:
# Install the CLI tool
cargo install --path .
# Run on graph files
vf3 --pattern pattern.json --target target.json
# With options
vf3 --pattern p.json --target t.json --node-induced --use-node-labels
This implementation follows the VF3L (lightweight) variant which:
The algorithm is particularly effective on sparse graphs and when the pattern is much smaller than the target.
MIT OR Apache-2.0