Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vf2pp graph isomorphism algorithm #595

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions benches/common/factories.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::marker::PhantomData;

use petgraph::data::Build;
use petgraph::prelude::*;
use petgraph::visit::NodeIndexable;
use std::fs::File;
use std::io::Read;
use std::marker::PhantomData;

use petgraph::EdgeType;

Expand Down Expand Up @@ -203,6 +204,15 @@ where
g
}

/// Parse a file in adjacency matrix format into a directed graph
pub fn graph_from_file(path: &str) -> Graph<(), (), Directed> {
let mut f = File::open(path).expect("file not found");
let mut contents = String::new();
f.read_to_string(&mut contents)
.expect("failed to read from file");
parse_graph::<Directed, _>(&contents)
}

pub struct GraphFactory<Ty, G = Graph<(), (), Ty>> {
ty: PhantomData<Ty>,
g: PhantomData<G>,
Expand Down
67 changes: 66 additions & 1 deletion benches/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
extern crate petgraph;
extern crate test;

use std::default;

use test::Bencher;

#[allow(dead_code)]
mod common;
use common::*;

use petgraph::algo::is_isomorphic;
use petgraph::algo::{is_isomorphic, vf2pp_is_isomorphism_matching};

#[bench]
fn petersen_iso_bench(bench: &mut Bencher) {
Expand All @@ -20,6 +22,15 @@ fn petersen_iso_bench(bench: &mut Bencher) {
assert!(is_isomorphic(&a, &b));
}

#[bench]
fn vf2pp_petersen_iso_bench(bench: &mut Bencher) {
let a = digraph().petersen_a();
let b = digraph().petersen_b();

bench.iter(|| vf2pp_is_isomorphism_matching(&a, &b, false));
assert!(vf2pp_is_isomorphism_matching(&a, &b, false));
}

#[bench]
fn petersen_undir_iso_bench(bench: &mut Bencher) {
let a = ungraph().petersen_a();
Expand All @@ -29,6 +40,15 @@ fn petersen_undir_iso_bench(bench: &mut Bencher) {
assert!(is_isomorphic(&a, &b));
}

#[bench]
fn vf2pp_petersen_undir_iso_bench(bench: &mut Bencher) {
let a = ungraph().petersen_a();
let b = ungraph().petersen_b();

bench.iter(|| vf2pp_is_isomorphism_matching(&a, &b, false));
assert!(vf2pp_is_isomorphism_matching(&a, &b, false));
}

#[bench]
fn full_iso_bench(bench: &mut Bencher) {
let a = ungraph().full_a();
Expand All @@ -38,6 +58,15 @@ fn full_iso_bench(bench: &mut Bencher) {
assert!(is_isomorphic(&a, &b));
}

#[bench]
fn vf2pp_full_iso_bench(bench: &mut Bencher) {
let a = ungraph().full_a();
let b = ungraph().full_b();

bench.iter(|| vf2pp_is_isomorphism_matching(&a, &b, false));
assert!(vf2pp_is_isomorphism_matching(&a, &b, false));
}

#[bench]
fn praust_dir_no_iso_bench(bench: &mut Bencher) {
let a = digraph().praust_a();
Expand All @@ -47,6 +76,15 @@ fn praust_dir_no_iso_bench(bench: &mut Bencher) {
assert!(!is_isomorphic(&a, &b));
}

#[bench]
fn vf2pp_praust_dir_no_iso_bench(bench: &mut Bencher) {
let a = digraph().praust_a();
let b = digraph().praust_b();

bench.iter(|| vf2pp_is_isomorphism_matching(&a, &b, false));
assert!(!vf2pp_is_isomorphism_matching(&a, &b, false));
}

#[bench]
fn praust_undir_no_iso_bench(bench: &mut Bencher) {
let a = ungraph().praust_a();
Expand All @@ -55,3 +93,30 @@ fn praust_undir_no_iso_bench(bench: &mut Bencher) {
bench.iter(|| is_isomorphic(&a, &b));
assert!(!is_isomorphic(&a, &b));
}

#[bench]
fn vf2pp_praust_undir_no_iso_bench(bench: &mut Bencher) {
let a = ungraph().praust_a();
let b = ungraph().praust_b();

bench.iter(|| vf2pp_is_isomorphism_matching(&a, &b, false));
assert!(!vf2pp_is_isomorphism_matching(&a, &b, false));
}

#[bench]
fn iso_large(bench: &mut Bencher) {
let g0 = graph_from_file("benches/res/graph_1000n_1000e.txt");
let g1 = graph_from_file("benches/res/graph_1000n_1000e.txt");

bench.iter(|| is_isomorphic(&g0, &g1));
assert!(is_isomorphic(&g0, &g1));
}

#[bench]
fn vf2pp_iso_large(bench: &mut Bencher) {
let g0 = graph_from_file("benches/res/graph_1000n_1000e.txt");
let g1 = graph_from_file("benches/res/graph_1000n_1000e.txt");

bench.iter(|| vf2pp_is_isomorphism_matching(&g0, &g1, false));
assert!(vf2pp_is_isomorphism_matching(&g0, &g1, false));
}
1,000 changes: 1,000 additions & 0 deletions benches/res/graph_1000n_1000e.txt

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions src/algo/isomorphism/label.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::data::DataMap;
use crate::visit::GraphBase;

pub const DEFAULT_NODE_LABEL: usize = 0usize;

pub trait NodeLabel<G: GraphBase> {
fn get_node_label(&mut self, g: G, node_id: G::NodeId) -> usize;
}

pub struct NoNodeLabel;

impl<G: GraphBase> NodeLabel<G> for NoNodeLabel {
fn get_node_label(&mut self, _g: G, _id: <G as GraphBase>::NodeId) -> usize {
DEFAULT_NODE_LABEL
}
}

impl<G, F> NodeLabel<G> for F
where
G: DataMap,
F: FnMut(&G::NodeWeight) -> usize,
{
fn get_node_label(&mut self, g: G, node_id: <G as GraphBase>::NodeId) -> usize {
let node_weight = g.node_weight(node_id).unwrap();
self(node_weight)
}
}
13 changes: 13 additions & 0 deletions src/algo/isomorphism/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod label;
mod semantic;
mod vf2;
mod vf2pp;

pub use vf2::{
is_isomorphic, is_isomorphic_matching, is_isomorphic_subgraph, is_isomorphic_subgraph_matching,
subgraph_isomorphisms_iter,
};
pub use vf2pp::{
vf2pp_is_isomorphism_matching, vf2pp_is_isomorphism_semantic_matching,
vf2pp_isomorphism_semantic_matching_iter, Vf2ppMatcherBuilder,
};
103 changes: 103 additions & 0 deletions src/algo/isomorphism/semantic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use crate::data::DataMap;
use crate::visit::{EdgeRef, GraphBase, IntoEdgesDirected};
use crate::Outgoing;

pub struct NoSemanticMatch;

pub trait NodeMatcher<G0: GraphBase, G1: GraphBase> {
fn enabled() -> bool;
fn eq(&mut self, _g0: &G0, _g1: &G1, _n0: G0::NodeId, _n1: G1::NodeId) -> bool;
}

impl<G0: GraphBase, G1: GraphBase> NodeMatcher<G0, G1> for NoSemanticMatch {
#[inline]
fn enabled() -> bool {
false
}
#[inline]
fn eq(&mut self, _g0: &G0, _g1: &G1, _n0: G0::NodeId, _n1: G1::NodeId) -> bool {
true
}
}

impl<G0, G1, F> NodeMatcher<G0, G1> for F
where
G0: GraphBase + DataMap,
G1: GraphBase + DataMap,
F: FnMut(&G0::NodeWeight, &G1::NodeWeight) -> bool,
{
#[inline]
fn enabled() -> bool {
true
}
#[inline]
fn eq(&mut self, g0: &G0, g1: &G1, n0: G0::NodeId, n1: G1::NodeId) -> bool {
if let (Some(x), Some(y)) = (g0.node_weight(n0), g1.node_weight(n1)) {
self(x, y)
} else {
false
}
}
}

pub trait EdgeMatcher<G0: GraphBase, G1: GraphBase> {
fn enabled() -> bool;
fn eq(
&mut self,
_g0: &G0,
_g1: &G1,
e0: (G0::NodeId, G0::NodeId),
e1: (G1::NodeId, G1::NodeId),
) -> bool;
}

impl<G0: GraphBase, G1: GraphBase> EdgeMatcher<G0, G1> for NoSemanticMatch {
#[inline]
fn enabled() -> bool {
false
}
#[inline]
fn eq(
&mut self,
_g0: &G0,
_g1: &G1,
_e0: (G0::NodeId, G0::NodeId),
_e1: (G1::NodeId, G1::NodeId),
) -> bool {
true
}
}

impl<G0, G1, F> EdgeMatcher<G0, G1> for F
where
G0: GraphBase + DataMap + IntoEdgesDirected,
G1: GraphBase + DataMap + IntoEdgesDirected,
F: FnMut(&G0::EdgeWeight, &G1::EdgeWeight) -> bool,
{
#[inline]
fn enabled() -> bool {
true
}
#[inline]
fn eq(
&mut self,
g0: &G0,
g1: &G1,
e0: (G0::NodeId, G0::NodeId),
e1: (G1::NodeId, G1::NodeId),
) -> bool {
let w0 = g0
.edges_directed(e0.0, Outgoing)
.find(|edge| edge.target() == e0.1)
.and_then(|edge| g0.edge_weight(edge.id()));
let w1 = g1
.edges_directed(e1.0, Outgoing)
.find(|edge| edge.target() == e1.1)
.and_then(|edge| g1.edge_weight(edge.id()));
if let (Some(x), Some(y)) = (w0, w1) {
self(x, y)
} else {
false
}
}
}