Efficiently Calculating the of a

Felix Limanta 13515065 Program Studi Teknik Informatika Sekolah Teknik Elektro dan Informatika Institut Teknologi Bandung, Jl. Ganesha 10 Bandung 40132, Indonesia [email protected] [email protected]

Abstract—There are three commonly-used algorithms to infotype is either real or integer, depending on calculate the determinant of a matrix: Laplace expansion, LU implementation, while matrix is an arbitrarily-sized decomposition, and the Bareiss algorithm. In this paper, we array of array of infotype. first discuss the underlying mathematical principles behind the algorithms. We then realize the algorithms in pseudocode Finally, we analyze the complexity and nature of the algorithms and compare them after one another. II. BASIC MATHEMATICAL CONCEPTS

Keywords—asymptotic time complexity, Bareiss algorithm, A. Determinant determinant, Laplace expansion, LU decomposition. The determinant is a real number associated with every . The determinant of a square matrix A is commonly denoted as det A, det(A), or |A|. I. INTRODUCTION Singular matrices are matrices which determinant is 0. In this paper, we describe three algorithms to find the Unimodular matrices are matrices which determinant is 1. determinant of a square matrix: Laplace expansion (also The determinant of a 1×1 matrix is the element itself. known as determinant expansion by minors), LU 퐴 = [푎] ⇒ det(퐴) = 푎 decomposition, and the Bareiss algorithm. The determinant of a 2×2 matrix is defined as the product The paper is organized as follows. Section II reviews the of the elements on the main diagonal minus the product of basic mathematical concepts for this paper, which includes the elements off the main diagonal. 푎 푏 , elementary matrix operations, algorithm | | = 푎푑 − 푏푐 푐 푑 complexities, time complexities, and asymptotic time Finding the determinant of larger matrices will be complexities. In Section III, the three methods are discussed in later sections. discussed in-depth mathematically, whereas in Section IV, Some basic properties of determinants are the three methods, as well as subroutines necessary to 1. det(퐼) = 1 implement them, are implemented in a language-agnostic 2. det(퐴푇) = det (퐴) pseudocode. In Section V, the methods implemented in 1 3. det(퐴−1) = Section V are analyzed regarding its asymptotic time det (퐴) complexity and other features before being compared to 4. For square matrices A and B of equal size, one another. Finally, Section VI concludes the paper while det(퐴퐵) = det(퐴) × det(퐵) suggesting further improvements. 5. The determinant of a is the product of the diagonal entries (pivots) d1, d2, …, dn. Terminology and Notation 푛 Unless noted otherwise, all mentions of matrices in this det(퐴) = 푎1,1푎2,2 … 푎푛,푛 = ∏ 푎푖,푖 paper are assumed to be square matrices. 푖=1 Determinants are useful for other mathematical subjects. To describe iterations mathematically, the notation 푖 ∈ For example, following Cramer’s rule, a matrix can be used [푎. . 푏] is introduced. The notation is taken from the to represent the coefficients of a system of linear equations, mathematical concept of number intervals, but instead of and the determinant can be used to solve the system of describing whether i is between a and b or not, it denotes linear equations if and only if the matrix is nonsingular. that the iterator i starts from a and ends in b, incrementing Determinants are also used in determining the inverse of a i by 1 after each iteration. It is equivalent to the for syntax matrix, where any matrix has a unique inverse if and only in programming languages, in which 푖 ∈ [푎. . 푏] is if the matrix is nonsingular. equivalent to for i = a to b. The pseudocode described in section III follows the B. Elementary Matrix Operations syntax detailed in reference [5]. For convenience, two data types are defined here: Elementary matrix operations play important roles in

Makalah IF2120 Matematika Diskrit – Sem. I Tahun 2016/2017 various matrix algebra applications, such as solving a D. Time Complexity system of linear equations using and finding the inverse of The time complexity of an algorithm is measured from a matrix. the number of instructions executed. The time complexity There are three kinds of elementary matrix operations: of an algorithm is not measured from the running time of 1. Switch two rows (or columns). said algorithm because of the differences between every 2. Multiply each element in a row (or column) by a computer’s architecture, every programming language and non-zero number. its compiler, and so on. Because of that, the same algorithm 3. Add or subtract a row (or column) by multiples of implemented in different languages or the same executable another row (or column). run in different machines would yield different running When these operations are performed on rows, they are times. called elementary row operations and, likewise, when they In calculating the time complexity of an algorithm, are performed on columns, they are called elementary ideally, all operations are accounted for. For decently column operations. complex algorithms, this is all but impossible, so Elementary matrix operations have the following effect practically, only basic operations are accounted for. For on determinants. example, the basic operation of a searching algorithm 1. Exchanging two rows or columns from a matrix would be the comparison of the searched element with reverse the sign of its determinant from positive to elements in the array. By counting how many comparisons negative and vice-versa. are made, we can obtain the relative efficiency of the 푎 푏 푐 푑 | | = − | | algorithm. 푐 푑 푎 푏 The time complexity of an algorithm can be divided into 2. Multiplying one row or column of a matrix by t three cases: multiplies the determinant by t. 푡푎 푡푏 푡푎 푏 푎 푏  Tmin(푛): Best case | | = | | = 푡 | | 푐 푑 푡푐 푑 푐 푑  Tmax(푛): Worst case 3. If two rows or two columns of a matrix are equal, or  Tavg(푛): Average case a row or column is the multiply of another row or column, its determinant is zero. Example 1. Find the best case, worst case, and average 푎 푏 | | = 0 case time complexity of the following sequential search 푡푎 푡푏 algorithm. 4. If 푖 ≠ 푗, adding or subtracting t times row i from row j or vice-versa does not change the determinant. function SeqSearch (a: array of infotype, 푎 푏 푎 푏 | | = | | n: integer, x: infotype) → integer 푐 − 푡푎 푑 − 푡푏 푐 푑 DICTIONARY 5. If A has a row that is all zeros, then det(퐴) = 0. i: integer 푎 푏 found: boolean | | = 0 0 0 ALGORITHM i ← 1 C. Algorithm Complexity found ← false while ((i ≤ n) and not(found)) do Algorithms are defined as the series of steps necessary found ← (a[i] = x) to solve a problem systematically. An algorithm, first and if not(found) then foremost, is not only required to be effective and actually i ← i + 1 solve the problem, but also expected to be efficient. An if (found) then → i algorithm might be able to effectively solve a problem, but else if the algorithm demands an unreasonable amount of → 0 processing time or takes up disproportionately large amount of memory, the algorithm is next to useless.  Best case: 푎1 = 푥 ⇒ Tmin(푛) = 1 In application, every algorithm uses up two resources  Worst case: 푎푛 = 푛 or 푛 not found ⇒ Tmax(푛) = 푛 which can be used as measurement: the number of steps 1+2+⋯+푛 푛+1  Average case: T (푛) = = taken (time) and the amount of memory reserved to execute avg 푛 2 those steps (space). The number of steps taken is defined as the time complexity, denoted as T(n), whereas the E. Asymptotic Time Complexity amount of memory reserved is defined as the space Usually, the exact time complexity of an algorithm is complexity, denoted as S(n). unnecessary. Instead, what is needed is a rough estimate of Matrices are a type of data structure which takes up a the time required by the algorithm and how fast that time relatively large amount of memory, compared to more requirement grow with the size of the input. This is because compact data structures such as linked lists. Therefore, the the efficiency of an algorithm would only be apparent at a space complexity of the algorithms discussed later will not large number of inputs. Running the determinant- be examined in favor of focusing on their time complexity. calculating algorithms on a 3×3 matrix would not yield any notable differences between the algorithm running

Makalah IF2120 Matematika Diskrit – Sem. I Tahun 2016/2017 times. However, if the algorithms are run on a large matrix More informally, expansion by minors can be written as (say, 5000×5000), differences between running times 푎11 푎12 푎13 ⋯ 푎1푛 푎 푎 푎 ⋯ 푎 would be much more apparent. | 21 22 23 2푛| The asymptotic time complexity is a rough estimate of ⋮ ⋮ ⋮ ⋱ ⋮ the time complexity as the number of inputs approaches 푎푛1 푎푛2 푎푛3 ⋯ 푎푛푛 푎 푎 ⋯ 푎 infinite. The asymptotic time complexity is commonly 22 23 2푛 = 푎 | ⋮ ⋮ ⋱ ⋮ | denoted with the Big-O notation, though other notations 11 푎푛1 푎푛2 ⋯ 푎푛푛 exist. Generally, the asymptotic time complexity of an 푎21 푎23 ⋯ 푎2푛 algorithm can be taken from the most significant term in − 푎12 | ⋮ ⋮ ⋱ ⋮ | + ⋯ the time complexity function. For example, 푎푛2 푎푛2 ⋯ 푎푛푛 5 푛 푛 T(푛) = 192푛 + 2 + log 푛! = O(2 ) 푎21 푎22 ⋯ 푎2(푛−1) 푛 because for non-negative integer values of 푛, 2 ± 푎1푛 | ⋮ ⋮ ⋱ ⋮ | contributes the most to the function T(푛). 푎푛1 푎푛2 ⋯ 푎푛(푛−1) Table I shows categories of algorithm based on its Big- O notation. Finding the determinant of a 3×3 matrix using expansion by minors can be illustrated with the following Table I. Algorithm categories based on asymptotic time complexity, ordered from smallest to largest example.

Complexity Category Example 2. Find det(A) using expansion by minors, where O(1) constant 1 3 5 O(log 푛) logarithmic 퐴 = [0 5 1]. O(푛) linear 6 5 0 O(푛 log 푛) 푛 log 푛 O(푛2) quadratic Following the above definition, the matrix A can be O(푛3) cubic expanded to matrices: 5 1 0 1 0 5 O(2푛) exponential det (퐴) = 1 | | − 3 | | + 5 | | 5 0 6 0 6 5 factorial O(푛!) = 1×(−5) − 3×(−6) + 5×(−30)

3 = −137 The top six (O(1) to O(푛 )) are called polynomial Determinant expansion by minors is the simplest method 푛 algorithms, while O(2 ) and O(푛!) are called exponential among the three and is commonly taught in class. A algorithms. Exponential algorithms grow much faster than derivation of determinant expansion by minors exclusive polynomial algorithms. For example, if 푛 = 100, then to 3×3 matrices, called the Sarrus method, is available, 3 6 푛 30 푛 = 10 , whereas 2 = 1.268×10 and 푛! = 9.333× although the method cannot be used for larger matrices. 157 10 . B. LU Decomposition

LU decomposition is a procedure for decomposing a III. MATHEMATICAL CONCEPTS OF DETERMINANT 푛×푛 matrix into a product of a lower triangular matrix L FINDING ALGORITHMS and an upper triangular matrix U. A. Determinant Expansion by Minors 퐿푈 = 퐴 Also known as the Laplace Expansion, expansion by For a 3×3 matrix, the LU decomposition is as the minors is a technique to find the determinant of a given following. 푢 푢 푢 square matrix. Although sufficiently efficient for small 푙11 0 0 11 12 13 푎11 푎12 푎13 0 푢 푢 푎 푎 푎 matrices, other techniques detailed later are much more [푙21 푙32 0 ] [ 22 23] = [ 21 22 23] 0 0 푢 푎 푎 푎 efficient for very large matrices. 푙31 푙32 푙33 33 31 32 33 Formally, expansion by minors is defined by Intuitively, LU decomposition can be performed by 푛 applying Gauss elimination, where matrix U is the 푖+푗 reduction of matrix A to an upper triangular matrix and det(퐴) = ∑(−1) 푎푖푗푀푖푗 푖=1 matrix L is the multipliers used in each elementary row where 푀푖푗 is the i, j minor matrix of A: the determinant of operation. the (푛 − 1)×(푛 − 1) matrix obtained by deleting the i-th More rigidly, let A be a 푛×푛 matrix. First, copy matrix row and the j-th column of A. A to matrix U. Iterations denoted by i and j process every element of the matrix, where 푖 = [1. . 푛] and 푗 = [(푖 + 1). . 푛], 푢푗푖 푙푖푗 = 푢푖푖 and Figure 1. Obtaining minor matrices from a larger main matrix 푢푗푘 = 푢푗푘 − 푙푖푗×푢푖푘

Makalah IF2120 Matematika Diskrit – Sem. I Tahun 2016/2017 where 푘 ∈ [1. . 푛]. For calculating determinants, properties 4 and 5 Example 4. Find det(A) using the Bareiss algorithm, where discussed in Section II-1 are used, where det(퐴) = 1 3 5 det(퐿) × det(푈) and the determinants of L and U are the 퐴 = [0 5 1]. product of their diagonals. 6 5 0

Example 3. Find det(A) using LU decomposition, where Following the algorithm denoted above, we begin by st 1 3 5 defining 푎00 = 1. For the 1 iteration (푘 = 1), we alter 퐴 = [0 5 1]. 푎22, 푎23, 푎32, and 푎33. For 푖 = 2 and 푗 = 2, we have 1×5 − 3×0 6 5 0 푎(2} = = 5. 22 1 Using Gauss elimination, matrices L and U can be found Continuing the process for the remaining elements gives 1 0 0 1 3 5 us to be 퐿 = [0 1 0] and 푈 = [0 5 1 ]. 1 3 5 13 137 (2) 6 − 1 0 0 − 퐴 = [0 5 1 ]. 5 5 0 −13 −30 To check if L and U are correct, simply multiply L with U For 푘 = 2, only 푎 is changed: and check if the product matrix is equal to A. 33 (3} 5×(−30) − 1×(−13) After obtaining L and U, find their determinants by 푎 = = −137, 33 1 multiplying their diagonals: giving det(퐿) = 1×1×1 = 1 1 3 5 137 (3) det(푈) = 1×5× (− ) = −137 퐴 = [0 5 1 ]. 5 0 −13 −137 Then, calculate the determinant of A by multiplying the (3) Thus, det(퐴) = 푎33 = −137. determinants of L and U: det(퐴) = det(퐿) × det(푈) = 1×(−137) = −137 As with LU decomposition, by zero is possible without a proper ordering. Before inputting the matrix into Without a proper ordering, the decomposition may fail the Bareiss algorithm, swap rows whose pivots are 0, then as a division by 0 occurs somewhere in the decomposition multiply it by -1. process and mistakenly imply that matrix A is singular. To rectify this, if pivot 푎푖푖 = 0 for 푖 ∈ [1. . 푛], simply swap row i with another row j where 푎푗푖 ≠ 0, then multiply the IV. IMPLEMENTING DETERMINANT FINDING entire row by -1 to preserve the determinant value. If this ALGORITHMS IN PROGRAMS is impossible (a column is composed entirely of 0s), then the matrix is singular and det(퐴) = 0. Note that the snippets in this section assumes that the In practice, because the determinant of L is always 1, indices of the matrix start from 1. If the snippets are det(퐴) = det (푈). Because of this, in algorithms to implemented in languages whose indices start from 0 (such calculate determinants using LU decomposition (including as C), simply decrease iterators by 1. the one in this paper), the matrix L is often not computed alongside matrix U and the multipliers used in each A. Forming Minor Matrices elementary row operation is discarded after every iteration. The following pseudocode snippet is used to form the minor matrix of a given larger matrix. The function C. Bareiss Algorithm receives the main matrix and integers i and j, which are the The Bareiss algorithm, named after Erwin Bareiss, is an respectively, the rows and columns to cross out, and returns algorithm to calculate the determinant or the echelon form the minor matrix formed. of an integer matrix using only integer arithmetic; that is, any divisions performed are exact (there is no remainder). The method is also used to compute the determinant of a real number matrix and avoids the introduction of rounding errors not present in the input matrix. Let A be a 푛×푛 matrix. For every iteration denoted by 푘 ∈ [1. . (푛 − 1)], every element in the matrix is processed with the following formula: (푘−1) (푘−1) 1 푎푘푘 푎 푎(푘+1) = | 푘푗 | 푖푗 푎 (푘−1) (푘−1) (푘−1)(푘−1) 푎푖푘 푎푖푗 where 푎00 = 1, 푖 ∈ [(푘 + 1). . 푛], and 푗 ∈ [(푘 + 1). . 푛]. (푛) The determinant is element 푎푛푛 ; that is, the element in the lower right corner after the last iteration is completed.

Makalah IF2120 Matematika Diskrit – Sem. I Tahun 2016/2017 function MinorMatrix (M: matrix, input i, check if any of the matrix’ pivots are 0. If there are, it then j: integer) → matrix searches the column where the offending pivot is for a non- DICTIONARY minor: matrix zero value. If found, the code then swaps the row where the n, a, b, c, d: integer non-zero value is found with the offending row. Otherwise, ALGORITHM if all values of a given column is zero, the matrix is singular n ← NRow(M) and the code terminates. c ← 1 i traversal [1..n] { Traverse rows } C. Laplace Expansion if (a ≠ i) then { Skip row i } d ← 1 The following pseudocode snippet is used to calculate j traversal [1..n] { Traverse columns } the determinant of the inputted matrix through Laplace if (b ≠ j) then { Skip column j } expansion recursively. The function receives the matrix minor[c][d] ← M[a][b] whose determinant shall be calculated and an integer n, d ← d + 1 which represents the number of rows or columns in the c ← c + 1 → minor matrix, and then returns the determinant of the matrix.

function DetLaplace (M: matrix, n: integer) The code snippet above traverses the main matrix and → infotype copies its values, but skips row i and column j. It first sets DICTIONARY the minor row iterator to 1, then traverses the row of the det: infotype main matrix. If the loop is not on row i, it resets the minor minor: matrix column iterator to 1, then traverses the column of the main i, cofactor: integer ALGORITHM matrix. If the loop is not on column j, it copies the value if (n = 1) then { Basis, single element matrix } from the main matrix to the minor matrix, then increment det ← M[1][1] the minor column iterator. After traversing the column, the else { Recurrence } minor row iterator is incremented. The resultant minor det ← 0 { Initialize determinant value } matrix is then returned. cofactor ← -1 { Initialize cofactor } i traversal [1..n] { Traverse rows } B. Swapping Rows Whose Pivots are 0 cofactor ← -cofactor { Alternate cofactor } minor ← MinorMatrix(M,1,i) Before implementing LU decomposition and/or Bareiss { Add determinant of current submatrix } algorithm in programs, rows whose pivots are 0 must first det ← det + cofactor * M[1,i] * be swapped in order to avoid a divide by 0 problem. DetLaplace(minor,n-1) The following pseudocode snippet describes the → det algorithm to achieve this. The procedure receives matrix M as input, and returns the changed matrix as well as well as The basis of the recursive function is that if the matrix a Boolean value denoting whether the matrix is singular or only has a single element, that element is returned as the not. determinant. In the recurrence of the function, the determinant is first procedure SwapRowsWith0Pivot (input/output initialized as 0 and the cofactor as -1. It then traverses the M: matrix, output singular: boolean) column of the matrix by first negating the cofactor, forming DICTIONARY the minor matrix, and finally adding the current value of n, i, j, k: integer the determinant with the product of cofactor, the iterated temp: infotype ALGORITHM element, and the value returned by the Laplace expansion n ← NRow(M) of the minor matrix. The resultant determinant of the main singular ← false matrix is then returned. i ← 1 { Search for 0 pivots } D. LU Decomposition while ((i ≤ n) and not(singular)) do if (M[i][i] = 0) then The following pseudocode snippet is used to calculate j ← 1 the determinant of the inputted matrix through LU { Search for swappable rows } decomposition iteratively. The function receives the matrix while ((j < n) and (M[j][i] = 0)) do whose determinant shall be calculated and then returns the j ← j + 1 determinant of the matrix. if (M[j][i] ≠ 0) then { Swap rows } k traversal [1..n] The code snippet below differs from the mathematical temp ← M[i][k] concept previously described in section II. In the concept, M[i][k] ← M[j][k] the ratios are stored in the matrix L, whereas in the code M[j][k] ← -temp below, the ratio is single-use; it is only used for that else particular iteration. This is because, in practice, the singular ← true determinant of the matrix L formed through the LU

decomposition method described in the concept is almost The code snippet above traverses the main diagonal to

Makalah IF2120 Matematika Diskrit – Sem. I Tahun 2016/2017 always 1, which makes det(퐴) = det (푈) and renders calculating L unnecessary. function DetBareiss (M: matrix) → infotype DICTIONARY function DetLU (M: matrix) → real pivot: infotype DICTIONARY n, i, j, k: integer det, ratio: real singular: boolean n, i, j, k: integer ALGORITHM singular: boolean SwapRowsWith0Pivot(M,singular) ALGORITHM if (singular) then { Singular matrix } SwapRowsWith0Pivot(M,singular) det ← 0 if (singular) then { Singular matrix } else { Nonsingular matrix } det ← 0 n ← NRow(M) else { Nonsingular matrix } pivot ← 1 n ← NRow(M) k traversal [1..(n-1)] { Traverse pivots } i traversal [1..n] { Traverse columns } i traversal [(k+1)..n] { Traverse rows } j traversal [(i+1)..n] { Traverse rows } j traversal [(k+1)..n] {Traverse columns} ratio ← M[j][i]/M[i][i] { Apply formula } M[i][j] ← M[k][k] * M[i][j] – k traversal [1..n] { Subtract elements } M[i][k] * M[k][j] M[j][k] ← M[j][k] - ratio * M[i][j] ← M[i][j]/pivot M[i][k] det ← 1 pivot ← M[k][k] { Set next pivot } i traversal [1..n] { Multiply pivots } det ← M[n][n] { Assign determinant } det ← det * M[i][i] → det → det Before going to the actual part of LU decomposition, the The code snippet above can be divided to two parts: code first calls SwapRowsWith0Pivot to transform the transforming the matrix to an upper triangular form and matrix so that there are no zeros in the pivot. If the function calculating the determinant itself. fails, the matrix is singular and its determinant is zero. Before going to the actual part of LU decomposition, the Otherwise, the code continues. code first calls SwapRowsWith0Pivot to transform the First, pivot is initialized to 1. In accordance to the matrix so that there are no zeros in the pivot. If the function Bareiss algorithm described in the concept, the code fails, the matrix is singular and its determinant is zero. traverses the main diagonal of the matrix, then transforms Otherwise, the code continues. every element to the bottom right of the traversed element The code then traverses the elements below the main through the formula previously described. pivot is then diagonal. It first calculates the ratio needed to gradually updated to the traversed diagonal element. The determinant zero out elements in the bottom, then traverses the column is the lower-rightmost element of the matrix after the while subtracting the traversed elements. At the end of the algorithm. iterations, the matrix would be in an upper triangular form. In a triangular form, the determinant is easily calculated as the product of the matrix’ pivot. V. ANALYSIS AND COMPARISON

A. Asymptotic Time Complexity Analysis E. Bareiss Algorithm The following analyses assume that all basic arithmetic The following pseudocode snippet is used to calculate operations, assignment, calls, etc. all run in O(1). All time the determinant of the inputted matrix through the Bareiss complexities calculated are worst-case scenarios. algorithm iteratively. The function receives the matrix whose determinant shall be calculated and then returns the For the minor-matrices-forming algorithm, the determinant of the matrix. asymptotic time complexity is as follows. The code snippet below differs from the mathematical  Outer loop (row traversal): O(푛) concept previously described in section II. In the concept, the elements are divided with the diagonal element of the  Inner loop (column traversal): O(푛) previous iteration, whereas in the code below, the variable Therefore, the asymptotic time complexity of the algorithm ( ) 2 pivot is introduced and used as the divisor. The value of is O 푛×푛 = O(푛 ). pivot starts at 1 and is continually updated in each For the row-swapping algorithm, the asymptotic time iteration as the diagonal element of the previous iteration. complexity is as follows. This is done because in most programming languages,  Outer loop (search for 0 pivots): O(푛) adding M[0][0] is unfeasible. If indices start with 1, st defining M[0][0] would take a considerable amount of  1 inner loop (search for swappable row): O(푛) nd space since, in effect, an entire row and column must be  2 inner loop (swap rows): O(푛) added. On the other hand, if indices start with 0, the value Therefore, the asymptotic time complexity of the algorithm 2 of M[0][0] is occupied already. is O(푛×max (푛, 푛)) = O(푛 ).

Makalah IF2120 Matematika Diskrit – Sem. I Tahun 2016/2017

For the Laplace expansion algorithm, the asymptotic in a real number matrix, the division will not significantly time complexity is as follows. change the length of the mantissa.  Loop (row traversal): O(푛) In conclusion, for arbitrarily large matrices, the Bareiss  Minor matrix forming: O(푛2) expansion is slightly superior to the LU decomposition and  Recursion: O((푛 − 1)!) both the Bareiss algorithm and LU decomposition are Therefore, the asymptotic time complexity of the algorithm vastly superior to Laplace expansion. is O(푛×max (푛2, (푛 − 1)!)) = O(푛!).

For LU decomposition, the asymptotic time complexity VI. CONCLUSION is as follows. For arbitrarily large matrices, the Bareiss expansion is 2  Row swapping: O(푛 ) slightly superior to the LU decomposition and both the  1st loop i (column traversal): O(푛) Bareiss algorithm and LU decomposition are vastly  Loop j (row traversal): O(푛) superior to Laplace expansion. The Laplace expansion is  Loop k (subtracting elements): O(푛) superior if and only if the size of the matrix does not exceed  2nd loop i (multiplying pivots): O(푛) 5×5—even then, the difference is only notable if the Therefore, the asymptotic time complexity of the algorithm algorithm is applied on multiple matrices. is O(max(푛2, 푛×푛×푛, 푛) = O(푛3). Besides the algorithms elaborated in this paper, there are also algorithms to find determinants using fast matrix For the Bareiss algorithm, the asymptotic time multiplication (Strassen algorithm with O(푛2.807) and complexity is as follows. Coppersmith–Winograd algorithm with O(푛2.376)), which  Row swapping: O(푛2) are theoretically better but use heavy mathematical  Loop i (pivot traversal): O(푛) calculations, thus rendering it impractical in most cases.  Loop j (row traversal): O(푛) LU decomposition and the Bareiss algorithm, although 3  Loop k (column): O(푛) theoretically worse with O(푛 ) complexity, does not use Therefore, the asymptotic time complexity of the algorithm any mathematical calculations more complex than is O(max(푛2, 푛×푛×푛) = O(푛3). division, making it more practical for all but the most sophisticated machines to use. B. Comparison Besides real and integer matrices, the aforementioned algorithms can also be expanded to cover complex number Because the Laplace expansion runs in O(푛!), Laplace or even quaternion matrices, with the resulting determinant expansion is only faster than the other two algorithms if the being a complex number or quaternion itself. matrix is at most 5×5—at which the difference is too minute to have any significant impact to running time unless the algorithm is performed consecutively on VII. ACKNOWLEDGMENT multiple matrices. For even moderately sized matrices, it is significantly outperformed by both the LU decomposition We thank God for His Blessing that enables this paper and the Bareiss algorithm. to be completed on schedule. We also thank Dr. Ir. Rinaldi At a glance, LU decomposition and the Bareiss Munir, M.T. and Dra. Harlili, M.Sc. as the lecturer of algorithm performs the same—their asymptotic time IF2121 Discrete Mathematics. We then thank our parent complexities are both O(푛3). Performance-wise, they are for granting us both material and moral support and the same, but LU decomposition has a weakness that makes guidance in the making of this paper. Finally, we thank our the Bareiss algorithm better than LU decomposition. classmates for helping us in class, giving us ideas for this LU decomposition has a weakness in that the ratio paper, and giving us moral support. needed to transform the matrix to an upper triangular form is not necessarily an integer. Meaning, if the determinant of an integer matrix is to be found using LU REFERENCES decomposition, the resultant upper triangular matrix will [1] Aho, A.V., J.E. Hopcroft, and J.D. Ullman, The Design and Analysis most likely be converted to a real number matrix. of Computer Algorithms. Boston, MA: Addison-Wesley Longman, Furthermore, finding the determinant of a real number 1974, ch. 6. matrix is subject to rounding errors due to limitations of [2] Bareiss, E.H., “Sylvester’s identity and multistep integer-preserving ,” Math. Comp. 22, 1968. information representation, making the resulting [3] Jones, J.. (accessed 2016, Dec 3). The Determinant of a Square determinant slightly less accurate. Matrix [Online]. Available: Neither the Laplace expansion nor the Bareiss algorithm https://people.richland.edu/james/lecture/m116/matrices/determina nt.html. have the weakness mentioned above; they both preserve [4] Leggett, D.R, “Fraction-Free Methods for Determinants,” M.Sc. the identity of the info type. The Laplace expansion does thesis, Dept. Math., Univ. Southern Mississippi, Hattiesburg, MS, not use division in its process. The Bareiss algorithm does 2011. [5] Liem, I., Draft Diktat Kuliah Dasar Pemrograman (Bagian have division, but it is guaranteed to be exact; that is, in an Pemrograman Prosedural). unpublished. integer matrix, the division will have no remainder, while

Makalah IF2120 Matematika Diskrit – Sem. I Tahun 2016/2017

[6] Munir, R., Matematika Diskrit, Ed. 3. Bandung: Teknik Informatika ITB, 2006, ch. 10. [7] Strang, G., Introduction to , 5th ed. Wellesey: Wellesey-Cambridge Press, 2016, ch. 1. [8] Weisstein, E.W.. (accessed 2016, Dec 3). Determinant [Online]. Available: http://mathworld.wolfram.com/Determinant.html. [9] Weisstein, E.W.. (accessed 2016, Dec 3). Determinant Expansion by Minors [Online]. Available: http://mathworld.wolfram.com/DeterminantExpansionbyMinors.ht ml. [10] Weisstein, E.W.. (accessed 2016, Dec 3). LU Decomposition [Online]. Available: http://mathworld.wolfram.com/LUDecomposition.html. [11] anonymous. (accessed 2016, Dec 3). Elementary Matrix Operations [Online]. Available: http://stattrek.com/matrix-algebra/elementary- operations.aspx. [12] anonymous. (accessed 2016, Dec 4). Fastest algorithm for computing the determinant of a matrix? [Online]. Available: https://stackoverflow.com/questions/27003062/fastest-algorithm- for-computing-the-determinant-of-a-matrix. [13] anonymous. (accessed 2016, Dec 3). Lecture 12 – LU Decomposition [Online]. Available: http://www.math.ohiou.edu/courses/math3600/lecture12.pdf.

PERNYATAAN Dengan ini saya menyatakan bahwa makalah yang saya tulis ini adalah tulisan saya sendiri, bukan saduran, atau terjemahan dari makalah orang lain, dan bukan plagiasi.

Bandung, 8 Desember 2016

Felix Limanta 13515065

Makalah IF2120 Matematika Diskrit – Sem. I Tahun 2016/2017