The present invention relates to data processing and storage systems for computers and, more particularly, to an efficient algorithm for maintaining symmetry of a bit matrix stored in computer memory.
A symmetric matrix is same as its transpose. In other words, if an element in the matrix E can be represented as E[R,C] where “R” represents the row number and “C” represents the column number, then any element in a symmetric matrix E[R,C] has the same value as its transpose element E[C,R]. The element E [C,R] can therefore be referred to as the transpose element of E[R,C]. For example, element [3,4] can be referred to as the transpose element for element [4,3]. Similarly, each row having an index (row number) has a corresponding transpose column having the same index (column number). For example, the column C=3 can be referred to as the transpose column for row R=3.
In order to keep the symmetry of a symmetric matrix intact whenever a row P is modified, it is also necessary to modify the column P in the same manner such that the values of row P remain the same as the values of column P and vice-versa. A generalized algorithm for maintaining the symmetry of a symmetric bit matrix uses the following concept: for each element of a row changed (X[R,C]), also change the transpose element (X[C,R]) so that each element and its transpose remain the same. An algorithmic procedure for maintaining the symmetry of a symmetric bit matrix may be expressed as:
For each element X[R,C] in row “R” being modified;
Set X[C, R]=X[R, C]; where “N” is the total number of columns in the matrix and “C” varies from 0 to N-1 or from 1 to N.
A bit matrix is a matrix whose individual elements are single bits that hold a value of either a zero or a one. In case of bit matrices stored in computer memory, however, it is not efficient to access each individual element, as the individual bits are not directly represented using basic data types such as 1 byte, 2 byte and 4 byte words. Hence X[C,R]=X[R,C] cannot be implemented directly on a bit-by-bit basis in the case of a bit matrix stored in computer memory. In addition, the elements of a row of a matrix stored in computer memory are typically stored sequentially in memory, which means that the elements of a column are not stored sequentially. This makes it inefficient to access the individual bits in a column.
Moreover, the generalized method for maintaining matrix symmetry (i.e., set X[C,R]=X[R,C] as “C” varies from 0 to N-1) is inefficient because it requires processing all the elements of the transpose column irrespective of the number of elements to be changed in the column. No existing algorithms are capable of ensuring the symmetry of a bit-matrix when a row of the matrix is modified without processing all of the elements of the transpose column of the modified row. There is, therefore, a continuing need for more efficient algorithms for maintaining the symmetry of a symmetric bit matrix stored in computer memory. More particularly, there is a need for an algorithm that maintains the symmetry of a bit matrix stored in computer memory without having to process all of the elements of a transpose column for a modified row by considering only the elements changed in that row.
The needs described above are met in a computer implemented bit-matrix symmetry algorithm that maintains the symmetry of a symmetric bit matrix stored in computer memory without having to process all of the elements of a transpose column for a modified row by considering only the elements changed in that row. The algorithm achieves this improvement by operating on groups of bits (e.g., byte, word, dword) forming rows of the matrix rather than processing the individual bit elements of the matrix one at a time. When modifying the bits of the transpose column, instead of checking whether each bit needs to be set or cleared, the algorithm directly toggles only those column bits that are the transpose elements of modified row elements, thereby taking advantage of the existing symmetry to eliminate unnecessary conditional operations. As a result, the algorithm modifies the matrix on a row-by-row basis and makes changes to only those column bits that correspond to modified row elements without having to check the value of any column elements that do not require modification.
More specifically, the algorithm is configured to operate on a bit matrix that has a symmetric initial state and maintain the symmetry of the matrix whenever a row of the matrix is modified. The algorithm operates by creating a bitmap of changed elements for the modified row “R” indicating which elements of the row have been modified. The bitmap of changed row elements will have bits corresponding to the changed row elements as set and bits corresponding to unchanged row elements will be zero. In this bitmap, a bit having index “M” will correspond to an element A[R,M] in row R and where M can vary from 0 to N-1 or 1 to N in an N×N matrix. As the elements of a column are not stored sequentially, it is inefficient to access the individual bits in a column for maintaining symmetry. So for a changed row “R”, a “transpose column bit toggling mask” is prepared, which has a single bit set corresponding to the transpose column of the modified row “R”. This mask is used on all the rows for which column “R” bit needs to be modified for symmetry with row “R”. Then, for each bit set in the bitmap of changed row elements, the algorithm gets the index “M” of a bit set in the bitmap of changed row elements, then in row “M”, toggles the column “R” bit using ‘transpose column bit toggling mask’, and clears that bit in the changed row elements bitmap to indicate that the row element modification has been processed for the current iteration. The algorithm repeats this process until the value of the bitmap of changed elements is equal to zero indicating that all of the modified row elements have been processed by toggling the corresponding transpose element in column “R”. Once all of the transpose modifications have been entered in column “R”, the original row “R” content is overwritten with the modified row “R” content, making the matrix “A” symmetric. The row “R” should be overwritten at the end instead of before toggling the column elements, to prevent double toggling of the Rth bit of row R i.e. A[R,R].
In a specific embodiment of the invention, the bitmap of changed elements for a modified row “R” may be determined by:
Bitmap of Changed Elements=OldRowContents XOR NewRowContents.
The transpose column bit toggling mask “B” may be determined by:
Transpose Column Bit Toggling Mask B=In a single row matrix setting all the elements to 0 except the transpose column bit (R) to 1. This can be achieved by shifting operation.
And the transpose of a column element for any modified Mth bit of changed row R is achieved by bit toggle operation:
Row M (modified)=Row M (prior) XOR Mask B.
The row contents is overwritten at the end of the process, after the transpose column elements have been modified, so that the element[R,R] of the row being processed does not get toggled twice to hold an incorrect value. This algorithm ensures that only elements that are different from the original value in the modified row are processed in the transpose column. As a result, the algorithm offers linearly improving efficiency when complete or part of the matrix is modified row by row with symmetrical input. Because the transpose elements are toggled only for the row elements that have been modified, the time consumed by the algorithm is less if a row is only partially changed. In other words, the performance of the algorithm is adaptive to the number of bits changed in a row. It should be noted that the algorithm can be implemented in software as well as hardware.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not necessarily restrictive of the invention as claimed. The accompanying drawings, which are incorporated in and constitute a part of the specification, illustrate embodiments of the invention and together with the general description, serve to explain the principles of the invention.
The numerous advantages of the invention may be better understood with reference to the accompanying figures in which:
The invention may be embodied in a computer implemented bit-matrix symmetry algorithm that maintains the symmetry of a symmetric bit matrix stored in computer memory without having to process all of the elements of a transpose column for a modified row by considering only the changed elements in the row.
In order to keep the symmetry of the matrix intact, whenever a row P is modified in the symmetric matrix, it is also necessary to modify the column P in the matrix such that row P remains the same as column P and vice versa. This is illustrated in
A bit matrix is a matrix whose individual elements are single bits which can hold either a zero or a one.
A generalized algorithm to keep a matrix X symmetric uses the following concept: for each element X[R,C] in row R being modified, set X[C, R] equal to X[R, C], where N is the total number of columns in the matrix and C varies from (0 to N-1) or (1 to N). In the case of bit matrices stored in computer memory, however, it is not efficient to access each element when each element is an individual bit, because individual bits are not directly represented using basic data types such as 1 byte, 2 byte and 4 byte words. Hence X[C, R]=X[R, C] cannot be used efficiently in the case of a bit matrix. In addition, the elements of a row are sequentially stored in memory whereas the elements of a column are not stored sequentially. This makes it inefficient to access the individual bits in a column. In addition, the generalized method for maintaining matrix symmetry is inefficient because it processes all the elements in the transpose column of a modified row irrespective of the number of elements changed in the row. No existing algorithms are available to ensure the symmetry of a bit-matrix when a row is modified without processing each element of the transpose column of a modified row.
The present invention provides an algorithm developed to improve the efficiency of maintaining the symmetry of a bit matrix stored in computer memory. The algorithm processes groups of bits (byte, word, dword) forming a row or part of a row of the matrix instead of processing bits forming the matrix elements individually. The algorithm also checks bits which have been modified in the matrix row and modifies only those bits in the transpose column that require modification. No processing is required for the elements of a transpose column that are not modified. As a result, when modifying the bits of the transpose column, instead of checking whether each bit needs to be set or cleared; the algorithm directly toggles only those column bits corresponding to row elements that have been modified, taking advantage of the existing symmetry to save conditional operations. The invention therefore provides an efficient algorithm for entering the modifications to the transpose column elements of a bit matrix without having to process column elements that do not require modification. The invention also allows the transpose column elements to be modified through row operations without having to locate the individual column elements for direct manipulation.
Bitmap of Changed Elements=OldRowContents XOR NewRowContents
The result is a bitmap of changed elements 52 with values [1, 1, 0, 0, 0, 0, 0, 0], as shown in
Row M XOR Toggling Mask B
As the position M of the left-most bit cleared in the bitmap 52 was “7”, so the adjustment to the transpose column C=3 is computed as:
Row 7 (item 58) XOR Toggling Mask B (item 54)=Adjusted Row 7 ((item 58′).
As shown in
The procedure described above is then repeated until all of the bits in the bitmap of changed bits 52 have been cleared (i.e., until all of the changed bits in the modified row have had their corresponding transpose column elements toggled). For the specific example shown in
If, on the other hand, the contents of the “Bitmap of Changed Bits” is not equal to zero, the “No” branch is followed from step 76 to step 80, in which the left-most bit set M in the “Bitmap of Changed Bits” is cleared. Step 80 is followed by step 82, in which transpose bit corresponding to the cleared bit is toggled through the operation “Row M” XOR “Toggling Bit Mask.” After step 82, routine 70 loops back to step to 76 to determine whether the final required transpose element modification has been entered. If the last transpose element modification has not been entered, the steps 80 and 82 are performed for the next transpose bit requiring modification (i.e., for the next left-most bit that is set in the “Bitmap of Changed Bits”), and the process repeats until all of the bits modified in row R have had their corresponding transpose elements toggled.
The above procedure assumes matrix row and column index starting from zero and for a matrix stored in computer memory the format of data is assumed to be little-endian. The algorithm will also be applicable for matrix row and column index starting from 1 by adjusting the number of shift operations such that the desired column bit is set. For matrix stored in computer memory in big-endian format the shift operation to prepare the transpose column bit toggling mask will involve right shift on single row matrix with only MSB bit set.
The above description uses transpose column bit toggling mask of size equal to the row width but for better efficiency in large matrices the size of this mask can be less than the size of the row. To make the transpose column bit toggling operation more efficient, a subset of transpose column bit toggling mask (such that it contains the desired transpose column bit) can be used to operate on corresponding subset in a row, so as to avoid the unnecessary XOR operation on remaining bits in that row. This would lead to greater efficiency in case of larger bit matrices in which a single row is stored using multiple bytes or dwords and the mask can be a single byte or single dword operating on a single byte or single dword at desired offset in the row.