CREATE OWN LIBRARY

Matrix Determinant

Back to Programming

Description

In algebra, determinant is the scalar value which is calculated from the elements of a square matrix. The determinant of the matrix A is denoted as det(A), det A, or |A|.

If we want to calculate the determinant of a 2×2 matrix, the following step is to be followed:

 

Then |A|= 4×8 - 6×3 = 32-18 = 14

Now for a 3×3 matrix:

 

To find out the determinant:

Algorithm

INPUT: A matrix
OUTPUT: The determinant of the matrix

PROCESS:
Step 1: [Taking the inputs]
	Read m, n [The rows and columns of the matrix]
	If m=n then
	    	[taking the elements of the matrix]
		For i=0 to m-1 repeat
			For j=0 to n-1 repeat
				Read a[i][j]
			[End of ‘for’ loop]
		[End of ‘for’ loop]
		[calling the function to find the determinant]
		Set d<-det(a, m)
		[printing the determinant]
		Print "The Determinant of the Matrix is : d”
	else
		print "Determinant is only possible for square matrix"
	[End of ‘if’]
Step 2: [function ‘det(a, l)’]
	Set z<-1
	Set d<-0
	If l=1 then
        		Return a[0][0]
    	else
    		Set d<-0
        		For i=0 to l-1 repeat
            			Set x<-0
            			Set y<-0
            			For j=0 to l-1 repeat
               			For k=0 to l-1 repeat
               				Set mat[j][k]<-0
               				If j ≠ 0 and k ≠ I then
               					Set mat[x][y]<-a[j][k]
               					If y<(l-2) then
               						Set y<-y+1
               					else
               						Set y<-0
               						Set x<-x+1
               					[End of ‘if’]
               				[End of ‘if’]
            				[End of ‘for’ loop]
            			[End of ‘for’ loop]
        			Set d<-d + z × (a[0][i] × det(mat,l-1))
               		Set z=-1 × z
        		[End of ‘for’ loop]
    	[End of ‘if’]
    	Return d
[End of function ‘det(a, l)’]
Step 3: Stop.

Code

TIME COMPLEXITY:

 if(l==1) ------------------------------- O(c1)

 { return(a[0][0]);  }

                else-

                {              d=0; ------------------------- O(1)

                                for(i=0;i<l;i++) -----------  O(l)

                               {    x=0;

                                      y=0;

                                        for(j=0;j<l;j++) { ------- O(l)

                           for(k=0;k<l;k++) {-----O(l)

                                 mat[j][k]=0;

                                 if((j != 0) && (k != i)) ------- O(c2)

                                                                 {   mat[x][y]=a[j][k];

                                                                    if(y<(l-2))

                                                                    y++;}

                                                                       else

                                                                                {              y=0;

                                                                                                x++; }

                                                                 }

                                                                     }

                                                      }

                                                d=d + z * (a[0][i] * det(mat,l-1)); -------- O(1)

                                z=-1 * z; ---------------------------------------- O(1)

                                }

                 }

                 //returning the determinant

                return(d); ---------------------- O(1)

The time complexity is O(n3).

Contributed by