![]() |
Sorting Network Search
Backtracking for Small Sorting Networks
|
Sorting network. More...
#include <SortingNetwork.h>
Public Member Functions | |
CSortingNetwork () | |
Constructor. More... | |
~CSortingNetwork () | |
Destructor. More... | |
![]() | |
CComparatorNetwork () | |
Constructor. More... | |
virtual | ~CComparatorNetwork () |
Destructor. More... | |
void | Save (const std::string &) |
Save to file. More... | |
Protected Member Functions | |
virtual void | Initialize () |
Initialize the sorting test. More... | |
virtual bool | Sorts () |
Does it sort? More... | |
virtual bool | StillSorts (const size_t) |
Does it still sort when a bit is changed? More... | |
size_t | FlipInput (size_t, const size_t, const size_t) |
Recompute network values when a bit is changed. More... | |
void | InitValues (const size_t, const size_t) |
Initialize the network values to the all zero input. More... | |
Protected Attributes | |
CBinaryGrayCode * | m_pGrayCode = nullptr |
Gray code generator. More... | |
size_t | m_nValue [MAXDEPTH][MAXINPUTS] = {0} |
Values at each level when sorting. More... | |
size_t | m_nZeros = 0 |
Number of zeros in the input. More... | |
![]() | |
size_t | m_nComparator [MAXDEPTH][MAXINPUTS] = {0} |
Comparator array. More... | |
Additional Inherited Members | |
![]() | |
static void | SetWidth (const size_t) |
Set width. More... | |
static void | SetDepth (const size_t) |
Set depth. More... | |
![]() | |
static size_t | m_nWidth = 9 |
Comparator network width. More... | |
static size_t | m_nDepth = 6 |
Comparator network depth. More... | |
CSortingNetwork
combines a comparator network with a binary Gray code generator CBinaryGrayCode
to test whether the comparator network sorts based on the Zero-One Principle, which says that a comparator network is a sorting network if and only if it sorts all inputs made up of only zeros and ones. See Knuth Volume 3 for the details. Using a Gray code generator instead of a standard binary string generator simplifies and speeds up the test. The main function of interest here is CSortingNetwork::Sorts()
which returns true
if the base comparator network sorts all inputs.
Definition at line 46 of file SortingNetwork.h.
CSortingNetwork::CSortingNetwork | ( | ) |
Create a binary Gray code generator.
Definition at line 30 of file SortingNetwork.cpp.
CSortingNetwork::~CSortingNetwork | ( | ) |
Delete the binary Gray code generator.
Definition at line 36 of file SortingNetwork.cpp.
|
protected |
Flip bit and propagate down the comparator network. Note that when a bit is flipped at a certain level, exactly one bit is changed at subsequent levels following a path to the outputs. An example is shown in the following figure.
On the left we see a 6-input sorting network of depth 5 on input \(1, 1, 1, 0, 0, 0\). The output is in ascending order from top to bottom. On the right we see what happens when we flip the second-from-last input from a 0 to a 1. That change follows a path from the input channel to the channel formerly carrying the last 0, shown in red.
j | Flip value in this channel. |
first | Flip value starting at this level. |
last | Propagate change down to this level. |
Definition at line 75 of file SortingNetwork.cpp.
|
protectedvirtual |
Initialize the network for the sorting test, that is, make the Gray code word for input be all zeros, and the values on every channel at every level be zero.
Reimplemented in C1NF, and CAutocomplete.
Definition at line 54 of file SortingNetwork.cpp.
|
protected |
Set the values on every channel between two levels to zero.
first | First level to set to zero. |
last | Last level to set to zero. |
Definition at line 44 of file SortingNetwork.cpp.
|
protectedvirtual |
Check whether sorting network sorts all inputs.
Reimplemented in C1NF, and CAutocomplete.
Definition at line 104 of file SortingNetwork.cpp.
|
protectedvirtual |
Check whether sorting network sorts when the input value on a given channel is flipped.
delta | Index of channel to flip. |
Reimplemented in C1NF, and CAutocomplete.
Definition at line 96 of file SortingNetwork.cpp.
Definition at line 49 of file SortingNetwork.h.
|
protected |
Definition at line 50 of file SortingNetwork.h.
|
protected |
Definition at line 48 of file SortingNetwork.h.