GFG App
Open App
Browser
Continue

# Determinant of N x N matrix using multi-threading

Given a matrix of N x N, task is to find the determinant of the matrix using multi-threading.

Examples :

```Input : mat = {{0, 4, 0, -3},
{1, 1, 5, 2},
{1, -2, 0, 6},
{ 3, 0, 0, 1}}
Output : -250

Input :  mat = {{1, 0, 2, -1},
{3, 0, 0, 5},
{2, 1, 4, -3},
{1, 0, 5, 0}}
Output: 30```

Approach :
It is known that finding the determinant of a matrix can be very costly in the amount of time it takes. So, computing things parallelly may lead to a comparatively less costly program. A code can be parallelized using threads, which is light weight process and divides a single flow program into a program with multiple flows. As for finding the determinant of a matrix, first find the determinant of the submatrices of the matrix, distribute the task of finding the determinant of submatrices to the threads, and these threads will run parallelly resulting in less time execution time than the sequential method.

Note: The code is Linux specific.

## C++

 `// CPP program for finding determinant matrix` `// with parallelizing the code` `#include ` `#include ` `#include ` `#define size 4`   `using` `namespace` `std;`   `// matrix whose determinant is required` `int` `mat[][size] = { { 0, 4, 0, -3 },` `                    ``{ 1, 1, 5, 2 },` `                    ``{ 1, -2, 0, 6 },` `                    ``{ 3, 0, 0, 1 } };` `int` `det[size];`   `// declaring variable for storing thread id` `pthread_t ``thread``[size];`   `// function for finding determinant` `int` `determinant(vector > mat2, ``int` `s)` `{`   `    ``if` `(s == 2) {` `        `  `        ``// if size of matrix is 2X2` `        ``// then returning the determinant` `        ``return` `mat2[0][0] * mat2[1][1] - ` `               ``mat2[0][1] * mat2[1][0];` `    ``}` `    ``else` `{` `        `  `        ``// else dividing the matrix in smaller part.` `        ``vector > mat1(s - 1), ` `                      ``mat3(s - 1), mat4(s - 1);` `        ``int` `k, l, m, i, j;` `        `  `        ``for` `(i = 0; i < s - 1; i++) {` `            ``mat1[i] = vector<``int``>(s - 1);` `            ``mat3[i] = vector<``int``>(s - 1);` `            ``mat4[i] = vector<``int``>(s - 1);` `        ``}` `        `  `        ``for` `(i = 1; i < s; i++) {` `            ``k = 0;` `            ``l = 0;` `            ``m = 0;` `            ``for` `(j = 0; j < s; j++) {` `                ``if` `(j != 0) {` `                    ``mat1[i - 1][k] = mat2[i][j];` `                    ``k++;` `                ``}` `                ``if` `(j != 1) {` `                    ``mat3[i - 1][l] = mat2[i][j];` `                    ``l++;` `                ``}` `                ``if` `(j != 2) {` `                    ``mat4[i - 1][m] = mat2[i][j];` `                    ``m++;` `                ``}` `            ``}` `        ``}`   `        ``return` `mat2[0][0] * determinant(mat1, s - 1) - ` `               ``mat2[0][1] * determinant(mat3, s - 1) + ` `               ``mat2[0][2] * determinant(mat4, s - 1);` `    ``}` `}`   `// function for finding determinant using first row` `// with each element of row a thread is associated.` `void``* createTd(``void``* arg)` `{` `    ``int` `*ar = (``int` `*)arg, i, j, k;` `    ``vector > mat2(size - 1);`   `    ``for` `(i = 0; i < size - 1; i++)` `        ``mat2[i] = vector<``int``>(size - 1);` `        `  `    ``// extracting the matrix smaller by size one.` `    ``// for finding the determinant.` `    ``for` `(i = 1; i < size; i++) {` `        ``k = 0;` `        ``for` `(j = 0; j < size; j++) {` `            ``if` `(j != (*ar)) {` `                ``mat2[i - 1][k] = mat[i][j];` `                ``k++;` `            ``}` `        ``}` `    ``}` `    `  `    ``// calling determinant function` `    ``det[*ar] = det[*ar] * determinant(mat2, size - 1);` `}`   `// driver function` `int` `main()` `{` `    ``int` `i, j, detfin = 0;` `    ``int` `p[size];` `    `  `    ``// storing the first row in a array` `    ``// for later multiplying with the determinant` `    ``// of smaller matrix` `    ``for` `(i = 0; i < size; i++)` `        ``det[i] = mat[0][i];` `        `  `    ``// creating thread` `    ``for` `(i = 0; i < size; i++) {` `        ``p[i] = i;` `        ``pthread_create(&``thread``[i], NULL, &createTd, (``void``*)&p[i]);` `    ``}` `    `  `    ``// waiting for all the threads to join` `    ``pthread_join(``thread``[0], NULL);` `    ``pthread_join(``thread``[1], NULL);` `    ``pthread_join(``thread``[2], NULL);` `    ``pthread_join(``thread``[3], NULL);` `    ``for` `(i = 0; i < size; i++) {` `        ``if` `(i % 2 == 0)` `            ``detfin += det[i];` `        ``else` `            ``detfin -= det[i];` `    ``}` `    `  `    ``cout << detfin << endl;` `    `  `    ``return` `0;` `}`

## Python3

 `import` `threading`   `size ``=` `4`   `# matrix whose determinant is required` `mat ``=` `[[``0``, ``4``, ``0``, ``-``3``],` `       ``[``1``, ``1``, ``5``, ``2``],` `       ``[``1``, ``-``2``, ``0``, ``6``],` `       ``[``3``, ``0``, ``0``, ``1``]]` `det ``=` `[``0``] ``*` `size`   `# function for finding determinant`     `def` `determinant(mat2, s):` `    ``if` `s ``=``=` `2``:` `        ``# if size of matrix is 2x2` `        ``# then returning the determinant` `        ``return` `mat2[``0``][``0``] ``*` `mat2[``1``][``1``] ``-` `mat2[``0``][``1``] ``*` `mat2[``1``][``0``]` `    ``else``:` `        ``# else dividing the matrix in smaller parts` `        ``mat1 ``=` `[[``0``] ``*` `(s ``-` `1``) ``for` `_ ``in` `range``(s ``-` `1``)]` `        ``mat3 ``=` `[[``0``] ``*` `(s ``-` `1``) ``for` `_ ``in` `range``(s ``-` `1``)]` `        ``mat4 ``=` `[[``0``] ``*` `(s ``-` `1``) ``for` `_ ``in` `range``(s ``-` `1``)]` `        ``for` `i ``in` `range``(``1``, s):` `            ``k, l, m ``=` `0``, ``0``, ``0` `            ``for` `j ``in` `range``(s):` `                ``if` `j !``=` `0``:` `                    ``mat1[i ``-` `1``][k] ``=` `mat2[i][j]` `                    ``k ``+``=` `1` `                ``if` `j !``=` `1``:` `                    ``mat3[i ``-` `1``][l] ``=` `mat2[i][j]` `                    ``l ``+``=` `1` `                ``if` `j !``=` `2``:` `                    ``mat4[i ``-` `1``][m] ``=` `mat2[i][j]` `                    ``m ``+``=` `1` `        ``return` `(mat2[``0``][``0``] ``*` `determinant(mat1, s ``-` `1``) ``-` `                ``mat2[``0``][``1``] ``*` `determinant(mat3, s ``-` `1``) ``+` `                ``mat2[``0``][``2``] ``*` `determinant(mat4, s ``-` `1``))`   `# function for finding determinant using first row` `# with each element of row a thread is associated`     `def` `createTd(ar):` `    ``global` `det` `    ``mat2 ``=` `[[``0``] ``*` `(size ``-` `1``) ``for` `_ ``in` `range``(size ``-` `1``)]` `    ``# extracting the matrix smaller by size one` `    ``# for finding the determinant` `    ``for` `i ``in` `range``(``1``, size):` `        ``k ``=` `0` `        ``for` `j ``in` `range``(size):` `            ``if` `j !``=` `ar:` `                ``mat2[i ``-` `1``][k] ``=` `mat[i][j]` `                ``k ``+``=` `1` `    ``# calling determinant function` `    ``det[ar] ``=` `det[ar] ``*` `determinant(mat2, size ``-` `1``)`     `# driver function` `if` `__name__ ``=``=` `"__main__"``:` `    ``threads ``=` `[]` `    ``detfin ``=` `0` `    ``# storing the first row in an array` `    ``# for later multiplying with the determinant` `    ``# of smaller matrix` `    ``for` `i ``in` `range``(size):` `        ``det[i] ``=` `mat[``0``][i]` `    ``# creating threads` `    ``for` `i ``in` `range``(size):` `        ``t ``=` `threading.Thread(target``=``createTd, args``=``(i,))` `        ``t.start()` `        ``threads.append(t)` `    ``# waiting for all the threads to join` `    ``for` `t ``in` `threads:` `        ``t.join()` `    ``for` `i ``in` `range``(size):` `        ``if` `i ``%` `2` `=``=` `0``:` `            ``detfin ``+``=` `det[i]` `        ``else``:` `            ``detfin ``-``=` `det[i]` `    ``print``(detfin)`

Output:

`-250`

My Personal Notes arrow_drop_up