Skip to content

Commit

Permalink
Add size hint to the subgraph isomorphism iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
vlvrd committed Jul 4, 2022
1 parent 5c1b920 commit cee07ea
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/algo/isomorphism.rs
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

use crate::data::DataMap;
use crate::visit::EdgeCount;
use crate::visit::EdgeRef;
Expand Down Expand Up @@ -755,6 +757,49 @@ mod matching {
&mut self.stack,
)
}

fn size_hint(&self) -> (usize, Option<usize>) {
// To calculate the upper bound of results we use n! where n is the
// number of nodes in graph 1. n! values fit into a 64-bit usize up
// to n = 20, so we don't estimate an upper limit for n > 20.
let n = self.st.0.graph.node_count();
const MAX_N: usize = 20;

if n > MAX_N {
return (0, None);
}

// We hardcode n! values into an array that accounts for architectures
// with smaller usizes to get our upper bound.
let upper_bounds: Vec<Option<usize>> = vec![
1u64,
1,
2,
6,
24,
120,
720,
5040,
40320,
362880,
3628800,
39916800,
479001600,
6227020800,
87178291200,
1307674368000,
20922789888000,
355687428096000,
6402373705728000,
121645100408832000,
2432902008176640000,
]
.iter()
.map(|n| usize::try_from(*n).ok())
.collect();

(0, upper_bounds[n])
}
}
}

Expand Down

0 comments on commit cee07ea

Please sign in to comment.