Skip to content

Commit

Permalink
Fix subgraph isomorphism (petgraph#472)
Browse files Browse the repository at this point in the history
* Fix subgraph isomorphism

* Run rustfmt
  • Loading branch information
cojuer authored and teuron committed Oct 9, 2022
1 parent e3e13e1 commit 12f1d33
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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

0 comments on commit 12f1d33

Please sign in to comment.