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

Fix subgraph isomorphism #472

Merged
merged 2 commits into from Jul 4, 2022
Merged
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
19 changes: 14 additions & 5 deletions src/algo/isomorphism.rs
Expand Up @@ -555,6 +555,7 @@ mod matching {
mut st: &mut (Vf2State<'_, G0>, Vf2State<'_, G1>),
node_match: &mut NM,
edge_match: &mut EM,
match_subgraph: bool,
) -> Option<bool>
where
G0: NodeCompactIndexable
Expand Down Expand Up @@ -611,7 +612,13 @@ mod matching {
return Some(true);
}
// Check cardinalities of Tin, Tout sets
if st.0.out_size == st.1.out_size && st.0.ins_size == st.1.ins_size {
if (!match_subgraph
&& st.0.out_size == st.1.out_size
&& st.0.ins_size == st.1.ins_size)
|| (match_subgraph
&& st.0.out_size <= st.1.out_size
&& st.0.ins_size <= st.1.ins_size)
{
let f0 = Frame::Unwind { nodes, open_list };
stack.push(f0);
stack.push(Frame::Outer);
Expand Down Expand Up @@ -661,7 +668,8 @@ where
}

let mut st = (Vf2State::new(&g0), Vf2State::new(&g1));
self::matching::try_match(&mut st, &mut NoSemanticMatch, &mut NoSemanticMatch).unwrap_or(false)
self::matching::try_match(&mut st, &mut NoSemanticMatch, &mut NoSemanticMatch, false)
.unwrap_or(false)
}

/// \[Generic\] Return `true` if the graphs `g0` and `g1` are isomorphic.
Expand Down Expand Up @@ -697,7 +705,7 @@ where
}

let mut st = (Vf2State::new(&g0), Vf2State::new(&g1));
self::matching::try_match(&mut st, &mut node_match, &mut edge_match).unwrap_or(false)
self::matching::try_match(&mut st, &mut node_match, &mut edge_match, false).unwrap_or(false)
}

/// \[Generic\] Return `true` if `g0` is isomorphic to a subgraph of `g1`.
Expand Down Expand Up @@ -747,7 +755,8 @@ where
}

let mut st = (Vf2State::new(&g0), Vf2State::new(&g1));
self::matching::try_match(&mut st, &mut NoSemanticMatch, &mut NoSemanticMatch).unwrap_or(false)
self::matching::try_match(&mut st, &mut NoSemanticMatch, &mut NoSemanticMatch, true)
.unwrap_or(false)
}

/// \[Generic\] Return `true` if `g0` is isomorphic to a subgraph of `g1`.
Expand Down Expand Up @@ -783,5 +792,5 @@ where
}

let mut st = (Vf2State::new(&g0), Vf2State::new(&g1));
self::matching::try_match(&mut st, &mut node_match, &mut edge_match).unwrap_or(false)
self::matching::try_match(&mut st, &mut node_match, &mut edge_match, true).unwrap_or(false)
}
2 changes: 1 addition & 1 deletion tests/iso.rs
Expand Up @@ -488,7 +488,7 @@ fn iso_multigraph_failure() {
#[test]
fn iso_subgraph() {
let g0 = Graph::<(), ()>::from_edges(&[(0, 1), (1, 2), (2, 0)]);
let g1 = Graph::<(), ()>::from_edges(&[(0, 1), (1, 2), (2, 0), (2, 3)]);
let g1 = Graph::<(), ()>::from_edges(&[(0, 1), (1, 2), (2, 0), (2, 3), (0, 4)]);
assert!(!is_isomorphic(&g0, &g1));
assert!(is_isomorphic_subgraph(&g0, &g1));
}
Expand Down