Javatpoint Logo
Javatpoint Logo

C++ Program to Find Determinant of a Matrix

In this article, we are going find the Determinant of a matrix using different approaches. Before finding the value of a determinant, we must know about a matrix determinant. A matrix's determinant is a particular integer specified only for square matrices (matrices having the same number of rows and columns). A determinant is used frequently in calculus and other algebraic matrices; it describes the matrix in the context of a real number that may be used to solve a system of linear equations and identify the inverse of a matrix.

How do you compute?

The determinant of a matrix can be determined as follows: for each element of the first row or column, obtain the cofactor of those components, multiply the component with the determinants of the associated cofactor, and then add them with opposite signs. In the most basic case, the determinant of a 1*1 matrix is the single value itself. The cofactor of an element is a matrix obtained by eliminating the element's columns and rows from that matrix.

Approach 1:

Filename: Determinant.cpp

Let's take an example to illustrate the determinant of the matrix in C++.

Output:

The determinant of the matrix is: 10

Time Complexity: O(N!)

Explanation:

The time complexity of the getCofactorMatrix() function is O(N^2). The following recurring relation can be used to calculate the time-based complexity of the determinantOfMatrix() method:

T(N) =N*T(N-1) + O(N^3)

The first number, N*T(N-1), indicates the time required to calculate the determinants of the (N-1) x (N-1) submatrices. In contrast, the second value, O(N-3), represents the time required to calculate the cofactors that are needed for every single component in the original matrix's first row. We can calculate the factor that determines an NxN matrix as a sum of its determinants (N-1)x(N-1) matrices, each of which calls for O(N^2) operations to get the cofactors, using expansion by digits. As a result, the determinantOfMatrix() method has a time-based complexity of O(N!), which is the most extreme case when the matrix is a recombination matrix.

The display() function has a time complexity of O(N^2) because it loops throughout all the matrix members to print them. Its computational complexity is O(N!) because the determinantOfMatrix() function dominates the entire time complexity of the program's code.

Space Complexity: O(N*N)

By applying the determinant properties, we iterate through each diagonal element, making all of the elements below the diagonal zero.

Approach 2:

If this diagonal element is zero, we will look for the next non-zero element in the same column.

There are two possibilities.

Case 1: If there isn't a non-zero element. In this case, the value of the determinant for the matrix is 0.

Case 2: If a non-zero element is present, there are two possibilities.

  • Case a: If the index matches a diagonal row member, we reduce all of the column items to zero using the determinant properties.
  • Case b: Here, we must swap the row with the corresponding diagonal element columns and continue the case 'a'; procedure.

Example:

Filename: Determinanat2.cpp

Output:

The Determinant of the given matrix is: 31

Complexity:

Time complexity: O(N3)

Space Complexity: O(N)

Approach 3: Gauss-Jordan Elimination Technique

Steps:

  1. Beginning with the provided matrix, and set the determinant to 1.
  2. While maintaining track of the determinant, use simple row operations to convert the matrix to its row-column form.
  3. If there are any row exchange operations, multiply the value of the determinant by -1.
  4. If any row contains a coefficient that leads to a value of zero, the determinant is zero, and we can stop here.
  5. The determinant is the product of the diagonal elements in the row-column form.
  6. Return the value of the determinant.

Example:

Filename: DeterminantByGauss.cpp

Output:

The Determinant value is= 85

Complexity:

Time Complexity: O(N3), where n is the total amount of rows (or columns) in a matrix.

Auxiliary Space: O(1) because the matrix has been modified in place.

Approach 4: Cofactor Expansion

Steps:

  1. Create a function to compute the determinate value of a matrix using cofactor expansions.
  2. If the matrix being evaluated is not a square matrix, return an error statement.
  3. Return an element if the matrix has 1×1.
  4. Return the value of the determinant if the combination of elements is 2×2 using the ad-bc formula.
  5. If the number of elements is larger than 2×2, loop over the first row and compute the cofactor for every single component.
  6. Multiply each cofactor by its associated element with the sign (+/-) of the element.
  7. Add the results of step 6 to obtain the determinant.

Example:

Filename: Cofactor.cpp

Output:

The Determinant is: 24

Complexity:

Time Complexity: O(N!)

Space Complexity: O(N^2)







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA