Files
ReactionSystems/src/rsprocess/bisimilarity/bisimilarity_paige_tarkan.rs

761 lines
22 KiB
Rust
Raw Normal View History

//! Bisimilarity by Paige and Tarjan from Three Partition Refinement Algorithms
//! by Robert Paige L., Robert Endre Tarjan; pages 977 to 983
2025-08-28 23:10:30 +02:00
//! <https://doi.org/10.1137/0216062>
2025-07-24 18:18:27 +02:00
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
2025-07-24 18:18:27 +02:00
use std::rc::Rc;
2025-07-16 00:06:40 +02:00
2025-07-24 18:18:27 +02:00
use petgraph::Direction::{Incoming, Outgoing};
use petgraph::visit::{
2025-09-10 22:41:40 +02:00
EdgeCount, EdgeRef, GraphBase, IntoEdgeReferences, IntoNeighborsDirected,
IntoNodeIdentifiers, IntoNodeReferences, NodeCount,
};
2025-07-24 18:18:27 +02:00
type NodeIdType = u32;
type GraphIdType = u32;
type NodeType = (GraphIdType, NodeIdType);
trait NextId<From, T> {
fn next_id_of_graph(&mut self, val: From) -> T;
2025-07-24 18:18:27 +02:00
}
struct Translator<From, To, State>
where
State: NextId<From, To>,
2025-07-24 18:18:27 +02:00
{
data: HashMap<From, To>,
reverse_data: HashMap<To, From>,
last_id: State,
2025-07-24 18:18:27 +02:00
}
impl<From, To, State> Translator<From, To, State>
where
To: std::hash::Hash + std::cmp::Eq + Copy,
From: std::hash::Hash + std::cmp::Eq + Clone,
State: NextId<From, To>,
2025-07-24 18:18:27 +02:00
{
pub fn new() -> Self
where
State: Default,
{
Translator {
data: HashMap::new(),
reverse_data: HashMap::new(),
last_id: State::default(),
}
}
pub fn encode(&mut self, val: From) -> To {
let id = *(self
.data
.entry(val.clone())
.or_insert(self.last_id.next_id_of_graph(val.clone())));
self.reverse_data.insert(id, val);
id
}
pub fn get(&self, val: &From) -> Option<&To> {
self.data.get(val)
}
pub fn decode(&self, val: &To) -> Option<&From> {
self.reverse_data.get(val)
}
2025-07-24 18:18:27 +02:00
}
2025-07-24 20:51:59 +02:00
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
2025-07-24 18:18:27 +02:00
struct NodeState<const N: usize> {
last_ids: [u32; N],
2025-07-24 18:18:27 +02:00
}
impl<const N: usize> NodeState<N> {
fn new() -> Self {
NodeState { last_ids: [0; N] }
}
2025-07-24 18:18:27 +02:00
}
impl<const N: usize> Default for NodeState<N> {
fn default() -> Self {
Self::new()
}
2025-07-24 18:18:27 +02:00
}
impl<const N: usize, T> NextId<(T, GraphIdType), NodeType> for NodeState<N> {
fn next_id_of_graph(&mut self, val: (T, GraphIdType)) -> NodeType {
let graph_id_usize = val.1 as usize;
assert!(graph_id_usize < N);
self.last_ids[graph_id_usize] += 1;
(val.1, self.last_ids[graph_id_usize])
}
2025-07-24 18:18:27 +02:00
}
type MyNodeTranslator<From, const N: usize> =
Translator<(From, GraphIdType), NodeType, NodeState<N>>;
2025-07-24 18:18:27 +02:00
2025-07-26 01:52:43 +02:00
type EdgeIdType = u32;
type EdgeType = EdgeIdType;
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct EdgeState {
last_ids: u32,
2025-07-26 01:52:43 +02:00
}
impl EdgeState {
fn new() -> Self {
EdgeState { last_ids: 0 }
}
2025-07-26 01:52:43 +02:00
}
impl Default for EdgeState {
fn default() -> Self {
Self::new()
}
2025-07-26 01:52:43 +02:00
}
impl<T> NextId<T, EdgeType> for EdgeState {
fn next_id_of_graph(&mut self, _val: T) -> EdgeType {
self.last_ids += 1;
self.last_ids
}
2025-07-26 01:52:43 +02:00
}
type MyEdgeTranslator<From> = Translator<From, EdgeType, EdgeState>;
2025-07-24 18:18:27 +02:00
type Block = Vec<NodeType>;
type BackEdges = HashMap<NodeType, Vec<NodeType>>;
type NodeToBlock = HashMap<NodeType, Rc<RefCell<SimpleBlock>>>;
type CompoundPartition = Vec<Rc<CompoundBlock>>;
type SimpleBlockPointer = Rc<RefCell<SimpleBlock>>;
type CompoundBlockPointer = Rc<CompoundBlock>;
type BackEdgesGrouped = HashMap<Block, BackEdgesGroup>;
struct SimpleBlock {
block: Block,
coarse_block_that_supersets_self: Rc<CompoundBlock>,
2025-07-24 18:18:27 +02:00
}
#[derive(Clone)]
struct CompoundBlock {
block: Block,
simple_blocks_subsets_of_self: RefCell<Vec<Rc<RefCell<SimpleBlock>>>>,
2025-07-24 18:18:27 +02:00
}
impl CompoundBlock {
fn add_simple_block(&self, fine_block: Rc<RefCell<SimpleBlock>>) {
self.simple_blocks_subsets_of_self
.borrow_mut()
.push(fine_block);
}
fn remove_simple_block(&self, fine_block: &Rc<RefCell<SimpleBlock>>) {
self.simple_blocks_subsets_of_self
.borrow_mut()
.retain(|x| !Rc::ptr_eq(x, fine_block));
}
fn simple_block_count(&self) -> usize {
self.simple_blocks_subsets_of_self.borrow().len()
}
2025-07-24 18:18:27 +02:00
}
struct BackEdgesGroup {
block: Rc<RefCell<SimpleBlock>>,
subblock: Block,
2025-07-24 18:18:27 +02:00
}
trait HasBlock {
fn block(&self) -> Block;
2025-07-24 18:18:27 +02:00
}
impl HasBlock for SimpleBlockPointer {
fn block(&self) -> Block {
(**self).borrow().block.clone()
}
2025-07-24 18:18:27 +02:00
}
impl HasBlock for CompoundBlock {
fn block(&self) -> Block {
self.block.clone()
}
2025-07-24 18:18:27 +02:00
}
#[allow(clippy::type_complexity)]
2025-07-24 18:18:27 +02:00
fn initialization<const N: usize, G>(
graphs: &[&G; N],
) -> (
(SimpleBlockPointer, SimpleBlockPointer),
CompoundPartition,
NodeToBlock,
MyNodeTranslator<G::NodeId, N>,
)
2025-07-24 18:18:27 +02:00
where
G: IntoNodeReferences + IntoNeighborsDirected,
G::NodeId: std::cmp::Eq + std::hash::Hash,
2025-07-24 18:18:27 +02:00
{
// translate into unique ids
let mut convert_nodes: MyNodeTranslator<G::NodeId, N> = Translator::new();
let graph_node_indices = {
let mut tmp: Block = vec![];
for (pos, graph) in graphs.iter().enumerate() {
tmp.extend(
graph
.node_identifiers()
.map(|val| convert_nodes.encode((val, pos as u32)))
.collect::<Vec<_>>(),
);
}
tmp
};
let compound_initial_block_pointer: Rc<CompoundBlock> = {
let compound_initial_block = CompoundBlock {
block: graph_node_indices.clone(),
simple_blocks_subsets_of_self: RefCell::new(vec![]),
};
2025-07-24 18:18:27 +02:00
Rc::new(compound_initial_block)
};
// minor optimization: split nodes between those that have outgoing edges
// and those that dont
let (leaf_node_block_pointer, non_leaf_node_block_pointer) = {
let (leaf_node_indices, non_leaf_node_indices): (Block, Block) =
graph_node_indices.clone().into_iter().partition(|x| {
let (node_id, graph_id) = convert_nodes.decode(x).unwrap();
graphs[*graph_id as usize]
.neighbors_directed(*node_id, Outgoing)
.count()
== 0
});
let leaf_node_block = SimpleBlock {
block: leaf_node_indices,
2025-09-10 22:41:40 +02:00
coarse_block_that_supersets_self: Rc::clone(
&compound_initial_block_pointer,
),
};
let non_leaf_node_block = SimpleBlock {
block: non_leaf_node_indices,
2025-09-10 22:41:40 +02:00
coarse_block_that_supersets_self: Rc::clone(
&compound_initial_block_pointer,
),
};
(
Rc::new(RefCell::new(leaf_node_block)),
Rc::new(RefCell::new(non_leaf_node_block)),
)
};
compound_initial_block_pointer
.simple_blocks_subsets_of_self
.borrow_mut()
.extend([
Rc::clone(&leaf_node_block_pointer),
Rc::clone(&non_leaf_node_block_pointer),
]);
let node_to_block = {
let mut node_to_block = HashMap::new();
(*non_leaf_node_block_pointer)
.borrow()
.block
.iter()
.copied()
.for_each(|value| {
2025-09-10 22:41:40 +02:00
node_to_block
.insert(value, Rc::clone(&non_leaf_node_block_pointer));
});
(*leaf_node_block_pointer)
.borrow()
.block
.iter()
.copied()
.for_each(|value| {
2025-09-10 22:41:40 +02:00
node_to_block
.insert(value, Rc::clone(&leaf_node_block_pointer));
});
node_to_block
};
(
(leaf_node_block_pointer, non_leaf_node_block_pointer),
vec![compound_initial_block_pointer],
node_to_block,
convert_nodes,
)
2025-07-24 18:18:27 +02:00
}
fn build_backedges<IndexHolder: HasBlock, const N: usize, G>(
graphs: &[&G; N],
block: IndexHolder,
convert_nodes: &MyNodeTranslator<G::NodeId, N>,
) -> BackEdges
2025-07-24 18:18:27 +02:00
where
G: IntoNeighborsDirected,
G::NodeId: std::cmp::Eq + std::hash::Hash,
2025-07-24 18:18:27 +02:00
{
let mut backedges = HashMap::new();
block.block().iter().for_each(|node_index_pointer| {
backedges.insert(*node_index_pointer, {
2025-09-10 22:41:40 +02:00
let (node_id, graph_id) =
convert_nodes.decode(node_index_pointer).unwrap();
graphs[*graph_id as usize]
.neighbors_directed(*node_id, Incoming)
.collect::<HashSet<_>>()
.into_iter()
// the back edges should be all in the same graph
.map(|e| convert_nodes.get(&(e, *graph_id)).unwrap())
.copied()
.collect::<Vec<_>>()
});
});
backedges
2025-07-24 18:18:27 +02:00
}
2025-09-10 22:41:40 +02:00
fn group_by_backedges(
backedges: BackEdges,
node_to_block: &NodeToBlock,
) -> BackEdgesGrouped {
let mut backedges_grouped: BackEdgesGrouped = HashMap::new();
for incoming_neighbor_group in backedges.values() {
for node in incoming_neighbor_group {
let block = Rc::clone(node_to_block.get(node).unwrap());
let key = (*block).borrow().block.clone();
match backedges_grouped.entry(key) {
2025-09-10 22:41:40 +02:00
Entry::Occupied(mut entry) => {
entry.get_mut().subblock.push(*node)
}
Entry::Vacant(entry) => {
entry.insert(BackEdgesGroup {
block: Rc::clone(&block),
subblock: Vec::from([*node]),
});
}
}
}
}
2025-07-24 18:18:27 +02:00
backedges_grouped
2025-07-24 18:18:27 +02:00
}
fn split_blocks_with_grouped_backedges(
mut backedges_grouped: BackEdgesGrouped,
node_to_block: &mut NodeToBlock,
2025-07-24 18:18:27 +02:00
) -> (
(Vec<SimpleBlockPointer>, Vec<SimpleBlockPointer>),
Vec<CompoundBlockPointer>,
2025-07-24 18:18:27 +02:00
) {
let mut all_new_simple_blocks: Vec<Rc<RefCell<SimpleBlock>>> = vec![];
let mut all_removed_simple_blocks: Vec<Rc<RefCell<SimpleBlock>>> = vec![];
let mut new_compound_blocks: Vec<Rc<CompoundBlock>> = vec![];
for (block, back_edges_group) in backedges_grouped.iter_mut() {
let borrowed_compound_block = Rc::clone(
&(*back_edges_group.block)
.borrow()
.coarse_block_that_supersets_self,
);
let proper_subblock = {
let simple_block = SimpleBlock {
block: back_edges_group.subblock.clone(),
2025-09-10 22:41:40 +02:00
coarse_block_that_supersets_self: Rc::clone(
&borrowed_compound_block,
),
};
Rc::new(RefCell::new(simple_block))
};
let prior_count = borrowed_compound_block.simple_block_count();
borrowed_compound_block.add_simple_block(Rc::clone(&proper_subblock));
if prior_count == 1 {
new_compound_blocks.push(Rc::clone(&borrowed_compound_block));
}
for node in back_edges_group.subblock.iter() {
node_to_block.insert(*node, Rc::clone(&proper_subblock));
}
// subtract subblock from block
(*back_edges_group.block).borrow_mut().block = block
.iter()
.filter(|x| !(*proper_subblock).borrow().block.contains(x))
.copied()
.collect();
if (*back_edges_group.block).borrow().block.is_empty() {
2025-09-10 22:41:40 +02:00
borrowed_compound_block
.remove_simple_block(&back_edges_group.block);
all_removed_simple_blocks.push(Rc::clone(&back_edges_group.block));
}
all_new_simple_blocks.push(Rc::clone(&proper_subblock));
}
(
(all_new_simple_blocks, all_removed_simple_blocks),
new_compound_blocks,
)
2025-07-24 18:18:27 +02:00
}
fn maximum_bisimulation<const N: usize, G>(
graphs: &[&G; N],
2025-07-26 01:52:43 +02:00
) -> (Option<Vec<Block>>, MyNodeTranslator<G::NodeId, N>)
2025-07-24 18:18:27 +02:00
where
G: IntoNodeReferences + IntoNeighborsDirected,
G::NodeId: std::cmp::Eq + std::hash::Hash,
2025-07-24 18:18:27 +02:00
{
let (
(simple_block_0, simple_block_1),
initial_compound_partition,
mut node_to_block,
converter,
) = initialization(graphs);
let mut queue: CompoundPartition = initial_compound_partition;
let mut all_simple_blocks = vec![simple_block_0, simple_block_1];
loop {
let (smaller_component, simple_splitter_block) = {
let splitter_block = match queue.pop() {
Some(coarse_block) => coarse_block,
None => break,
};
let mut simple_blocks_in_splitter_block = splitter_block
.simple_blocks_subsets_of_self
.borrow()
.clone();
let smaller_component_index = {
match simple_blocks_in_splitter_block
.iter()
.enumerate()
.min_by(|(_, x), (_, y)| {
(***x)
.borrow()
.block
.len()
.cmp(&(***y).borrow().block.len())
})
.map(|(index, _)| index)
{
Some(v) => v,
None => return (None, converter),
}
};
2025-09-10 22:41:40 +02:00
let smaller_component =
simple_blocks_in_splitter_block.remove(smaller_component_index);
let simple_splitter_block_values: Block = splitter_block
.block
.clone()
.iter()
.filter(|x| !(*smaller_component).borrow().block.contains(x))
.copied()
.collect();
let simple_splitter_block = CompoundBlock {
block: simple_splitter_block_values,
2025-09-10 22:41:40 +02:00
simple_blocks_subsets_of_self: RefCell::new(
simple_blocks_in_splitter_block,
),
};
let simple_splitter_block_pointer = Rc::new(simple_splitter_block);
if simple_splitter_block_pointer
.simple_blocks_subsets_of_self
.borrow()
.len()
> 1
{
queue.push(Rc::clone(&simple_splitter_block_pointer));
}
(smaller_component, simple_splitter_block_pointer)
};
simple_splitter_block
.simple_blocks_subsets_of_self
.borrow()
.iter()
.for_each(|x| {
(*x).borrow_mut().coarse_block_that_supersets_self =
Rc::clone(&simple_splitter_block);
});
2025-09-10 22:41:40 +02:00
let mut back_edges =
build_backedges(graphs, smaller_component, &converter);
let back_edges_group =
group_by_backedges(back_edges.clone(), &node_to_block);
let (
(new_simple_blocks, removeable_simple_blocks),
compound_block_that_are_now_compound,
) = split_blocks_with_grouped_backedges(
back_edges_group,
&mut node_to_block,
);
all_simple_blocks.extend(new_simple_blocks);
2025-09-10 22:41:40 +02:00
all_simple_blocks.retain(|x| {
!removeable_simple_blocks.iter().any(|y| Rc::ptr_eq(x, y))
});
queue.extend(compound_block_that_are_now_compound);
// back edges = E^{-1}(B) - E^{-1}(S-B)
{
2025-09-10 22:41:40 +02:00
let back_edges_splitter_complement = build_backedges(
graphs,
(*simple_splitter_block).clone(),
&converter,
);
back_edges_splitter_complement.keys().for_each(|node| {
back_edges.remove(node);
});
}
let back_edges_group = group_by_backedges(back_edges, &node_to_block);
2025-09-10 22:41:40 +02:00
let (
(new_fine_blocks, removeable_fine_blocks),
coarse_block_that_are_now_compound,
) = split_blocks_with_grouped_backedges(
back_edges_group,
&mut node_to_block,
);
all_simple_blocks.extend(new_fine_blocks);
2025-09-10 22:41:40 +02:00
all_simple_blocks.retain(|x| {
!removeable_fine_blocks.iter().any(|y| Rc::ptr_eq(x, y))
});
queue.extend(coarse_block_that_are_now_compound);
}
(
Some(
all_simple_blocks
.iter()
.map(|x| (**x).borrow().block.clone())
// remove leaf block when there are no leaves
.filter(|x| !x.is_empty())
.collect(),
),
converter,
)
2025-07-24 18:18:27 +02:00
}
2025-07-26 01:52:43 +02:00
/// Creates a new graph with nodes as signifiers instead of different weights on
/// the edges.
2025-07-28 22:15:53 +02:00
fn create_modified_graph<G>(
graph: &G,
converter_edges: &MyEdgeTranslator<G::EdgeWeight>,
2025-07-28 22:15:53 +02:00
) -> (petgraph::Graph<u32, u32>, HashSet<u32>)
2025-07-26 01:52:43 +02:00
where
2025-07-28 22:15:53 +02:00
G: NodeCount + EdgeCount + IntoEdgeReferences + IntoNodeIdentifiers,
G::NodeId: std::cmp::Eq + std::hash::Hash,
G::EdgeWeight: std::cmp::Eq + std::hash::Hash + Clone,
2025-07-26 01:52:43 +02:00
{
let mut new_graph_a: petgraph::Graph<_, u32> =
2025-09-10 22:41:40 +02:00
petgraph::Graph::with_capacity(
graph.node_count() * 4,
graph.edge_count() * 4,
);
let mut association_weight_id = HashMap::new();
2025-07-28 22:15:53 +02:00
let mut original_nodes = HashSet::new();
let mut last_id = 0;
for edge in graph.edge_references() {
let source_id = match association_weight_id.get(&edge.source()) {
Some(id) => *id,
None => {
let id = new_graph_a.add_node(last_id);
original_nodes.insert(last_id);
last_id += 1;
association_weight_id.insert(edge.source(), id);
id
}
};
let target_id = match association_weight_id.get(&edge.target()) {
Some(id) => *id,
None => {
let id = new_graph_a.add_node(last_id);
original_nodes.insert(last_id);
last_id += 1;
association_weight_id.insert(edge.target(), id);
id
}
};
let weight = *converter_edges.get(edge.weight()).unwrap();
let middle_node_id = new_graph_a.add_node(0);
new_graph_a.add_edge(source_id, middle_node_id, weight);
new_graph_a.add_edge(middle_node_id, target_id, weight);
let mut previous = middle_node_id;
for _ in 0..weight {
let path = new_graph_a.add_node(0);
new_graph_a.add_edge(previous, path, weight);
previous = path;
}
}
2025-07-26 01:52:43 +02:00
2025-07-28 22:15:53 +02:00
for node in graph.node_identifiers() {
let mut previous = *association_weight_id.get(&node).unwrap();
for _ in 0..converter_edges.last_id.last_ids + 2 {
let path = new_graph_a.add_node(0);
new_graph_a.add_edge(previous, path, 0);
previous = path;
}
}
2025-07-26 01:52:43 +02:00
2025-07-28 22:15:53 +02:00
(new_graph_a, original_nodes)
2025-07-26 01:52:43 +02:00
}
2025-07-28 22:15:53 +02:00
#[allow(clippy::type_complexity)]
fn modify_graph<G>(
graph_a: &G,
graph_b: &G,
) -> (
(petgraph::Graph<u32, u32>, HashSet<u32>),
(petgraph::Graph<u32, u32>, HashSet<u32>),
)
2025-07-26 01:52:43 +02:00
where
2025-07-28 22:15:53 +02:00
G: IntoNodeReferences + IntoNeighborsDirected + NodeCount + EdgeCount,
G: IntoEdgeReferences,
G::NodeId: std::cmp::Eq + std::hash::Hash,
G::EdgeWeight: std::cmp::Eq + std::hash::Hash + Clone,
2025-07-26 01:52:43 +02:00
{
let converter_edges: MyEdgeTranslator<G::EdgeWeight> = {
let mut converter_edges = Translator::new();
let mut labels: HashMap<G::EdgeWeight, u32> = HashMap::new();
for edge in graph_a.edge_references() {
*labels.entry(edge.weight().clone()).or_default() += 1;
}
for edge in graph_b.edge_references() {
*labels.entry(edge.weight().clone()).or_default() += 1;
}
// slight optimization: we reorder the edges such that edges with the
// most occurrences have smaller index
2025-09-10 22:41:40 +02:00
let mut labels: Vec<(G::EdgeWeight, u32)> =
labels.into_iter().collect();
labels.sort_by(|a, b| b.1.cmp(&a.1));
for (label, _) in labels.into_iter() {
let _ = converter_edges.encode(label);
}
converter_edges
};
let new_graph_a = create_modified_graph(graph_a, &converter_edges);
let new_graph_b = create_modified_graph(graph_b, &converter_edges);
2025-07-28 22:15:53 +02:00
(new_graph_a, new_graph_b)
}
/// check if every block contains either no original nodes or nodes from both
/// graphs
fn check_bisimilarity<G>(
val: Vec<Vec<NodeType>>,
converter_bisimulated_graph: &MyNodeTranslator<
<petgraph::Graph<u32, u32> as GraphBase>::NodeId,
2,
>,
original_nodes: [HashSet<u32>; 2],
2025-07-28 22:15:53 +02:00
) -> bool
where
G: IntoEdgeReferences,
G::EdgeWeight: std::cmp::Eq + std::hash::Hash + Clone,
{
val.into_iter().all(|el| {
let mut keep_track = [false, false];
for e in el {
2025-09-10 22:41:40 +02:00
let (_node_id, graph_id) =
converter_bisimulated_graph.decode(&e).unwrap();
if original_nodes[*graph_id as usize].contains(&e.0) {
keep_track[*graph_id as usize] = true;
}
}
!(keep_track[0] ^ keep_track[1])
})
2025-07-28 22:15:53 +02:00
}
// -----------------------------------------------------------------------------
pub fn bisimilarity<G>(graph_a: &G, graph_b: &G) -> bool
2025-07-28 22:15:53 +02:00
where
G: IntoNodeReferences + IntoNeighborsDirected + NodeCount + EdgeCount,
G: IntoEdgeReferences,
G::NodeId: std::cmp::Eq + std::hash::Hash,
G::EdgeWeight: std::cmp::Eq + std::hash::Hash + Clone,
{
if graph_a.node_count() == 0 && graph_b.node_count() == 0 {
return true;
2025-07-28 22:15:53 +02:00
}
if graph_a.node_count() == 0 || graph_b.node_count() == 0 {
return false;
2025-07-28 22:15:53 +02:00
}
let ((new_graph_a, original_nodes_a), (new_graph_b, original_nodes_b)) =
modify_graph(graph_a, graph_b);
2025-09-10 22:41:40 +02:00
let (result, _converter) =
match maximum_bisimulation(&[&&new_graph_a, &&new_graph_b]) {
(None, _) => return false,
(Some(val), converter) => (
check_bisimilarity::<G>(
val,
&converter,
[original_nodes_a, original_nodes_b],
),
converter,
),
};
2025-07-28 22:15:53 +02:00
result
}
pub fn bisimilarity_ignore_labels<G>(graph_a: &G, graph_b: &G) -> bool
2025-07-28 22:15:53 +02:00
where
G: IntoNodeReferences + IntoNeighborsDirected + NodeCount,
G::NodeId: std::cmp::Eq + std::hash::Hash,
2025-07-28 22:15:53 +02:00
{
if graph_a.node_count() == 0 && graph_b.node_count() == 0 {
return true;
2025-07-28 22:15:53 +02:00
}
if graph_a.node_count() == 0 || graph_b.node_count() == 0 {
return false;
2025-07-28 22:15:53 +02:00
}
let (result, _converter) = match maximum_bisimulation(&[graph_a, graph_b]) {
(None, _) => return false,
(Some(val), converter) => (
val.into_iter().find(|el| {
let mut keep_track = [false, false];
for e in el {
let (_node_id, graph_id) = converter.decode(e).unwrap();
keep_track[*graph_id as usize] = true;
}
!keep_track[0] || !keep_track[1]
}),
converter,
),
};
2025-07-26 01:52:43 +02:00
result.is_none()
2025-07-26 01:52:43 +02:00
}