API Reference
This section provides detailed API documentation for all modules and classes in the BiSC algorithm package.
bisc_package
BiSC Algorithm Package
Implementation of the BiSC algorithm from “BiSC: An algorithm for discovering generalized permutation patterns” by Henning Ulfarsson.
The BiSC algorithm automatically discovers forbidden patterns in sets of permutations, bridging computer science and mathematics through automated conjecture generation.
- class bisc_package.Permutation(sequence)[source]
Bases:
objectRepresents a permutation in one-line notation.
A permutation is a bijection from {1, 2, …, n} to itself. We represent it as a list where position i contains the value π(i).
- contains_classical_pattern(pattern)[source]
Check if this permutation contains a classical pattern.
- subwords(max_length)[source]
Generate all subwords of length at most max_length.
- Parameters:
max_length (
int) – Maximum length of subwords to generate- Returns:
subword is the subsequence of values
positions is the list of indices where these values occur
- Return type:
List of tuples (subword, positions) where
- class bisc_package.MeshPattern(pattern, shading=None)[source]
Bases:
objectRepresents a mesh pattern (classical pattern + shading).
A mesh pattern consists of: 1. A classical pattern (permutation) 2. A set of shaded regions that forbid elements from appearing
- add_shading(region)[source]
Return a new mesh pattern with an additional shaded region.
- Parameters:
- Return type:
- Returns:
New MeshPattern with the additional shading
- is_shading_maximal(allowed_shadings)[source]
Check if this shading is maximal among allowed shadings.
- bisc_package.bisc_algorithm(permutations, max_pattern_length)[source]
The full BiSC algorithm.
Combines the MINE and GEN algorithms to discover forbidden patterns in a set of permutations.
- Parameters:
permutations (
List[Permutation]) – Input set of permutationsmax_pattern_length (
int) – Upper bound on pattern length to search for
- Return type:
- Returns:
List of conjectured forbidden patterns
Examples
>>> # Stack-sortable permutations >>> stack_sortable = [Permutation([1]), Permutation([1,2]), ...] >>> forbidden = bisc_algorithm(stack_sortable, max_pattern_length=3) >>> # Should find pattern 231 as forbidden
- bisc_package.mine_algorithm(permutations, max_pattern_length)[source]
The MINE algorithm (Algorithm 1 from the paper).
Finds all mesh patterns that are contained in permutations in the input. This is the first step of the BiSC algorithm.
- Parameters:
permutations (
List[Permutation]) – List of input permutationsmax_pattern_length (
int) – Upper bound on pattern length to search for
- Return type:
- Returns:
Dictionary mapping classical patterns to sets of maximal shadings
Note
The output maps pattern tuples to sets of frozensets, where each frozenset represents a maximal shading for that pattern.
- bisc_package.gen_algorithm(S)[source]
The GEN algorithm (Algorithm 2 from the paper).
Generates forbidden patterns from the allowed patterns found by MINE. This is the second step of the BiSC algorithm.
- Parameters:
S (
Dict[Tuple[int,...],Set[FrozenSet]]) – Output from MINE algorithm - patterns with their allowed shadings- Return type:
- Returns:
List of forbidden mesh patterns
Note
This generates minimal forbidden shadings that are not contained in any allowed shading.
Core Modules
bisc_package.core.bisc_algorithm
Main BiSC algorithm implementation.
This module contains the core MINE and GEN algorithms, as well as the main BiSC algorithm that combines them.
- bisc_package.core.bisc_algorithm.mine_algorithm(permutations, max_pattern_length)[source]
The MINE algorithm (Algorithm 1 from the paper).
Finds all mesh patterns that are contained in permutations in the input. This is the first step of the BiSC algorithm.
- Parameters:
permutations (
List[Permutation]) – List of input permutationsmax_pattern_length (
int) – Upper bound on pattern length to search for
- Return type:
- Returns:
Dictionary mapping classical patterns to sets of maximal shadings
Note
The output maps pattern tuples to sets of frozensets, where each frozenset represents a maximal shading for that pattern.
- bisc_package.core.bisc_algorithm.gen_algorithm(S)[source]
The GEN algorithm (Algorithm 2 from the paper).
Generates forbidden patterns from the allowed patterns found by MINE. This is the second step of the BiSC algorithm.
- Parameters:
S (
Dict[Tuple[int,...],Set[FrozenSet]]) – Output from MINE algorithm - patterns with their allowed shadings- Return type:
- Returns:
List of forbidden mesh patterns
Note
This generates minimal forbidden shadings that are not contained in any allowed shading.
- bisc_package.core.bisc_algorithm.bisc_algorithm(permutations, max_pattern_length)[source]
The full BiSC algorithm.
Combines the MINE and GEN algorithms to discover forbidden patterns in a set of permutations.
- Parameters:
permutations (
List[Permutation]) – Input set of permutationsmax_pattern_length (
int) – Upper bound on pattern length to search for
- Return type:
- Returns:
List of conjectured forbidden patterns
Examples
>>> # Stack-sortable permutations >>> stack_sortable = [Permutation([1]), Permutation([1,2]), ...] >>> forbidden = bisc_algorithm(stack_sortable, max_pattern_length=3) >>> # Should find pattern 231 as forbidden
- bisc_package.core.bisc_algorithm.bisc_with_pruning(permutations, max_pattern_length)[source]
BiSC algorithm with additional pruning step.
This version includes post-processing to remove redundant patterns.
- Parameters:
permutations (
List[Permutation]) – Input set of permutationsmax_pattern_length (
int) – Upper bound on pattern length to search for
- Return type:
- Returns:
List of minimal forbidden patterns
bisc_package.core.permutation
Permutation class for the BiSC algorithm.
This module provides the core Permutation class with methods for subword enumeration and pattern analysis.
- class bisc_package.core.permutation.Permutation(sequence)[source]
Bases:
objectRepresents a permutation in one-line notation.
A permutation is a bijection from {1, 2, …, n} to itself. We represent it as a list where position i contains the value π(i).
- subwords(max_length)[source]
Generate all subwords of length at most max_length.
- Parameters:
max_length (
int) – Maximum length of subwords to generate- Returns:
subword is the subsequence of values
positions is the list of indices where these values occur
- Return type:
List of tuples (subword, positions) where
- contains_classical_pattern(pattern)[source]
Check if this permutation contains a classical pattern.
bisc_package.core.mesh_pattern
Mesh pattern class for the BiSC algorithm.
This module provides the MeshPattern class for representing classical patterns with shaded regions.
- class bisc_package.core.mesh_pattern.MeshPattern(pattern, shading=None)[source]
Bases:
objectRepresents a mesh pattern (classical pattern + shading).
A mesh pattern consists of: 1. A classical pattern (permutation) 2. A set of shaded regions that forbid elements from appearing
- add_shading(region)[source]
Return a new mesh pattern with an additional shaded region.
- Parameters:
- Return type:
- Returns:
New MeshPattern with the additional shading
- remove_shading(region)[source]
Return a new mesh pattern with a shaded region removed.
- Parameters:
- Return type:
- Returns:
New MeshPattern without the specified shading
Utility Modules
bisc_package.utils.pattern_utils
Pattern manipulation utilities for the BiSC algorithm.
- bisc_package.utils.pattern_utils.flatten(word)[source]
Flatten a word to a permutation by replacing values with their relative order.
This is a fundamental operation in pattern analysis. Given a word of distinct integers, it replaces the i-th smallest value with i.
- Parameters:
- Return type:
- Returns:
The flattened permutation
Examples
>>> flatten([3, 1, 4]) [2, 1, 3] >>> flatten([5, 2, 8, 1]) [3, 2, 4, 1]
- bisc_package.utils.pattern_utils.contains_pattern(permutation, pattern)[source]
Check if permutation contains the given classical pattern.
- Parameters:
- Return type:
- Returns:
True if the pattern is contained, False otherwise
Examples
>>> contains_pattern([3, 1, 4, 2], [2, 1]) True >>> contains_pattern([1, 2, 3], [3, 2, 1]) False
- bisc_package.utils.pattern_utils.generate_all_permutations(max_length)[source]
Generate all permutations up to a given length.
- bisc_package.utils.pattern_utils.find_missing_patterns(input_perms, max_pattern_length)[source]
Find classical patterns that are missing from the input set.
- bisc_package.utils.pattern_utils.permutation_to_string(perm)[source]
Convert a permutation to a string representation.
- bisc_package.utils.pattern_utils.string_to_permutation(s)[source]
Convert a string to a permutation.
- Parameters:
s (
str) – String representation of the permutation- Return type:
- Returns:
The permutation as a list
- Raises:
ValueError – If the string doesn’t represent a valid permutation
- bisc_package.utils.pattern_utils.reverse_permutation(perm)[source]
Return the reverse of a permutation.
bisc_package.utils.mesh_utils
Mesh pattern utilities for the BiSC algorithm.
- bisc_package.utils.mesh_utils.get_maximal_shading(pattern, subword, positions, permutation)[source]
Compute the maximal shading for a pattern occurrence.
This is a core operation in the MINE algorithm. For a given occurrence of a classical pattern in a permutation, we find the maximal set of mesh regions that can be shaded while still preserving the occurrence.
- Parameters:
- Return type:
- Returns:
Set of shaded regions (i,j) where i is row, j is column
- bisc_package.utils.mesh_utils.is_pattern_contained(small_pattern, large_pattern)[source]
Check if small_pattern is contained in large_pattern as a classical pattern.
- bisc_package.utils.mesh_utils.is_shading_consequence(forbidden_short, candidate_long)[source]
Check if the long pattern’s forbidden shading is a consequence of the short pattern.
This is used in the GEN algorithm to remove redundant forbidden patterns. A mesh pattern is redundant if any permutation containing it also contains a previously forbidden pattern.
- Parameters:
forbidden_short (
MeshPattern) – A previously identified forbidden patterncandidate_long (
MeshPattern) – A candidate forbidden pattern to check
- Return type:
- Returns:
True if candidate_long is a consequence of forbidden_short
Note
This is a simplified implementation. A complete version would require more sophisticated analysis of mesh pattern containment relationships.
- bisc_package.utils.mesh_utils.mesh_pattern_contains(permutation, mesh_pattern)[source]
Check if a permutation contains a mesh pattern.
This is a simplified implementation that checks classical pattern containment. A complete implementation would also verify mesh constraints.
- Parameters:
permutation (
Permutation) – The permutation to checkmesh_pattern (
MeshPattern) – The mesh pattern to search for
- Return type:
- Returns:
True if the mesh pattern is contained
Note
This implementation only checks classical pattern containment. Full mesh pattern checking requires geometric constraint verification.
- bisc_package.utils.mesh_utils.generate_mesh_regions(pattern_length)[source]
Generate all possible mesh regions for a pattern of given length.
- bisc_package.utils.mesh_utils.is_shading_minimal(shading, forbidden_shadings)[source]
Check if a shading is minimal among forbidden shadings.
- bisc_package.utils.mesh_utils.visualize_mesh_pattern(mesh_pattern)[source]
Create a simple text visualization of a mesh pattern.
- Parameters:
mesh_pattern (
MeshPattern) – The mesh pattern to visualize- Return type:
- Returns:
String representation of the pattern with shading
Example Modules
bisc_package.examples.known_classes.stack_sortable
Stack-sortable permutations example.
Stack-sortable permutations are those that can be sorted using a single stack. They are characterized by avoiding the pattern 231.
bisc_package.examples.known_classes.smooth_permutations
Smooth permutations example.
Smooth permutations are related to smooth Schubert varieties in algebraic geometry. They are characterized by avoiding patterns 1324 and 2143.
bisc_package.examples.known_classes.baxter_permutations
Baxter permutations example.
Baxter permutations have applications in various areas of mathematics and are characterized by avoiding certain mesh patterns.
- bisc_package.examples.known_classes.baxter_permutations.generate_baxter_approximation(max_length)[source]
Generate an approximation of Baxter permutations using classical patterns.
Note: True Baxter permutations are defined by mesh patterns (2413, {(2,2)}) and (3142, {(2,2)}), but we use classical patterns as an approximation.
- Parameters:
max_length (
int) – Maximum length of permutations to generate- Return type:
- Returns:
List of permutations avoiding classical patterns 2413 and 3142
bisc_package.examples.bisc_demo
Demonstration of the BiSC algorithm implementation.
This reproduces the core BiSC algorithm from the paper: “BiSC: An algorithm for discovering generalized permutation patterns” by Henning Ulfarsson.
Command Line Interface
bisc_package.cli
Command-line interface for the BiSC algorithm package.
This module provides console commands for running examples and demonstrations.
Testing Modules
bisc_package.tests.unit.test_permutation
Unit tests for the Permutation class.
bisc_package.tests.integration.verify_examples
Verification of examples from the BiSC paper. Tests our implementation against the specific examples mentioned in the paper.
- bisc_package.tests.integration.verify_examples.generate_all_permutations(max_length)[source]
Generate all permutations up to given length.
- bisc_package.tests.integration.verify_examples.contains_pattern(permutation, pattern)[source]
Check if permutation contains the given classical pattern.
- Return type:
- bisc_package.tests.integration.verify_examples.verify_stack_sortable()[source]
Verify Example 1: Stack-sortable permutations avoid 231.
- bisc_package.tests.integration.verify_examples.verify_west_2_stack_sortable()[source]
Verify Example 2: West-2-stack-sortable permutations.
- bisc_package.tests.integration.verify_examples.verify_difficult_example()[source]
Verify the difficult example from equation (2): 1, 21, 321, 2341, 4123, 4321.