C Program to Find Determinant of Matrix
determinant() and getCofactor() functions.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> void getCofactor(int mat[10][10], int temp[10][10], int p, int q, int n) { int i = 0, j = 0; for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { if (row != p && col != q) { temp[i][j++] = mat[row][col]; if (j == n - 1) { j = 0; i++; } } } } } int determinant(int mat[10][10], int n) { if (n == 1) return mat[0][0]; int det = 0, temp[10][10]; int sign = 1; for (int f = 0; f < n; f++) { getCofactor(mat, temp, 0, f, n); det += sign * mat[0][f] * determinant(temp, n - 1); sign = -sign; } return det; } int main() { int n = 3; int mat[10][10] = { {6, 1, 1}, {4, -2, 5}, {2, 8, 7} }; printf("Determinant: %d\n", determinant(mat, n)); return 0; }
Dry Run
Let's trace the determinant calculation for the 3x3 matrix [[6,1,1],[4,-2,5],[2,8,7]] through the code.
Start determinant calculation
Matrix size n=3, start expanding along first row elements: 6, 1, 1.
Calculate cofactor for element 6 (position 0,0)
Remove row 0 and column 0, cofactor matrix is [[-2,5],[8,7]].
Calculate determinant of cofactor [[-2,5],[8,7]]
det = (-2*7) - (5*8) = -14 - 40 = -54.
Calculate cofactor for element 1 (position 0,1)
Remove row 0 and column 1, cofactor matrix is [[4,5],[2,7]].
Calculate determinant of cofactor [[4,5],[2,7]]
det = (4*7) - (5*2) = 28 - 10 = 18.
Calculate cofactor for element 1 (position 0,2)
Remove row 0 and column 2, cofactor matrix is [[4,-2],[2,8]].
Calculate determinant of cofactor [[4,-2],[2,8]]
det = (4*8) - (-2*2) = 32 + 4 = 36.
Combine results with signs
determinant = 6*(-54) - 1*(18) + 1*(36) = -324 - 18 + 36 = -306.
| Element | Cofactor Determinant | Sign | Contribution |
|---|---|---|---|
| 6 | -54 | + | -324 |
| 1 | 18 | - | -18 |
| 1 | 36 | + | 36 |
Why This Works
Step 1: Base case for recursion
When the matrix size is 1, the determinant is just the single element, so return it directly.
Step 2: Finding cofactors
For each element in the first row, create a smaller matrix by removing that element's row and column to find its cofactor.
Step 3: Recursive determinant calculation
Calculate the determinant of each cofactor matrix recursively, multiplying by the element and the sign (+ or -) based on position.
Step 4: Summing contributions
Add all these signed products together to get the determinant of the original matrix.
Alternative Approaches
#include <stdio.h> int determinant(int mat[10][10], int n) { int det = 1; for (int i = 0; i < n; i++) { int pivot = i; while (pivot < n && mat[pivot][i] == 0) pivot++; if (pivot == n) return 0; if (pivot != i) { for (int j = 0; j < n; j++) { int temp = mat[i][j]; mat[i][j] = mat[pivot][j]; mat[pivot][j] = temp; } det = -det; } det *= mat[i][i]; for (int k = i + 1; k < n; k++) { int factor = mat[k][i] / mat[i][i]; for (int j = i; j < n; j++) { mat[k][j] -= factor * mat[i][j]; } } } return det; } int main() { int n = 3; int mat[10][10] = { {6, 1, 1}, {4, -2, 5}, {2, 8, 7} }; printf("Determinant: %d\n", determinant(mat, n)); return 0; }
#include <stdio.h> int determinant2x2(int mat[2][2]) { return mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0]; } int main() { int mat[2][2] = {{1, 2}, {3, 4}}; printf("Determinant: %d\n", determinant2x2(mat)); return 0; }
Complexity: O(n!) time, O(n^2) space
Time Complexity
The recursive method expands cofactors for each element, leading to factorial time complexity because each step reduces matrix size by one but calls multiple recursive calls.
Space Complexity
Extra space is used for temporary cofactor matrices of size (n-1)x(n-1) at each recursion level, leading to O(n^2) space.
Which Approach is Fastest?
Gaussian elimination is faster (O(n^3)) for large matrices, while recursion is simpler but slow for big sizes.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive Cofactor Expansion | O(n!) | O(n^2) | Small matrices (n <= 5) |
| Gaussian Elimination | O(n^3) | O(1) | Large matrices |
| Hardcoded 2x2 Formula | O(1) | O(1) | 2x2 matrices only |